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
192
193
|
#include <stdint.h>
#include <stdio.h>
#include "keyb.h"
#include "vga.h"
#include "text.h"
#include "map.h"
#include "data.h"
#include "timer.h"
#include "entities.h"
#include "player.h"
#include "game.h"
#define GAME_LIVES_START 3
#define GAME_LIVES_MAX 9
#define GAME_TIME_MAX 60
#define HUD_CLEAN 0
#define HUD_LIVES 1
#define HUD_SCORE 2
#define HUD_STAGE 4
#define HUD_TIME 8
#define HUD_ALL 255
#define GAMEOVER_DELAY 96
static uint32_t hiscore = 15000;
static uint8_t hud;
static volatile uint8_t clock_updated;
/* game variables */
static uint8_t lives;
static uint32_t score;
static uint8_t stage;
static uint8_t gold;
static uint8_t time;
static uint8_t gameover;
static void hud_render()
{
char b[32];
if (hud & HUD_ALL)
{
Rect src = { 128, 32, 144, 144};
Rect dst = { 4, 0, 16, 16};
/* lives */
blitrc(binary_sprites_start, &src, &dst);
put_text(132, 4, "TIME", 1);
put_text(249, 4, "STAGE", 1);
}
if (hud & HUD_LIVES)
{
sprintf(b, "%d", lives);
put_text(18, 4, b, 1);
}
if (hud & HUD_SCORE)
{
sprintf(b, "%06li", score);
put_text(34, 4, b, 5);
}
if (hud & HUD_TIME)
{
sprintf(b, "%02d", time);
put_text(172, 4, b, time > 10 ? 1 : 15);
}
if (hud & HUD_STAGE)
{
sprintf(b, "%02d", stage + 1);
put_text(297, 4, b, 1);
}
hud = HUD_CLEAN;
}
static void run_gameover()
{
blit_erase(0);
/* restore the HUD after erasing the screen */
hud = HUD_ALL;
hud_render();
put_text(124, 90, "GAME OVER", 1);
wait_vsync();
blit_update();
/* wait some time */
for (uint8_t i = 0; i < 255; i++)
wait_vsync();
}
void run_game()
{
lives = GAME_LIVES_START;
score = 0;
stage = 0;
gold = 30;
time = GAME_TIME_MAX;
hud = HUD_ALL;
blit_erase(0);
map_init(binary_stage_start);
map_render();
entities_draw();
player_draw();
timer_start(GAME_TIME_MAX, &clock_updated);
while (!keys[KEY_ESC])
{
if (clock_updated)
{
time = timer_value();
hud |= HUD_TIME;
}
if (hud)
hud_render();
/* erase first the last we draw */
player_erase();
entities_erase();
player_update();
entities_update();
entities_draw();
player_draw();
wait_vsync();
blit_update();
if (gameover)
{
gameover--;
if (gameover == 1)
{
run_gameover();
break;
}
}
}
/* wait for ESC to be release */
while (keys[KEY_ESC])
wait_vsync();
}
void add_score(uint8_t v)
{
score += v;
hud |= HUD_SCORE;
}
uint32_t get_hiscore()
{
return hiscore;
}
uint8_t dec_lives()
{
lives--;
if (lives == 0)
{
gameover = GAMEOVER_DELAY;
timer_stop();
}
hud |= HUD_LIVES;
return lives;
}
void reset_time()
{
time = GAME_TIME_MAX;
hud |= HUD_TIME;
timer_start(GAME_TIME_MAX, &clock_updated);
}
|