جواب «You Win» چالش 6 از جلسهی 11
سؤال
بازی سکه و بمب: سکه +۱ با صدای coin، بمب یک جان کم با صدای hit. وقتی امتیاز به ۱۰ رسید state = "win" شود، صدای win پخش شود و بنویسد YOU WIN!؛ وقتی جان صفر شد state = "over" با صدای lose و GAME OVER. در هر دو حالت بازی میایستد.
راهنما
چهار صدا، دو حالت پایان. در draw دو if برای "win" و "over". در update اول if state != "playing": return.
یک جواب درست
import pgzrun
import random
WIDTH = 500
HEIGHT = 400
hero = Actor("hero", (250, 200))
coin = Actor("coin", (100, 100))
bomb = Actor("bomb", (400, 300))
score = 0
lives = 3
state = "playing"
def respawn(actor):
actor.x = random.randint(30, 470)
actor.y = random.randint(30, 370)
def draw():
screen.fill("black")
hero.draw()
coin.draw()
bomb.draw()
screen.draw.text("Score: " + str(score), topleft=(10, 10), fontsize=30)
screen.draw.text("Lives: " + str(lives), topright=(490, 10), fontsize=30)
if state == "win":
screen.draw.text("YOU WIN!", center=(250, 200), fontsize=70, color="gold")
if state == "over":
screen.draw.text("GAME OVER", center=(250, 200), fontsize=70, color="red")
def update():
global score, lives, state
if state != "playing":
return
if keyboard.right:
hero.x += 4
if keyboard.left:
hero.x -= 4
if keyboard.up:
hero.y -= 4
if keyboard.down:
hero.y += 4
if hero.colliderect(coin):
score += 1
sounds.coin.play()
respawn(coin)
if score >= 10:
state = "win"
sounds.win.play()
if hero.colliderect(bomb):
lives -= 1
sounds.hit.play()
respawn(bomb)
if lives == 0:
state = "over"
sounds.lose.play()
pgzrun.go()
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن