gpt4 book ai didi

python - 如何添加片尾画面?

转载 作者:行者123 更新时间:2023-12-01 00:50:38 24 4
gpt4 key购买 nike

我尝试了这里和其他地方的不同代码,但我似乎无法让我的最终屏幕工作。

我已经尝试了很多次转换到最终屏幕,但要么代码完全失败,要么我的 Sprite 出现在最终屏幕的顶部。这就是我现在所拥有的,因为它允许我的代码运行。

collide = pygame.sprite.spritecollide(player, enemy_list, False)
if collide:
run = False

我希望当敌方 Sprite 接触玩家 Sprite 时游戏结束,但由于上面的代码使敌方 Sprite 跟随玩家 Sprite ,总是出现错误:“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

# [...]

关于python - 如何添加片尾画面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56591220/

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