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
|
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameover;
const int width=40; // Width of map = 40
const int height=20; // Height of map = 20
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 directon {STOP=0, LEFT, RIGHT, UP, DOWN}; // Tracks direction of snake when button is pressed
directon d;
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;
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
{
score+=10;
fruit_X= rand() % width;
fruit_Y= rand() % height;
tail++;
}
}
int main()
{
Setup();
while (!gameover) // While game_over is false, run Draw, Input, Logic
{
Draw();
Input();
Logic();
Sleep(10); // Slows game down (Number can be changed or removed to change the speed that the snake moves at, making it more or less challenging)
}
return 0;
}
|