【问题标题】:Python: rect argument is invalid [duplicate]Python:rect参数无效[重复]
【发布时间】:2020-02-01 16:55:16
【问题描述】:

您好,我正在尝试学习 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

【问题讨论】:

  • 您的标题似乎根本不适用于您的问题。

标签: python pygame arguments rect invalid-argument


【解决方案1】:

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

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

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 2020-04-20
    • 2014-06-29
    • 1970-01-01
    相关资源
    最近更新 更多