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

جواب «Three Lives» چالش 3 از جلسه‌ی 11

سؤال

بمبی از بالا می‌افتد. lives = 3 بساز. هر بار بمب به قهرمان خورد، یک جان کم شود، صدای hit پخش شود و بمب از بالا با x تصادفی برگردد. جان‌ها را بالا سمت راست نشان بده.

راهنما

screen.draw.text("Lives: " + str(lives), topright=(490, 10), fontsize=30) و در update: if bomb.colliderect(hero): lives -= 1 و sounds.hit.play().

یک جواب درست

import pgzrun
import random

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

def draw():
    screen.fill("black")
    hero.draw()
    bomb.draw()
    screen.draw.text("Lives: " + str(lives), topright=(490, 10), fontsize=30)

def update():
    global lives
    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)

pgzrun.go()

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