python - 我的性格不会下降。飞扬的小鸟克隆
问题描述
我的性格不会下降。我不知道该怎么办。我的角色只会向上而不向下。我已经尝试了很多事情,但我似乎无法弄清楚该怎么做。我是 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()
解决方案
就像@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()
推荐阅读
- python - 自动减少分段函数组件 - Pyomo
- sql - 如何解决字典集合中未找到键的异常
- swift4 - 我必须在我的协调器代码中使用 NavigationController 吗?
- ios - 什么是更好的使用xib或故事板?
- r - RSelenium - 将数据存储在数组中
- javascript - 错误 TS2688:找不到“tooltip.js”的类型定义文件
- javascript - 使用 jquery 如何使元素只能点击一次?
- javascript - 如何修复 React 中的“不变违反”错误
- c++ - 在 C++ 中使用具有已定义主体的宏
- c# - 将正则表达式与我的自定义“BigStringBuilder”类集成