私はかなり新しいプログラマーで、codeskulptor から simplegui の使用方法を学んでいます! 当てっこゲームを作ったり、ボタンが2つあるキャンバスを作ったりしました。1 つのボタンは、推測できる数字の範囲を 0 から 100 に設定し、もう 1 つのボタンは 0 から 1000 に設定します。押すとすべてがリセットされ、フレームから出ることなく再び再生できます。私のコードはhttp://www.codeskulptor.org/#user27_FQ1uDycAiykat13_0.pyで見ることができます。助けてくれてありがとう!
#imports
import math
import simplegui
import random
#global variables
secret_num = random.randint(0, 100)
max_guesses = 7
#define event handlers for control panel
def range100():
global secret_num, max_guesses
secret_num = random.randint(0, 100)
max_guesses = 7
print "You've selected to guess a number between 0 and 100! Good luck!\n"
#button that changes range to [0,100)
def range1000():
global secret_num, max_guesses
secret_num = random.randint(0, 1000)
max_guesses = 10
print "You've selected to guess a number between 0 and 1000! Good luck!\n"
#button that changes range to 0,1000
def get_input(guess):
global count, max_guesses
max_guesses -= 1
num_guessed = int(guess)
if num_guessed == secret_num:
print guess + " IS CORRECT! YOU WIN!\n"
elif max_guesses > 0:
if num_guessed > secret_num:
print "You guessed", num_guessed, "."
print "You need to guess LOWER! You have", max_guesses, "guesses remaining!\n"
elif num_guessed < secret_num:
print "You guessed", num_guessed, "."
print "You need to guess HIGHER! You have", max_guesses, "guesses remaining!\n"
else:
print "YOU LOSE, LOSER! TRY AGAIN FOR BETTER LUCK!"
print "The correct answer was", secret_num
#create a frame
f = simplegui.create_frame('Guessing Game!', 250, 250)
f.set_canvas_background('Cyan')
#create buttons
f.add_button("Guess between (0,100)", range100)
f.add_button("Guess between (0,1000)", range1000)
f.add_button("New game!", get_input)
f.add_input("Enter a guess!", get_input, 200)
f.start()
print "Welcome to Matt Schaefer's guessing game!"
print "Please select the number range you wish to guess, and start your game!\n"