python - Python:矩形参数无效
问题描述
嗨,我正在尝试学习 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, 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)
推荐阅读
- python - Pandas 数据框绘制每列的回归曲线
- domain-driven-design - 事件溯源关系和基础知识
- vuejs2 - 单击时根据搜索热更新 v-for 循环
- python - nbconvert 在哪里存储中间 .tex 文件?
- html - 如何使用电子邮件处理媒体查询/非内联样式。
- java - 在表格中显示属性
- r - 在 R 中的 duplicated() 中使用无与伦比的参数
- java - 我可以使用 HTML 为我的桌面应用程序定义 GUI 吗?
- reactjs - 使用 ReactJS 和 Laravel 向服务器(文件)发送请求
- java - 线程“主”java.lang.Error 中的异常:未解决的编译问题