我目前正在尝试制作一个pygame版本的flappy bird(比我想象的更复杂)并坚持使管道产生随机高度。任何人都可以帮我这个吗?为了使这个动画相同,我想在屏幕右边生成一些管道并将它们稍微删除一点。当我运行当前代码时,管道图像位于左上角的彼此顶部,不会移动。 (鸟虽然工作)
import pygame, random, sys
pygame.init()
icon = pygame.image.load('flappybirdicon.png')
pygame.display.set_icon(icon)
screen = pygame.display.set_mode([284, 512])
pygame.display.set_caption("Flappy Bird")
bg = pygame.image.load('flappybirdbackground.png')
bgrect = bg.get_rect()
clock = pygame.time.Clock()
pipex = 335
class Bird(pygame.sprite.Sprite):
def __init__(self, image, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.pos = [x, y]
class Pipe(pygame.sprite.Sprite):
def __init__(self, image, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.height = height
self.pos = [pipex, height]
def scroll(self):
self.rect.move_ip(-3, 0)
self.pos[0] -= 3
self.rect.center = self.pos
def draw_pipes():
pipe1_height = random.randint(115, screen.get_height())
pipe1 = Pipe('flappybirdpipe.png', pipe1_height)
pipe2_height = 397 - pipe1_height
pipe2 = Pipe('flappybirdpipe2.png', pipe2_height)
screen.blit(pipe1.image, pipe1.rect)
screen.blit(pipe2.image, pipe2.rect)
bird = Bird('flappybirdbird.png', 142, 256)
draw_pipes()
while True:
clock.tick(30)
bird.pos[1] += 5
bird.rect.center = bird.pos
screen.blit(bg, bgrect)
pipe1.scroll()
pipe2.scroll()
screen.blit(bird.image, bird.rect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird.pos[1] -= 75
elif event.type == pygame.MOUSEBUTTONDOWN:
bird.pos[1] -= 75
答案 0 :(得分:0)
我不熟悉PyGame,但看起来你并没有在任何地方为你的管道设置X坐标。您正在设置高度,但从不设置位置。
看起来你似乎并没有在任何地方保存管道数据,所以我想你的代码也会在每次调用draw_pipes()时创建/绘制新的管道,这看起来是每一帧。 / p>