Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
FanFEI_Jeu
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
Fan FEI
FanFEI_Jeu
Commits
1e42d88b
Commit
1e42d88b
authored
Oct 31, 2020
by
Fan FEI
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
version "1.0"
add notes
parent
b793d23f
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
15 deletions
+25
-15
snake_FanFEI.c
snake_FanFEI.c
+25
-15
No files found.
snake_FanFEI.c
View file @
1e42d88b
...
...
@@ -9,6 +9,14 @@
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
*
* Expanded on this basis by Fan FEI
*
*
*
*
*
*
********************************************************************************************/
#include "raylib.h"
...
...
@@ -26,7 +34,7 @@
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef
struct
Snake
{
typedef
struct
Snake
{
//
Vector2
position
;
Vector2
size
;
Vector2
speed
;
...
...
@@ -51,8 +59,8 @@ static bool gameOver = false;
static
bool
pause
=
false
;
static
Food
fruit
=
{
0
};
static
Snake
snake
[
SNAKE_LENGTH
]
=
{
0
};
static
Vector2
snakePosition
[
SNAKE_LENGTH
]
=
{
0
};
static
Snake
snake
[
SNAKE_LENGTH
]
=
{
0
};
//Maximum length of snake
static
Vector2
snakePosition
[
SNAKE_LENGTH
]
=
{
0
};
//The coordinates of the snake body
static
bool
allowMove
=
false
;
static
Vector2
offset
=
{
0
};
static
int
counterTail
=
0
;
...
...
@@ -73,12 +81,12 @@ int main(void)
{
// Initialization (Note windowTitle is unused on Android)
//---------------------------------------------------------
InitWindow
(
screenWidth
,
screenHeight
,
"sample game: snake By FanFEI"
);
//Initialize the interface and title
InitWindow
(
screenWidth
,
screenHeight
,
"sample game: snake By FanFEI"
);
//
Initialize the interface and title
InitGame
();
//Initialize the game
InitGame
();
//Initialize the game
#if defined(PLATFORM_WEB)
emscripten_set_main_loop
(
UpdateDrawFrame
,
60
,
1
);
emscripten_set_main_loop
(
UpdateDrawFrame
,
60
,
1
);
// The number of frames can adjust the game speed
#else
SetTargetFPS
(
60
);
//--------------------------------------------------------------------------------------
...
...
@@ -110,10 +118,10 @@ int main(void)
void
InitGame
(
void
)
{
framesCounter
=
0
;
gameOver
=
false
;
//Game state
gameOver
=
false
;
//
Game state
pause
=
false
;
counterTail
=
2
;
//The initial length of the snake
counterTail
=
2
;
//
The initial length of the snake
allowMove
=
false
;
offset
.
x
=
screenWidth
%
SQUARE_SIZE
;
...
...
@@ -123,10 +131,11 @@ void InitGame(void)
{
snake
[
i
].
position
=
(
Vector2
){
offset
.
x
/
2
,
offset
.
y
/
2
};
snake
[
i
].
size
=
(
Vector2
){
SQUARE_SIZE
,
SQUARE_SIZE
};
snake
[
i
].
speed
=
(
Vector2
){
SQUARE_SIZE
,
0
};
snake
[
i
].
speed
=
(
Vector2
){
SQUARE_SIZE
,
0
};
// Initial direction to the right
if
(
i
==
0
)
snake
[
i
].
color
=
DARKBLUE
;
//Snake head color
else
snake
[
i
].
color
=
BLUE
;
//Snake body color
if
(
i
==
0
)
snake
[
i
].
color
=
PINK
;
// Snake head color
else
if
(
i
%
3
!=
0
)
snake
[
i
].
color
=
PURPLE
;
// Snake body color
else
snake
[
i
].
color
=
DARKPURPLE
;
}
for
(
int
i
=
0
;
i
<
SNAKE_LENGTH
;
i
++
)
...
...
@@ -134,8 +143,8 @@ void InitGame(void)
snakePosition
[
i
]
=
(
Vector2
){
0
.
0
f
,
0
.
0
f
};
}
fruit
.
size
=
(
Vector2
){
SQUARE_SIZE
,
SQUARE_SIZE
};
//The color, coordinates and status of the props
fruit
.
color
=
SKYBLUE
;
fruit
.
size
=
(
Vector2
){
SQUARE_SIZE
,
SQUARE_SIZE
};
//
The color, coordinates and status of the props
fruit
.
color
=
RED
;
fruit
.
active
=
false
;
}
...
...
@@ -144,11 +153,12 @@ void UpdateGame(void)
{
if
(
!
gameOver
)
{
if
(
IsKeyPressed
(
'P'
))
pause
=
!
pause
;
//Keyboard input p pause
if
(
IsKeyPressed
(
'P'
))
pause
=
!
pause
;
//
Keyboard input p pause
if
(
!
pause
)
{
// Player control
// Only when the snake is moving in the vertical direction can it be turned horizontally
if
(
IsKeyPressed
(
KEY_RIGHT
)
&&
(
snake
[
0
].
speed
.
x
==
0
)
&&
allowMove
)
{
snake
[
0
].
speed
=
(
Vector2
){
SQUARE_SIZE
,
0
};
...
...
@@ -173,7 +183,7 @@ void UpdateGame(void)
// Snake movement
for
(
int
i
=
0
;
i
<
counterTail
;
i
++
)
snakePosition
[
i
]
=
snake
[
i
].
position
;
if
((
framesCounter
%
5
)
==
0
)
if
((
framesCounter
%
10
)
==
0
)
{
for
(
int
i
=
0
;
i
<
counterTail
;
i
++
)
{
...
...
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