为什么我收到此错误:“无效的rectstyle参数”

时间:2013-03-29 22:54:27

标签: python pygame

我目前正在使用python进行游戏,其中一个球击中了一组盒子。我已经加载了一个草的图像,我希望盒子能够站立,草图像是600 x 151像素。到目前为止,我已经把所有的盒子和球完美地呈现出来了,然而,目前无法正常工作的是当我尝试加载图像并显示它以便盒子可以站在草图上而不是掉下来时。

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 个答案:

答案 0 :(得分:1)

没有回溯或错误日志,因此有点难以确定错误点,一个有根据的猜测是你在某处错误地传递了Rectstyle参数。有关如何正确传递Rectstyle args的信息,请参阅pygame.Rect的文档。

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

答案 1 :(得分:1)

问题是

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

要将曲面blit到屏幕,有效的blit是:

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

所以你想要:

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

注意:额外的args让我觉得你可能正在尝试使用source rect参数来使用tileset。但在这种情况下,你不会两次将世界转换为屏幕坐标。就在目的地,而不是来源。

如果您确实需要spritesheet / tileset,那么请How do you select a sprite image from a sprite sheet in python?Pygame.org/cookbook/SpriteSheet

答案 2 :(得分: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)

答案 3 :(得分:0)

问题是因为您对screen.blit的呼叫不正确。请参阅here

电话可能是

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

或者如果你想使用area参数,

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

此外,您在每个循环中加载guess.png,而不是将其移出while循环,您的代码将表现得更好。