python - 我的 pygame rects 给出的 rect 参数无效错误
问题描述
大约 15 分钟以来,我一直在寻找解决此错误的方法,但没有任何帖子或其他网站涵盖我遇到的特定错误。这是有问题的代码片段:
import pygame
win = pygame.display.set_mode((100,100))
width = 10
height = 40
pipex = 10
pipe1y = 10
pipe1 = pygame.draw.rect(win,(0, 255, 100), (width, height1))
pygame.win.blit(pipe1, pipex, pipe1y)
pygame.display.update()
这是完整的代码:
import pygame
import time
import random
from bird import brd
pygame.init()
win = pygame.display.set_mode((400,600))
pygame.display.set_caption('Flappy Bird')
pipex = 345
pipe1y = -270
pipe2y = 420
width = 65
height1 = 400
height2 = 3000
vel = 5
bird_x = 20
bird_y = 300
isJump = 0
jumpCount = 0
yy = 0
class fb:
def move():
global pipex
global yy
global pipe1y
global pipe2y
global bird_x
global bird_y
pipex -= 1
if pipex < -60:
pipex = 345
yy = random.randint(-350,0)
pipe1y = yy
pipe2y = pipe1y + 555
pygame.draw.rect(win,(0,0,0), (0,0,1000,1000))
pipe1 = pygame.draw.rect(win,(0, 255, 100), (width, height1))
pipe2 = pygame.draw.rect(win,(0, 255, 0), (width, height2))
bird = pygame.draw.rect(win,(255,0,0),(30, 30))
pygame.win.blit(pipe1, pipex, pipe1y)
pygame.win.blit(pipe2, pipex, pipe2y)
pygame.win.blit(bird, bird_x, bird_y)
pygame.display.update()
bird_y += 2
def gameover():
pygame.quit()
def checks():
global yy
global bird_x
global bird_y
global pipex
if bird_y > 600:
fb.gameover()
while True:
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
bird_y -= 4
if keys[pygame.K_DOWN]:
bird_y += 1
fb.move()
time.sleep(0.005)
fb.checks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
break
我需要将矩形作为对象或变量,以便稍后在赛道上检查碰撞。
解决方案
的第三个参数pygame.draw.rect()
定义了由颜色填充的矩形。它是一个包含 4 个组件(x、y、width、height)的元组。( x , y ) 定义矩形的左上角, ( width , height ) 定义矩形的大小。
在您的情况下 ( pipex
, pipe1y
) 似乎是矩形的位置:
pipe1 = pygame.draw.rect(win,(0, 255, 100), (width, height1))
pipe1 = pygame.draw.rect(win, (0, 255, 100), (pipex, pipe1y, width, height1))
推荐阅读
- angular - 如何通过外部翻译 url 在 ionic 3 中使用 ngx translate
- angular - forwardRef 在角度上有什么作用?
- angular - 显示已在 Angular 5 中作为 Blob 对象接收的图像
- javascript - 如果我在 react-select 中使用 optionsRenderer,如何使用 valueComponent 道具呈现所选项目
- python - 如何将熊猫框架值中的问号替换为缺失值?
- c++ - 需要在 MSVC 中安装 clang
- javascript - 带有弹出式成功或失败框的联系表
- html - 尝试 angular MutationObserver 所以我可以限制 div 的高度
- mysql - mysql.createConnection 在使用 node.js 连接 MySQL 服务器时不是函数
- java - Elasticsearch:在 QueryBuilders.termsQuery() 中添加 arraylist 会引发错误