-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
124 lines (91 loc) · 2.35 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <conio.h>
//#include <windows.h>
using namespace std;
const int width = 80;
const int height = 20;
//decalring some global variables for this project
int x,y;
int foodX, foodY;
int score;
int snakeTailX[50], snakeTailY[50];
int snakeLen;
enum snakeDirection
{
STOP = 0,LEFT,RIGHT,UP,DOWN
};
// int x,y;
// int foodX, foodY;
// int score;
// int snakeTailX[50], snakeTailY[50];
// int snakeLen;
// enum snakeDirection
// {
// STOP = 0,LEFT,RIGHT,UP,DOWN
// };
snakeDirection sDir;
bool isGameOver;
//initialising some game variables
void GameInit (void)
{
isGameOver = false;
sDir = STOP;
x = height/2;
y = width/2;
foodX = rand() & width; //so this will basically generate a randome coordinate for
foodY = rand() & height; //the fruit, and % will make sure that it lands withing the area given by height and width
}
//craeting a function which will render the game and end it when game is over
void GameRender(string playerName)
{
system("cls"); //this will clear the console
//rendor the top walls
for(int i=0;i<width+2;i++)
{
cout<<"-";
}
cout<<endl;
//now to create the side walls which will be repeated height number of times
for(int i=0;i<height;i++)
{
for(int j =0;j<=width;j++)
{
if(j == 0 || j == width)
{
cout<<"|";
}
//creation of the snakes head
if(j == y && j == x)
{
cout<<"O";
}
else if(i == foodY && j == foodX)
{
cout<<"@";
}
else
{
bool prTail = false;
for(int k = 0;k<snakeLen;k++)
{
if(snakeTailX[k] == j && snakeTailY[k] == i)
{
cout<<"o";
prTail = true;
}
}
if (!prTail)
cout<<" ";
}
}
}
cout<<endl;
//now to create the bottom walls
for(int i =0;i<width+2;i++)
{
cout<<"-";
}
cout<<endl;
//and now displaying the score
cout<<"score for "<<playerName<<" is "<<score<<endl;
}