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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
| #ifndef GAME_APP_H #define GAME_APP_H
#include "graphics_engine.h" #include "input_engine.h" #include <stdint.h> #include <stdbool.h>
void game_app_init(void); void game_app_update(void);
#endif
#include "game_app.h" #include "graphics_engine.h" #include "input_engine.h" #include "player.h" #include "bullet.h" #include "enemy.h" #include "background.h" #include "os.h"
typedef enum { GAME_STATE_MENU, GAME_STATE_PLAYING, GAME_STATE_PAUSE, GAME_STATE_GAME_OVER } GameState;
static GameState current_game_state = GAME_STATE_MENU; static Player_t player; static Bullet_t bullets[32]; static uint8_t bullet_count = 0; static Enemy_t enemies[16]; static uint8_t enemy_count = 0; static Background_t background; static uint32_t last_enemy_spawn_time = 0;
static const uint16_t player_bitmap[] = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, }; static const uint16_t bullet_bitmap[] = { COLOR_RED, COLOR_RED, COLOR_RED, COLOR_RED, COLOR_RED, COLOR_RED, COLOR_RED, COLOR_RED, COLOR_RED, }; static const uint16_t enemy_bitmap[] = { COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, COLOR_GREEN, };
void game_app_init(void) {
background_init(&background);
player_init(&player, LCD_WIDTH / 2, LCD_HEIGHT - 20, player_bitmap, 5, 5);
input_engine_register_handler(INPUT_EVENT_JOYSTICK_LEFT_DOWN, handle_input_left); input_engine_register_handler(INPUT_EVENT_JOYSTICK_RIGHT_DOWN, handle_input_right); input_engine_register_handler(INPUT_EVENT_BUTTON_A_DOWN, handle_input_fire); }
void game_app_update(void) {
graphics_engine_clear_screen(COLOR_BLACK);
background_draw(&background);
for (int i = 0; i < enemy_count; i++) { enemy_update(&enemies[i]); enemy_draw(&enemies[i]); if (enemies[i].y > LCD_HEIGHT) { enemy_remove(enemies, &enemy_count, i); i--; } }
for (int i = 0; i < bullet_count; i++) { bullet_update(&bullets[i]); bullet_draw(&bullets[i]); if (bullets[i].y < 0) { bullet_remove(bullets, &bullet_count, i); i--; } }
player_update(&player); player_draw(&player);
for (int i = 0; i < bullet_count; i++) { for (int j = 0; j < enemy_count; j++) { if (bullet_collide_enemy(&bullets[i], &enemies[j])) { bullet_remove(bullets, &bullet_count, i); enemy_remove(enemies, &enemy_count, j); i--; j--; break; } } }
if (os_get_tick_count() - last_enemy_spawn_time > 100) { enemy_spawn(enemies, &enemy_count, enemy_bitmap, 7, 7); last_enemy_spawn_time = os_get_tick_count(); }
graphics_engine_draw_text(10, 10, "射击游戏示例", COLOR_WHITE, COLOR_BLACK);
}
void handle_input_left(InputEventType event) { if (current_game_state == GAME_STATE_PLAYING) { player.x -= 5; if (player.x < 0) player.x = 0; } }
void handle_input_right(InputEventType event) { if (current_game_state == GAME_STATE_PLAYING) { player.x += 5; if (player.x > LCD_WIDTH - player.width) player.x = LCD_WIDTH - player.width; } }
void handle_input_fire(InputEventType event) { if (current_game_state == GAME_STATE_PLAYING && bullet_count < 32) { bullet_spawn(bullets, &bullet_count, player.x + player.width / 2 - 1, player.y, bullet_bitmap, 3, 3); } }
#ifndef PLAYER_H #define PLAYER_H
#include "graphics_engine.h" #include <stdint.h> #include <stdbool.h>
typedef struct { int16_t x, y; const uint16_t *bitmap; int16_t width, height; } Player_t;
void player_init(Player_t *player, int16_t x, int16_t y, const uint16_t *bitmap, int16_t width, int16_t height); void player_update(Player_t *player); void player_draw(Player_t *player);
#endif
#include "player.h" #include "graphics_engine.h"
void player_init(Player_t *player, int16_t x, int16_t y, const uint16_t *bitmap, int16_t width, int16_t height) { player->x = x; player->y = y; player->bitmap = bitmap; player->width = width; player->height = height; }
void player_update(Player_t *player) { }
void player_draw(Player_t *player) { Sprite_t sprite = {player->x, player->y, player->bitmap, player->width, player->height}; graphics_engine_draw_sprite(&sprite); }
#ifndef BULLET_H #define BULLET_H
#include "graphics_engine.h" #include "enemy.h" #include <stdint.h> #include <stdbool.h>
typedef struct { int16_t x, y; const uint16_t *bitmap; int16_t width, height; int16_t speed_y; } Bullet_t;
void bullet_init(Bullet_t *bullet, int16_t x, int16_t y, const uint16_t *bitmap, int16_t width, int16_t height); void bullet_update(Bullet_t *bullet); void bullet_draw(Bullet_t *bullet); void bullet_spawn(Bullet_t *bullets, uint8_t *bullet_count, int16_t x, int16_t y, const uint16_t *bitmap, int16_t width, int16_t height); void bullet_remove(Bullet_t *bullets, uint8_t *bullet_count, uint8_t index); bool bullet_collide_enemy(Bullet_t *bullet, Enemy_t *enemy);
#endif
#include "bullet.h" #include "graphics_engine.h" #include <string.h>
void bullet_init(Bullet_t *bullet, int16_t x, int16_t y, const uint16_t *bitmap, int16_t width, int16_t height) { bullet->x = x; bullet->y = y; bullet->bitmap = bitmap; bullet->width = width; bullet->height = height; bullet->speed_y = -5; }
void bullet_update(Bullet_t *bullet) { bullet->y += bullet->speed_y; }
void bullet_draw(Bullet_t *bullet) { Sprite_t sprite = {bullet->x, bullet->y, bullet->bitmap, bullet->width, bullet->height}; graphics_engine_draw_sprite(&sprite); }
void bullet_spawn(Bullet_t *bullets, uint8_t *bullet_count, int16_t x, int16_t y, const uint16_t *bitmap, int16_t width, int16_t height) { Bullet_t *bullet = &bullets[*bullet_count]; bullet_init(bullet, x, y, bitmap, width, height); (*bullet_count)++; }
void bullet_remove(Bullet_t *bullets, uint8_t *bullet_count, uint8_t index) { if (index < *bullet_count) { memmove(&bullets[index], &bullets[index + 1], (*bullet_count - 1 - index) * sizeof(Bullet_t)); (*bullet_count)--; } }
bool bullet_collide_enemy(Bullet_t *bullet, Enemy_t *enemy) { return (bullet->x < enemy->x + enemy->width && bullet->x + bullet->width > enemy->x && bullet->y < enemy->y + enemy->height && bullet->y + bullet->height > enemy->y); }
#ifndef ENEMY_H #define ENEMY_H
#include "graphics_engine.h" #include <stdint.h> #include <stdbool.h>
typedef struct { int16_t x, y; const uint16_t *bitmap; int16_t width, height; int16_t speed_y; } Enemy_t;
void enemy_init(Enemy_t *enemy, int16_t x, int16_t y, const uint16_t *bitmap, int16_t width, int16_t height); void enemy_update(Enemy_t *enemy); void enemy_draw(Enemy_t *enemy); void enemy_spawn(Enemy_t *enemies, uint8_t *enemy_count, const uint16_t *bitmap, int16_t width, int16_t height); void enemy_remove(Enemy_t *enemies, uint8_t *enemy_count, uint8_t index);
#endif
#include "enemy.h" #include "graphics_engine.h" #include <stdlib.h> #include <string.h>
void enemy_init(Enemy_t *enemy, int16_t x, int16_t y, const uint16_t *bitmap, int16_t width, int16_t height) { enemy->x = x; enemy->y = y; enemy->bitmap = bitmap; enemy->width = width; enemy->height = height; enemy->speed_y = 1; }
void enemy_update(Enemy_t *enemy) { enemy->y += enemy->speed_y; }
void enemy_draw(Enemy_t *enemy) { Sprite_t sprite = {enemy->x, enemy->y, enemy->bitmap, enemy->width, enemy->height}; graphics_engine_draw_sprite(&sprite); }
void enemy_spawn(Enemy_t *enemies, uint8_t *enemy_count, const uint16_t *bitmap, int16_t width, int16_t height) { Enemy_t *enemy = &enemies[*enemy_count]; enemy_init(enemy, rand() % (LCD_WIDTH - width), 0 - height, bitmap, width, height); (*enemy_count)++; }
void enemy_remove(Enemy_t *enemies, uint8_t *enemy_count, uint8_t index) { if (index < *enemy_count) { memmove(&enemies[index], &enemies[index + 1], (*enemy_count - 1 - index) * sizeof(Enemy_t)); (*enemy_count)--; } }
#ifndef BACKGROUND_H #define BACKGROUND_H
#include "graphics_engine.h" #include <stdint.h> #include <stdbool.h>
typedef struct { uint16_t color; } Background_t;
void background_init(Background_t *background); void background_draw(Background_t *background);
#endif
#include "background.h" #include "graphics_engine.h"
void background_init(Background_t *background) { background->color = COLOR_BLACK; }
void background_draw(Background_t *background) { graphics_engine_clear_screen(background->color); }
|