diff options
author | BoredGuy <osome3717@gmail.com> | 2024-12-31 10:22:43 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2024-12-31 10:22:43 +0300 |
commit | dd83126b20122915ca673f0f385d089743f8bc4b (patch) | |
tree | ff53fc94153b03a6301f048d15352f1885b058f1 | |
parent | df5888dde7755175f4ee300a87c2e983c5b384c4 (diff) |
More Muvement Improvements
- You can now hold a direction to turn to in the next frame
-rw-r--r-- | Week1-Pacman/src/main.c | 6 | ||||
-rw-r--r-- | Week1-Pacman/src/pacman.c | 48 | ||||
-rw-r--r-- | Week1-Pacman/src/pacman.h | 2 |
3 files changed, 26 insertions, 30 deletions
diff --git a/Week1-Pacman/src/main.c b/Week1-Pacman/src/main.c index 0fe38bf..5d2439a 100644 --- a/Week1-Pacman/src/main.c +++ b/Week1-Pacman/src/main.c @@ -35,6 +35,8 @@ int main(int argc, char** argv) { struct map map = {0}; load_map("assets/Maps/maze.csv", &map); + const char* keyboard = + SDL_GetKeyboardState(NULL); for(int i = 0; i < 4; i++) init_animation(&pacman.animations[i], &(struct animation_init){ @@ -76,10 +78,10 @@ int main(int argc, char** argv) { if(e.type == SDL_QUIT) { running = false; } - - handle_pacman_input(&e, &pacman, &map); } + handle_pacman_input(keyboard, &pacman, &map); + int now = SDL_GetTicks(); float dt = (float)(now - then) / 1000; then = now; diff --git a/Week1-Pacman/src/pacman.c b/Week1-Pacman/src/pacman.c index e91e773..5a46a6d 100644 --- a/Week1-Pacman/src/pacman.c +++ b/Week1-Pacman/src/pacman.c @@ -9,7 +9,7 @@ SDL_Rect* get_colliding_tile_raw(SDL_Rect*, struct map* map); SDL_Rect* get_colliding_tile(struct pacman* pacman, struct map* map); -void handle_pacman_input(SDL_Event* e, struct pacman* pacman, struct map* map) { +void handle_pacman_input(const char* keyboard, struct pacman* pacman, struct map* map) { SDL_Rect r = { .x = pacman->xpos, .y = pacman->ypos, @@ -17,32 +17,26 @@ void handle_pacman_input(SDL_Event* e, struct pacman* pacman, struct map* map) { .h = PACMAN_SIDE }; - if(e->type == SDL_KEYDOWN) { - switch(e->key.keysym.sym) { - case SDLK_UP: - r.y--; - if(get_colliding_tile_raw(&r, map) == NULL) - pacman->facing = FACING_UP; - break; - case SDLK_DOWN: - r.y++; - - if(get_colliding_tile_raw(&r, map) == NULL) - pacman->facing = FACING_DOWN; - break; - case SDLK_LEFT: - r.x--; - - if(get_colliding_tile_raw(&r, map) == NULL) - pacman->facing = FACING_LEFT; - break; - case SDLK_RIGHT: - r.x++; - - if(get_colliding_tile_raw(&r, map) == NULL) - pacman->facing = FACING_RIGHT; - break; - } + if(keyboard[SDL_SCANCODE_UP]) { + r.y--; + + if(get_colliding_tile_raw(&r, map) == NULL) + pacman->facing = FACING_UP; + } else if(keyboard[SDL_SCANCODE_DOWN]) { + r.y++; + + if(get_colliding_tile_raw(&r, map) == NULL) + pacman->facing = FACING_DOWN; + } else if(keyboard[SDL_SCANCODE_LEFT]) { + r.x--; + + if(get_colliding_tile_raw(&r, map) == NULL) + pacman->facing = FACING_LEFT; + } else if(keyboard[SDL_SCANCODE_RIGHT]) { + r.x++; + + if(get_colliding_tile_raw(&r, map) == NULL) + pacman->facing = FACING_RIGHT; } } diff --git a/Week1-Pacman/src/pacman.h b/Week1-Pacman/src/pacman.h index c800910..028a73f 100644 --- a/Week1-Pacman/src/pacman.h +++ b/Week1-Pacman/src/pacman.h @@ -26,7 +26,7 @@ struct pacman { struct animation animations[4]; }; -void handle_pacman_input(SDL_Event* e, struct pacman* pacman, struct map* map); +void handle_pacman_input(const char* keyboard, struct pacman* pacman, struct map* map); void update_pacman(struct pacman* pacman, float dt, struct map* map); void draw_pacman(struct demo* demo, const struct pacman* pacman); |