|
| 1 | +from __future__ import print_function |
| 2 | +from Kulka import Kulka |
| 3 | +from textwrap import dedent |
1 | 4 | from random import randint |
2 | | -from sphero import core |
3 | | -import time |
4 | | -import thread |
5 | | - |
6 | | -# this program is a simple implementation of a game, |
7 | | -# where the user is supposed to guess the number |
8 | | -# in a range 1 to 100, while the program can give |
9 | | -# him a hint, whether the number provided is greater, |
10 | | -# equal or less to the one provided by the player |
11 | | - |
12 | | -# pulse rate means how close is user to guess the number |
13 | | -# the smaller the value is, the closer the user is. |
14 | | -# range: 0-100 |
15 | | - |
16 | | -s = core.Sphero("/dev/tty.Sphero-OOB-AMP-SPP") #create Sphero controller |
17 | | -print "Connecting to Sphero..." |
18 | | -s.connect() #initialize connection |
19 | | - |
20 | | -def show_hint_in_sphero(pulse_rate): |
21 | | - print "pulse rate: %s" % pulse_rate |
22 | | - green_channel=abs(255-int(pulse_rate*2.55))%255 |
23 | | - color="10,%s,10" % (green_channel) |
24 | | - s.set_rgb(0,green_channel,0) |
25 | | - print "color: "+color |
26 | | - |
27 | | -def show_hint(difference): |
28 | | - if difference > 0: |
29 | | - print "My number is greater!" |
30 | | - else: |
31 | | - print "My number is smaller!" |
32 | | - pulse_rate = abs(difference) |
33 | | - show_hint_in_sphero(pulse_rate) |
34 | | - |
35 | | -def game(): |
36 | | - print """ |
37 | | -Welcome to the SpheroRiddle game! Guess my number! |
38 | | -If you want to have a hint about how close you are, |
39 | | -take a look at the Sphero. The faster it blinks and |
40 | | -the brighter it shines, the closer you are. |
41 | | --------------------------------------------------- |
42 | | -Type in the number! |
| 5 | + |
| 6 | +try: |
| 7 | + input_ = raw_input |
| 8 | +except NameError: |
| 9 | + input_ = input |
| 10 | + |
| 11 | + |
| 12 | +ADDR = 'XX:XX:XX:XX:XX:XX' |
| 13 | + |
| 14 | + |
43 | 15 | """ |
| 16 | + This program is a simple implementation of a game, where the user is |
| 17 | + supposed to guess the number in a range 1 to 100, while the program |
| 18 | + can give him a hint, whether the number provided is greater, equal |
| 19 | + or less to the one provided by the player |
| 20 | +
|
| 21 | + pulse rate means how close is user to guess the number the smaller |
| 22 | + the value is, the closer the user is. range: 0-100 |
| 23 | +""" |
| 24 | + |
| 25 | + |
| 26 | +def show_hint_in_sphero(kulka, pulse_rate): |
| 27 | + green_channel = abs(255 - int(pulse_rate * 2.55)) % 255 |
| 28 | + kulka.set_rgb(0, green_channel, 0) |
| 29 | + print("pulse rate: %s" % pulse_rate) |
| 30 | + print("color: 10, {}, 10".format(green_channel)) |
| 31 | + |
| 32 | + |
| 33 | +def show_hint(kulka, difference): |
| 34 | + if difference > 0: |
| 35 | + print("My number is greater!") |
| 36 | + else: |
| 37 | + print("My number is smaller!") |
| 38 | + |
| 39 | + pulse_rate = abs(difference) |
| 40 | + show_hint_in_sphero(kulka, pulse_rate) |
| 41 | + |
| 42 | + |
| 43 | +def game(kulka): |
| 44 | + print(dedent("""\ |
| 45 | + Welcome to the SpheroRiddle game! Guess my number! |
| 46 | + If you want to have a hint about how close you are, |
| 47 | + take a look at the Sphero. The faster it blinks and |
| 48 | + the brighter it shines, the closer you are. |
| 49 | + -------------------------------------------------- |
| 50 | + Type in the number! |
| 51 | + """)) |
| 52 | + |
| 53 | + secret_number = randint(1, 100) |
| 54 | + guess = '' |
| 55 | + trials = 0 |
| 56 | + |
| 57 | + while True: |
| 58 | + guess = input_("> ") |
| 59 | + |
| 60 | + if guess == 'q' or guess == 'quit': |
| 61 | + break |
| 62 | + elif guess.isdigit(): |
| 63 | + guess = int(guess) |
| 64 | + trials += 1 |
| 65 | + |
| 66 | + if secret_number is not guess: |
| 67 | + show_hint(kulka, secret_number - int(guess)) |
| 68 | + else: |
| 69 | + print("Bravo! You've guessed my number in %s trials!" % trials) |
| 70 | + break |
| 71 | + else: |
| 72 | + print("Please input the number or 'q'/'quit' if you want to end " |
| 73 | + "the game.") |
44 | 74 |
|
45 | | - secret_number=randint(1,100) |
46 | | - guess=-1 |
47 | | - trials=0 |
48 | | - end=False |
49 | | - while(not end): |
50 | | - guess=raw_input("> ") |
51 | | - if guess == 'q' or guess == 'quit': |
52 | | - end = True |
53 | | - elif guess.isdigit(): |
54 | | - guess=int(guess) |
55 | | - trials = trials + 1 |
56 | | - if secret_number is not guess: |
57 | | - show_hint(secret_number-guess) |
58 | | - else: |
59 | | - print "Bravo! You've guessed my number in %s trials!" % trials |
60 | | - end=True |
61 | | - else: |
62 | | - print "Please input the number or 'q'/'quit' if you want to end the game." |
63 | 75 |
|
64 | 76 | def play_next(): |
65 | | - next=raw_input("Do you want to play another game? (y/n): ").lower() |
66 | | - return next=="y" or next =="yes" |
| 77 | + next_ = input_("Do you want to play another game? (y/n): ") |
| 78 | + return next_.lower() in ["y", "yes"] |
| 79 | + |
| 80 | + |
| 81 | +def main(): |
| 82 | + with Kulka(ADDR) as kulka: |
| 83 | + while True: |
| 84 | + game(kulka) |
| 85 | + |
| 86 | + if not play_next(): |
| 87 | + break |
67 | 88 |
|
68 | | -game() |
69 | | -while play_next(): |
70 | | - game() |
71 | 89 |
|
| 90 | +main() |
0 commit comments