Python交互小游戏(一):Guess the number——Event-driven programming,local/global variables

One of the simplest two-player games is “Guess the number”. The first player thinks of a secret number in some known range while the second player attempts to guess the number. After each guess, the first player answers either “Higher”, “Lower” or “Correct!” depending on whether the secret number is higher, lower or equal to the guess. In this project, you will build a simple interactive program in Python where the computer will take the role of the first player while you play as the second player.

You will interact with your program using an input field and several buttons. For this project, we will ignore the canvas and print the computer’s responses in the console. Building an initial version of your project that prints information in the console is a development strategy that you should use in later projects as well. Focusing on getting the logic of the program correct before trying to make it display the information in some “nice” way on the canvas usually saves lots of time since debugging logic errors in graphical output can be tricky.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# "Guess the number" mini-project
# input will come from buttons and an input field
# all output for the game will be printed in the console
import simplegui
import random
import math

secret_number = None
range = 100
remain_num = 7
is_begin = True


# helper function to start and restart the game
def new_game():
# initialize global variables used in your code here
global secret_number, range, remain_num, is_begin

if(is_begin):
is_begin = False
else:
print
remain_num = int(math.ceil(math.log(range + 1, 2)))
print "New game. Range is from 0 to",range
print "Number of remaining guesses is", remain_num
secret_number = random.randrange(0, range)
# print "secret_number =", secret_number


# define event handlers for control panel
def range100():
# button that changes the range to [0,100) and starts a new game
global range
range = 100
new_game()

def range1000():
# button that changes the range to [0,1000) and starts a new game
global range
range = 1000
new_game()


def input_guess(guess):
# main game logic goes here
global secret_number, remain_num

guess_number = int(guess)
remain_num = remain_num - 1
print
print "Guess was", guess_number
print "Number of remaining guesses is", remain_num

# compares the entered number to secret_number
if(secret_number == guess_number):
print "Correct!"
new_game()
else:
if(secret_number > guess_number): print "Higher!"
elif(secret_number < guess_number): print "Lower!"

if(remain_num <= 0):
print "hahahaha~~ YOU LOSE! The number was", secret_number
new_game()


# create frame
frame = simplegui.create_frame("Guess the number", 200,200)

# register event handlers for control elements and start frame
frame.add_button("Range is [0,100)", range100, 200)
frame.add_button("Range is [0,1000)", range1000, 200)
frame.add_input("Enter a guess", input_guess, 200)
#frame.start()

# call new_game
new_game()
frame.start()