1: import simplegui
2: import random
3:
4: #define global variables
5: message = "Guess the Number!"
6: message1 = "Range: [0, 100]"
7: code = 0
8: counter = 0
9:
10: def init():
11: global code, counter
12: code = random.randrange(100)
13: counter = 7
14: frame.start()
15: print code
16:
17:
18: # Handler for text input
19: def guess(num):
20: global message, counter
21: cai = int(num)
22: counter -= 1
23: print counter
24: if (cai == code) and (counter > 0):
25: message = "Correct!"
26: elif (cai == code) and (counter <= 0):
27: message = "No more guesses!"
28: elif (cai != code) and (counter <= 0):
29: message = "No more guesses!"
30: else:
31: if cai > code:
32: message = "Lower!"
33: else:
34: message = "Higher!"
35: # Handler for restart
36: def ng_button():
37: init()
38:
39:
40:
41: # Handler to draw on canvas
42: def draw(canvas):
43: canvas.draw_text(message, [25,112], 15, "white")
44: canvas.draw_text(message1, [25,40], 15, "red")
45: canvas.draw_circle((150, 150), 20, 15, "green")
46: if counter >= 0:
47: canvas.draw_text("You have" + " " + str(counter) + " " + "guesses left", [35, 60], 15, "blue")
48: else:
49: canvas.draw_text("You have" + " " + "0" + " " + "guesses left", [35, 60], 15, "blue")
50:
51: # Create a frame and assign callbacks to event handlers
52: frame = simplegui.create_frame("Home", 300, 200)
53: inp = frame.add_input("Guess:", guess, 100)
54: restart = frame.add_button("New Game", ng_button)
55: frame.set_draw_handler(draw)
56:
57: # Start the frame animation
58:
59:
60: init()
61:
62:
Friday, August 2, 2013
Guess the Number with Python
Subscribe to:
Post Comments (Atom)
-
Notes from China Written by Jerod Michel Edited by Gao Rong Cover art and illustrations by Jerod Michel The following is a program ...
-
1: (require 2htdp/image) 2: 3: 4: ;; ================= 5: ;; Constants: 6: 7: (define STEP (/ 2 5)) 8: (define ...
No comments:
Post a Comment