Python: rect argument is invalid [duplicate]

雨燕双飞 提交于 2021-01-11 02:57:46

问题


Hi I'm trying to learn python and but i'm stuck with this problem, when i run my program it says rect argument is invalid, this is my code:

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()

Error:

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

回答1:


The 3rd argument of pygame.draw.rect has to be a tuple with 4 elements:

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

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

Alternatively it can be a pygame.Rect object, too:

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


来源:https://stackoverflow.com/questions/60019097/python-rect-argument-is-invalid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!