2013-03-29 104 views
2

我目前正在python中製作一個遊戲,其中一個球擊中一組框。我已經加載了一個我想讓這些箱子站立的草地圖像,草圖像是600 x 151像素。到目前爲止,我已經將所有的盒子和球完美地展現出來,但是當前沒有工作的是當我嘗試加載圖像並顯示它時,這些盒子可以站在草圖上而不是脫落。爲什麼我得到這個錯誤:「無效的rectstyle參數」

import pygame 
from pygame.locals import * 
from pygame.color import * 
import pymunk as pm 
from pymunk import Vec2d 
import sys 
from random import randint 

def to_pygame(p): 
    """Small hack to convert pymunk to pygame coordinates""" 
    return int(p[0]), int(-p[1]+600) 


def add_box(space, size, pos, mass=1.0): 
    # pos is a Vec2d object 
    points = [(-size, -size), (-size, size), (size,size), (size, -size)] 
    moment = pm.moment_for_poly(int(mass), points, (0,0)) 

    body = pm.Body(mass, moment) 
    body.position = pos 

    shape = pm.Poly(body, points, (0,0)) 
    shape.friction = 1 
    space.add(body,shape) 

    return shape 

def draw_box(screen, box): 
    ps = box.get_points() 
    ps.append(ps[0]) 
    ps = map(to_pygame, ps) 
    pygame.draw.polygon(screen, THECOLORS["blue"], ps, 3) 


def main(): 
    pygame.init() 
    screen = pygame.display.set_mode((600, 600)) 
    pygame.display.set_caption("Piling boxes") 
    clock = pygame.time.Clock() 

    boxes = [] 
    x = 170 
    y = 120 
    space = pm.Space() 
    space.gravity = (0.0, -100.0) 

    ### ground 
    body = pm.Body() 
    shape = pm.Segment(body, (150,100), (450,100), .0) 
    shape.friction = 4.4 
    space.add(shape) 

    for i in range(5): 
     # what is the mass of the box? 
     mass = randint(1,3) 
     # what size is the box? 
     size = randint(10, 35) 
     # calculate its position & hold it in a Vec2d 

     #x = x + size + 10 
     y = y + size 
     pos = Vec2d(x,y) 

     box = add_box(space, size, pos, mass) 
     boxes.append(box) 

    while 1: 
     clock.tick(15) 
     space.step(1/30.0) 

     for event in pygame.event.get(): 
      if event.type == QUIT: 
       pygame.quit() 
       sys.exit(0) 
      elif event.type == KEYDOWN: 
       if event.key == K_ESCAPE: 
        pygame.quit() 
        sys.exit(0) 

     screen.fill(THECOLORS["white"]) 
     test_image = pygame.image.load("grass.png") 

     screen.blit(test_image, to_pygame((450,100)), to_pygame((150,100)),3) 


     for i in range(len(boxes)): 
      draw_box(screen, boxes[i]) 

     pygame.display.flip() 


if __name__ == '__main__': 
    main() 
+4

發佈完整的追溯,所以人們不必猜測錯誤發生的位置。 – Cairnarvon

回答

1

沒有追蹤或錯誤日誌所以它是一個有點難以找出錯誤的點,一個受過教育的猜測是,你逝去的Rectstyle參數不正確的地方。有關如何正確傳遞Rectstyle參數的信息,請參閱pygame.Rect的文檔。

pygame.Rect(left, top, width, height): return Rect 
pygame.Rect((left, top), (width, height)): return Rect 
pygame.Rect(object): return Rect 
1

問題是

screen.blit(test_image, to_pygame((450,100)), to_pygame((150,100)),3) 

做塊的表面,以篩選,有效blit的是:

screen.blit(test_image, (450,100)) 

所以,你想:

screen.blit(test_image, to_pygame((450,100))) 

注意:額外的參數讓我想也許你正在嘗試使用源代碼rect參數,以使用tileset。但在這種情況下,你不會將世界轉換成屏幕座標兩次。只是在目的地,而不是來源。

如果你想spritesheet /地形設置,然後進行How do you select a sprite image from a sprite sheet in python?Pygame.org/cookbook/SpriteSheet

1

blit第三個參數是源area的說法,應該是一個矩形,沒有座標:

screen.blit(test_image, to_pygame((450,100)), to_pygame((150,100)),3) 

也許你想要這個:

screen.blit(test_image, to_pygame((450,100)), pygame.Rect((0,0), (150,100)), 3) 
0

問題是因爲您致電screen.blit是不正確的。見here

呼叫可能是

screen.blit(test_image, to_pygame((450,100))) 

或許,如果你想使用面積說法,

screen.blit(test_image, to_pygame((450,100)), pygame.Rect((0,0), (150,100)), 3) 

此外,要裝載在每個循環guess.png,而不是將其移出的while循環,你的代碼將表現得更好。

相關問題