gpt4 book ai didi

python - 每当使用 pygame 的鸟和管道 x 位置相等时,我如何写一些东西来改变我在 flappy bird 中的分数?

转载 作者:行者123 更新时间:2023-12-04 15:18:17 25 4
gpt4 key购买 nike

我在隔离期间开始学习代码,并决定学习如何使用 pygame(因为我只知道 python 的基础知识),我在 youtube 上学习了一个名为“通过制作 flappy bird 学习 pygame”的教程。教程很好,但是那家伙忘记了计分在游戏中是如何工作的,而是写了一个计时器来给分数加1。在过去一周左右的时间里,我一直在尝试让游戏识别小鸟和管道的 x 位置是否相同,以便为分数增加 1 分,但我一直无法弄清楚。我还没有写任何关于分数变化的东西,所以如果有人能伸出援手,我将不胜感激。如果代码有点困惑,我很抱歉,就像我说的,我刚开始学习。

import pygame
import sys
import random


def draw_floor():
screen.blit(floor_surface, (floor_x_pos, 440))
screen.blit(floor_surface, (floor_x_pos + 288, 440))


def create_pipe():
new_pipe_pos = random.choice(pipe_height)
bottom_pipe = pipe_surface.get_rect(midtop=(400, new_pipe_pos))
top_pipe = pipe_surface.get_rect(midbottom=(400, new_pipe_pos - 110))
return top_pipe, bottom_pipe


def move_pipes(pipes):
for pipe in pipes:
pipe.centerx -= 3
return pipes


def draw_pipes(pipes):
for pipe in pipes:
if pipe.bottom >= 400:
screen.blit(pipe_surface, pipe)
else:
flip_pipe = pygame.transform.flip(pipe_surface, False, True)
screen.blit(flip_pipe, pipe)


def check_collision(pipes):
for pipe in pipes:
if bird_rect.colliderect(pipe):
return False
if bird_rect.top <= - 100 or bird_rect.bottom >= 460:
return False
return True


def rotate_bird(bird):
new_bird = pygame.transform.rotozoom(bird_surface, -bird_mvt * 3, 1)
return new_bird


def bird_animation():
new_bird = bird_frame[bird_index]
new_bird_rect = new_bird.get_rect(center=(35, bird_rect.centery))
return new_bird, new_bird_rect


def score_display():
score_surface = game_font.render(str(score), True, (255, 255, 255))
score_rect = score_surface.get_rect(center=(144, 35))
screen.blit(score_surface, score_rect)


pygame.init()
screen = pygame.display.set_mode((288, 512))
pygame.display.set_caption('Flappy Bird')
clock = pygame.time.Clock()

# Variables
gravity = 0.25
bird_mvt = 0
game_active = True
score = 0
highscore = 0
game_font = pygame.font.Font('04B_19.TTF', 20)

bg_surface = pygame.image.load('background-day.png').convert()
floor_surface = pygame.image.load('base.png').convert()
floor_x_pos = 0

bird_upflap = pygame.image.load('yellowbird-upflap.png').convert_alpha()
bird_midflap = pygame.image.load('yellowbird-midflap.png').convert_alpha()
bird_downflap = pygame.image.load('yellowbird-downflap.png').convert_alpha()
bird_frame = [bird_downflap, bird_midflap, bird_upflap]
bird_index = 0
bird_surface = bird_frame[bird_index]
bird_rect = bird_surface.get_rect(center=(35, 256))

pipe_list = []
pipe_height = [256, 280, 144]
pipe_surface = pygame.image.load('pipe-green.png').convert()
SPAWNPIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE, 1500)

BIRDFLAP = pygame.USEREVENT + 1
pygame.time.set_timer(BIRDFLAP, 200)

# Game Loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_active:
bird_mvt = 0
bird_mvt -= 8
if event.key == pygame.K_SPACE and game_active is False:
if event.key == pygame.K_SPACE:
game_active = True
pipe_list.clear()
bird_rect.center = (35, 256)
bird_mvt = 0
if event.type == SPAWNPIPE:
pipe_list.extend(create_pipe())
if event.type == BIRDFLAP:
if bird_index < 2:
bird_index += 1
else:
bird_index = 0
bird_surface, bird_rect = bird_animation()
screen.blit(bg_surface, (0, 0))

if game_active:
# Bird
bird_mvt += gravity
rotated_bird = rotate_bird(bird_surface)
bird_rect.centery += bird_mvt
screen.blit(rotated_bird, bird_rect)
game_active = check_collision(pipe_list)

# Pipes
pipe_list = move_pipes(pipe_list)
draw_pipes(pipe_list)
score_display()

# Floor
floor_x_pos -= 1
draw_floor()
clock.tick(100)
if floor_x_pos <= -288:
floor_x_pos = 0
pygame.display.update()

最佳答案

因此,以上评论中提出的观点 100% 正确,并且是正确的处理方法。在您的游戏中,您有一个可以利用的简化,这可能会与您的技能水平相适应。

您的游戏以恒定速率连续滚动……您似乎每帧将管道向左移动 3 个单位。所以,你的挑战更具体。您只需要跟踪其中一根管道何时“通过”鸟的 x 坐标。

注意:在一个更复杂的游戏中,你可能会有上面提到的几个检查......

对于您的构建,您有一个管道列表和实例变量 centerx。因此,您可以创建一个快速函数来检查所有管道——类似于您的 move_pipes 函数,以查看中心是否在鸟的 x 坐标的 1 次移动范围内。在伪代码中:

for pipe in pipes:
if bird.x - step_size < pipe.centerx <= bird.x:
# add to score

假设 step_size 可能是移动量的全局变量,您将在其他代码中使用它以确保其准确。无论您为鸟的 x 坐标设置什么,都可以使用,这在您的游戏中是固定的。

另外...您会注意到您正在继续添加到您的管道列表中。如果您擅长您的游戏,这最终会导致一些迟缓,因为 pygame 仍然会尝试绘制这些东西,即使它们在可 window 口之外。 (它会做数学运算,但你不会看到任何东西)。您不必担心列表的大小,它可以毫无问题地达到数百万,但是您可以通过检查绘制函数中的 x 坐标来限制绘制它们……类似于您现在对 y 坐标所做的。 ..

if pipe.centerx < -10:
pass
else:
if pipe.bottom ...:
# draw stuff
else:
# draw other stuff

关于python - 每当使用 pygame 的鸟和管道 x 位置相等时,我如何写一些东西来改变我在 flappy bird 中的分数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63930308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com