Snake bewegt sich, Fenster wird mit Portal und Food erstellt

This commit is contained in:
Julia09055x 2023-12-10 16:52:20 +01:00
parent 859b4c99b2
commit e66567d232
4 changed files with 76 additions and 75 deletions

View File

@ -7,4 +7,6 @@
void renderGame(); void renderGame();
//erstellt aus Spielfeldstruktur die graphische Anzeige mit SDL-Framework //erstellt aus Spielfeldstruktur die graphische Anzeige mit SDL-Framework
int CreateSDLWindow(); int CreateSDLWindow();
void DestroyWindwo();

View File

@ -9,13 +9,13 @@
// global struct for storing all game data // global struct for storing all game data
// default values where needed: // default values where needed:
gameData_t game = { gameData_t game = {
.snake.length = 0, .snake.length = 2,
.foodX = 0, .foodX = 0,
.foodY = 0, .foodY = 0,
.mapIsLoaded = false, .mapIsLoaded = false,
.lifesRemaining = 1, .lifesRemaining = 1,
.timestampLastCycle = 0, .timestampLastCycle = 0,
.gameState = MENU .gameState = RUNNING,
}; };
@ -108,6 +108,7 @@ void runGameCycle()
} }
//--- update frame --- //--- update frame ---
renderGame(); renderGame();
//printMap(game.map); (render game to console) //printMap(game.map); (render game to console)
return; return;

View File

@ -1,4 +1,4 @@
#include <stdio.h>
extern "C" { extern "C" {
#include "food.h" #include "food.h"
@ -8,6 +8,11 @@ extern "C"{
#include "render.h" #include "render.h"
} }
extern "C"{
#include "game.h"
#include "common.h"
}
//initialize SDL window //initialize SDL window
//ruft showStartScreen //ruft showStartScreen
//initialize game //initialize game
@ -17,6 +22,25 @@ extern "C"{
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
gameInit();
// Initialisiere SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL konnte nicht initialisiert werden! SDL_Error: %s\n", SDL_GetError());
return 1;
}
CreateSDLWindow(); CreateSDLWindow();
while(1){
if(game.gameState != EXIT) {
if (game.gameState == RUNNING) {
DELAY(config.cycleDurationMs);
runGameCycle();
}
} else {SDL_Quit(); return 0;}
}
return 0; return 0;
} }

View File

@ -3,116 +3,90 @@
#include "game.h" #include "game.h"
#include "snake.h" #include "snake.h"
#include "food.h" #include "food.h"
#include "config.h"
#include <stdio.h> #include <stdio.h>
void renderGame(SDL_Renderer* renderer){ SDL_Renderer* renderer;
SDL_Window* window;
//Snake-Baustein kreieren_________________________________________________________________
void renderGame(){
SDL_RenderClear(renderer);
//Snake-Baustein kreieren_________________________________________________________________
for(int i = game.snake.length; i>0; i--){
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); //RGB-Farbe Schlange SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); //RGB-Farbe Schlange
SDL_Rect rect; SDL_Rect rect;
rect.x = 500/*game.snake.headX * config_t.blockSizePx*/; //Abstand links rect.x = (game.snake.headX * config.blockSizePx) + 350; //Abstand links
rect.y = 300/*game.snake.headY * config_t.blockSizePx*/; //Abstand rechts rect.y = (game.snake.headY * config.blockSizePx) + 50; //Abstand rechts
rect.w = 20/*config_t.blockSizePx*/; rect.w = config.blockSizePx;
rect.h = 20/*config_t.blockSizePx*/; rect.h = config.blockSizePx;
SDL_RenderFillRect(renderer, &rect); //Rechteck rendern SDL_RenderFillRect(renderer, &rect); //Rechteck rendern
}
//Portal kreieren________________________________________________________________________ //Portal kreieren________________________________________________________________________
SDL_SetRenderDrawColor(renderer, 0, 100, 255, 255); //RGB-Farbe Portal SDL_SetRenderDrawColor(renderer, 0, 100, 255, 255); //RGB-Farbe Portal
SDL_Rect rect;
rect.x = 500; //Abstand links rect.x = 500; //Abstand links
rect.y = 200; //Abstand rechts rect.y = 500; //Abstand rechts
rect.w = config.blockSizePx;
rect.h = config.blockSizePx;
SDL_RenderDrawRect(renderer, &rect); //Rechteck rendern SDL_RenderDrawRect(renderer, &rect); //Rechteck rendern
//Food kreieren_________________________________________________________________________ //Food kreieren_________________________________________________________________________
SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255); //RGB-Farbe Food SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255); //RGB-Farbe Food
rect.x = 300/*foodX*/; //Abstand links rect.x = (game.foodX*config.blockSizePx) + 350; //Abstand links
rect.y = 600/*foodY*/; //Abstand rechts rect.y = (game.foodY* config.blockSizePx) + 50; //Abstand rechts
rect.w = config.blockSizePx;
rect.h = config.blockSizePx;
SDL_RenderFillRect(renderer, &rect); //Rechteck rendern SDL_RenderFillRect(renderer, &rect); //Rechteck rendern
SDL_RenderDrawRect(renderer, &rect); //Rechteck rendern
//Wand kreieren_________________________________________________________________________
for(int i = 0; i < game.map.collisionCount; i++){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); //RGB-Farbe Wand
rect.x = (game.map.collisions[i].posX*config.blockSizePx) + 350; //Abstand links
rect.y = (game.map.collisions[i].posX* config.blockSizePx) + 50; //Abstand rechts
rect.w = config.blockSizePx;
rect.h = config.blockSizePx;
SDL_RenderFillRect(renderer, &rect); //Rechteck rendern
}
SDL_RenderPresent(renderer); //Fenster aktualisieren SDL_RenderPresent(renderer); //Fenster aktualisieren
} }
int CreateSDLWindow(){ int CreateSDLWindow(){
// Initialisiere SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL konnte nicht initialisiert werden! SDL_Error: %s\n", SDL_GetError());
return 1;
}
// Erstelle ein SDL-Fenster // Erstelle ein SDL-Fenster
SDL_Window* window = SDL_CreateWindow("Snake", 350, 50, 800, 800, SDL_WINDOW_OPENGL); SDL_Window* window = SDL_CreateWindow("Snake", 350, 50, config.windowSize, config.windowSize, SDL_WINDOW_OPENGL);
if (window == NULL) { if (window == NULL) {
printf("Fenster konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError()); printf("Fenster konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError());
return 1; return 1;
} }
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) { if (renderer == NULL) {
printf("Renderer konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError()); printf("Renderer konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError());
return 1; return 1;
} }
renderGame(renderer);
// Warte
SDL_Delay(5000);
// Zerstöre das Fenster und beende SDL
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0; return 0;
} }
void DestroyWindow(){
/*SDL_Event event; // Zerstöre das Fenster und beende SDL
SDL_Window* window = NULL; SDL_DestroyRenderer(renderer);
SDL_Surface* surface = NULL; SDL_DestroyWindow(window);
int done = 0; }
atexit(SDL_Quit);
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
return 1;
}
window = SDL_CreateWindow(
"SDL Tutorial",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640, 480,
SDL_WINDOW_SHOWN
);
if (window == NULL){
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
return 1;
}
surface = SDL_GetWindowSurface(window);
if (surface == NULL){
printf( "Surface could not be created! SDL_Error: %s\n", SDL_GetError() );
return 1;
}
while (done == 0)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
done = 1;
break;
}
}
}
SDL_DestroyWindow(window);*/