gpt4 book ai didi

python - “Rect argument is invalid”

转载 作者:行者123 更新时间:2023-12-03 09:12:39 26 4
gpt4 key购买 nike

我一直在尝试编写游戏代码,但它一直给我一个"rect argument is invalid"错误。如果有人可以告诉我我在做什么错,那太好了。我相信它与代码中的列表和元组有关,但是我已经对其进行了彻底的审查,但它无法正常运行。该代码只是不断向我显示相同的错误消息。
我的代码:

import pygame
import sys
import random

pygame.init()

Width = 800
Height = 600

Red = (255, 0, 0)
Blue = (0, 0, 255)
White = (255, 255, 255)
backgroundColor = (0, 0, 0)

playerSize = 50
playerPos = [100, 300]

columnSize = [50, 150]
columnPos1 = [700, random.randint(0,random.randint(0,100))]
columnPos2 = [700, random.randint(0,random.randint(400,500))]

screen = pygame.display.set_mode((Width, Height))

game_over = False

clock = pygame.time.Clock()

while not game_over:

for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

if event.type == pygame.KEYDOWN:

x = playerPos[0]
y = playerPos[1]

if event.key == pygame.K_LEFT:
x -= 20
elif event.key == pygame.K_RIGHT:
x += 20
elif event.key == pygame.K_UP:
y -= 20
elif event.key == pygame.K_DOWN:
y += 20

playerPos = [x,y]

screen.fill(backgroundColor)
if columnPos1[0] >= 0 and columnPos1[0] < Width and columnPos2[0] >= 0 and columnPos2[0] < Width:
columnPos1[0] -= 10
columnPos2[0] -= 10
else:
columnPos1[0] = 700
columnPos2[0] = 700

columnPos1[1] = [700, random.randint(0,random.randint(0,100))]
columnPos2[1] = [700, random.randint(0,random.randint(400,500))]

pygame.draw.rect(screen, Red, (playerPos[0], playerPos[1], playerSize, playerSize))
pygame.draw.rect(screen, Red, (columnPos1[0], columnPos1[1], columnSize[0], columnSize[1])) #this is where rect is invalid
pygame.draw.rect(screen, Red, (columnPos2[0], columnPos2[1], columnSize[0], columnSize[1])) #here as well

clock.tick(30)

pygame.display.update()
添加后,错误开始发生
columnPos1[1] = [700, random.randint(0,random.randint(0,100))]
columnPos2[1] = [700, random.randint(0,random.randint(400,500))]

最佳答案

columnSize[50, 150],因此您的rect参数看起来像

(columnPos1[0], columnPos1[1], [50, 150], [50, 150])
你可能是说
(columnPos1[0], columnPos1[1], columnSize[0], columnSize[1])

关于python - “Rect argument is invalid”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63389205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com