Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
snake
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
leo.muff
snake
Commits
c216e358
Commit
c216e358
authored
2 years ago
by
leo.muff
Browse files
Options
Downloads
Patches
Plain Diff
snake moving
parent
3ad37e62
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
snake.c
+55
-30
55 additions, 30 deletions
snake.c
snake.h
+18
-6
18 additions, 6 deletions
snake.h
with
73 additions
and
36 deletions
snake.c
+
55
−
30
View file @
c216e358
#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
--
;
}
}
}
}
/*
/*
...
...
This diff is collapsed.
Click to expand it.
snake.h
+
18
−
6
View file @
c216e358
#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_
ove
r
(
snake_t
snake
,
int
size_x
,
int
size_y
,
cases_t
tab
[
size_x
][
size_y
]
);
game_status_t
m
ove
(
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment