python - 如何添加片尾画面?
问题描述
我从这里和其他地方尝试了不同的代码,但我似乎无法让我的最终屏幕工作。
我已经尝试过多次转换到结束屏幕,但要么代码完全失败,要么我的精灵出现在结束屏幕的顶部。这就是我现在拥有的,因为它允许我的代码运行。
collide = pygame.sprite.spritecollide(player, enemy_list, False)
if collide:
run = False
我希望当敌人精灵接触玩家精灵时游戏结束,但是由于上面的代码使敌人精灵跟随玩家精灵,所以总是出现错误:“float Division 0”。不过,使用错误的代码可能只是我的错。
我不确定其他代码是否会影响最终屏幕代码,但以防万一,这是我的整个代码:
Code is removed for now. Will re-upload in 1 to 2 months
解决方案
给程序添加一个gameover
状态,设置玩家碰撞时的状态。
为游戏结束屏幕创建一个单独的函数。该gameover
函数有自己的事件循环:
def gameOverScreen():
global run, gameover
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
# do event handling which continues the game
# [...]
# if [...]
# gameover = False
# draw the game over screen
# [...]
pygame.display.flip()
clock.tick(100)
gameover
根据主循环中的状态调用此函数。
使用continue
词干立即继续主循环。
gameover = False
run = True
while run:
# [...]
if not gameover and time_difference >= 1500:
# [...]
win.fill(white)
win.blit(background.image, background.rect)
if not pygame.mixer.music.get_busy():
pygame.mixer.music.load('bgm.mp3')
pygame.mixer.music.play()
if gameover:
gameOverScreen()
continue # continue main loop
for e in enemy_list:
e.move(player)
collide = pygame.sprite.spritecollide(player, enemy_list, False)
if collide:
gameover = True
# [...]
推荐阅读
- r - 单击按钮时将 NULL 设置为响应式
- python - 重叠挖掘的对话
- google-apps-script - 谷歌电子表格脚本:发送带有附加电子表格 XLS 的 Outlook 电子邮件
- arrays - 出口使用状态
- php - 错误:无法以非 root 用户身份放弃特权:容器在 Google Cloud Compute Engine 上不断重启
- vue.js - 如何根据当前视图设置全日历可选选项?
- android - Flutter admob 广告未显示。广告加载失败:3
- java - 如何将作为列表对象中的字段的地图流式传输到另一个地图中?
- ruby-on-rails - 如何在生产中运行 Rails 服务器?
- reactjs - 获取“预期的服务器 HTML 以包含匹配
- 在
。在 nextjs 中插入 Converkit 脚本时出错