summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoredGuy <osome3717@gmail.com>2025-01-02 10:53:09 +0300
committerBoredGuy <osome3717@gmail.com>2025-01-02 10:53:09 +0300
commit1d1554652888fa4f0cd12a51fda8d9bd54dbcee6 (patch)
tree200b6c77e9097d91aeed45cb5712ba2d4022207a
parentdd83126b20122915ca673f0f385d089743f8bc4b (diff)
Movement Finished
- Smooth edge turning means that movement is now convincingly (pacmanny)
-rw-r--r--Week1-Pacman/src/main.c7
-rw-r--r--Week1-Pacman/src/pacman.c82
-rw-r--r--Week1-Pacman/src/pacman.h2
3 files changed, 73 insertions, 18 deletions
diff --git a/Week1-Pacman/src/main.c b/Week1-Pacman/src/main.c
index 5d2439a..66e1278 100644
--- a/Week1-Pacman/src/main.c
+++ b/Week1-Pacman/src/main.c
@@ -9,6 +9,8 @@
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
+const Uint8* keyboard;
+
int main(int argc, char** argv) {
(void)argc;
(void)argv;
@@ -35,7 +37,7 @@ int main(int argc, char** argv) {
struct map map = {0};
load_map("assets/Maps/maze.csv", &map);
- const char* keyboard =
+ keyboard =
SDL_GetKeyboardState(NULL);
for(int i = 0; i < 4; i++)
@@ -80,8 +82,6 @@ int main(int argc, char** argv) {
}
}
- handle_pacman_input(keyboard, &pacman, &map);
-
int now = SDL_GetTicks();
float dt = (float)(now - then) / 1000;
then = now;
@@ -98,5 +98,4 @@ int main(int argc, char** argv) {
SDL_DestroyWindow(demo.win);
SDL_Quit();
return 0;
-
}
diff --git a/Week1-Pacman/src/pacman.c b/Week1-Pacman/src/pacman.c
index 5a46a6d..6b189ae 100644
--- a/Week1-Pacman/src/pacman.c
+++ b/Week1-Pacman/src/pacman.c
@@ -5,11 +5,16 @@
#define PACMAN_SPEED 261
#define PACMAN_SIDE 40
+#define EDGE_TURN_LIMIT 5
+
+extern const Uint8* keyboard;
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(const char* keyboard, struct pacman* pacman, struct map* map) {
+void rect_step_one_pixel(SDL_Rect* r, enum facing);
+
+void handle_pacman_input(struct pacman* pacman, struct map* map) {
SDL_Rect r = {
.x = pacman->xpos,
.y = pacman->ypos,
@@ -17,30 +22,64 @@ void handle_pacman_input(const char* keyboard, struct pacman* pacman, struct map
.h = PACMAN_SIDE
};
- if(keyboard[SDL_SCANCODE_UP]) {
+ if(keyboard[SDL_SCANCODE_UP] && pacman->facing != FACING_UP) {
r.y--;
- if(get_colliding_tile_raw(&r, map) == NULL)
- pacman->facing = FACING_UP;
- } else if(keyboard[SDL_SCANCODE_DOWN]) {
+ for(int i = 0; i < EDGE_TURN_LIMIT + 1; i++) {
+ if(get_colliding_tile_raw(&r, map) == NULL) {
+ pacman->xpos = r.x;
+ pacman->facing = FACING_UP;
+
+ break;
+ }
+
+ rect_step_one_pixel(&r, pacman->facing);
+ }
+ } else if(keyboard[SDL_SCANCODE_DOWN] && pacman->facing != FACING_DOWN) {
r.y++;
- if(get_colliding_tile_raw(&r, map) == NULL)
- pacman->facing = FACING_DOWN;
- } else if(keyboard[SDL_SCANCODE_LEFT]) {
+ for(int i = 0; i < EDGE_TURN_LIMIT + 1; i++) {
+ if(get_colliding_tile_raw(&r, map) == NULL) {
+ pacman->xpos = r.x;
+ pacman->facing = FACING_DOWN;
+
+ break;
+ }
+
+ rect_step_one_pixel(&r, pacman->facing);
+ }
+ } else if(keyboard[SDL_SCANCODE_LEFT] && pacman->facing != FACING_LEFT) {
r.x--;
- if(get_colliding_tile_raw(&r, map) == NULL)
- pacman->facing = FACING_LEFT;
- } else if(keyboard[SDL_SCANCODE_RIGHT]) {
+ for(int i = 0; i < EDGE_TURN_LIMIT + 1; i++) {
+ if(get_colliding_tile_raw(&r, map) == NULL) {
+ pacman->ypos = r.y;
+ pacman->facing = FACING_LEFT;
+
+ break;
+ }
+
+ rect_step_one_pixel(&r, pacman->facing);
+ }
+ } else if(keyboard[SDL_SCANCODE_RIGHT] && pacman->facing != FACING_RIGHT) {
r.x++;
- if(get_colliding_tile_raw(&r, map) == NULL)
- pacman->facing = FACING_RIGHT;
+ for(int i = 0; i < EDGE_TURN_LIMIT + 1; i++) {
+ if(get_colliding_tile_raw(&r, map) == NULL) {
+ pacman->ypos = r.y;
+ pacman->facing = FACING_RIGHT;
+
+ break;
+ }
+
+ rect_step_one_pixel(&r, pacman->facing);
+ }
}
}
void update_pacman(struct pacman* pacman, float dt, struct map* map) {
+ handle_pacman_input(pacman, map);
+
if(pacman->facing == FACING_UP) {
pacman->ypos -= PACMAN_SPEED * dt;
@@ -126,3 +165,20 @@ SDL_Rect* get_colliding_tile_raw(SDL_Rect* r, struct map* map) {
return NULL;
}
+
+void rect_step_one_pixel(SDL_Rect* r, enum facing facing) {
+ switch(facing) {
+ case FACING_UP:
+ r->y--;
+ break;
+ case FACING_DOWN:
+ r->y++;
+ break;
+ case FACING_LEFT:
+ r->x--;
+ break;
+ case FACING_RIGHT:
+ r->x++;
+ break;
+ }
+}
diff --git a/Week1-Pacman/src/pacman.h b/Week1-Pacman/src/pacman.h
index 028a73f..e2a3c4d 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(const char* keyboard, struct pacman* pacman, struct map* map);
+void handle_pacman_input(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);