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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
struct players
{
string username; // Username of player
int highscore;
};
struct participants
{
players playerlist[3]; // Array of players/max 3 players
};
// Prototypes
void print (players, int);
players player1 (string);
players player2 (string);
players player3 (string);
bool gameover;
const int width=40; // Width of map = 20 (becomes slower and more glitchy when width is increased)
const int height=20; // Height of map = 20 (becomes slower and more glitchy when height is increased)
int x, y, fruit_X, fruit_Y, score; // Variables for “head” position and for “fruit” position
int tail_X[100], tail_Y[100];
int tail;
enum direction {STOP=0, LEFT, RIGHT, UP, DOWN}; // Tracks direction of snake when button is pressed
direction d;
participants z;
void Setplayer()
{
for(int i=0; i<3; i++)
{
string username; // Username of player
cout << "Enter username: " << endl;
cin >> z.playerlist[i].username;
cout << endl;
}
}
// Displays high score and usernames of all 3 players
void print(players a, players b, players c)
{
cout << "High score of player 1: " << a.highscore << endl; // High score of player with username 1
cout << "High score of player 2: " << b.highscore << endl; // High score of player with username 2
cout << "High score of player 3: " << c.highscore << endl; // High score of player with username 3
}
void Setup() // Game setup function
{
gameover=false;
d=STOP;
x=width/2;
y=height/2;
fruit_X=rand() % width;
fruit_Y=rand() % height;
score=0;
}
void Draw() // Map setup function
{
system("cls");
for(int i=0; i<width+2; i++) // Displays top border of map
cout << "*"; // Character used
cout << endl;
for(int i=0; i<height; i++) // Loop for sides of map
{
for(int j=0; j<width; j++)
{
if(j==0) // If j = 0, print first *
cout << "*"; // Character used
if(i==y && j==x)
cout << "D";
else if(i==fruit_Y && j==fruit_X)
cout << "o";
else
{
bool print=false;
for(int k=0; k<tail; k++) // Loop to display tail
{
if(tail_X[k]==j && tail_Y[k]==i)
{
cout << "o";
print=true;
}
}
if(!print)
cout << " ";
}
if(j==width-1) // If j = -1, print last *
cout << "*"; // Character used
}
cout << endl;
}
for(int i=0; i<width+2; i++) // Displays bottom border of map
cout << "*"; // Character used
cout << endl;
cout << "Score:" << score << endl; // displays score
}
void Input() // Character and direction setup function
{
if(_kbhit()) // <conio.h>
{
switch(_getch())
{
case 'a': // If a is pressed, move left
d=LEFT;
break;
case 'd': // If d is pressed, move right
d=RIGHT;
break;
case 'w': // If w is pressed, move up
d=UP;
break;
case 's': // If s is pressed, move down
d=DOWN;
break;
case 'o': // If o is pressed, terminate game
gameover=true;
break;
}
}
}
void Logic()
{
int prev_X=tail_X[0]; // Previous x coordinate of tail
int prev_Y=tail_Y[0]; // Previous y coordinate of tail
int prev2_X, prev2_Y;
tail_X[0]=x;
tail_Y[0]=y;
for(int i=1; i<tail; i++) // Loop for tail
{
prev2_X=tail_X[i];
prev2_Y=tail_Y[i];
tail_X[i]=prev_X;
tail_Y[i]=prev_Y;
prev_X=prev2_X;
prev_Y=prev2_Y;
}
switch(d)
{ case LEFT:
x--; // Decrease x coordinate of snake
break;
case RIGHT:
x++; // increase x coordinate of snake
break;
case UP:
y--; // Decrease y coordinate of snake
break;
case DOWN:
y++; // increase y coordinate of snake
break;
default:
break;
}
if(x>width || x<0 || y>height || y<0) // If snake touches the borders, game over
gameover=true;
// if (x>=width) x=0; else if (x<0) x=width-1; // Lets snake go through the borders
// if (y>=height) y=0; else if (y<0) y=height-1; // Lets snake go through the borders
for(int i=0; i<tail; i++)
if(tail_X[i]==x && tail_Y[i]==y) // If snake touches the tail, game over
gameover=true;
if(x==fruit_X && y==fruit_Y) // If fruit is “eaten”, add 10, move new friut to random location and add 1 piece to tail
{
score+=10;
fruit_X= rand() % width;
fruit_Y= rand() % height;
tail++;
}
}
int main()
{
Setplayer();
for(int r=0; r>2; r++)
{
}
Setup();
for(int i=0; i<2; i++)
{
while(!gameover) // While game_over is false, run Draw, Input, Logic
{
Draw();
Input();
Logic();
Sleep(10); // (OPTIONAL) Slows game down (Number can be changed or removed to change the speed that the snake moves at, making it more or less challenging)
//z.playerlist[i].highscore=highscore;
}
}
return 0;
}
|