diff --git a/README.md b/README.md index f0e5c5a..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 @@ -8,3 +9,16 @@ SDL2.dll muss sich zudem im Ordner befinden erforderliche externe SDL-Libaries: https://www.libsdl.org/projects/SDL_image/ + +--- + +### Compile +``` +cd [PATH_TO_LOCAL_REPOSITORY] +mingw32_make +``` + +SDL2.dll has to be added to this folder beforehand + +link to external library: +https://www.libsdl.org/projects/SDL_image/ diff --git a/main.c b/main.c index e4e215c..7fa8699 100644 --- a/main.c +++ b/main.c @@ -10,11 +10,17 @@ // speed in pixel per second #define SPEED (300) #define COLLECTVALUE (32) +// Button Left #define LEFT 1 -#define UP 2 +// Button up +#define UP 2 +// Button down #define DOWN 3 +// Button right #define RIGHT 4 +// Button / Sequence to quit #define QUIT 1337 +// Button / Sequence to pause #define PAUSE 155 int dir; @@ -24,6 +30,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 +82,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 +92,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 +101,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 +144,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 +162,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 +179,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 +210,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 +243,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 +258,8 @@ int main(int argc, char* argv[]) { } } - - // clear the window / renderer - - // draw fruit //draw head of snake @@ -304,7 +306,6 @@ int main(int argc, char* argv[]) { } - // swap current visible rendered window with buffered // (double buffering) SDL_RenderPresent(rend); @@ -318,8 +319,6 @@ int main(int argc, char* argv[]) { SDL_Delay(1000/60); } /* END OF GAME LOOP */ - - // cleanup ressources SDL_DestroyTexture(tex_sheet); SDL_DestroyRenderer(rend);