snake-C++
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
bool over;
int x,y,FrX,FrY,score=0,i,j;
char m;
const int height=20;
const int width=40;
enum direction {STOP=0,RIGHT,LEFT,UP,DOWN};
direction dir;
void setup()
{
over=false;
dir=STOP;
y=height/2;
x=width/2;
FrX=rand()%width;
FrY=rand()%height;
score=0;
}
void draw()
{
system("cls");
for(j=0;j<=width+2;j++){
cout<<"#";
}
cout<<endl;
for(i=0;i<=height;i++){
for(j=0;j<=width;j++){
if(j==0) cout<<"#";
if(i==y && j==x) cout<<"O";
else if(i==FrY && j==FrX) cout<<"F";
else cout<<" ";
if(j==width) cout<<"#";
}
cout<<endl;
}
for(j=0;j<=width+2;j++){
cout<<"#";
}
cout<<endl;
cout<<"SCORE: "<<score<<endl;
}
void input(){
if(_kbhit()){
switch(_getch()){
case 'a': dir=LEFT; break;
case 'l': dir=RIGHT; break;
case 'u': dir=UP; break;
case 'd': dir=DOWN; break;
case 'x': over=true; break;
}
}
}
void logic(){
switch(dir)
{
case LEFT: x--; break;
case RIGHT: x++; break;
case UP: y--; break;
case DOWN: y++; break;
default: break;
}
if(x>width||x<0||y>height || y<0) over=true;
if(x==FrX && y==FrY){
score+=10;
FrX=rand()%width;
FrY=rand()%height;
}
}
int main(){
ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
setup();
while(!over){
draw();
input();
logic();
Sleep(10);
}
return 0;
}
No comments