-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathspheroRiddle.py
90 lines (66 loc) · 2.28 KB
/
spheroRiddle.py
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
79
80
81
82
83
84
85
86
87
88
89
90
from __future__ import print_function
from Kulka import Kulka
from textwrap import dedent
from random import randint
try:
input_ = raw_input
except NameError:
input_ = input
ADDR = 'XX:XX:XX:XX:XX:XX'
"""
This program is a simple implementation of a game, where the user is
supposed to guess the number in a range 1 to 100, while the program
can give him a hint, whether the number provided is greater, equal
or less to the one provided by the player
pulse rate means how close is user to guess the number the smaller
the value is, the closer the user is. range: 0-100
"""
def show_hint_in_sphero(kulka, pulse_rate):
green_channel = abs(255 - int(pulse_rate * 2.55)) % 255
kulka.set_rgb(0, green_channel, 0)
print("pulse rate: %s" % pulse_rate)
print("color: 10, {}, 10".format(green_channel))
def show_hint(kulka, difference):
if difference > 0:
print("My number is greater!")
else:
print("My number is smaller!")
pulse_rate = abs(difference)
show_hint_in_sphero(kulka, pulse_rate)
def game(kulka):
print(dedent("""\
Welcome to the SpheroRiddle game! Guess my number!
If you want to have a hint about how close you are,
take a look at the Sphero. The faster it blinks and
the brighter it shines, the closer you are.
--------------------------------------------------
Type in the number!
"""))
secret_number = randint(1, 100)
guess = ''
trials = 0
while True:
guess = input_("> ")
if guess == 'q' or guess == 'quit':
break
elif guess.isdigit():
guess = int(guess)
trials += 1
if secret_number is not guess:
show_hint(kulka, secret_number - int(guess))
else:
print("Bravo! You've guessed my number in %s trials!" % trials)
break
else:
print("Please input the number or 'q'/'quit' if you want to end "
"the game.")
def play_next():
next_ = input_("Do you want to play another game? (y/n): ")
return next_.lower() in ["y", "yes"]
def main():
with Kulka(ADDR) as kulka:
while True:
game(kulka)
if not play_next():
break
main()