Step 1: Walk Up and Ask چالش 1 از جلسهی 40، آسان
بازی آماده است: با فلشها راه برو؛ تابع near() میگوید کنار بولت هستی یا نه. on_key_down را کامل کن:
- اگر key == keys.RETURN: اگر near() بود، reply = ask(typed)؛ وگرنه reply = "Come closer to Bolt first.". بعد typed = "".
- elif key == keys.BACKSPACE: یک حرف از آخر typed پاک کن.
- elif unicode and len(typed) < 40: حرف را به typed اضافه کن.
اجرا کن، نزدیک برو و بپرس!
import pgzrun
from ai import ask
WIDTH = 600
HEIGHT = 400
TITLE = "Bolt the Talking Robot"
hero = Actor("hero", (100, 250))
bolt = Actor("robot", (450, 150))
typed = ""
reply = "Walk to Bolt and type a question."
def update():
if keyboard.left:
hero.x -= 3
if keyboard.right:
hero.x += 3
if keyboard.up:
hero.y -= 3
if keyboard.down:
hero.y += 3
def near():
return hero.distance_to(bolt) < 120
def on_key_down(key, mod, unicode):
global typed, reply
pass
def draw():
screen.fill("darkgreen")
hero.draw()
bolt.draw()
screen.draw.filled_rect(Rect(0, 330, 600, 70), "black")
screen.draw.text("> " + typed, (10, 338), fontsize=24, color="white")
screen.draw.text(reply[:70], (10, 368), fontsize=20, color="yellow")
pgzrun.go()
راهنما و جواب این چالش · برگشت به درس جلسهی 40: پروژه: شخصیت سخنگوی بازی · چالش بعدی: Step 2: Bolt Knows the Game
بیشتر بخوان: بازیسازی با پایتون و Pygame Zero