Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Snake
Dieses Repo enstand als finales Projekt fuer das Modul Prozedurale Programmierung an der TUHH.

zum Kompilieren:
Es wird eine Installation von GCC vorrausgesetzt.

Kompilieren:
in der cmd: mingw32-make

SDL2.dll muss sich zudem im Ordner befinden
Expand All @@ -21,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.
47 changes: 20 additions & 27 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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
*/



Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -252,12 +252,8 @@ int main(int argc, char* argv[]) {
}
}



// clear the window / renderer



// draw fruit

//draw head of snake
Expand Down Expand Up @@ -304,7 +300,6 @@ int main(int argc, char* argv[]) {

}


// swap current visible rendered window with buffered
// (double buffering)
SDL_RenderPresent(rend);
Expand All @@ -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);
Expand Down