首页 > 解决方案 > TypeError: Rect 参数无效 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()

我试图制作一个你是汽车的游戏并避免积木,但我无法添加积木,因为我收到错误

标签: pygame

解决方案


您的things函数格式不正确。你有参数列表,(thingx, thingy, thingw, color, thingh)但你把它称为(thingx, thingy, things, thingh, color). 要使错误消失,您需要做的就是在函数定义开关color中使用thingh


推荐阅读