如何为飞扬的小鸟制作记分牌?
How to make a scoreboard for the flappy bird?
我正在为 Flappy Bird 制作游戏,我需要制作一个计分器,它就在那里,但它是歪的。它在第一个管道之后计算了一大堆点,并且在死亡后不会重置它们。下面我会附上代码本身和积分的代码。
如果你能写出代码就正确了。
import pygame#импортирование модуля pygame
import random#Добавляет возможность спавнить разные трубы
pygame.init()
WIDTH, HEIGHT = 800, 600#FPS и размеры окна
FPS = 60
window = pygame.display.set_mode((WIDTH, HEIGHT))#Создание окна
clock = pygame.time.Clock()
font1 = pygame.font.Font(None, 35)#шрифты
font2 = pygame.font.Font(None, 80)
imgBG = pygame.image.load('images/fon.png')#Добавление картинок
imgBIRD = pygame.image.load('images/bird.png')
imgPB = pygame.image.load('images/PB.png')
imgPT = pygame.image.load('images/PT.png')
py, sy, ay = HEIGHT // 2, 0, 0 #Значение по py, sy, ay
player = pygame.Rect(HEIGHT // 3, py, 50, 50) #расположение игрока по центру
state = 'start'#состояние старта
timer = 10 #Задержка
pipes = [] #Список труб
bges = [] #Список заднего фона
bges.append(pygame.Rect(0, 0, 288, 600)) #Отображение заднего фона
lives = 3 #Жизни
scores = 0 #Очки
play = True
while play:
for event in pygame.event.get():
if event.type == pygame.QUIT:
play = False
press = pygame.mouse.get_pressed() #Нажатие мыши
keys = pygame.key.get_pressed() #Нажатие пробела
click = press[0] or keys[pygame.K_SPACE]
if timer > 0:
timer -= 1
for i in range(len(bges)-1, -1, -1): #Проверка на удаление и добавление фона
fon = bges[i]
fon.x -= 1
if fon.right < 0:
bges.remove(fon)
if bges[len(bges)-1].right <= WIDTH:
bges.append(pygame.Rect(bges[len(bges)-1].right, 0, 288, 512))##########
for i in range(len(pipes)-1, -1, -1): #Добавление и удаление труб
pipe = pipes[i]
pipe.x -= 3
if pipe.right < 0:
pipes.remove(pipe)###################
if state == 'start':#Состояние статического
if click and timer == 0 and len(pipes) == 0:
state = 'play'
py += (HEIGHT // 2 - py) * 0.1
player.y = py
elif state == 'play': #Состояние если игрок нажимает на кнопку то оно переходит в состояние игры
if click:
ay = -2
else:
ay = 0
py += sy
sy = (sy + ay + 1) * 0.98
player.y = py
if len(pipes) == 0 or pipes[len(pipes)-1].x < WIDTH - 200: #отображение труб
pipes.append(pygame.Rect(WIDTH, 0, 52, 200))
pipes.append(pygame.Rect(WIDTH, 400, 52, 200))
if player.top < 0 or player.bottom > HEIGHT:#Проверка столкновение с трубами
state = 'fall'
for pipe in pipes:
if player.colliderect(pipe):
state = 'fall'
for pipe in pipes:
if player > pipe:
scores += 1
elif state == 'fall':####Состояние когда игрок не может ничего делать
sy, ay = 0, 0
state = 'start'
timer = 60
else:
pass
for fon in bges: #Отображение заднего фона
window.blit(imgBG, fon)##########
for pipe in pipes:#Рисовка труб
if pipe.y == 0:
rect = imgPB.get_rect(bottomleft = pipe.bottomleft)
window.blit(imgPB, rect)
else:
rect = imgPT.get_rect(topleft = pipe.topleft)
window.blit(imgPT, rect)##########
window.blit(imgBIRD, player)#Рисовка игрока
text = font1.render('Очки: ' + str(scores), 1, pygame.Color('White')) #Текст очки
window.blit(text, (10, 10))
text = font1.render('Жизни: ' + str(lives), 1, pygame.Color('White'))#Текст жизни
window.blit(text, (110, 10))
pygame.display.update()#Обновление экрана
clock.tick(FPS)
pygame.quit()
代码分数
for pipe in pipes:
if player > pipe:
scores += 1
您应该将 if player > pipe:
替换为 if pipe.x < player <= (pipe.x + 3):
以修复游戏并添加一堆分数(而不是为到目前为止通过的每个管道的分数添加 1,如果您是,它会添加 1 在 管道中)。
通过管道的速度更改 pipe + 3
的 3
(如果您的程序是 3,所以我输入 3)。
分数不重置,建议更换这个
elif state == 'fall'
sy, ay = 0, 0
state = 'start'
timer = 60
由此
elif state == 'fall'
sy, ay = 0, 0
state = 'start'
timer = 60
scores = 0
假设这是在 'death' 上做事的部分。
不确定它是否会起作用,但它可能会起作用。
编辑:
对于像 TypeError
这样的任何问题,请将之前测试 (if pipe < player <= (pipe + 3):
) 中的 pipe
替换为 pipe.x
,这样它就会得到 if pipe.x < player <= (pipe.x + 3):
。它现在应该可以工作了...
我正在为 Flappy Bird 制作游戏,我需要制作一个计分器,它就在那里,但它是歪的。它在第一个管道之后计算了一大堆点,并且在死亡后不会重置它们。下面我会附上代码本身和积分的代码。
如果你能写出代码就正确了。
import pygame#импортирование модуля pygame
import random#Добавляет возможность спавнить разные трубы
pygame.init()
WIDTH, HEIGHT = 800, 600#FPS и размеры окна
FPS = 60
window = pygame.display.set_mode((WIDTH, HEIGHT))#Создание окна
clock = pygame.time.Clock()
font1 = pygame.font.Font(None, 35)#шрифты
font2 = pygame.font.Font(None, 80)
imgBG = pygame.image.load('images/fon.png')#Добавление картинок
imgBIRD = pygame.image.load('images/bird.png')
imgPB = pygame.image.load('images/PB.png')
imgPT = pygame.image.load('images/PT.png')
py, sy, ay = HEIGHT // 2, 0, 0 #Значение по py, sy, ay
player = pygame.Rect(HEIGHT // 3, py, 50, 50) #расположение игрока по центру
state = 'start'#состояние старта
timer = 10 #Задержка
pipes = [] #Список труб
bges = [] #Список заднего фона
bges.append(pygame.Rect(0, 0, 288, 600)) #Отображение заднего фона
lives = 3 #Жизни
scores = 0 #Очки
play = True
while play:
for event in pygame.event.get():
if event.type == pygame.QUIT:
play = False
press = pygame.mouse.get_pressed() #Нажатие мыши
keys = pygame.key.get_pressed() #Нажатие пробела
click = press[0] or keys[pygame.K_SPACE]
if timer > 0:
timer -= 1
for i in range(len(bges)-1, -1, -1): #Проверка на удаление и добавление фона
fon = bges[i]
fon.x -= 1
if fon.right < 0:
bges.remove(fon)
if bges[len(bges)-1].right <= WIDTH:
bges.append(pygame.Rect(bges[len(bges)-1].right, 0, 288, 512))##########
for i in range(len(pipes)-1, -1, -1): #Добавление и удаление труб
pipe = pipes[i]
pipe.x -= 3
if pipe.right < 0:
pipes.remove(pipe)###################
if state == 'start':#Состояние статического
if click and timer == 0 and len(pipes) == 0:
state = 'play'
py += (HEIGHT // 2 - py) * 0.1
player.y = py
elif state == 'play': #Состояние если игрок нажимает на кнопку то оно переходит в состояние игры
if click:
ay = -2
else:
ay = 0
py += sy
sy = (sy + ay + 1) * 0.98
player.y = py
if len(pipes) == 0 or pipes[len(pipes)-1].x < WIDTH - 200: #отображение труб
pipes.append(pygame.Rect(WIDTH, 0, 52, 200))
pipes.append(pygame.Rect(WIDTH, 400, 52, 200))
if player.top < 0 or player.bottom > HEIGHT:#Проверка столкновение с трубами
state = 'fall'
for pipe in pipes:
if player.colliderect(pipe):
state = 'fall'
for pipe in pipes:
if player > pipe:
scores += 1
elif state == 'fall':####Состояние когда игрок не может ничего делать
sy, ay = 0, 0
state = 'start'
timer = 60
else:
pass
for fon in bges: #Отображение заднего фона
window.blit(imgBG, fon)##########
for pipe in pipes:#Рисовка труб
if pipe.y == 0:
rect = imgPB.get_rect(bottomleft = pipe.bottomleft)
window.blit(imgPB, rect)
else:
rect = imgPT.get_rect(topleft = pipe.topleft)
window.blit(imgPT, rect)##########
window.blit(imgBIRD, player)#Рисовка игрока
text = font1.render('Очки: ' + str(scores), 1, pygame.Color('White')) #Текст очки
window.blit(text, (10, 10))
text = font1.render('Жизни: ' + str(lives), 1, pygame.Color('White'))#Текст жизни
window.blit(text, (110, 10))
pygame.display.update()#Обновление экрана
clock.tick(FPS)
pygame.quit()
代码分数
for pipe in pipes:
if player > pipe:
scores += 1
您应该将 if player > pipe:
替换为 if pipe.x < player <= (pipe.x + 3):
以修复游戏并添加一堆分数(而不是为到目前为止通过的每个管道的分数添加 1,如果您是,它会添加 1 在 管道中)。
通过管道的速度更改 pipe + 3
的 3
(如果您的程序是 3,所以我输入 3)。
分数不重置,建议更换这个
elif state == 'fall'
sy, ay = 0, 0
state = 'start'
timer = 60
由此
elif state == 'fall'
sy, ay = 0, 0
state = 'start'
timer = 60
scores = 0
假设这是在 'death' 上做事的部分。
不确定它是否会起作用,但它可能会起作用。
编辑:
对于像 TypeError
这样的任何问题,请将之前测试 (if pipe < player <= (pipe + 3):
) 中的 pipe
替换为 pipe.x
,这样它就会得到 if pipe.x < player <= (pipe.x + 3):
。它现在应该可以工作了...