Skip to content
Snippets Groups Projects
Commit c216e358 authored by leo.muff's avatar leo.muff
Browse files

snake moving

parent 3ad37e62
No related branches found
No related tags found
No related merge requests found
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#include <assert.h>
#include <time.h>
#include "snake.h" #include "snake.h"
#include "gfx/gfx.h"
#include "queue/queue_ptr_int.h" #include "queue/queue_ptr_int.h"
snake_t create_snake(int size_x,int size_y){ snake_t create_snake(int size_x,int size_y){
...@@ -16,48 +20,55 @@ snake_t create_snake(int size_x,int size_y){ ...@@ -16,48 +20,55 @@ snake_t create_snake(int size_x,int size_y){
} }
void init_cases(int size_x, int size_y, cases_t tab[size_x][size_y]){ board_t create_board(int size_x, int size_y){
for (int i=0; i < size_y; i++){ uint32_t *tab = malloc(size_y*size_x*sizeof(uint32_t));
for (int j=0; j< size_x; j++){ assert(tab != NULL);
for (int j=0; j<size_y; j++){
for (int i=0; i<size_x; i++){
if(i == size_y-1 || i == 0 || j== size_x-1 || j == 0 ){ if(i == size_y-1 || i == 0 || j== size_x-1 || j == 0 ){
tab[i][j] = WALL; tab[size_x*j+i] = WALL;
} }
else{ else{
tab[i][j] = EMPTY; tab[size_x*j+i] = EMPTY;
} }
} }
} }
board_t board;
board.pixels = tab;
board.size_x = size_x;
board.size_y = size_y;
return board;
} }
void destroy_board(board_t *board){
free(board->pixels);
board->pixels = NULL;
}
game_status_t game_over(snake_t snake, int size_x, int size_y, cases_t tab[size_x][size_y]){ game_status_t game_over(snake_t snake, board_t board){
coord_t coord = queue_tete(snake.body); coord_t coord = queue_debut(snake.body);
if(tab[coord.x][coord.y] == SNAKE || tab[coord.x][coord.y] == WALL){ if(board.pixels[board.size_x*coord.y + coord.x] == SNAKE || board.pixels[board.size_x*coord.y + coord.x] == WALL){
return LOST; return LOST;
} }
else { else if(queue_count(snake.body) == (board.size_x-2)*(board.size_y-2) ){
for (int i=0; i < size_x; i++){
for (int j=0; j< size_y; j++){
if (tab[coord.x][coord.y] == EMPTY || tab[coord.x][coord.y] == FOOD){
return NOT_OVER;
}
}
}
}
return WON; return WON;
} }
return NOT_OVER;
}
void set_snake(snake_t snake, int size_x, int size_y, cases_t tab[size_x][size_y]){ void set_snake(snake_t snake, board_t *board){
while (snake.body.tete->next != NULL){ while (snake.body.tete->next != NULL){
tab[snake.body.tete->coordinates.y][snake.body.tete->coordinates.x] = SNAKE; board->pixels[board->size_x*snake.body.tete->coordinates.y+snake.body.tete->coordinates.x] = SNAKE;
snake.body.tete = snake.body.tete->next; snake.body.tete = snake.body.tete->next;
} }
tab[snake.body.tete->coordinates.y][snake.body.tete->coordinates.x] = SNAKE; board->pixels[board->size_x*snake.body.tete->coordinates.y+snake.body.tete->coordinates.x] = SNAKE;
} }
game_status_t move(snake_t *snake, int size_x, int size_y, cases_t tab[size_x][size_y]){ game_status_t move(snake_t *snake, board_t *board){
coord_t new_case; coord_t new_case;
coord_t snake_tail; coord_t snake_tail;
game_status_t status;
new_case = snake->body.debut->coordinates; new_case = snake->body.debut->coordinates;
switch(snake->dir){ switch(snake->dir){
case W: case W:
...@@ -72,17 +83,31 @@ game_status_t move(snake_t *snake, int size_x, int size_y, cases_t tab[size_x][s ...@@ -72,17 +83,31 @@ game_status_t move(snake_t *snake, int size_x, int size_y, cases_t tab[size_x][s
case D: case D:
new_case.x += 1; new_case.x += 1;
} }
if(tab[new_case.y][new_case.x] == SNAKE || tab[new_case.y][new_case.x] == WALL ){ queue_inserer(&snake->body, new_case);
return LOST; status = game_over(*snake, *board);
} if(status == NOT_OVER){
queue_inserer(&snake->body, new_case); //bouger le reste du serpent if (board->pixels[board->size_x*new_case.y+new_case.x] != FOOD){
if (tab[new_case.y][new_case.x] != FOOD){
snake_tail = queue_extraire(&snake->body); snake_tail = queue_extraire(&snake->body);
tab[snake_tail.y][snake_tail.x] = EMPTY; board->pixels[board->size_x*snake_tail.y+snake_tail.x] = EMPTY;
}
set_snake(*snake, board);
}
return status;
} }
set_snake(*snake, size_x, size_y, tab); void gen_food(int max, board_t *board){
return NOT_OVER; srand(time(NULL));
int nb_foob = rand() % max;
for(int i=0; i<nb_foob; i++){
int x = rand() % board->size_x;
int y = rand() % board->size_y;
if (board->pixels[board->size_x*y+x] != FOOD){
board->pixels[board->size_x*y+x] = FOOD;
}
else {
i--;
}
}
} }
/* /*
......
#ifndef _SNAKE_H_ #ifndef _SNAKE_H_
#define _SNAKE_H_ #define _SNAKE_H_
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include "queue/queue_ptr_int.h" #include "queue/queue_ptr_int.h"
#include "gfx/gfx.h"
typedef enum _cases { typedef enum _cases {
EMPTY, SNAKE, FOOD, WALL EMPTY = COLOR_BLACK, SNAKE = COLOR_BLUE, FOOD = COLOR_GREEN, WALL = COLOR_WHITE
} cases_t; } cases_t;
...@@ -13,7 +15,7 @@ typedef enum _game_status { ...@@ -13,7 +15,7 @@ typedef enum _game_status {
} game_status_t; } game_status_t;
typedef enum _direction { typedef enum _direction {
W,A,S,D W = SDLK_w, A = SDLK_a, S = SDLK_s, D = SDLK_d
} direction_t; } direction_t;
typedef struct _snake{ typedef struct _snake{
...@@ -21,14 +23,24 @@ typedef struct _snake{ ...@@ -21,14 +23,24 @@ typedef struct _snake{
direction_t dir; direction_t dir;
}snake_t; }snake_t;
typedef struct _board{
int size_x, size_y;
uint32_t *pixels;
} board_t;
snake_t create_snake(int size_x, int size_y); snake_t create_snake(int size_x, int size_y);
void init_cases(int size_x, int size_y, cases_t tab[size_x][size_y]); board_t create_board(int size_x, int size_y);
void destroy_board(board_t *board);
game_status_t game_over(snake_t snake, board_t board);
void set_snake(snake_t snake, board_t *board);
game_status_t game_over(snake_t snake, int size_x, int size_y, cases_t tab[size_x][size_y]); game_status_t move(snake_t *snake, board_t *board);
void set_snake(snake_t snake, int size_x, int size_y, cases_t tab[size_x][size_y]); void gen_food(int max, board_t *board);
game_status_t move(snake_t *snake, int size_x, int size_y, cases_t tab[size_x][size_y]);
#endif #endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment