-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattendagotchi.py
191 lines (145 loc) · 3.99 KB
/
attendagotchi.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# Attendagotchi - Attendee Tracker
from microbit import *
import music
import random
name = "@kianryan"
happiness = 5
thirst = 0
movement = 0
boredom = 0
walking = [ Image("00000:09900:99090:90090:09900"),
Image("00000:09900:90990:90090:09900"),
Image("00000:00990:09099:09009:00990"),
Image("00000:00990:09909:09009:00990")]
last_gx = accelerometer.get_x()
move_count = 0
music_played = False
last_time = running_time()
def increase_happiness():
global happiness
if happiness < 10:
happiness = happiness + 1
display.show(Image.HAPPY)
music.play(music.DADADADUM)
def decrease_happiness():
global happiness
if happiness > 0:
happiness = happiness - 1
display.show(Image.SAD)
music.play(music.WAWAWAWAA)
def show_introduction():
global happiness, music_played
#display.scroll("Hello, I'm " + name + ".")
for _ in range(2):
display.show(walking, loop=False, delay=500)
if happiness > 3:
display.show(Image.HAPPY)
if not music_played:
music.play(music.POWER_UP)
music_played = True
else:
sleep(1000)
elif happiness > 0:
display.show(Image.CONFUSED)
sleep(1000)
else:
display.show(Image.SAD)
if not music_played:
music.play(music.POWER_DOWN)
music_played = True
else:
sleep(1000)
# Play the traditional game of chance
def play_game():
score = 0
for x in range(0, 5):
# Set result
actual = random.choice(["A", "B"])
# Guess
display.show(Image.DIAMOND)
guess = None
while(guess == None):
if button_a.is_pressed():
guess = "A"
elif button_b.is_pressed():
guess = "B"
# Show
if actual == "A":
display.show(Image.ARROW_W)
else:
display.show(Image.ARROW_E)
sleep(1000)
# Judge
if guess == actual:
score = score + 1
display.show(Image.HAPPY)
music.play(music.DADADADUM)
else:
display.show(Image.SAD)
music.play(music.WAWAWAWAA)
return score > 2
def update_counters():
global thirst, move_count, boredom, happiness, last_time
# Update counters by seconds since last
# read.
curr_time = running_time()
interval_time = int((curr_time - last_time) / 1000)
last_time = curr_time
# Thirst
thirst = thirst + interval_time
# Movement
curr_gx = accelerometer.get_x()
if abs(curr_gx - last_gx) > 100:
move_count = move_count + interval_time
else:
move_count = 0
# Boredom
boredom = boredom + interval_time
def update_happiness():
global thirst, movement, boredom, happiness
# Thirst
if thirst > 900: # 15 minutes
decrease_happiness()
thirst = 0
# Movement
if move_count > 30:
movement = 0
increase_happiness()
else:
movement = movement + 1
if movement > 3600:
decrease_happiness()
movement = 0
# Boredom
if boredom > 3600:
happiness = happiness - 1
boredom = 0
def read_buttons():
global thirst, boredom
# Left Button - Drink
if button_a.was_pressed():
display.show(Image.PACMAN)
sleep(1000)
if (thirst > 100):
increase_happiness()
if (thirst < 20):
decrease_happiness()
thirst = 0
# Right Button - Game
if button_b.was_pressed():
boredom = 0
if (play_game() == True):
increase_happiness()
else:
decrease_happiness()
def print_debug():
print("Happiness:" + str(happiness) +
" Thirst:" + str(thirst) +
" Movement:" + str(movement) +
" Boredom:" + str(boredom))
while True:
show_introduction()
print_debug()
update_counters()
update_happiness()
read_buttons()