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

جواب «Coin Sound» چالش 2 از جلسه‌ی 11

سؤال

بازی سکه از جلسه‌ی قبل را داری. فقط یک چیز کم دارد: هر بار که قهرمان سکه را می‌گیرد، صدای coin پخش شود (sounds.coin.play()).

راهنما

داخل if hero.colliderect(coin): یک خط اضافه کن: sounds.coin.play().

یک جواب درست

import pgzrun
import random

WIDTH = 500
HEIGHT = 400
hero = Actor("hero", (250, 200))
coin = Actor("coin", (100, 100))
score = 0

def draw():
    screen.fill("purple")
    hero.draw()
    coin.draw()
    screen.draw.text("Score: " + str(score), topleft=(10, 10), fontsize=30)

def update():
    global score
    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()
        coin.x = random.randint(30, 470)
        coin.y = random.randint(30, 370)

pgzrun.go()

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