# template for "Guess the number" mini-project
import simplegui import random import math
g_secret_number = -1 g_guess_num = 0
g_has_ok = False g_has_input = False g_status = 'started'
def set_range100():
global g_frame,g_secret_number,g_guess_num,g_handle_input
global g_has_ok,g_has_input
g_frame.set_draw_handler(draw_guessing_text)
g_guess_num = 0
g_secret_number=random.randint(0,100)
if g_has_input == False:
g_handle_input = g_frame.add_input('Input your guess here', guess_input, 50)
g_has_input = True
if g_has_ok == False:
g_has_ok = True
def set_range1000():
global g_frame,g_secret_number,g_guess_num,g_handle_input
global g_has_ok,g_has_input
g_frame.set_draw_handler(draw_guessing_text)
g_guess_num = 0
g_secret_number=random.randint(0,1000)
if g_has_input == False:
g_handle_input = g_frame.add_input('Input your guess here', guess_input, 50)
g_has_input = True
if g_has_ok == False:
g_has_ok = True
def guess_input(canvas):
global g_handle_input,g_secret_number
if g_secret_number == -1:
g_frame.set_draw_handler(draw_cheat_text)
return
str_input = g_handle_input.get_text()
if valid_input(str_input):
input_number=int(str_input)
campare_input(input_number)
else:
g_frame.set_draw_handler(draw_wrong_text)
def campare_input(input_number): global g_secret_number,g_guess_num camp=g_secret_number - input_number g_guess_num+=1 if camp>0:
g_frame.set_draw_handler(draw_higher_text)
elif camp<0:
g_frame.set_draw_handler(draw_lower_text)
else:
print 'you took ' + str(g_guess_num)+' times'
g_frame.set_draw_handler(draw_success_text)
g_secret_number=-1
g_guess_num=0
def valid_input(txt): if txt == '': return False is_int = all(c in "0123456789" for c in txt) if is_int: return True else: return False
def draw_image(canvas): image = simplegui.load_image('http://img4.imgtn.bdimg.com/it/u=754650713,954354305&fm=23&gp=0.jpg') canvas.draw_image(image,(1521 / 2, 1818 / 2), (1521, 1818), (50, 50), (100, 100))
def draw_started_text(canvas): message = 'Welcome to the GUESS NUMBER game' canvas.draw_text(message,(5,90),20,'white') message = 'pls click generate button to get started!' canvas.draw_text(message,(5,120),20,'white')
def draw_guessing_text(canvas): message = 'Number generated!' canvas.draw_text(message,(5,50),20,'white') message = 'Press ENTER to press' canvas.draw_text(message,(5,90),20,'white') message = 'Good Luck' canvas.draw_text(message,(5,120),20,'white')
def draw_cheat_text(canvas): message = 'HAHA, regenerate the number' canvas.draw_text(message,(5,90),20,'red')
def draw_wrong_text(canvas): message = 'POSITIVE NUMBER needed' canvas.draw_text(message,(5,90),20,'red')
def draw_lower_text(canvas): message = 'too high, try again' canvas.draw_text(message,(5,90),20,'red')
def draw_higher_text(canvas): message = 'too low, try again' canvas.draw_text(message,(5,90),20,'red')
def draw_success_text(canvas): global g_guess_num message = 'Your are amazing!' canvas.draw_text(message,(5,90),25,'white') message = 'click generate button to play again' canvas.draw_text(message,(5,150),20,'white') g_guess_num=-1
g_frame = simplegui.create_frame("Let's gusss number",600,300) g_frame.add_button('Generate Number!Range [0,100]',set_range100) g_frame.add_button('Generate Number!nRange [0,1000]',set_range1000)
g_frame.set_draw_handler(draw_started_text) g_frame.start()