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
0f55a51e
Commit
0f55a51e
authored
Oct 28, 2020
by
Benjamin LEROUX
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ajout de sons + correction bug apparition des fruits
parent
e2285cd3
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
4 deletions
+31
-4
snake.c
snake.c
+31
-4
No files found.
snake.c
View file @
0f55a51e
...
...
@@ -66,6 +66,9 @@ static Food deadFruit = {0};
static
Rectangle
recTab
[
NUMBEROFWALL
];
static
int
highscore
;
static
Sound
fruitSound
;
static
Sound
goldenFruitSound
;
static
Sound
deathSound
;
//------------------------------------------------------------------------------------
// Module Functions Declaration (local)
...
...
@@ -86,6 +89,7 @@ int main(void)
InitWindow
(
screenWidth
,
screenHeight
,
"sample game: snake"
);
InitGame
();
InitAudioDevice
();
// Initialize audio device
#if defined(PLATFORM_WEB)
emscripten_set_main_loop
(
UpdateDrawFrame
,
0
,
1
);
...
...
@@ -124,6 +128,7 @@ void InitGame(void)
gameOver
=
false
;
pause
=
false
;
counterTail
=
1
;
allowMove
=
false
;
...
...
@@ -198,6 +203,10 @@ void InitGame(void)
recTab
[
7
].
height
=
(
SQUARE_SIZE
)
-
0
.
1
;
highscore
=
LoadStorageValue
(
0
);
fruitSound
=
LoadSound
(
"SnakeSounds
\\
coin.wav"
);
// Load audio files
goldenFruitSound
=
LoadSound
(
"SnakeSounds
\\
sound.wav"
);
deathSound
=
LoadSound
(
"SnakeSounds
\\
tanatana.ogg"
);
}
void
drawWalls
(){
...
...
@@ -276,7 +285,10 @@ void UpdateGame(void)
}
//collison mur
if
(
checkCollisionMur
(
snake
[
0
].
position
))
gameOver
=
true
;
if
(
checkCollisionMur
(
snake
[
0
].
position
)){
gameOver
=
true
;
PlaySound
(
deathSound
);
}
//teleport wall
if
((
snake
[
0
].
position
.
x
)
>
(
screenWidth
-
offset
.
x
)){
...
...
@@ -294,7 +306,10 @@ void UpdateGame(void)
// Collision with yourself
for
(
int
i
=
1
;
i
<
counterTail
;
i
++
)
{
if
((
snake
[
0
].
position
.
x
==
snake
[
i
].
position
.
x
)
&&
(
snake
[
0
].
position
.
y
==
snake
[
i
].
position
.
y
))
gameOver
=
true
;
if
((
snake
[
0
].
position
.
x
==
snake
[
i
].
position
.
x
)
&&
(
snake
[
0
].
position
.
y
==
snake
[
i
].
position
.
y
)){
gameOver
=
true
;
PlaySound
(
deathSound
);
}
}
...
...
@@ -320,7 +335,10 @@ 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
};
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
))
||
checkCollisionMur
(
goldenFruit
.
position
))
if
(
checkCollisionMur
(
goldenFruit
.
position
)){
//permet de ne pas embourber les conditions du while
goldenFruit
.
position
=
fruit
.
position
;
//si le fruit est sur un mur alors on reste dans le while
}
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
)))
{
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
;
...
...
@@ -330,7 +348,10 @@ 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
};
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
))
||
checkCollisionMur
(
deadFruit
.
position
))
if
(
checkCollisionMur
(
deadFruit
.
position
)){
deadFruit
.
position
=
fruit
.
position
;
}
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
)))
{
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
;
...
...
@@ -348,6 +369,7 @@ void UpdateGame(void)
counterTail
+=
1
;
fruit
.
active
=
false
;
snake
[
0
].
score
+=
100
;
PlaySound
(
fruitSound
);
}
// Collision goldenFruit
...
...
@@ -359,6 +381,7 @@ void UpdateGame(void)
counterTail
+=
1
;
goldenFruit
.
position
=
(
Vector2
)
{
-
100
,
-
100
};
//disparition du goldenFruit
snake
[
0
].
score
+=
250
;
PlaySound
(
goldenFruitSound
);
}
// Collision deadFruit
...
...
@@ -366,6 +389,7 @@ void UpdateGame(void)
(
snake
[
0
].
position
.
y
<
(
deadFruit
.
position
.
y
+
deadFruit
.
size
.
y
)
&&
(
snake
[
0
].
position
.
y
+
snake
[
0
].
size
.
y
)
>
deadFruit
.
position
.
y
))
{
gameOver
=
true
;
PlaySound
(
deathSound
);
}
framesCounter
++
;
...
...
@@ -440,6 +464,9 @@ void DrawGame(void)
void
UnloadGame
(
void
)
{
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
UnloadSound
(
fruitSound
);
UnloadSound
(
goldenFruitSound
);
UnloadSound
(
deathSound
);
}
// Update and Draw (one frame)
...
...
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