- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在隔离期间开始学习代码,并决定学习如何使用 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/
我正在使用 Assets 管道来管理我的 Grails 3.0 应用程序的前端资源。但是,似乎没有创建 CoffeeScript 文件的源映射。有什么办法可以启用它吗? 我的 build.gradle
我有一个我想要的管道: 提供一些资源, 运行一些测试, 拆资源。 我希望第 3 步中的拆卸任务运行 不管 测试是否通过或失败,在第 2 步。据我所知 runAfter如果前一个任务成功,则只运行一个任
如果我运行以下命令: Measure-Command -Expression {gci -Path C:\ -Recurse -ea SilentlyContinue | where Extensio
我知道管道是一个特殊字符,我需要使用: Scanner input = new Scanner(System.in); String line = input.next
我再次遇到同样的问题,我有我的默认处理方式,但它一直困扰着我。 有没有更好的办法? 所以基本上我有一个运行的管道,在管道内做一些事情,并想从管道内返回一个键/值对。 我希望整个管道返回一个类型为 ps
我有三个环境:dev、hml 和 qa。 在我的管道中,根据分支,阶段有一个条件来检查它是否会运行: - stage: Project_Deploy_DEV condition: eq(varia
我有 Jenkins Jenkins ver. 2.82 正在运行并想在创建新作业时使用 Pipeline 功能。但我没有看到这个列为选项。我只能在自由式项目、maven 项目、外部项目和多配置之间进
在对上一个问题 (haskell-data-hashset-from-unordered-container-performance-for-large-sets) 进行一些观察时,我偶然发现了一个奇
我正在寻找有关如何使用管道将标准输出作为其他命令的参数传递的见解。 例如,考虑这种情况: ls | grep Hello grep 的结构遵循以下模式:grep SearchTerm PathOfFi
有没有办法不因声明性管道步骤而失败,而是显示警告?目前我正在通过添加 || exit 0 来规避它到 sh 命令行的末尾,所以它总是可以正常退出。 当前示例: sh 'vendor/bin/phpcs
我们正在从旧的 Jenkins 设置迁移到所有计划都是声明性 jenkinsfile 管道的新服务器……但是,通过使用管道,我们无法再手动清除工作区。我如何设置 Jenkins 以允许 手动点播清理工
我在 Python 中阅读了有关 Pipelines 和 GridSearchCV 的以下示例: http://www.davidsbatista.net/blog/2017/04/01/docume
我有一个这样的管道脚本: node('linux'){ stage('Setup'){ echo "Build Stage" } stage('Build'){ echo
我正在使用 bitbucket 管道进行培训 这是我的 bitbucket-pipelines.yml: image: php:7.2.9 pipelines: default:
我正在编写一个程序,其中输入文件被拆分为多个文件(Shamir 的 secret 共享方案)。 这是我想象的管道: 来源:使用 Conduit.Binary.sourceFile 从输入中读取 导管:
我创建了一个管道,它有一个应该只在开发分支上执行的阶段。该阶段还需要用户输入。即使我在不同的分支上,为什么它会卡在这些步骤的用户输入上?当我提供输入时,它们会被正确跳过。 stage('Deplo
我正在尝试学习管道功能(%>%)。 当试图从这行代码转换到另一行时,它不起作用。 ---- R代码--原版----- set.seed(1014) replicate(6,sample(1:8))
在 Jenkins Pipeline 中,如何将工件从以前的构建复制到当前构建? 即使之前的构建失败,我也想这样做。 最佳答案 Stuart Rowe 还在 Pipeline Authoring Si
我正在尝试使用 执行已定义的作业构建 使用 Jenkins 管道的方法。 这是一个简单的例子: build('jenkins-test-project-build', param1 : 'some-
当我使用 where 过滤器通过管道命令排除对象时,它没有给我正确的输出。 PS C:\Users\Administrator> $proall = Get-ADComputer -filter *
我是一名优秀的程序员,十分优秀!