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

جواب «Step 3: Score and Catch» چالش 3 از جلسه‌ی 4

سؤال

حالا امتیاز: score = 0 بساز. یک تابع new_star() بنویس که ستاره را به بالا و یک x تصادفی ببرد. در update() (با global score) اگر basket.colliderect(star) بود، score یکی زیاد شود و new_star()؛ اگر ستاره از پایین رد شد هم new_star(). در draw() بنویس Score: 0 (با عدد امتیاز).

راهنما

screen.draw.text("Score: " + str(score), (10, 10), fontsize=32, color="yellow"). یادت نرود global score اول update().

یک جواب درست

import pgzrun
import random

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

basket = Actor("box", (300, 360))
star = Actor("star", (random.randint(20, 580), 0))
score = 0

def draw():
    screen.fill("black")
    basket.draw()
    star.draw()
    screen.draw.text("Score: " + str(score), (10, 10), fontsize=32, color="yellow")

def new_star():
    star.y = 0
    star.x = random.randint(20, 580)

def update():
    global score
    if keyboard.left:
        basket.x -= 6
    if keyboard.right:
        basket.x += 6
    star.y += 3
    if basket.colliderect(star):
        score += 1
        new_star()
    if star.y > HEIGHT:
        new_star()

pgzrun.go()

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