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

جواب «Step 4: Catch It!» چالش 4 از جلسه‌ی 6

سؤال

آخرین قدم: گرفتن ستاره. در update() بازی قدم ۳، بعد از افتادن ستاره، اگر star.bottom > cat.top بود و abs(star.x - cat.x) < 40 بود، یعنی گربه ستاره را گرفته: score را یکی زیاد کن و reset_star() را صدا بزن. global score یادت نرود. بازی‌ات کامل شد!

راهنما

if star.bottom > cat.top and abs(star.x - cat.x) < 40: و زیرش score += 1 و reset_star().

یک جواب درست

import pgzrun
import random

WIDTH = 600
HEIGHT = 400
TITLE = "Catch the Stars"

cat = Actor("cat")
cat.midbottom = (300, HEIGHT)
star = Actor("star", (300, 0))
score = 0

def reset_star():
    star.x = random.randint(20, 580)
    star.bottom = 0

def draw():
    screen.fill("navy")
    cat.draw()
    star.draw()
    screen.draw.text("Score: " + str(score), topleft=(10, 10), fontsize=30)

def update():
    global score
    if keyboard.left:
        cat.x -= 6
    if keyboard.right:
        cat.x += 6
    if cat.left < 0:
        cat.left = 0
    if cat.right > WIDTH:
        cat.right = WIDTH
    star.y += 4
    if star.bottom > cat.top and abs(star.x - cat.x) < 40:
        score += 1
        reset_star()
    if star.top > HEIGHT:
        reset_star()

pgzrun.go()

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