TypeError:Rect参数无效pygame

时间:2019-07-18 18:50:53

标签: pygame

我是pygame的新手,我想制作一款游戏,但我在https://www.youtube.com/playlistlist=PLQVvvaa0QuDdLkP8MrOXLe_rKuf6r80KO上找到了一个播放列表,但我却发现了这样的错误

Traceback (most recent call last):
File "/home/pi/firstgamepyg.py", line 78, in <module>
game_loop()
File "/home/pi/firstgamepyg.py", line 65, in game_loop
things(thing_startx, thing_starty, thing_width, thing_height, black)
File "/home/pi/firstgamepyg.py", line 15, in things
 pygame.draw.rect(gamedisplay, color,(thingx, thingy, thingw, thingh))
TypeError: Rect argument is invalid

这是我的代码

import pygame
import time
import random

pygame.init()
dw = 800
dh = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
cw = 96
gamedisplay = pygame.display.set_mode((dw,dh))
cimg = pygame.image.load('/home/pi/Downloads/car.jpeg')

def things(thingx, thingy, thingw, color, thingh): 
    #error on next line 
    pygame.draw.rect(gamedisplay, color,(thingx, thingy, thingw, thingh))

def car(x,y):
    gamedisplay.blit(cimg,(x,y))

def text_objects(text, font):
    textsurface = font.render(text, True, red)
    return textsurface, textsurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((dw/2),(dh/2))
    gamedisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    time.sleep(2)
    game_loop()

def crash():
    message_display('wasted')

def game_loop():    
    x = (dw * 0.45)
    y = (dh * 0.65)
    x_change = 0
    thing_startx = random.randrange(0,800)
    thing_starty = -600
    thing_speed = 7
    thing_width = 100
    thing_height = 100
    pygame.display.set_caption('A bit Racey')
    clock = pygame.time.Clock()
    gameexit = False

    while not gameexit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameexit = True
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key==pygame.K_RIGHT:
                    x_change = 0

        x += x_change
        gamedisplay.fill(white)
        things(thing_startx, thing_starty, thing_width, thing_height, black)
        thing_starty += thing_speed

        car(x,y)
        if x  > dw - cw or x < 0:
            crash()
            gameexit = True

        pygame.display.update()
        clock.tick(100)

game_loop()
pygame.quit() 
quit()

我正在尝试制作一款您喜欢汽车的游戏,避免使用积木,但是由于出现错误,我无法添加积木

1 个答案:

答案 0 :(得分:0)

您的things函数格式错误。您将参数列表命名为(thingx, thingy, thingw, color, thingh),但随后将其称为(thingx, thingy, things, thingh, color)。要使错误消失,您所需要做的就是在带有color的功能定义开关thingh