Skip to content
Snippets Groups Projects
Commit 445cebbd authored by antoinemeyer5's avatar antoinemeyer5
Browse files

Merge branch 'player' into main

parents 32149716 78048149
No related branches found
No related tags found
No related merge requests found
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
......
File added
...@@ -8,8 +8,5 @@ int main(){ ...@@ -8,8 +8,5 @@ int main(){
running = 1; running = 1;
gameState = MENU; gameState = MENU;
readMapFromFile("map.txt"); readMapFromFile("map.txt");
initPlayer();
mainLoop(); mainLoop();
} }
\ No newline at end of file
...@@ -2,42 +2,126 @@ ...@@ -2,42 +2,126 @@
player_t player; player_t player;
void initPlayer(){ void initPlayer(){
player.x = 0; player.x = 18*32;
player.y = 0; player.y = 0;
player.w = CELLSIZE*0.8;
player.h = CELLSIZE*0.8;
player.waterLevel = 0; player.waterLevel = 0;
player.speed = 1; player.speed = 1;
player.isMoving = 0;
}
int giveCaseBelowPosition(int x, int y){
int x2 = x/CELLSIZE;
int y2 = y/CELLSIZE;
return map[y2][x2];
}
int collisionsLeftPlayer(){
int collision = 0;
int dotTopLeft = giveCaseBelowPosition(player.x, player.y);
int dotDownLeft = giveCaseBelowPosition(player.x, player.y+player.h);
//collision window
if(player.x <= 0){
collision = 1;
}
//collision tree
if(dotTopLeft==1 || dotDownLeft==1){
collision = 1;
}
return collision;
}
int collisionsRightPlayer(){
int collision = 0;
int dotTopRight = giveCaseBelowPosition(player.x+player.w, player.y);
int dotDownRight = giveCaseBelowPosition(player.x+player.w, player.y+player.h);
//collision window
if(player.x + player.w >= MAPSIZE * CELLSIZE){
collision = 1;
}
//collision tree
if(dotTopRight==1 || dotDownRight==1){
collision = 1;
}
return collision;
}
int collisionsUpPlayer(){
int collision = 0;
int dotTopRight = giveCaseBelowPosition(player.x+player.w, player.y);
int dotTopLeft = giveCaseBelowPosition(player.x, player.y);
//collision window
if(player.y <= 0){
collision = 1;
}
//collision tree
if(dotTopRight==1 || dotTopLeft==1){
collision = 1;
}
return collision;
}
int collisionsDownPlayer(){
int collision = 0;
int dotDownRight = giveCaseBelowPosition(player.x+player.w, player.y+player.h);
int dotDownLeft = giveCaseBelowPosition(player.x, player.y+player.h);
//collision window
if(player.y + player.h >= MAPSIZE * CELLSIZE){
collision = 1;
}
//collision tree
if(dotDownRight==1 || dotDownLeft==1){
collision = 1;
}
return collision;
} }
void moveRightPlayer(){ void moveRightPlayer(){
player.x = player.x + player.speed; if(!collisionsRightPlayer()){
player.x = player.x + player.speed;
}
} }
void moveLeftPlayer(){ void moveLeftPlayer(){
player.x = player.x - player.speed; if(!collisionsLeftPlayer()){
player.x = player.x - player.speed;
}
} }
void moveUpPlayer(){ void moveUpPlayer(){
player.y = player.y - player.speed; if(!collisionsUpPlayer()){
player.y = player.y - player.speed;
}
} }
void moveDownPlayer(){ void moveDownPlayer(){
player.y = player.y + player.speed; if(!collisionsDownPlayer()){
player.y = player.y + player.speed;
}
} }
void manageMovement(){ void manageMovement(){
if(keys[PLAYER_UP]){ if(keys[PLAYER_UP]){
moveUpPlayer(); moveUpPlayer();
player.isMoving = 1;
} }
if(keys[PLAYER_DOWN] == 1){ if(keys[PLAYER_DOWN]){
moveDownPlayer(); moveDownPlayer();
player.isMoving = 1;
} }
if(keys[PLAYER_LEFT] == 1){ if(keys[PLAYER_LEFT]){
moveLeftPlayer(); moveLeftPlayer();
player.isMoving = 1;
} }
if(keys[PLAYER_RIGHT] == 1){ if(keys[PLAYER_RIGHT]){
moveRightPlayer(); moveRightPlayer();
player.isMoving = 1;
} }
player.isMoving = 0;
} }
int selectStateHover(){ int selectStateHover(){
......
...@@ -12,8 +12,11 @@ ...@@ -12,8 +12,11 @@
typedef struct player{ typedef struct player{
int x; int x;
int y; int y;
int w;
int h;
int waterLevel; int waterLevel;
int speed; int speed;
int isMoving;
} player_t; } player_t;
extern player_t player; extern player_t player;
......
...@@ -143,6 +143,7 @@ void drawGame(){ ...@@ -143,6 +143,7 @@ void drawGame(){
void mainLoop(){ void mainLoop(){
createWindow(); createWindow();
initPlayer();
grassSurface = IMG_Load("Res/grass.png"); grassSurface = IMG_Load("Res/grass.png");
grassTexture = SDL_CreateTextureFromSurface(renderer, grassSurface); grassTexture = SDL_CreateTextureFromSurface(renderer, grassSurface);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment