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

جواب «Whack the Mole» چالش 5 از جلسه‌ی 7

سؤال

موش (mouse) در جایی از صفحه نشسته است. هر بار که رویش کلیک کنی، یک امتیاز بگیر و موش به یک جای تصادفی تازه بپرد (random.randint برای x بین ۵۰ تا ۴۵۰ و y بین ۵۰ تا ۳۵۰). امتیاز را نشان بده.

راهنما

بعد از if mole.collidepoint(pos): بنویس mole.x = random.randint(50, 450) و همین‌طور برای y.

یک جواب درست

import pgzrun
import random

WIDTH = 500
HEIGHT = 400
mole = Actor("mouse", (250, 200))
score = 0

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

def on_mouse_down(pos):
    global score
    if mole.collidepoint(pos):
        score = score + 1
        mole.x = random.randint(50, 450)
        mole.y = random.randint(50, 350)

pgzrun.go()

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