pygame - 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()
我试图制作一个你是汽车的游戏并避免积木,但我无法添加积木,因为我收到错误
解决方案
您的things
函数格式不正确。你有参数列表,(thingx, thingy, thingw, color, thingh)
但你把它称为(thingx, thingy, things, thingh, color)
. 要使错误消失,您需要做的就是在函数定义开关color
中使用thingh
推荐阅读
- python - 将 numpy.bmat 与 numba 一起使用
- mongodb - db.collections.updateOne(filter,update,options) 不适用于 $pull 运算符
- sql-server - 使用 powershell 更改 SQL Server 的实例级别排序规则
- elasticsearch - 没有更新的 Ruflin/Elastica 批量插入
- html - 收缩、媒体查询和网格时布局的响应式布局问题
- android - SMS Retriever API 在哪里使用 11 位生成的哈希密钥?
- google-cloud-platform - Terraform - 间隙中的私有 dns 区域
- slick.js - Google 控制台中的 Slick Slider 错误
- java - 如何将带有增量参数的休眠生成器 =“序列”转换为 JPA
- c++ - 如何避免在类的构造函数中需要两次提供模板?