-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
199 lines (192 loc) · 5.65 KB
/
game.c
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
/**
* @file game.c
* @author Lukas Nejezchleb ([email protected]), Petr Kucera ([email protected])
* @brief Module where the game is taking place
* @version 0.1
* @date 2021-05-04
*
* @copyright Copyright (c) 2021
*
*/
#include "game.h"
#include "update_peripherals.h"
#include <stdlib.h>
#include <stdio.h>
#include "draw_shapes.h"
#include "map_to_fb.h"
#include "map_from_template.h"
#include "config.h"
#include "text_fb.h"
#include "font_types.h"
int run_game(game_init_data_t *game_data, peripherals_data_t *peripherals)
{
map_data *map = create_map_data(peripherals->lcd_w, peripherals->lcd_h, game_data->map);
if (map == NULL)
{
// return to show error with malloc
return -1;
}
fb_data fb;
fb.fb = malloc(sizeof(uint16_t) * peripherals->lcd_w * peripherals->lcd_h);
fb.width = peripherals->lcd_w;
fb.height = peripherals->lcd_h;
if (fb.fb == NULL)
{
free(map->board_arr);
free(map);
return -1;
}
pacman_type pacman = create_pacman(map, game_data->pacman_lives, 0);
ghost_type ghost[game_data->ghost_nr];
for (int i = 0; i < game_data->ghost_nr; ++i)
{
ghost[i] = create_ghost(map, i);
}
printf(GAME_START_TERMINAL, KEY_UP, KEY_DWN, KEY_LEFT, KEY_RIGHT, PAUSE_KEY, KEY_QUIT);
// actual game
char read = ' ';
bool coins_to_eat = true;
int scare_countdown = -1;
while ((read != KEY_QUIT) && (read != KEY_QUIT + 'A' - 'a') && (pacman.lives > 0) && (coins_to_eat))
{
for (int i = 0; i < GAME_SPEED; ++i)
{
if (game_tick(map, &pacman, ghost, game_data->ghost_nr, &scare_countdown))
{
break;
}
}
// do the rendering
led_blink(peripherals->led_mem_base, scare_countdown, pacman.score);
coins_to_eat = render_map(map, &fb);
draw_pacman(&pacman, &fb, map);
for (int i = 0; i < game_data->ghost_nr; ++i)
{
draw_ghost(&fb, &ghost[i], map);
}
led_strip_number(peripherals->led_mem_base, game_data->pacman_lives, pacman.lives);
lcd_from_fb(&fb, peripherals->lcd_mem_base);
// get new key
pthread_mutex_lock(&mtx);
read = read_thread_data.last_read;
pthread_mutex_unlock(&mtx);
if (read == PAUSE_KEY || read == PAUSE_KEY + 'A' - 'a')
{
pause_game(&fb, peripherals);
}
}
pthread_mutex_lock(&mtx);
read_thread_data.last_read = ' ';
pthread_mutex_unlock(&mtx);
printf(GAME_END_TERMINAL, pacman.score, pacman.lives);
// termination
free(fb.fb);
free(map->board_arr);
free(map);
return pacman.score;
}
void led_blink(unsigned char *led_mem_base, int scare_countdown, int pacman_score)
{
static int period = 0;
static int time = 0;
static uint32_t color = LED_NORMAL_COLOR;
if ((scare_countdown > 0) && (period == 0))
{ // scared regime began
period = scare_countdown / 30 + 1;
time = 0;
color = LED_SCARE_COLOR2;
}
else if (scare_countdown > 0)
{
time++;
if (time >= period)
{
period = scare_countdown / 30 + 1;
time = 0;
// change color
color = (color == LED_SCARE_COLOR1) ? LED_SCARE_COLOR2 : LED_SCARE_COLOR1;
}
}
else
{
// no scare regime
color = (((uint8_t)(255 * (pacman_score / (float)MAX_SCORE))) << 8);
}
sel_leds_color(led_mem_base, color);
}
bool game_tick(map_data *map, pacman_type *pacman, ghost_type *ghost_arr, int num_ghosts, int *scare)
{
bool ret = false;
if (*scare == 0)
{
for (int j = 0; j < num_ghosts; ++j)
{
ghost_arr[j].scared = false;
ghost_arr[j].moving_randomly = true;
}
ret = true;
}
else if (*scare > 0)
{
(*scare)--;
}
if (pacman_move(pacman, map)) // eaten supercoin
{
*scare = SCARE_REGIME_DURATION;
for (int j = 0; j < num_ghosts; ++j)
{
ghost_arr[j].scared = true;
ghost_arr[j].moving_randomly = true;
}
ret = true;
}
bool scare_regime = false; // find out if by chance all ghost have not been eaten
for (int i = 0; i < num_ghosts; ++i)
{
// move every ghost and check if pacman has not been eaten
if (ghost_move(&ghost_arr[i], map, pacman))
{
*pacman = create_pacman(map, pacman->lives - 1, pacman->score - SCORE_DEATH_PENALTY);
for (int j = 0; j < num_ghosts; ++j)
{
ghost_arr[j] = create_ghost(map, j);
}
pthread_mutex_lock(&mtx);
read_thread_data.last_read = ' ';
pthread_mutex_unlock(&mtx);
ret = true;
break;
}
scare_regime = scare_regime || (ghost_arr[i].scared);
}
if (!scare_regime)
{
(*scare) = -1;
}
return ret;
}
void pause_game(fb_data *fb, peripherals_data_t *peripherals)
{
char read = ' ';
draw_text_center(fb, PAUSE_TEXT, peripherals->lcd_w / 2, peripherals->lcd_h / 2,
PAUSE_TEXT_SIZE, &PAUSE_FONT, PAUSE_COLOR);
char subtext[sizeof(PAUSE_SUBTEXT) + 10];
snprintf(subtext, sizeof(PAUSE_SUBTEXT) + 10, PAUSE_SUBTEXT, PAUSE_KEY, KEY_QUIT);
draw_text_center(fb, subtext, peripherals->lcd_w / 2, peripherals->lcd_h / 2 + PAUSE_SUBTEXT_OFFSET,
PAUSE_SUBTEXT_SIZE, &PAUSE_FONT, PAUSE_COLOR);
lcd_from_fb(fb, peripherals->lcd_mem_base);
while ((read != PAUSE_KEY && read != PAUSE_KEY + 'A' - 'a') && (read != KEY_QUIT && read != KEY_QUIT + 'A' - 'a'))
{
pthread_mutex_lock(&mtx);
pthread_cond_wait(&character_has_been_read, &mtx);
read = read_thread_data.last_read;
pthread_mutex_unlock(&mtx);
}
if (read == PAUSE_KEY || read == PAUSE_KEY + 'A' - 'a')
{
read = ' ';
pthread_mutex_lock(&mtx);
read_thread_data.last_read = ' ';
pthread_mutex_unlock(&mtx);
}
}