From 5b242e50fd17332f3557eb4d23417be8c79595d6 Mon Sep 17 00:00:00 2001 From: netrunner1303 Date: Mon, 21 Oct 2019 23:30:46 +0200 Subject: [PATCH 1/4] Add Comments and made structure better --- main.c | 47 ++++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/main.c b/main.c index e4e215c..d6300a2 100644 --- a/main.c +++ b/main.c @@ -24,6 +24,7 @@ int input (void); int main(int argc, char* argv[]) { /* BEGINNING OF SDL INIT */ + // // initialize SDLs video and timer sdubsystem if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) { printf("Fehler beim Initialisieren von SDL: %s\n", SDL_GetError()); @@ -75,6 +76,8 @@ int main(int argc, char* argv[]) { SDL_Quit(); return 1; } + + // create texture from surface which the renderer can draw to the backgroud SDL_Texture* tex_backgr = SDL_CreateTextureFromSurface(rend, surf_backgr); if (tex_backgr == NULL) { printf("Textur konnte nicht erstellt werden: %s\n", SDL_GetError()); @@ -83,6 +86,7 @@ int main(int argc, char* argv[]) { SDL_Quit(); return 1; } + // we can free the surface afterwards SDL_FreeSurface(surf_sheet); SDL_FreeSurface(surf_backgr); @@ -91,15 +95,19 @@ int main(int argc, char* argv[]) { // structs for coordinates and dimensions of certain elements SDL_Rect fruit; - SDL_Rect snake[50]; //Maximale Länge von 50 Teilen -> Später durch dynamisches array oder linked list ersetzen! - /*snake[0].w = 64 / 2; //Kopf - snake[0].h = 64 / 2; - snake[1].w = 64 / 2; - snake[1].h = 64 / 2; - snake[2].w = 64 / 2; - snake[2].h = 64 / 2; - snake[3].w = 64 / 2; - snake[3].h = 64 / 2; //ende */ + + //Maximale Länge von 50 Teilen -> Später durch dynamisches array oder linked list ersetzen! + SDL_Rect snake[50]; + /* + * snake[0].w = 64 / 2; // -> Head + * snake[0].h = 64 / 2; + * snake[1].w = 64 / 2; + * snake[1].h = 64 / 2; + * snake[2].w = 64 / 2; + * snake[2].h = 64 / 2; + * snake[3].w = 64 / 2; + * snake[3].h = 64 / 2; // -> Tail + */ @@ -130,6 +138,7 @@ int main(int argc, char* argv[]) { /* END OF SDL INIT */ /* BEGINNING OF GAME INIT */ + // // Koordinatenursprung ist oben links, positive y-Achse zeigt nach unten snake[0].x = (WINDOW_WIDTH - snake[0].w) / 2; snake[0].y = (WINDOW_HEIGHT - snake[0].h) / 2; @@ -147,7 +156,7 @@ int main(int argc, char* argv[]) { - + // Spawn fruit at random location fruit.x = (rand() % (WINDOW_WIDTH - 2 * fruit.w)) + fruit.w; fruit.y = (rand() % (WINDOW_HEIGHT - 2 * fruit.h)) + fruit.h; @@ -164,16 +173,12 @@ int main(int argc, char* argv[]) { - + // set randomizer seed srand(time(NULL)); /* END OF GAME INIT */ /* BEGINNING OF GAME LOOP */ while (dir != QUIT) { - - - - // process events // determine velocity @@ -199,10 +204,6 @@ int main(int argc, char* argv[]) { dir_old = dir; } - - - - // update positions snake[0].x += x_vel / 60; snake[0].y += y_vel / 60; @@ -236,7 +237,6 @@ int main(int argc, char* argv[]) { } - // collision detection with bounds and "wrap around" if (snake[0].x < 0) snake[0].x = WINDOW_WIDTH - snake[0].w; if (snake[0].y < 0) snake[0].y = WINDOW_HEIGHT - snake[0].h; @@ -252,12 +252,8 @@ int main(int argc, char* argv[]) { } } - - // clear the window / renderer - - // draw fruit //draw head of snake @@ -304,7 +300,6 @@ int main(int argc, char* argv[]) { } - // swap current visible rendered window with buffered // (double buffering) SDL_RenderPresent(rend); @@ -318,8 +313,6 @@ int main(int argc, char* argv[]) { SDL_Delay(1000/60); } /* END OF GAME LOOP */ - - // cleanup ressources SDL_DestroyTexture(tex_sheet); SDL_DestroyRenderer(rend); From 668b2782a8ce12fa06517162183cb026746d24f6 Mon Sep 17 00:00:00 2001 From: netrunner1303 Date: Thu, 1 Oct 2020 13:56:36 +0200 Subject: [PATCH 2/4] Add Explanation --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7b1a467..f301ccf 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # Snake +Dieses Repo enstand als finales Projekt fuer das Modul Prozedurale Programmierung an der TUHH. zum Kompilieren: in der cmd: mingw32-make From a68309040799c244b0c0d93e748d43d59bdce94a Mon Sep 17 00:00:00 2001 From: netrunner1303 Date: Thu, 1 Oct 2020 13:58:57 +0200 Subject: [PATCH 3/4] Add Version --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f301ccf..1fbd392 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Snake Dieses Repo enstand als finales Projekt fuer das Modul Prozedurale Programmierung an der TUHH. +Es wird eine Installation von GCC vorrausgesetzt. + zum Kompilieren: in der cmd: mingw32-make From 386797c979f2e6867338315df09b94faa65f4c56 Mon Sep 17 00:00:00 2001 From: netrunner1303 Date: Thu, 1 Oct 2020 14:02:58 +0200 Subject: [PATCH 4/4] Add Instructions --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1fbd392..fcc24d0 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Dieses Repo enstand als finales Projekt fuer das Modul Prozedurale Programmierun Es wird eine Installation von GCC vorrausgesetzt. -zum Kompilieren: +Kompilieren: in der cmd: mingw32-make SDL2.dll muss sich zudem im Ordner befinden @@ -24,3 +24,7 @@ SDL2.dll has to be added to this folder beforehand link to external library: https://www.libsdl.org/projects/SDL_image/ + + +### How to play +The snake can be controlled via the correspondign arrow keys on the keyboard.