forked from xhbang/Xadow-digital-watch
/
games_game2.cpp
executable file
·320 lines (276 loc) · 6.13 KB
/
games_game2.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Project: Digital Wristwatch
* Author: Zak Kemble, contact@zakkemble.co.uk
* Copyright: (C) 2013 by Zak Kemble
* License: GNU GPL v3 (see License.txt)
* Web: http://blog.zakkemble.co.uk/diy-digital-wristwatch/
*/
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "games_game2.h"
#include "display.h"
#include "draw.h"
#include "devices_oled.h"
#include "devices_buttons.h"
#include "watchface.h"
#include "devices_buzzer.h"
#include "devices_led.h"
#include "menu.h"
#include "resources.h"
#include "animation.h"
#define UPT_MOVE_NONE 0
#define UPT_MOVE_UP 1
#define UPT_MOVE_DOWN 2
#define CAR_COUNT 3
#define CAR_WIDTH 12
#define CAR_LENGTH 15
#define ROAD_SPEED 6
typedef struct {
// bool ok;
byte x;
byte y;
byte speed;
}s_otherCars;
typedef struct {
bool hit;
byte lane;
byte y;
}s_myCar;
static const byte carImg[] PROGMEM ={
0x40,0xF8,0xEC,0x2C,0x2C,0x38,0xF0,0x10,0xD0,0x30,0xE8,0x4C,0x4C,0x9C,0xF0,
0x02,0x1F,0x37,0x34,0x34,0x1C,0x0F,0x08,0x0B,0x0C,0x17,0x32,0x32,0x39,0x0F,
};
static const byte roadMarking[] PROGMEM ={
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
};
static bool select(void);
static bool down(void);
static bool up(void);
static display_t draw(void);
static const uint eepHighscore EEMEM = 0;
static uint highscore;
static uint score;
static byte uptMove;
static byte lives;
static s_otherCars cars[CAR_COUNT];
void game2_start()
{
menu_close();
srand(millis());
display_setDrawFunc(draw);
buttons_setFunc(BTN_SELECT, select);
buttons_setFunc(BTN_DOWN, down);
buttons_setFunc(BTN_UP, up);
LOOP(CAR_COUNT, i)
{
cars[i].y = i * 16;
cars[i].speed = i + 1;
cars[i].x = FRAME_WIDTH;
}
eeprom_read_block(&highscore, &eepHighscore, sizeof(uint));
score = 0;
uptMove = UPT_MOVE_NONE;
lives = 3;
}
static bool select()
{
if(lives == 255)
game2_start();
else
animation_start(watchface_loadFace, ANIM_MOVE_OFF);
return true;
}
static bool down()
{
uptMove = UPT_MOVE_DOWN;
return true;
}
static bool up()
{
uptMove = UPT_MOVE_UP;
return true;
}
/*
static void music(void)
{
static byte idx;
static const uint tune[] PROGMEM = {
TONE_2KHZ,
TONE_2_5KHZ,
TONE_3KHZ,
TONE_3KHZ,
TONE_3KHZ,
TONE_2_5KHZ,
TONE_2_5KHZ,
TONE_2_5KHZ,
TONE_3KHZ,
TONE_4KHZ,
TONE_3KHZ,
TONE_4KHZ
};
if(!buzzer_buzzing())
{
buzzer_buzz(200, pgm_read_word(&tune[idx++]), VOL_UI);
if(idx > (sizeof(tune) / sizeof(uint)) - 1)
idx = 0;
}
}
*/
static display_t draw()
{
static s_myCar myCar;
static millis_t hitTime;
static bool newHighscore;
// music();
// Change lane
if(uptMove == UPT_MOVE_UP && myCar.lane < 3)
myCar.lane++;
else if(uptMove == UPT_MOVE_DOWN&& myCar.lane > 0)
myCar.lane--;
uptMove = UPT_MOVE_NONE;
// Move to new lane
byte y = myCar.lane * 16;
if(myCar.y > y)
myCar.y -= 2;
else if(myCar.y < y)
myCar.y += 2;
if(lives != 255)
{
// Move cars
LOOP(CAR_COUNT, i)
{
// Move car
cars[i].x -= cars[i].speed;
// Gone off screen
if(cars[i].x > 200 && cars[i].x < 255 - 16)
{
cars[i].x = FRAME_WIDTH;
cars[i].y = (rand() % 4) * 16;
score++;
}
}
// Stop cars from going through each other
// Its a little bit glitchy
LOOP(CAR_COUNT, i)
{
LOOP(CAR_COUNT, c)
{
if(i != c && cars[i].y == cars[c].y && cars[i].x > cars[c].x && cars[i].x < cars[c].x + CAR_LENGTH)
cars[i].x = cars[c].x + CAR_LENGTH + 1;
}
}
// Collision
if(!myCar.hit)
{
LOOP(CAR_COUNT, i)
{
if(cars[i].x < CAR_LENGTH)
{
byte carY = cars[i].y + 2;
byte myCarY = myCar.y + 2;
if((carY >= myCarY && carY <= myCarY + CAR_WIDTH) || (carY + CAR_WIDTH >= myCarY && carY + CAR_WIDTH <= myCarY + CAR_WIDTH))
{
myCar.hit = true;
hitTime = millis();
lives--;
if(lives == 255)
{
// Check for new highscore
if(score > highscore)
{
newHighscore = true;
highscore = score;
eeprom_write_block(&highscore, (uint*)&eepHighscore, sizeof(uint));
}
else
newHighscore = false;
led_flash(LED_RED, 250, 255);
buzzer_buzz(250, TONE_2KHZ, VOL_UI);
}
else
{
led_flash(LED_RED, 30, 255);
buzzer_buzz(100, TONE_2KHZ, VOL_UI);
}
}
}
}
}
}
millis_t now = millis();
if(myCar.hit && now - hitTime >= 1000)
myCar.hit = false;
// Quake
static byte quakeY;
if(myCar.hit && now - hitTime < 350)
{
if(quakeY == 2)
quakeY = -2;
else
quakeY = 2;
}
else
quakeY = 0;
// Draw my car
s_image img = {0, myCar.y + quakeY, carImg, 15, 16, WHITE, NOINVERT, 0};
if(!myCar.hit || (myCar.hit && (now & 64)))
draw_bitmap_s2(&img);
char buff[6];
if(lives != 255)
{
// Draw other cars
LOOPR(CAR_COUNT, i)
{
img.x = cars[i].x;
img.y = cars[i].y + quakeY;
draw_bitmap_s2(&img);
}
// Draw road markings
img.bitmap = roadMarking;
img.width = 8;
img.height = 8;
static byte dotX[3] = {0, 45, 90};
LOOP(3, i)
{
dotX[i] -= ROAD_SPEED;
if(dotX[i] >= FRAME_WIDTH && dotX[i] < 255 - 8)
dotX[i] = FRAME_WIDTH;
img.x = dotX[i];
LOOP(3, x)
{
img.y = (x * 16) + quakeY + 16;
draw_bitmap_s2(&img);
}
}
// Draw score
sprintf_P(buff, PSTR("%u"), score);
draw_string(buff, false, FRAME_WIDTH - (7*5), 1);
// Draw lives
img.bitmap = livesImg;
img.width = 7;
img.y = 1;
LOOP(lives, i)
{
img.x = 32 + (8*i);
draw_bitmap_s2(&img);
}
}
else
{
// Draw end game stuff
draw_string("GAMEOVER!", false, 20, 0);
draw_string("Score:", false, 20, 16);
draw_string("Highscore:", false, 20, 32);
if(newHighscore)
draw_string("!NEW HIGHSCORE!", false, 20, 48);
sprintf_P(buff, PSTR("%u"), score);
draw_string(buff, false, 96, 16);
sprintf_P(buff, PSTR("%u"), highscore);
draw_string(buff, false, 96, 32);
}
return DISPLAY_BUSY;
}