首页 > 解决方案 > 我的性格不会下降。飞扬的小鸟克隆

问题描述

我的性格不会下降。我不知道该怎么办。我的角色只会向上而不向下。我已经尝试了很多事情,但我似乎无法弄清楚该怎么做。我是 Python 新手,所以如果这很明显,那么对不起。

import pygame

black = pygame.Color("#000000")
white = pygame.Color("#FFFFFF")
blue = pygame.Color("#7ec0ee")

pygame.init()

size = 1024,768
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Flappy Bird v.1b")

done = False
clock = pygame.time.Clock()

def ball(x,y):
  pygame.draw.circle(screen,black,[x,y], 20)

def gameover():
  text = render("Game Over!", True, black)
  screen.blit(text, [150, 250])


x = 350
y = 250
x_speed = 0
y_speed = 0
ground = 480

while not done:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        done = True

      if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_UP:
          y_speed = -10

        if event.type == pygame.KEYUP:
          if event.key == pygame.K_UP:
            y_speed = 5

      screen.fill(blue)
      ball(x,y)

      y += y_speed

      if  y > ground:
        gameover()
        y_speed = 0

      pygame.display.flip()
      clock.tick(60)

pygame.quit()

标签: pythonpython-2.7pygame

解决方案


就像@sam-pyt 所说的那样,这似乎是一个缩进问题。我修复了间距不一致的问题,它对我有用。以下是对我有用的代码。

import pygame

black = pygame.Color("#000000")
white = pygame.Color("#FFFFFF")
blue = pygame.Color("#7ec0ee")

pygame.init()

size = 1024,768
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Flappy Bird v.1b")

done = False
clock = pygame.time.Clock()

def ball(x,y):
    pygame.draw.circle(screen,black,[x,y], 20)

def gameover():
    text = render("Game Over!", True, black)
    screen.blit(text, [150, 250])


x = 350
y = 250
x_speed = 0
y_speed = 0
ground = 480

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                y_speed = -10

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                y_speed = 5

    screen.fill(blue)
    ball(x,y)

    y += y_speed

    if  y > ground:
        gameover()
        y_speed = 0

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

推荐阅读