Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
aostap
/
ussimang
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Merge Requests
0
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
4e1d9812
authored
May 21, 2023
by
caraun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lisadega snakegame
parent
187cb22d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
928 additions
and
0 deletions
snakegame.c
snakegame.c
0 → 100644
View file @
4e1d9812
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ncurses.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#define ROWS 20
#define COLS 45
#define PA_ROWS 50
#define PA_COLS 37
#define MENU_ROWS 25
#define START_LENGTH 1
#define GOLD 1
#define MAX_LENGTH 900
#define MAX_SPAWNS 900
#define GOL_MAX_SPAWNS 5
#define STRUCT_LEN 64
#define MAX_SCORES 1000
#define GAME_WIN_SCORE 810000
#define SCORE_OUTPUT_MAX 3
#define FOOD_CHAR 'O'
#define BLANK CHAR ' '
#define STOP 0
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
#define vk_enter 10
typedef
struct
{
int
x
,
y
;
}
position
;
int
fieldX
,
fieldY
;
int
fX
,
fY
;
WINDOW
*
field
;
void
Colors
();
void
MenuWindow
();
void
SnakeGame
();
void
SnakeStartingPosition
(
int
*
x
,
int
*
y
,
position
*
snake
);
void
SpawnGoldenApple
(
int
*
fx
,
int
*
fy
,
position
*
golden_apple
,
position
*
snake
,
int
length
);
int
SpawnApple
(
int
*
fx
,
int
*
fy
,
position
*
apple
,
position
*
snake
,
int
length
);
void
GrowSnake
(
position
*
snake
,
int
n
);
void
ReadScores
(
int
max
,
int
a
,
WINDOW
*
score
,
int
center
);
void
RefreshScores
(
int
max
);
void
PrintScore
(
int
score
);
void
ScoreWindow
();
void
TutorialWindow
();
void
MenuSelect
(
int
center
);
void
LiveScoreWindow
(
int
score
);
void
PrintLiveScore
(
int
score
);
void
PlaceBackBlocks
(
position
*
snake
,
int
n
);
void
MoveSnakeHead
(
int
x
,
int
y
,
position
*
snake
);
bool
CheckSelfCollision
(
position
*
snake
,
int
length
);
bool
CheckBorderCollision
(
position
*
snake
);
void
RemoveSnakeTail
(
position
*
snake
,
int
n
);
void
UpdateSnake_Apple
(
position
*
snake
,
int
n
);
_Bool
EmptyCoordinates
(
int
*
fx
,
int
*
fy
,
position
*
snake
,
int
n
);
bool
gameover
=
false
;
bool
EXIT
=
false
;
void
Setup
()
{
// initialize ncurses
initscr
();
// enable colors
start_color
();
Colors
();
// disable line buffering, allow each keystroke to be processed immediately
cbreak
();
// don't echo user input to the screen
noecho
();
srand
(
time
(
NULL
));
// remove cursor
curs_set
(
0
);
}
void
Colors
()
{
init_pair
(
1
,
COLOR_GREEN
,
COLOR_GREEN
);
// snake color
init_pair
(
2
,
COLOR_RED
,
COLOR_RED
);
// apple color
init_pair
(
3
,
COLOR_GREEN
,
COLOR_BLACK
);
// green ascii snake
init_pair
(
4
,
COLOR_RED
,
COLOR_BLACK
);
// red borders
init_pair
(
5
,
COLOR_WHITE
,
COLOR_BLACK
);
// white text
init_pair
(
6
,
COLOR_WHITE
,
COLOR_WHITE
);
// gray borders
init_pair
(
7
,
COLOR_MAGENTA
,
COLOR_WHITE
);
// black text, white background
init_pair
(
8
,
COLOR_MAGENTA
,
COLOR_BLACK
);
// black text, white background
init_pair
(
9
,
COLOR_YELLOW
,
COLOR_BLACK
);
// black text, white background
init_pair
(
10
,
COLOR_RED
,
COLOR_WHITE
);
// black text, white background
init_pair
(
11
,
COLOR_YELLOW
,
COLOR_YELLOW
);
// black text, white background
}
void
Menu
()
{
MenuWindow
();
// counter for default
int
count
=
0
;
while
(
count
<=
0
)
{
int
input
=
getch
();
switch
(
input
)
{
case
's'
:
SnakeGame
();
count
++
;
break
;
case
'h'
:
ScoreWindow
();
count
++
;
break
;
case
'q'
:
count
++
;
break
;
case
't'
:
TutorialWindow
();
count
++
;
default:
break
;
}
}
}
void
MenuWindow
()
{
clear
();
refresh
();
int
xMax
,
yMax
;
getmaxyx
(
stdscr
,
yMax
,
xMax
);
WINDOW
*
menu
=
newwin
(
yMax
/
2
,
xMax
/
2
,
yMax
/
4
,
xMax
/
4
);
box
(
menu
,
0
,
0
);
int
center
=
xMax
/
4
;
mvwprintw
(
menu
,
2
,
5
,
".oPYo. 8 .oPYo."
);
mvwprintw
(
menu
,
3
,
5
,
"8 8 8 8 "
);
mvwprintw
(
menu
,
4
,
5
,
"`Yooo. odYo. .oPYo. 8 .o .oPYo. 8 .oPYo. ooYoYo. .oPYo."
);
mvwprintw
(
menu
,
5
,
5
,
" `8 8' `8 .oooo8 8oP' 8oooo8 8 oo .oooo8 8' 8 8 8oooo8 "
);
mvwprintw
(
menu
,
6
,
5
,
" 8 8 8 8 8 8 `b. 8. 8 8 8 8 8 8 8 8. "
);
mvwprintw
(
menu
,
7
,
5
,
"`YooP' 8 8 `YooP8 8 `o. `Yooo' `YooP8 `YooP8 8 8 8 `Yooo' "
);
mvwprintw
(
menu
,
8
,
5
,
":.....:..::..:.....:..::...:.....::::....8 :.....:..:..:..:.....:"
);
mvwprintw
(
menu
,
9
,
5
,
":::::::::::::::::::::::::::::::::::::::::8 ::::::::::::::::::::::"
);
mvwprintw
(
menu
,
10
,
5
,
":::::::::::::::::::::::::::::::::::::::::..::::::::::::::::::::::"
);
mvwprintw
(
menu
,
13
,
center
-
25
,
"---===e-< PRESS 's' TO PLAY SNAKE GAME >-a===---"
);
mvwprintw
(
menu
,
14
,
center
-
25
,
"--------- PRESS 'h' TO VIEW HIGH SCORES ---------"
);
mvwprintw
(
menu
,
15
,
center
-
25
,
"--------- PRESS 't' TO READ TUTORIAL ---------"
);
mvwprintw
(
menu
,
16
,
center
-
25
,
"---===e-< PRESS 'q' TO QUIT >-a===---"
);
wattron
(
menu
,
COLOR_PAIR
(
7
));
// red borders
wborder
(
menu
,
'|'
,
'|'
,
'-'
,
'-'
,
'+'
,
'+'
,
'+'
,
'+'
);
wattroff
(
menu
,
COLOR_PAIR
(
7
));
// end red borders
wrefresh
(
menu
);
}
void
TutorialWindow
()
{
clear
();
refresh
();
int
xMax
,
yMax
;
getmaxyx
(
stdscr
,
yMax
,
xMax
);
WINDOW
*
TutorialMenu
=
newwin
(
yMax
/
2
,
xMax
/
2
,
yMax
/
4
,
xMax
/
4
);
box
(
TutorialMenu
,
0
,
0
);
int
center
=
xMax
/
4
;
mvwprintw
(
TutorialMenu
,
2
,
5
,
".oPYo. 8 .oPYo."
);
mvwprintw
(
TutorialMenu
,
3
,
5
,
"8 8 8 8 "
);
mvwprintw
(
TutorialMenu
,
4
,
5
,
"`Yooo. odYo. .oPYo. 8 .o .oPYo. 8 .oPYo. ooYoYo. .oPYo."
);
mvwprintw
(
TutorialMenu
,
5
,
5
,
" `8 8' `8 .oooo8 8oP' 8oooo8 8 oo .oooo8 8' 8 8 8oooo8 "
);
mvwprintw
(
TutorialMenu
,
6
,
5
,
" 8 8 8 8 8 8 `b. 8. 8 8 8 8 8 8 8 8. "
);
mvwprintw
(
TutorialMenu
,
7
,
5
,
"`YooP' 8 8 `YooP8 8 `o. `Yooo' `YooP8 `YooP8 8 8 8 `Yooo' "
);
mvwprintw
(
TutorialMenu
,
8
,
5
,
":.....:..::..:.....:..::...:.....::::....8 :.....:..:..:..:.....:"
);
mvwprintw
(
TutorialMenu
,
9
,
5
,
":::::::::::::::::::::::::::::::::::::::::8 ::::::::::::::::::::::"
);
mvwprintw
(
TutorialMenu
,
10
,
5
,
":::::::::::::::::::::::::::::::::::::::::..::::::::::::::::::::::"
);
mvwprintw
(
TutorialMenu
,
12
,
center
-
33
,
"---===e-< SNAKE MOVES WITH AWSD >-a===---"
);
mvwprintw
(
TutorialMenu
,
13
,
center
-
33
,
"--------- DON'T HIT THE WALL AND DON'T BITE YOUR OWN TAIL, ---------"
);
mvwprintw
(
TutorialMenu
,
14
,
center
-
33
,
"--------- OTHERWISE GAME WILL END. HIGH SCORE IS CALCULATED --------"
);
mvwprintw
(
TutorialMenu
,
15
,
center
-
33
,
"--------- BASED ON THE AMOUNT OF APPLES EATEN. GAME IS WON ---------"
);
mvwprintw
(
TutorialMenu
,
16
,
center
-
33
,
"--------- WHEN THERE IS NO MORE ROOM TO GROW. ---------"
);
mvwprintw
(
TutorialMenu
,
17
,
center
-
33
,
"--------- GOLDEN APPLES VALUES ARE MULTIPLIED BY 5 TIMES! ---------"
);
mvwprintw
(
TutorialMenu
,
18
,
center
-
33
,
"---===e-< PRESS 'b' TO GO BACK OR 'q' TO QUIT. >-a===---"
);
wattron
(
TutorialMenu
,
COLOR_PAIR
(
7
));
// red borders
wborder
(
TutorialMenu
,
'|'
,
'|'
,
'-'
,
'-'
,
'+'
,
'+'
,
'+'
,
'+'
);
wattroff
(
TutorialMenu
,
COLOR_PAIR
(
7
));
// end red borders
refresh
();
wrefresh
(
TutorialMenu
);
// counter for default
int
count
=
0
;
while
(
count
<=
0
)
{
int
input
=
getch
();
switch
(
input
)
{
case
'b'
:
Menu
();
count
++
;
break
;
case
'q'
:
count
++
;
break
;
default:
refresh
();
break
;
}
}
}
void
ScoreWindow
(
WINDOW
*
score
,
int
center
)
{
clear
();
refresh
();
int
xMax
,
yMax
;
getmaxyx
(
stdscr
,
yMax
,
xMax
);
score
=
newwin
(
15
,
45
,
yMax
/
4
,
xMax
/
4
);
box
(
score
,
0
,
0
);
center
=
23
;
mvwprintw
(
score
,
3
,
5
,
"HIGH SCORE!"
);
ReadScores
(
MAX_SCORES
,
0
,
score
,
center
);
mvwprintw
(
score
,
3
,
center
,
"LAST THREE SCORES"
);
ReadScores
(
MAX_SCORES
,
1
,
score
,
center
);
mvwprintw
(
score
,
10
,
center
-
10
,
"PRESS 'b' TO GO BACK"
);
wattron
(
score
,
COLOR_PAIR
(
10
));
// red borders
wborder
(
score
,
'|'
,
'|'
,
'-'
,
'-'
,
'+'
,
'+'
,
'+'
,
'+'
);
wattroff
(
score
,
COLOR_PAIR
(
10
));
// end red borders
wrefresh
(
score
);
// counter for default
int
count
=
0
;
while
(
count
<=
0
)
{
int
input
=
getch
();
switch
(
input
)
{
case
'b'
:
Menu
();
count
++
;
break
;
default:
refresh
();
break
;
}
}
}
void
Gamefield
()
{
clear
();
refresh
();
int
xMax
,
yMax
;
getmaxyx
(
stdscr
,
yMax
,
xMax
);
fieldY
=
(
yMax
/
2
)
-
(
ROWS
/
2
);
fieldX
=
(
xMax
/
2
)
-
(
COLS
/
2
);
fX
=
(
yMax
/
2
)
+
(
ROWS
/
2
);
fY
=
(
xMax
/
2
)
+
(
COLS
/
2
);
field
=
newwin
(
ROWS
,
COLS
,
fieldY
,
fieldX
);
box
(
field
,
0
,
0
);
nodelay
(
field
,
TRUE
);
wattron
(
field
,
COLOR_PAIR
(
6
));
// white borders
wborder
(
field
,
'|'
,
'|'
,
'-'
,
'-'
,
'+'
,
'+'
,
'+'
,
'+'
);
wattroff
(
field
,
COLOR_PAIR
(
6
));
// end white borders
wrefresh
(
field
);
}
void
LiveScoreWindow
(
int
score
)
{
clear
();
refresh
();
int
xMax
,
yMax
;
getmaxyx
(
stdscr
,
yMax
,
xMax
);
fieldY
=
(
yMax
/
2
)
-
(
ROWS
/
2
)
-
5
;
fieldX
=
(
xMax
/
2
)
-
(
COLS
/
2
)
-
2
;
WINDOW
*
scoreWindow
=
newwin
(
PA_COLS
,
PA_ROWS
,
fieldY
,
fieldX
);
box
(
scoreWindow
,
0
,
0
);
int
center
=
PA_ROWS
-
PA_COLS
;
mvwprintw
(
scoreWindow
,
2
,
22
,
"SCORE"
);
wattron
(
scoreWindow
,
COLOR_PAIR
(
8
));
// red borders
mvwprintw
(
scoreWindow
,
3
,
12
,
"<><><><><><><><><><><><>"
);
wattroff
(
scoreWindow
,
COLOR_PAIR
(
8
));
// end red borders
mvwprintw
(
scoreWindow
,
4
,
22
,
"%d"
,
score
);
wattron
(
scoreWindow
,
COLOR_PAIR
(
8
));
// red borders
mvwprintw
(
scoreWindow
,
5
,
12
,
"<><><><><><><><><><><><>"
);
wattroff
(
scoreWindow
,
COLOR_PAIR
(
8
));
// end red borders
mvwprintw
(
scoreWindow
,
6
,
12
,
"PRESS 'y' TO PLAY AGAIN"
);
mvwprintw
(
scoreWindow
,
7
,
12
,
"PRESS 'n' TO GO TO MENU"
);
wattron
(
scoreWindow
,
COLOR_PAIR
(
8
));
// red borders
mvwprintw
(
scoreWindow
,
8
,
12
,
"<><><><><><><><><><><><>"
);
wattroff
(
scoreWindow
,
COLOR_PAIR
(
8
));
// end red borders
wattron
(
scoreWindow
,
COLOR_PAIR
(
3
));
mvwprintw
(
scoreWindow
,
10
,
center
,
" _______"
);
mvwprintw
(
scoreWindow
,
11
,
center
,
" // _ _
\\
"
);
mvwprintw
(
scoreWindow
,
12
,
center
,
" // (.) (.)
\\
"
);
mvwprintw
(
scoreWindow
,
13
,
center
,
" ( _________ ) "
);
mvwprintw
(
scoreWindow
,
14
,
center
,
"
\\
`-V-|-V-'//"
);
mvwprintw
(
scoreWindow
,
15
,
center
,
"
\\
| //"
);
mvwprintw
(
scoreWindow
,
16
,
center
,
"
\\
^ //"
);
mvwprintw
(
scoreWindow
,
17
,
center
,
"
\\
\\
"
);
mvwprintw
(
scoreWindow
,
18
,
center
,
"
\\
`-_ G"
);
mvwprintw
(
scoreWindow
,
19
,
center
,
" `-_ -_ A"
);
mvwprintw
(
scoreWindow
,
20
,
center
,
" -_ -_ M"
);
mvwprintw
(
scoreWindow
,
21
,
center
,
" _- _- E"
);
mvwprintw
(
scoreWindow
,
22
,
center
,
" _- _-"
);
mvwprintw
(
scoreWindow
,
23
,
center
,
" _- _- O"
);
mvwprintw
(
scoreWindow
,
24
,
center
,
" _- _- V"
);
mvwprintw
(
scoreWindow
,
25
,
center
,
" -_ -_ E"
);
mvwprintw
(
scoreWindow
,
26
,
center
,
" -_ -_ R"
);
mvwprintw
(
scoreWindow
,
27
,
center
,
" -_ -_ !"
);
wattroff
(
scoreWindow
,
COLOR_PAIR
(
3
));
// end green snake
wattron
(
scoreWindow
,
COLOR_PAIR
(
8
));
mvwprintw
(
scoreWindow
,
28
,
center
,
" ,-=:_-_-_-_ _ _-_-_-_:=-."
);
mvwprintw
(
scoreWindow
,
29
,
center
,
" =I=I=I=I=I=I=I=I=I=I=I=I= "
);
mvwprintw
(
scoreWindow
,
30
,
center
,
" =I=I=I=I=I=I=I=I=I=I=I=I="
);
mvwprintw
(
scoreWindow
,
31
,
center
,
" =I=I=I=I=I=I=I=I=I=I=I= "
);
mvwprintw
(
scoreWindow
,
32
,
center
,
" =I=I=I=I=I=I=I=I=I=I= "
);
mvwprintw
(
scoreWindow
,
33
,
center
,
" =I=I=I=I=I=I=I=I=I= "
);
mvwprintw
(
scoreWindow
,
34
,
center
,
" `=================' "
);
wattroff
(
scoreWindow
,
COLOR_PAIR
(
8
));
// end black and white box
wattron
(
scoreWindow
,
COLOR_PAIR
(
7
));
// red borders
wborder
(
scoreWindow
,
'|'
,
'|'
,
'-'
,
'-'
,
'+'
,
'+'
,
'+'
,
'+'
);
wattroff
(
scoreWindow
,
COLOR_PAIR
(
7
));
// end red borders
// Refresh windows
refresh
();
wrefresh
(
field
);
wrefresh
(
scoreWindow
);
PrintScore
(
score
);
// counter for default
int
count
=
0
;
while
(
count
<=
0
)
{
int
input
=
getch
();
switch
(
input
)
{
case
'y'
:
gameover
=
false
;
SnakeGame
();
count
++
;
break
;
case
'n'
:
Menu
();
count
++
;
break
;
case
'q'
:
count
++
;
break
;
default:
break
;
}
}
}
void
GameWon
()
{
clear
();
refresh
();
int
xMax
,
yMax
;
getmaxyx
(
stdscr
,
yMax
,
xMax
);
WINDOW
*
Won
=
newwin
(
yMax
/
2
,
xMax
/
2
,
yMax
/
4
,
xMax
/
4
);
box
(
Won
,
0
,
0
);
int
center
=
xMax
/
4
;
wattron
(
Won
,
COLOR_PAIR
(
9
));
// red borders
mvwprintw
(
Won
,
2
,
7
,
".oPYo. o o .oPYo. o o 88"
);
mvwprintw
(
Won
,
3
,
7
,
"8 8 8 8 8 8 8b 8 88 "
);
mvwprintw
(
Won
,
4
,
7
,
"8 .oPYo. ooYoYo. .oPYo. 8 8 8 8 8`b 8 88"
);
mvwprintw
(
Won
,
5
,
7
,
"8 oo .oooo8 8' 8 8 8oooo8 8 db 8 8 8 8 `b 8 88"
);
mvwprintw
(
Won
,
6
,
7
,
"8 8 8 8 8 8 8 8. `b.PY.d' 8 8 8 `b8 `' "
);
mvwprintw
(
Won
,
7
,
7
,
"`YooP8 `YooP8 8 8 8 `Yooo' `8 8' `YooP' 8 `8 88 "
);
mvwprintw
(
Won
,
8
,
7
,
":....8 :.....:..:..:..:.....:::::..:..:::.....:..:::..::..."
);
mvwprintw
(
Won
,
9
,
7
,
":::::8 ::::::::::::::::::::::::::::::::::::::::::::::::::::"
);
mvwprintw
(
Won
,
10
,
7
,
":::::..::::::::::::::::::::::::::::::::::::::::::::::::::::"
);
wattroff
(
Won
,
COLOR_PAIR
(
9
));
// end red borders
mvwprintw
(
Won
,
12
,
center
-
30
,
"---===e-< CONGRATULATIONS! >-a===---"
);
mvwprintw
(
Won
,
13
,
center
-
30
,
"--------- A TRUE SNAKE GAME MASTER ---------"
);
mvwprintw
(
Won
,
14
,
center
-
30
,
"---===e-< PRESS 'b' TO GO BACK OR 'q' TO QUIT. >-a===---"
);
wattron
(
Won
,
COLOR_PAIR
(
11
));
// red borders
wborder
(
Won
,
'|'
,
'|'
,
'-'
,
'-'
,
'+'
,
'+'
,
'+'
,
'+'
);
wattroff
(
Won
,
COLOR_PAIR
(
11
));
// end red borders
refresh
();
wrefresh
(
Won
);
// counter for default
int
count
=
0
;
while
(
count
<=
0
)
{
int
input
=
getch
();
switch
(
input
)
{
case
'b'
:
Menu
();
count
++
;
break
;
case
'q'
:
count
++
;
break
;
default:
refresh
();
break
;
}
}
}
void
SnakeGame
()
{
Gamefield
();
position
snake
[
MAX_LENGTH
];
position
apple
[
MAX_SPAWNS
];
gameover
=
false
;
int
length
;
int
direction
;
// snake coordinates
int
x
,
y
;
// apple coordinates
int
fx
,
fy
;
int
score
=
0
;
// Time delay for snake movement
struct
timespec
sleep_time
;
sleep_time
.
tv_sec
=
0
;
sleep_time
.
tv_nsec
=
100000000
;
length
=
START_LENGTH
;
// Inital direction waits for user input
direction
=
STOP
;
// Snake starting point
SnakeStartingPosition
(
&
x
,
&
y
,
snake
);
// Apple starting point and spawns in apples
SpawnApple
(
&
fx
,
&
fy
,
apple
,
snake
,
length
);
while
(
!
gameover
)
{
// Get user input
int
input
=
wgetch
(
field
);
switch
(
input
)
{
case
'a'
:
if
(
direction
!=
RIGHT
)
{
direction
=
LEFT
;
}
break
;
case
'd'
:
if
(
direction
!=
LEFT
)
{
direction
=
RIGHT
;
}
break
;
case
'w'
:
if
(
direction
!=
DOWN
)
{
direction
=
UP
;
}
break
;
case
's'
:
if
(
direction
!=
UP
)
{
direction
=
DOWN
;
}
break
;
// Pause game
case
'p'
:
while
(
getch
()
!=
'p'
);
break
;
// Exit game
case
'q'
:
gameover
=
true
;
break
;
default:
break
;
}
// Input Logic
switch
(
direction
)
{
case
UP
:
y
--
;
break
;
case
DOWN
:
y
++
;
break
;
case
LEFT
:
x
--
;
break
;
case
RIGHT
:
x
++
;
break
;
}
// Check if apple has been eaten
int
color
=
0
;
if
(
x
==
fx
&&
y
==
fy
)
{
GrowSnake
(
snake
,
length
);
color
=
SpawnApple
(
&
fx
,
&
fy
,
apple
,
snake
,
length
);
if
(
color
>=
GOLD
)
{
length
+=
5
;
score
+=
500
;
}
else
{
length
++
;
score
+=
100
;
}
}
else
{
RemoveSnakeTail
(
snake
,
length
);
}
PlaceBackBlocks
(
snake
,
length
);
MoveSnakeHead
(
x
,
y
,
snake
);
// Game over situations
if
(
CheckBorderCollision
(
snake
))
{
gameover
=
true
;
LiveScoreWindow
(
score
);
}
if
(
CheckSelfCollision
(
snake
,
length
))
{
gameover
=
true
;
LiveScoreWindow
(
score
);
}
PrintLiveScore
(
score
);
UpdateSnake_Apple
(
snake
,
length
);
refresh
();
nanosleep
(
&
sleep_time
,
NULL
);
// Game is won
if
(
score
>=
GAME_WIN_SCORE
)
{
GameWon
();
score
=
0
;
}
}
PrintScore
(
score
);
RefreshScores
(
MAX_SCORES
);
}
void
SnakeStartingPosition
(
int
*
x
,
int
*
y
,
position
*
snake
)
{
*
x
=
rand
()
%
(
COLS
-
2
)
+
fieldX
+
1
;
*
y
=
rand
()
%
(
ROWS
-
2
)
+
fieldY
+
1
;
snake
[
0
].
x
=
*
x
;
snake
[
0
].
y
=
*
y
;
}
int
SpawnApple
(
int
*
fx
,
int
*
fy
,
position
*
apple
,
position
*
snake
,
int
length
)
{
int
gold
=
0
;
srand
(
time
(
NULL
));
int
randomNumber
=
rand
()
%
100
+
1
;
do
{
*
fx
=
rand
()
%
(
COLS
-
2
)
+
fieldX
+
1
;
*
fy
=
rand
()
%
(
ROWS
-
2
)
+
fieldY
+
1
;
apple
[
0
].
x
=
*
fx
;
apple
[
0
].
y
=
*
fy
;
}
while
(
!
EmptyCoordinates
(
fx
,
fy
,
snake
,
length
));
if
(
randomNumber
>
80
)
{
gold
++
;
attron
(
COLOR_PAIR
(
11
));
// set food color
mvaddch
(
apple
->
y
,
apple
->
x
,
FOOD_CHAR
);
attroff
(
COLOR_PAIR
(
11
));
// unset food color
}
else
{
attron
(
COLOR_PAIR
(
2
));
// set food color
mvaddch
(
apple
->
y
,
apple
->
x
,
FOOD_CHAR
);
attroff
(
COLOR_PAIR
(
2
));
// unset food color
}
return
gold
;
}
_Bool
EmptyCoordinates
(
int
*
fx
,
int
*
fy
,
position
*
snake
,
int
n
)
{
// Check if the coordinates are occupied by the snake
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
if
(
*
fx
==
snake
[
i
].
x
&&
*
fy
==
snake
[
i
].
y
)
{
return
FALSE
;
}
}
return
TRUE
;
}
void
GrowSnake
(
position
*
snake
,
int
n
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
snake
[
n
].
x
=
snake
[
n
-
1
].
x
;
snake
[
n
].
y
=
snake
[
n
-
1
].
y
;
}
}
void
MoveSnakeHead
(
int
x
,
int
y
,
position
*
snake
)
{
snake
[
0
].
x
=
x
;
snake
[
0
].
y
=
y
;
}
bool
CheckSelfCollision
(
position
*
snake
,
int
length
)
{
for
(
int
i
=
1
;
i
<
length
;
i
++
)
{
if
(
snake
[
i
].
x
==
snake
[
0
].
x
&&
snake
[
i
].
y
==
snake
[
0
].
y
)
{
return
true
;
}
}
return
false
;
}
bool
CheckBorderCollision
(
position
*
snake
)
{
// left border
if
(
snake
[
0
].
x
==
fieldX
)
{
return
true
;
}
// upper border
if
(
snake
[
0
].
y
==
fieldY
)
{
return
true
;
}
// lower border
if
(
snake
[
0
].
y
==
fieldY
+
ROWS
-
1
){
return
true
;
}
// right border
if
(
snake
[
0
].
x
==
fieldX
+
COLS
-
1
){
return
true
;
}
return
false
;
}
void
PlaceBackBlocks
(
position
*
snake
,
int
n
)
{
int
i
;
position
temp
;
for
(
i
=
n
;
i
>
0
;
i
--
)
{
temp
=
snake
[
i
-
1
];
snake
[
i
]
=
temp
;
}
}
void
RemoveSnakeTail
(
position
*
snake
,
int
n
)
{
mvaddch
(
snake
[
n
-
1
].
y
,
snake
[
n
-
1
].
x
,
' '
);
snake
[
n
-
1
].
x
=
-
1
;
snake
[
n
-
1
].
y
=
-
1
;
}
void
UpdateSnake_Apple
(
position
*
snake
,
int
n
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
if
(
snake
[
i
].
y
!=
-
1
&&
snake
[
i
].
x
!=
-
1
)
{
attron
(
COLOR_PAIR
(
1
));
// set snake color
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
mvaddch
(
snake
[
i
].
y
,
snake
[
i
].
x
,
' '
|
A_REVERSE
);
}
attroff
(
COLOR_PAIR
(
1
));
// unset snake color
}
}
}
void
PrintLiveScore
(
int
score
)
{
int
n
=
0
;
if
(
score
>=
1000
)
{
n
=
1
;
}
else
if
(
score
>=
10000
)
{
n
=
2
;
}
else
if
(
score
>=
100000
)
{
n
=
3
;
}
mvprintw
(
fieldY
+
ROWS
,
fieldX
,
"SCORE"
);
mvprintw
(
fieldY
+
ROWS
,
fieldX
+
COLS
-
3
-
n
,
"%d"
,
score
);
mvprintw
(
fieldY
-
4
,
fieldX
,
"---===e-< PRESS 'p' TO PAUSE GAME >-a===---"
);
mvprintw
(
fieldY
-
2
,
fieldX
,
"---===e-< PRESS 'q' TO QUIT >-a===---"
);
}
void
PrintScore
(
int
score
)
{
FILE
*
fp
=
fopen
(
"scores.txt"
,
"a"
);
if
(
fp
==
NULL
)
{
printf
(
"Error opening file!
\n
"
);
endwin
();
}
time_t
currentTime
;
struct
tm
*
currentTimeStruct
;
time
(
&
currentTime
);
currentTimeStruct
=
localtime
(
&
currentTime
);
char
timeString
[
STRUCT_LEN
];
strftime
(
timeString
,
STRUCT_LEN
,
"%Y.%m.%d %H:%M:%S"
,
currentTimeStruct
);
fprintf
(
fp
,
"%s score: %d
\n
"
,
timeString
,
score
);
fclose
(
fp
);
}
void
RefreshScores
(
int
max
)
{
FILE
*
fp
=
fopen
(
"scores.txt"
,
"r"
);
if
(
fp
==
NULL
)
{
printf
(
"Error opening file!
\n
"
);
}
int
last_scores
[
MAX_SCORES
];
int
year
,
month
,
day
;
int
i
=
0
;
int
count
=
0
;
while
(
fscanf
(
fp
,
"%d.%d.%d %*s %*s %d"
,
&
year
,
&
month
,
&
day
,
&
last_scores
[
i
])
==
4
)
{
i
++
;
count
++
;
if
(
i
>=
max
)
{
printf
(
"Error! Count has reached the limit!
\n
"
);
break
;
}
if
(
count
==
0
)
{
printf
(
"No scores have yet been saved
\n
"
);
}
}
}
void
ReadScores
(
int
max
,
int
a
,
WINDOW
*
score
,
int
center
)
{
FILE
*
fp
=
fopen
(
"scores.txt"
,
"r"
);
if
(
fp
==
NULL
)
{
fopen
(
"scores.txt"
,
"w"
);
fopen
(
"scores.txt"
,
"r"
);
printf
(
"Error opening file!
\n
"
);
endwin
();
}
int
last_scores
[
MAX_SCORES
];
int
year
,
month
,
day
;
int
i
=
0
;
int
count
=
0
;
while
(
fscanf
(
fp
,
"%d.%d.%d %*s %*s %d"
,
&
year
,
&
month
,
&
day
,
&
last_scores
[
i
])
==
4
)
{
i
++
;
count
++
;
if
(
i
>=
max
)
{
printf
(
"Error! Count has reached the limit!
\n
"
);
break
;
}
if
(
count
==
0
)
{
printf
(
"No scores have yet been saved
\n
"
);
}
}
if
(
a
==
0
)
{
int
max_score
=
last_scores
[
0
];
// start with the first score
if
(
count
==
0
)
{
mvwprintw
(
score
,
4
,
center
-
25
,
"NO SCORES HAVE BEEN SAVED!"
);
}
else
{
for
(
int
i
=
1
;
i
<
count
;
i
++
)
{
if
(
last_scores
[
i
]
>
max_score
)
{
max_score
=
last_scores
[
i
];
// update the max score if a higher score is found
}
}
char
n
[
10
];
if
(
last_scores
[
i
]
==
0
)
{
strcpy
(
n
,
"---"
);
}
else
if
(
last_scores
[
i
]
>=
100
)
{
strcpy
(
n
,
"--"
);
}
wattron
(
score
,
COLOR_PAIR
(
4
));
// red borders
mvwprintw
(
score
,
4
,
2
,
"<><><><><><><><><><>"
);
wattroff
(
score
,
COLOR_PAIR
(
4
));
// end red borders
mvwprintw
(
score
,
5
,
2
,
"--------------------"
);
mvwprintw
(
score
,
6
,
2
,
"%d.%d.%d---%d--%s"
,
year
,
month
,
day
,
max_score
,
n
);
mvwprintw
(
score
,
7
,
2
,
"--------------------"
);
wattron
(
score
,
COLOR_PAIR
(
4
));
// red borders
mvwprintw
(
score
,
8
,
2
,
"<><><><><><><><><><>"
);
wattroff
(
score
,
COLOR_PAIR
(
4
));
// end red borders
}
}
else
{
if
(
count
==
0
)
{
mvwprintw
(
score
,
5
,
center
-
15
,
"NO SCORES HAVE BEEN SAVED!"
);
}
else
{
int
j
=
0
;
wattron
(
score
,
COLOR_PAIR
(
4
));
// red borders
mvwprintw
(
score
,
4
,
center
,
"<><><><><><><><><><> "
);
wattroff
(
score
,
COLOR_PAIR
(
4
));
// end red borders
for
(
i
=
count
-
1
;
i
>
0
&&
j
<
SCORE_OUTPUT_MAX
;
i
--
)
{
char
n
[
10
];
if
(
last_scores
[
i
]
==
0
)
{
strcpy
(
n
,
"------"
);
}
else
if
(
last_scores
[
i
]
>=
100
)
{
strcpy
(
n
,
"----"
);
}
else
if
(
last_scores
[
i
]
>=
1000
)
{
strcpy
(
n
,
""
);
}
mvwprintw
(
score
,
5
+
j
,
center
,
"%d.%d.%d---%d-%s"
,
year
,
month
,
day
,
last_scores
[
i
],
n
);
j
++
;
}
wattron
(
score
,
COLOR_PAIR
(
4
));
// red borders
mvwprintw
(
score
,
8
,
center
,
"<><><><><><><><><><> "
);
wattroff
(
score
,
COLOR_PAIR
(
4
));
// end red borders
}
}
fclose
(
fp
);
}
int
main
()
{
Setup
();
Menu
();
endwin
();
return
0
;
}
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