-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
149 lines (124 loc) · 4.38 KB
/
engine.py
File metadata and controls
149 lines (124 loc) · 4.38 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
import pygame, utils, engine_files.engine_temp, engine_files.smoke, sys, time
from random import randint
# Global variables
X = 0
Y = 0
car = 0
background = 0
def end_anim(screen, win):
"""
This function handles the final animation of a game.
- 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 car
global background
if win:
img = pygame.image.load("engine_files" + utils.sep + "win.png").convert_alpha()
else:
img = pygame.image.load("engine_files" + utils.sep + "lose.png").convert_alpha()
screen.fill((0, 0, 0))
screen.blit(background, (0, 0))
screen.blit(car, (X, Y))
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 engine_game(screen, get_data, decrease_lives):
"""
This function handles the 'wheelie' minigame.
- screen: pygame.display to draw to.
- get_data: get_data function to retrieve data
from the microbit.
"""
global angle
global angle_opponent
global X
global Y
global car
global background
blow_points = 0
old_blow_points = 0
stage = 0
# Load car sprite
car = pygame.image.load("engine_files" + utils.sep + "car.png").convert_alpha()
# Car sprite's coordinates
X = 600
Y = utils.height - 20 - 320 # 320 is the sprite's height
# Init engine temperature sprite
temp_indicator = engine_files.engine_temp.EngineTemp()
indicator_sprites = pygame.sprite.Group()
indicator_sprites.add(temp_indicator)
# Init smoke sprite
smoke = engine_files.smoke.Smoke()
smoke_sprites = pygame.sprite.Group()
smoke_sprites.add(smoke)
# Load backgound image
background = pygame.image.load("engine_files" + utils.sep + "background.png").convert_alpha()
# Game
pygame.mixer.music.load("engine_files" + utils.sep + "music.ogg")
pygame.mixer.music.play(1) # Do not loop the song, play it once
seconds_counter = time.time()
utils.text_colour = (190, 150, 200)
while True:
old_blow_points = blow_points
if stage == 5:
pygame.mixer.music.stop()
end_anim(screen, True)
utils.time_remaining = 0 # Make sure the game stops
if time.time() - seconds_counter > 1:
utils.time_remaining -= 1
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))
# Map the data coming from the microbit to a
# scale of 0 to 100.
# If the engine is not spinning, the pin is floating
# due to the diode protection. Might need adjustement.
blow = utils.map(utils.data[3], 80, 560, 0, 100)
if blow > 70:
blow_points += 0.02
if int(blow_points) > int(old_blow_points):
stage += 1
utils.points += 1
temp_indicator.change_temp(stage)
smoke.animate()
screen.blit(background, (0, 0))
indicator_sprites.draw(screen)
screen.blit(car, (X, Y))
smoke_sprites.draw(screen)
smoke_sprites.update()
indicator_sprites.update()
# Info
utils.draw_text(screen, "Blow on the fan!", utils.width / 2, 322)
utils.draw_points(screen)
utils.draw_time(screen, utils.time_remaining)
utils.run_in_thread(utils.draw_volume(screen))
pygame.display.flip()
utils.clock.tick(60)
else:
pygame.mixer.music.stop()
if stage < 5:
decrease_lives()
end_anim(screen, False)
utils.minigame_end(screen, get_data)
break
return