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

Step 1: Count the Player's Moves چالش 1 از جلسه‌ی 24، آسان

قدم ۱. صفحه، سه دکمه و امتیاز آماده‌اند و کامپیوتر فعلاً تصادفی بازی می‌کند. دیکشنری counts = {"rock": 0, "paper": 0, "scissors": 0} را بساز و در play بعد از انتخاب کامپیوتر، counts[move] را یکی زیاد کن. در draw زیر امتیاز بنویس:
You played: rock 2, paper 1, scissors 0

import pgzrun
import random

WIDTH = 600
HEIGHT = 400
TITLE = "Mind Reader"

MOVES = ["rock", "paper", "scissors"]
BEATS = {"rock": "paper", "paper": "scissors", "scissors": "rock"}
buttons = {
    "rock": Rect(40, 310, 160, 60),
    "paper": Rect(220, 310, 160, 60),
    "scissors": Rect(400, 310, 160, 60),
}
wins = 0
losses = 0
draws = 0
message = "Click a button to play!"

def computer_move():
    return random.choice(MOVES)

def play(move):
    global wins, losses, draws, message
    comp = computer_move()
    if comp == move:
        draws += 1
        message = "Both " + move + ": draw"
    elif BEATS[move] == comp:
        wins += 1
        message = "My " + comp + " beats your " + move
    else:
        losses += 1
        message = "Your " + move + " beats my " + comp

def on_mouse_down(pos):
    for move in MOVES:
        if buttons[move].collidepoint(pos):
            play(move)

def draw():
    screen.fill("navy")
    screen.draw.text(message, center=(300, 30), fontsize=32, color="white")
    score = "Computer " + str(wins) + "   You " + str(losses) + "   Draws " + str(draws)
    screen.draw.text(score, center=(300, 70), fontsize=28, color="gold")
    for move in MOVES:
        screen.draw.filled_rect(buttons[move], "indigo")
        screen.draw.text(move, center=buttons[move].center, fontsize=32, color="white")

pgzrun.go()

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 24: پروژه: سنگ‌کاغذقیچی ذهن‌خوان · چالش بعدی: Step 2: Predict and Beat It

بیشتر بخوان: بازی‌سازی با پایتون و Pygame Zero