-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwheelie.py
More file actions
174 lines (144 loc) · 5.34 KB
/
wheelie.py
File metadata and controls
174 lines (144 loc) · 5.34 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
import pygame, wheelie_files.bike, utils, sys, time
from random import randint
bgX = 0 # Start of the screen
angle = 0
angle_opponent = randint(0, 70)
background = 0
def scroll_background(background, screen):
"""
This function handles the infinite background
scrolling.
- background: pygame.image backgound image
- screen: pygame.display to draw to
"""
global bgX
# Infinite scroll by attaching 2 background images
# and resetting their position once one of them
# is completely out of the screen.
# Based off https://www.youtube.com/watch?v=US3HSusUBeI
rel_x = bgX % background.get_rect().width
mx = rel_x - background.get_rect().width
screen.blit(background, (mx, 0))
if rel_x < utils.width:
screen.blit(background, (rel_x, 0))
bgX -= 5
def end_anim(screen, win):
"""
This function handles the final animation of the wheelie minigame.
- screen: pygame.display to draw to.
- win: Whether or not the user won. Play a sound and
display an image accordingly.
"""
global X
global Y
global background
if win:
img = pygame.image.load("wheelie_files" + utils.sep + "win.png").convert_alpha()
else:
img = pygame.image.load("wheelie_files" + utils.sep + "lose.png").convert_alpha()
screen.fill((0, 0, 0))
screen.blit(background, (0, 0))
img_rect = img.get_rect()
img_rect.center = (utils.width / 2, utils.height/2)
screen.blit(img, img_rect)
# Sound
if win:
pygame.mixer.Sound.play(utils.win_sound).set_volume(utils.volume)
else:
pygame.mixer.Sound.play(utils.lose_sound).set_volume(utils.volume)
# Info
utils.draw_points(screen)
utils.draw_time(screen, utils.time_remaining)
pygame.display.flip()
pygame.time.wait(3000)
def wheelie_game(screen, get_data, decrease_lives):
"""
This function handles the 'wheelie' minigame.
- screen: pygame screen.
- get_data: get_data function to retrieve data
from the microbit.
"""
global angle
global angle_opponent
global background
# Initialise points variable
points_counter = utils.points
# Init bike sprite
X = 350
Y = (utils.height - 15) - (372 / 2) # 372 is the sprite's height, 20 is the floor's height
bike = wheelie_files.bike.Bike(X, Y, False)
bike_sprites = pygame.sprite.Group()
bike_sprites.add(bike)
angle = 0
# Init opponent bike sprite
X = 950
opponent = wheelie_files.bike.Bike(X, Y, True)
opponent_sprites = pygame.sprite.Group()
opponent_sprites.add(opponent)
# Init backgound images
background = pygame.image.load("wheelie_files" + utils.sep + "background.png").convert_alpha()
# Game
pygame.mixer.music.load("wheelie_files" + utils.sep + "music.ogg")
pygame.mixer.music.play(1) # Do not loop the song, play it once. -1 to play in a loop if you ever need it.
seconds_counter = time.time()
mathcing_time_remaining = 3
utils.text_colour = (255, 0, 0)
while True:
if time.time() - seconds_counter > 1:
utils.time_remaining -= 1
mathcing_time_remaining -= 1
# Check if the user has matched the opponent
if mathcing_time_remaining == 0:
mathcing_time_remaining = 3
if abs(angle - angle_opponent) <= 15:
utils.points += 1
angle_opponent = randint(0, 70)
seconds_counter = time.time()
if utils.time_remaining > 0:
utils.run_in_thread(get_data)
utils.check_data_integrity(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((0, 0, 0))
# Backgound
scroll_background(background, screen)
# Check what button has been pressed
if utils.data[0] == 3 and angle > 0: # Down
angle -= 0.5
elif utils.data[0] == 1 and angle < 70: # Up
angle += 0.5
bike_sprites.draw(screen)
opponent_sprites.draw(screen)
bike.wheel_angle(angle)
opponent.wheel_angle(angle_opponent)
bike_sprites.update()
opponent_sprites.update()
# Info
utils.draw_text(screen, "U/D to wheelie", utils.width / 2, 322)
utils.draw_points(screen)
utils.draw_time(screen, utils.time_remaining)
utils.draw_number_counter(screen, mathcing_time_remaining)
utils.run_in_thread(utils.draw_volume(screen))
pygame.display.flip()
utils.clock.tick(60)
else:
pygame.mixer.music.stop()
if utils.points - points_counter < 3:
# If true, the user lost.
decrease_lives()
end_anim(screen, False)
else:
end_anim(screen, True)
utils.minigame_end(screen, get_data)
while True:
get_data()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if type(utils.data[0]) == float and utils.data[0] != 0:
break
break
return