بتل کد Battle Codeدارد باز می‌شود…

جواب «Game Over» چالش 4 از جلسه‌ی 11

سؤال

بازی جان‌ها را ادامه بده: وقتی lives صفر شد، state به "over" تبدیل شود، صدای lose پخش شود، دیگر هیچ‌چیز حرکت نکند و وسط صفحه با حروف بزرگ GAME OVER نوشته شود.

راهنما

بعد از lives -= 1: if lives == 0: state = "over" و sounds.lose.play(). خط اول update: if state != "playing": return. در draw: if state == "over": متن را بنویس.

یک جواب درست

import pgzrun
import random

WIDTH = 500
HEIGHT = 400
hero = Actor("hero", (250, 360))
bomb = Actor("bomb", (250, 0))
lives = 3
state = "playing"

def draw():
    screen.fill("black")
    hero.draw()
    bomb.draw()
    screen.draw.text("Lives: " + str(lives), topright=(490, 10), fontsize=30)
    if state == "over":
        screen.draw.text("GAME OVER", center=(250, 200), fontsize=70, color="red")

def update():
    global lives, state
    if state != "playing":
        return
    if keyboard.right:
        hero.x += 5
    if keyboard.left:
        hero.x -= 5
    bomb.y += 4
    if bomb.top > HEIGHT:
        bomb.y = 0
        bomb.x = random.randint(30, 470)
    if bomb.colliderect(hero):
        lives -= 1
        sounds.hit.play()
        bomb.y = 0
        bomb.x = random.randint(30, 470)
        if lives == 0:
            state = "over"
            sounds.lose.play()

pgzrun.go()

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن