我的 pygame rects 给出了一个 rect 参数无效错误 [英] My pygame rects are giving a rect argument is invalid error
问题描述
我一直在寻找解决此错误的方法大约 15 分钟,但没有任何帖子或其他网站涵盖我遇到的特定错误.这是有问题的代码片段:
I've been looking for a solution for this error for about 15 minutes now and no posts or other websites covered the specific error i've been having. Here's the code snippet in question:
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
我需要矩形是对象或变量,以便我可以在轨道上检查碰撞.
I need the rects to be objects or variables so i can check for collision later down the track.
推荐答案
pygame.draw.rect()
定义由颜色填充的矩形.它是一个包含 4 个组件(x、y、width、height)的元组.(x, y) 定义矩形的左上角, (width, height) 定义大小的矩形.
The 3rd argument of pygame.draw.rect()
defines the rectangular which is filled by the color. It is a tuple with 4 components (x, y, width, height). (x, y) defien the top left corner of the rectangle and (width, height) defines the size of the rectangle.
在你的情况下 (pipex
, pipe1y
) 似乎是 rectnagle 的位置:
In your case (pipex
, pipe1y
) seem to be the position of the rectnagle:
pipe1 = pygame.draw.rect(win,(0, 255, 100), (width, height1))
pipe1 = pygame.draw.rect(win, (0, 255, 100), (pipex, pipe1y, width, height1))
这篇关于我的 pygame rects 给出了一个 rect 参数无效错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!