问题描述
大约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),1000,1000))
pipe1 = pygame.draw.rect(win,height1))
pipe2 = pygame.draw.rect(win,height2))
bird = pygame.draw.rect(win,(255,(30,30))
pygame.win.blit(pipe1,pipe1y)
pygame.win.blit(pipe2,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
我需要将rects当作对象或变量,以便稍后可以检查是否存在碰撞。
解决方法
pygame.draw.rect()
的第3个自变量定义用颜色填充的矩形。它是一个具有4个组成部分( x , y ,宽度,高度)的元组。 ( x , y )定义矩形的左上角,而( width , height )定义大小矩形。
在您的情况下(pipex
,pipe1y
)似乎是老生常谈的位置:
pipe1 = pygame.draw.rect(win,(0,255,100),(width,height1))
pipe1 = pygame.draw.rect(win,(pipex,pipe1y,width,height1))