我的角色不会下降。我不知道该怎么办。我的角色只会上演而不是失败。我已经尝试了很多事情,但似乎无法弄清楚该怎么做。我是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()
答案 0 :(得分:0)
就像@ 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()