-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
190 lines (169 loc) · 5.37 KB
/
utils.py
File metadata and controls
190 lines (169 loc) · 5.37 KB
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
import os
import pygame
from threading import Thread
import sys
data = [0, 0, 0]
clock = 0
width = 1600
height = 900
sep = os.path.sep
points = 0
time_remaining = 27
text_colour = (0, 0, 0)
win_sound = 0
lose_sound = 0
done_setup = False
stage = 0
lives = 8
volume = 1
text_size = 100
def map(x, in_min, in_max, out_min, out_max):
"""
From https://www.arduino.cc/reference/en/language/functions/math/map/
Re-maps a number from one range to another.
That is, a value of fromLow would get mapped to toLow,
a value of fromHigh to toHigh, values in-between to values in-between, etc.
- value: the number to map.
- fromLow: the lower bound of the value’s current range.
- fromHigh: the upper bound of the value’s current range.
- toLow: the lower bound of the value’s target range.
- toHigh: the upper bound of the value’s target range.
"""
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def check_data_integrity(screen):
"""
This functions makes sure that the microbit has not been disconnected.
All the items in the array have a value of -1 if the connection is lost.
In case it is, the user needs to restart the game.
- screen: The screen to draw directions on.
"""
global data
if type(data) == bool or type(data[0]) == bool:
data = [0, 0, 0, 0]
if data[0] == -1:
screen.fill((0, 0, 0))
text_colour = (255, 255, 255)
draw_text(screen, "Controller disconnected", width / 2, height / 2)
draw_text(screen, "Please restart the game", width / 2, (height / 2) + 200)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
def run_in_thread(func):
"""
This function is used to run a function
fun in a separate thread.
- func: The function to run in a different thread.
"""
thread = Thread(target=func)
thread.daemon = True
thread.start()
def draw_text(screen, text, X, Y):
"""
This function draws any text to the screen.
- screen: pygame.displayto draw to.
- text: string of the text to draw.
- X: x coordinate of the center of the text.
- Y: y coordinate of the center of the text.
"""
font = pygame.font.Font("fonts/dpcomic/dpcomic.ttf", text_size)
text_img = font.render(text, True, text_colour)
text_rect = text_img.get_rect()
text_rect.center = (X, Y)
screen.blit(text_img, text_rect)
return [text_img, text_rect]
def draw_number_counter(screen, number):
"""
This function draws the remaining time to
complete an action.
- screen: pygame.display to draw to.
- number: int of time remaining.
"""
X = int(width / 2)
Y = 200
draw_text(screen, str(number), X, Y)
def draw_points(screen):
"""
This function draws the points scored by the user
during the game.
- screen: pygame.display to draw to.
"""
X = width - 200
Y = 50
draw_text(screen, "Points: {}".format(points), X, Y)
def draw_time(screen, time):
"""
This function draws the remaining game time.
- screen: pygame.display to draw to.
- time: int of time remaining.
"""
if time == 10:
X = 390
else:
X = 370
Y = 50
draw_text(screen, "Time remaining: {}s".format(time), X, Y)
def draw_volume(screen):
global volume
X = 280
Y = 150
draw_text(screen, "Volume: {}%".format(int(volume * 100)), X, Y)
def minigame_recap(screen):
"""
This function is called to display the recap screen
after each minigame.
- screen: pygame.display to draw to.
"""
# Reset time
time_remaining = 27
screen.fill((0, 0, 0))
# Points
font = pygame.font.Font("fonts/dpcomic/dpcomic.ttf", 100)
points_img = font.render("Points scored: {}".format(points), True, (255, 255, 255))
points_rect = points_img.get_rect()
X = int(width / 2)
Y = int(height / 5)
points_rect.center = (X, Y)
# Lives
lives_img = font.render("Lives: {}".format(lives), True, (255, 255, 255))
lives_rect = lives_img.get_rect()
X = int(width / 2)
Y = int(height / 2)
lives_rect.center = (X, Y)
# Continue
continue_img = font.render("Press any button to continue", True, (255, 255, 255))
continue_rect = continue_img.get_rect()
X = int(width / 2)
Y = height - int(height / 5)
continue_rect.center = (X, Y)
# Draw
screen.blit(points_img, points_rect)
screen.blit(lives_img, lives_rect)
screen.blit(continue_img, continue_rect)
pygame.display.flip()
def minigame_end(screen, get_data):
"""
This function is used to handle the break after a minigame.
It calls minigame_recap to display infromation and then wait for
input from the user to return.
- screen: pygame.display to draw to.
- get_data: get_data function to retrieve data
from the microbit.
"""
minigame_recap(screen)
while True:
get_data()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if type(data[0]) == float and data[0] != 0:
break
return
def update_volume():
global volume
volume = map(data[2], 0, 1023, 0, 1)
print("Volume: {}".format(volume))
pygame.mixer.music.set_volume(volume)