Skip to content
Open
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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/
55 changes: 27 additions & 28 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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
*/



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

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



// clear the window / renderer



// draw fruit

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

}


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