Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
SnakeProject
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Benjamin LEROUX
SnakeProject
Commits
15e6d0a8
Commit
15e6d0a8
authored
Oct 27, 2020
by
Benjamin LEROUX
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ajout de murs + gestion collision
parent
214cc162
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
23 deletions
+88
-23
snake.c
snake.c
+88
-23
No files found.
snake.c
View file @
15e6d0a8
...
@@ -22,12 +22,9 @@
...
@@ -22,12 +22,9 @@
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
#define SNAKE_LENGTH 256
#define SNAKE_LENGTH 256
#define SQUARE_SIZE 31
#define SQUARE_SIZE 31
#define NUMBEROFWALL 8
//----------------------------------------------------------------------------------
// More Defines
//----------------------------------------------------------------------------------
#define BordureX 12.5
#define BordureY 8
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
// Types and Structures Definition
// Types and Structures Definition
...
@@ -67,6 +64,8 @@ static int counterTail = 0;
...
@@ -67,6 +64,8 @@ static int counterTail = 0;
static
Food
goldenFruit
=
{
0
};
static
Food
goldenFruit
=
{
0
};
static
Food
deadFruit
=
{
0
};
static
Food
deadFruit
=
{
0
};
static
Rectangle
recTab
[
NUMBEROFWALL
];
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// Module Functions Declaration (local)
// Module Functions Declaration (local)
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
...
@@ -136,7 +135,7 @@ void InitGame(void)
...
@@ -136,7 +135,7 @@ void InitGame(void)
snake
[
i
].
size
=
(
Vector2
){
SQUARE_SIZE
,
SQUARE_SIZE
};
snake
[
i
].
size
=
(
Vector2
){
SQUARE_SIZE
,
SQUARE_SIZE
};
snake
[
i
].
speed
=
(
Vector2
){
SQUARE_SIZE
,
0
};
snake
[
i
].
speed
=
(
Vector2
){
SQUARE_SIZE
,
0
};
if
(
i
==
0
)
snake
[
i
].
color
=
DARK
BLUE
;
if
(
i
==
0
)
snake
[
i
].
color
=
DARK
GREEN
;
else
snake
[
i
].
color
=
BLUE
;
else
snake
[
i
].
color
=
BLUE
;
}
}
...
@@ -146,7 +145,7 @@ void InitGame(void)
...
@@ -146,7 +145,7 @@ void InitGame(void)
}
}
fruit
.
size
=
(
Vector2
){
SQUARE_SIZE
,
SQUARE_SIZE
};
fruit
.
size
=
(
Vector2
){
SQUARE_SIZE
,
SQUARE_SIZE
};
fruit
.
color
=
SKYBLUE
;
fruit
.
color
=
GREEN
;
fruit
.
active
=
false
;
fruit
.
active
=
false
;
goldenFruit
.
size
=
(
Vector2
){
SQUARE_SIZE
,
SQUARE_SIZE
};
goldenFruit
.
size
=
(
Vector2
){
SQUARE_SIZE
,
SQUARE_SIZE
};
...
@@ -156,6 +155,73 @@ void InitGame(void)
...
@@ -156,6 +155,73 @@ void InitGame(void)
deadFruit
.
size
=
(
Vector2
){
SQUARE_SIZE
,
SQUARE_SIZE
};
deadFruit
.
size
=
(
Vector2
){
SQUARE_SIZE
,
SQUARE_SIZE
};
deadFruit
.
color
=
BLACK
;
deadFruit
.
color
=
BLACK
;
deadFruit
.
active
=
false
;
deadFruit
.
active
=
false
;
recTab
[
0
].
x
=
2
*
SQUARE_SIZE
+
offset
.
x
/
2
;
//mur haut gauche
recTab
[
0
].
y
=
2
*
SQUARE_SIZE
+
offset
.
y
/
2
;
recTab
[
0
].
width
=
(
SQUARE_SIZE
)
-
0
.
1
;
recTab
[
0
].
height
=
(
SQUARE_SIZE
*
4
)
-
0
.
1
;
recTab
[
1
].
x
=
3
*
SQUARE_SIZE
+
offset
.
x
/
2
;
recTab
[
1
].
y
=
2
*
SQUARE_SIZE
+
offset
.
y
/
2
;
recTab
[
1
].
width
=
(
SQUARE_SIZE
*
3
)
-
0
.
1
;
recTab
[
1
].
height
=
(
SQUARE_SIZE
)
-
0
.
1
;
recTab
[
2
].
x
=
screenWidth
-
(
offset
.
x
/
2
)
-
(
3
*
SQUARE_SIZE
);
//mur haut droit
recTab
[
2
].
y
=
2
*
SQUARE_SIZE
+
offset
.
y
/
2
;
recTab
[
2
].
width
=
(
SQUARE_SIZE
)
-
0
.
1
;
recTab
[
2
].
height
=
(
SQUARE_SIZE
*
4
)
-
0
.
1
;
recTab
[
3
].
x
=
screenWidth
-
(
offset
.
x
/
2
)
-
(
6
*
SQUARE_SIZE
);
recTab
[
3
].
y
=
2
*
SQUARE_SIZE
+
offset
.
y
/
2
;
recTab
[
3
].
width
=
(
SQUARE_SIZE
*
3
)
-
0
.
1
;
recTab
[
3
].
height
=
(
SQUARE_SIZE
)
-
0
.
1
;
recTab
[
4
].
x
=
2
*
SQUARE_SIZE
+
offset
.
x
/
2
;
//mur bas gauche
recTab
[
4
].
y
=
screenHeight
-
(
offset
.
y
/
2
)
-
(
6
*
SQUARE_SIZE
);
recTab
[
4
].
width
=
(
SQUARE_SIZE
)
-
0
.
1
;
recTab
[
4
].
height
=
(
SQUARE_SIZE
*
3
)
-
0
.
1
;
recTab
[
5
].
x
=
2
*
SQUARE_SIZE
+
offset
.
x
/
2
;
recTab
[
5
].
y
=
screenHeight
-
(
offset
.
y
/
2
)
-
(
3
*
SQUARE_SIZE
);
recTab
[
5
].
width
=
(
SQUARE_SIZE
*
4
)
-
0
.
1
;
recTab
[
5
].
height
=
(
SQUARE_SIZE
)
-
0
.
1
;
recTab
[
6
].
x
=
screenWidth
-
(
offset
.
x
/
2
)
-
(
3
*
SQUARE_SIZE
);
//mur bas droit
recTab
[
6
].
y
=
screenHeight
-
(
offset
.
y
/
2
)
-
(
6
*
SQUARE_SIZE
);
recTab
[
6
].
width
=
(
SQUARE_SIZE
)
-
0
.
1
;
recTab
[
6
].
height
=
(
SQUARE_SIZE
*
4
)
-
0
.
1
;
recTab
[
7
].
x
=
screenWidth
-
(
offset
.
x
/
2
)
-
(
6
*
SQUARE_SIZE
);
recTab
[
7
].
y
=
screenHeight
-
(
offset
.
y
/
2
)
-
(
3
*
SQUARE_SIZE
);
recTab
[
7
].
width
=
(
SQUARE_SIZE
*
3
)
-
0
.
1
;
recTab
[
7
].
height
=
(
SQUARE_SIZE
)
-
0
.
1
;
}
void
drawWalls
(){
//mur haut gauche
DrawRectangleRec
(
recTab
[
0
],
DARKBROWN
);
DrawRectangleRec
(
recTab
[
1
],
DARKBROWN
);
//mur haut droit
DrawRectangleRec
(
recTab
[
2
],
DARKBROWN
);
DrawRectangleRec
(
recTab
[
3
],
DARKBROWN
);
//mur bas gauche
DrawRectangleRec
(
recTab
[
4
],
DARKBROWN
);
DrawRectangleRec
(
recTab
[
5
],
DARKBROWN
);
//mur bas droit
DrawRectangleRec
(
recTab
[
6
],
DARKBROWN
);
DrawRectangleRec
(
recTab
[
7
],
DARKBROWN
);
}
bool
checkCollisionMur
(
Vector2
position
){
bool
res
=
false
;
for
(
int
i
=
0
;
i
<
NUMBEROFWALL
;
i
++
){
if
(
CheckCollisionPointRec
(
position
,
recTab
[
i
]))
res
=
true
;
}
return
res
;
}
}
// Update game (one frame)
// Update game (one frame)
...
@@ -205,16 +271,9 @@ void UpdateGame(void)
...
@@ -205,16 +271,9 @@ void UpdateGame(void)
else
snake
[
i
].
position
=
snakePosition
[
i
-
1
];
else
snake
[
i
].
position
=
snakePosition
[
i
-
1
];
}
}
}
}
// Wall behaviour
//collison mur
/*
if
(
checkCollisionMur
(
snake
[
0
].
position
))
gameOver
=
true
;
if (((snake[0].position.x) > (screenWidth - offset.x)) ||
((snake[0].position.y) > (screenHeight - offset.y)) ||
(snake[0].position.x < 0) || (snake[0].position.y < 0))
{
gameOver = true;
}
*/
//teleport wall
//teleport wall
if
((
snake
[
0
].
position
.
x
)
>
(
screenWidth
-
offset
.
x
)){
if
((
snake
[
0
].
position
.
x
)
>
(
screenWidth
-
offset
.
x
)){
...
@@ -246,7 +305,7 @@ void UpdateGame(void)
...
@@ -246,7 +305,7 @@ void UpdateGame(void)
for
(
int
i
=
0
;
i
<
counterTail
;
i
++
)
for
(
int
i
=
0
;
i
<
counterTail
;
i
++
)
{
{
while
((
fruit
.
position
.
x
==
snake
[
i
].
position
.
x
)
&&
(
fruit
.
position
.
y
==
snake
[
i
].
position
.
y
))
while
((
(
fruit
.
position
.
x
==
snake
[
i
].
position
.
x
)
&&
(
fruit
.
position
.
y
==
snake
[
i
].
position
.
y
))
||
checkCollisionMur
(
fruit
.
position
))
{
{
fruit
.
position
=
(
Vector2
){
GetRandomValue
(
0
,
(
screenWidth
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
x
/
2
,
GetRandomValue
(
0
,
(
screenHeight
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
y
/
2
};
fruit
.
position
=
(
Vector2
){
GetRandomValue
(
0
,
(
screenWidth
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
x
/
2
,
GetRandomValue
(
0
,
(
screenHeight
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
y
/
2
};
i
=
0
;
i
=
0
;
...
@@ -258,7 +317,7 @@ void UpdateGame(void)
...
@@ -258,7 +317,7 @@ void UpdateGame(void)
goldenFruit
.
position
=
(
Vector2
){
GetRandomValue
(
0
,
(
screenWidth
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
x
/
2
,
GetRandomValue
(
0
,
(
screenHeight
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
y
/
2
};
goldenFruit
.
position
=
(
Vector2
){
GetRandomValue
(
0
,
(
screenWidth
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
x
/
2
,
GetRandomValue
(
0
,
(
screenHeight
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
y
/
2
};
for
(
int
i
=
0
;
i
<
counterTail
;
i
++
)
for
(
int
i
=
0
;
i
<
counterTail
;
i
++
)
{
{
while
(((
goldenFruit
.
position
.
x
==
snake
[
i
].
position
.
x
)
&&
(
goldenFruit
.
position
.
y
==
snake
[
i
].
position
.
y
))
||
((
goldenFruit
.
position
.
x
==
fruit
.
position
.
x
)
&&
(
goldenFruit
.
position
.
y
==
fruit
.
position
.
y
)))
while
(((
goldenFruit
.
position
.
x
==
snake
[
i
].
position
.
x
)
&&
(
goldenFruit
.
position
.
y
==
snake
[
i
].
position
.
y
))
||
((
goldenFruit
.
position
.
x
==
fruit
.
position
.
x
)
&&
(
goldenFruit
.
position
.
y
==
fruit
.
position
.
y
))
||
checkCollisionMur
(
goldenFruit
.
position
)
)
{
{
goldenFruit
.
position
=
(
Vector2
){
GetRandomValue
(
0
,
(
screenWidth
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
x
/
2
,
GetRandomValue
(
0
,
(
screenHeight
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
y
/
2
};
goldenFruit
.
position
=
(
Vector2
){
GetRandomValue
(
0
,
(
screenWidth
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
x
/
2
,
GetRandomValue
(
0
,
(
screenHeight
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
y
/
2
};
i
=
0
;
i
=
0
;
...
@@ -268,7 +327,7 @@ void UpdateGame(void)
...
@@ -268,7 +327,7 @@ void UpdateGame(void)
deadFruit
.
position
=
(
Vector2
){
GetRandomValue
(
0
,
(
screenWidth
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
x
/
2
,
GetRandomValue
(
0
,
(
screenHeight
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
y
/
2
};
deadFruit
.
position
=
(
Vector2
){
GetRandomValue
(
0
,
(
screenWidth
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
x
/
2
,
GetRandomValue
(
0
,
(
screenHeight
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
y
/
2
};
for
(
int
i
=
0
;
i
<
counterTail
;
i
++
)
for
(
int
i
=
0
;
i
<
counterTail
;
i
++
)
{
{
while
(((
deadFruit
.
position
.
x
==
snake
[
i
].
position
.
x
)
&&
(
deadFruit
.
position
.
y
==
snake
[
i
].
position
.
y
))
||
((
deadFruit
.
position
.
x
==
fruit
.
position
.
x
)
&&
(
goldenFruit
.
position
.
y
==
fruit
.
position
.
y
)))
while
(((
deadFruit
.
position
.
x
==
snake
[
i
].
position
.
x
)
&&
(
deadFruit
.
position
.
y
==
snake
[
i
].
position
.
y
))
||
((
deadFruit
.
position
.
x
==
fruit
.
position
.
x
)
&&
(
goldenFruit
.
position
.
y
==
fruit
.
position
.
y
))
||
checkCollisionMur
(
deadFruit
.
position
)
)
{
{
deadFruit
.
position
=
(
Vector2
){
GetRandomValue
(
0
,
(
screenWidth
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
x
/
2
,
GetRandomValue
(
0
,
(
screenHeight
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
y
/
2
};
deadFruit
.
position
=
(
Vector2
){
GetRandomValue
(
0
,
(
screenWidth
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
x
/
2
,
GetRandomValue
(
0
,
(
screenHeight
/
SQUARE_SIZE
)
-
1
)
*
SQUARE_SIZE
+
offset
.
y
/
2
};
i
=
0
;
i
=
0
;
...
@@ -342,15 +401,20 @@ void DrawGame(void)
...
@@ -342,15 +401,20 @@ void DrawGame(void)
// Draw snake
// Draw snake
for
(
int
i
=
0
;
i
<
counterTail
;
i
++
)
DrawRectangleV
(
snake
[
i
].
position
,
snake
[
i
].
size
,
snake
[
i
].
color
);
for
(
int
i
=
0
;
i
<
counterTail
;
i
++
)
DrawRectangleV
(
snake
[
i
].
position
,
snake
[
i
].
size
,
snake
[
i
].
color
);
//Draw walls
drawWalls
();
// Draw fruit to pick
// Draw fruit to pick
DrawRectangleV
(
fruit
.
position
,
fruit
.
size
,
fruit
.
color
);
DrawRectangleV
(
fruit
.
position
,
fruit
.
size
,
fruit
.
color
);
if
(
counterTail
%
4
==
0
)
DrawRectangleV
(
goldenFruit
.
position
,
goldenFruit
.
size
,
goldenFruit
.
color
);
if
(
counterTail
%
4
==
0
)
DrawRectangleV
(
goldenFruit
.
position
,
goldenFruit
.
size
,
goldenFruit
.
color
);
if
(
counterTail
%
7
==
0
)
DrawRectangleV
(
deadFruit
.
position
,
deadFruit
.
size
,
deadFruit
.
color
);
if
(
counterTail
%
7
==
0
)
DrawRectangleV
(
deadFruit
.
position
,
deadFruit
.
size
,
deadFruit
.
color
);
// DrawText(TextFormat("TIME: %.02f", (float)framesCounter/60), 10, 10, 20, BLACK);
//DrawText(TextFormat("SCORE: %04i",snake[0].score), 10, 30, 20, BLACK);
//score et timer en temps reel
DrawText
(
TextFormat
(
"TIME: %.02f"
,
(
float
)
framesCounter
/
60
),
10
,
10
,
20
,
BLACK
);
DrawText
(
TextFormat
(
"SCORE: %04i"
,
snake
[
0
].
score
),
10
,
30
,
20
,
BLACK
);
if
(
pause
)
{
if
(
pause
)
{
DrawText
(
"GAME PAUSED"
,
screenWidth
/
2
-
MeasureText
(
"GAME PAUSED"
,
80
)
/
2
,
screenHeight
/
2
-
160
,
8
0
,
BLACK
);
DrawText
(
"GAME PAUSED"
,
screenWidth
/
2
-
MeasureText
(
"GAME PAUSED"
,
70
)
/
2
,
screenHeight
/
2
-
120
,
7
0
,
BLACK
);
DrawText
(
TextFormat
(
"TIME: %.02f"
,
(
float
)
framesCounter
/
60
),
screenWidth
/
2
-
MeasureText
(
"TIME: 00.00"
,
60
)
/
2
,
screenHeight
-
140
,
60
,
BLUE
);
DrawText
(
TextFormat
(
"TIME: %.02f"
,
(
float
)
framesCounter
/
60
),
screenWidth
/
2
-
MeasureText
(
"TIME: 00.00"
,
60
)
/
2
,
screenHeight
-
140
,
60
,
BLUE
);
DrawText
(
TextFormat
(
"SCORE: %04i"
,
snake
[
0
].
score
),
screenWidth
/
2
-
MeasureText
(
"SCORE: 0000"
,
60
)
/
2
,
screenHeight
-
200
,
60
,
BLUE
);
DrawText
(
TextFormat
(
"SCORE: %04i"
,
snake
[
0
].
score
),
screenWidth
/
2
-
MeasureText
(
"SCORE: 0000"
,
60
)
/
2
,
screenHeight
-
200
,
60
,
BLUE
);
}
}
...
@@ -375,3 +439,4 @@ void UpdateDrawFrame(void)
...
@@ -375,3 +439,4 @@ void UpdateDrawFrame(void)
UpdateGame
();
UpdateGame
();
DrawGame
();
DrawGame
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment