gpt4 book ai didi

Python:矩形参数无效

转载 作者:行者123 更新时间:2023-11-30 21:51:54 24 4
gpt4 key购买 nike

嗨,我正在尝试学习 python,但我遇到了这个问题,当我运行我的程序时,它说 rect 参数无效,这是我的代码:

import pygame
pygame.init()

win = pygame.display.set_mode((500,500))

pygame.display.set_caption("First game")

x = 50
y = 50
width = 40
height = 60
vel = 5

run = True
while run:
pygame.time.delay(100)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

pygame.draw.rect(win, (255, 0, 0),(x, y, width, height, vel))
pygame.display.update()

pygame.quit()

错误:

Traceback (most recent call last):
File "...", line 25, in <module>
pygame.draw.rect(win, (255, 0, 0),(x, y, width, height, vel))
TypeError: Rect argument is invalid

最佳答案

pygame.draw.rect 的第三个参数必须是包含 4 个元素的元组:

pygame.draw.rect(win, (255, 0, 0),(x, y, 宽度, 高度, 速度))

pygame.draw.rect(win, (255, 0, 0),(x, y, width, height))

或者它可以是 pygame.Rect对象也是:

rect = pygame.Rect(x, y, width, height)
pygame.draw.rect(win, (255, 0, 0), rect)

关于Python:矩形参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60019097/

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