Код:
#include <iostream>
#include <conio.h>
#include <queue>
#include <windows.h>
#include <ctime>
#include <cstdio>
#define speed 45
#define sizeX 14
#define sizeY 24
using namespace std;
char table[51][51];
deque <int> snakex;
deque <int> snakey;
char direction='d';
bool gennew=true;
int foodx, foody;
void cleart()
{
for (int i=0 ; i<=sizeX ; i++)
for (int z=0 ; z<=sizeY ; z++)
table[i][z]='#';
}
void draw()
{
int ssize=snakex.size();
for (int i=0 ; i<ssize ; i++) {
table[snakex.front()][snakey.front()]=' ';
snakex.push_back(snakex.front());
snakex.pop_front();
snakey.push_back(snakey.front());
snakey.pop_front();
}
if (direction=='a' or direction=='A')
table[snakex.front()][snakey.front()]='<';
else if (direction=='s' or direction=='S')
table[snakex.front()][snakey.front()]='V';
else if (direction=='d' or direction=='D')
table[snakex.front()][snakey.front()]='>';
else
table[snakex.front()][snakey.front()]='A';
if(!gennew)
table[foodx][foody]='.';
}
void update()
{
int update=1;
if(kbhit())
update=getch();
if ( (update==97 or update==100 or update==65 or update==68)
and (direction=='w' or direction=='s' or direction=='W' or direction=='S')) {
direction=(char)update;
}
else if ( (update==115 or update==119 or update==83 or update==87)
and (direction=='a' or direction=='d' or direction=='A' or direction=='D')) {
direction=(char)update;
}
}
void food()
{
int randx=50, randy=50;
while(table[randx][randy]!='#') {
randx=rand()%15;
randy=rand()%25;
foodx=randx;
foody=randy;
}
table[randx][randy]='.';
gennew=false;
}
void snakemove()
{
int x=snakex.front(), y=snakey.front();
if (direction=='a' or direction=='A') {
snakex.push_front(snakex.front());
snakex.pop_back();
if(y>0)
snakey.push_front(snakey.front()-1);
else
snakey.push_front(sizeY);
snakey.pop_back();
}
if (direction=='d' or direction=='D') {
snakex.push_front(snakex.front());
snakex.pop_back();
if (y<sizeY)
snakey.push_front(snakey.front()+1);
else
snakey.push_front(0);
snakey.pop_back();
}
if (direction=='s' or direction=='S') {
if (x<sizeX)
snakex.push_front(snakex.front()+1);
else
snakex.push_front(0);
snakex.pop_back();
snakey.push_front(snakey.front());
snakey.pop_back();
}
if (direction=='w' or direction=='W') {
if(x>0)
snakex.push_front(snakex.front()-1);
else
snakex.push_front(sizeX);
snakex.pop_back();
snakey.push_front(snakey.front());
snakey.pop_back();
}
}
void eating()
{
if (table[snakex.front()][snakey.front()]=='.') {
table[snakex.front()][snakey.front()]='O';
gennew=true;
snakex.push_back(50);
snakey.push_back(50);
}
}
int onhit()
{
int headx=snakex.front(), heady=snakey.front();
int repeat=0;
int ssize=snakex.size();
for (int i=0 ; i<ssize ; i++) {
snakex.push_back(snakex.front());
snakex.pop_front();
snakey.push_back(snakey.front());
snakey.pop_front();
if (headx==snakex.front() and heady==snakey.front())
repeat++;
}
if (repeat>1)
return 1;
return 0;
}
int main()
{
char newgame='z';
system("color 0f");
system("title BoyWithBike's Snake Game");
srand( time(NULL) );
snakey.push_front(7);
snakex.push_front(7);
snakey.push_front(8);
snakex.push_front(7);
snakey.push_front(9);
snakex.push_front(7);
for (int n=0 ; ;n++) {
cleart();
update();
snakemove();
draw();
if (gennew)
food();
eating();
if (onhit()) {
system("cls");
cout<<"Game Over"<<endl;
cout<<"Your score is: "<<snakex.size()-3<<endl;
break;
}
system("cls");
for (unsigned long long i=0 ; i<=sizeX ; i++) {
printf("\n");
for (unsigned long long z=0 ; z<=sizeY ; z++)
printf(" %c", table[i][z]);
}
Sleep(speed);
}
cout<<"\nStart again? Y/N\n";
while(newgame!='Y' and newgame!='y' and newgame!='n' and newgame!='N') {
system("cls");
cout<<"Game Over"<<endl;
cout<<"Your score is: "<<snakex.size()-3<<endl;
cout<<"\nStart again? Y/N\n";
newgame=getch();
}
if(newgame=='Y' or newgame=='y') {
while(!snakex.empty()) {
snakex.pop_back();
snakey.pop_back();
}
foodx=0;
foody=0;
gennew=true;
direction='d';
main();
}
return 0;
}
Ето ви код на Snake в терминал-а