#include<iostream>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include<string.h>
#define LINE 20
#define ROW 20
using namespace std;
void run();
enum dir {up,down,lef,rit} direct1=rit;
class CFrame
{
public:
void InFrame();
void OutFrame();
public:
char point[LINE][ROW];
} f;
class CSnake;
class CSnakeNode
{
public:
CSnakeNode()
{
prior=NULL;
next=NULL;
}
friend class CSnake;
CSnakeNode *prior;
CSnakeNode *next;
public:
int x;
int y;
};
void CFrame::InFrame()
{
for(int i=0;i<LINE;i++)
for(int j=0;j<ROW;j++)
{
if(i==0||j==0||i==(LINE-1)||j==(ROW-1))
point[i][j]='*';
else
point[i][j]=' ';
}
}
void CFrame::OutFrame()
{
for(int i=0;i<LINE;i++)
{
for(int j=0;j<ROW;j++)
{
cout<<point[i][j]<<' ';
}
cout<<endl;
}
}
class CMove;
class CSnake
{
public:
CSnake()
{
head=NULL;
tail=NULL;
}
void Clear();
void AddHead(int x,int y);
void DelTail();
friend class CMove;
friend void Isexit(int x,int y,CSnake snake);
public:
CSnakeNode *head;
CSnakeNode *tail;
};
void CSnake::Clear()
{
CSnakeNode *node;
while(head)
{
node=head;
f.point[head->x][head->y]=' ';
head=head->next;
delete node;
}
}
void CSnake::AddHead(int x,int y)
{
CSnakeNode *p=new CSnakeNode;
p->x=x;
p->y=y;
p->prior=NULL;
p->next=head;
if(head!=NULL)
{
head->prior=p;
f.point[head->x][head->y]='*';
}
head=p;
if(tail==NULL)
tail=head;
f.point[x][y]='@';
}
void CSnake::DelTail()
{
CSnakeNode *q=tail;
f.point[tail->x][tail->y]=' ';
if(tail==head)
tail= head= NULL;
else
{
tail= tail->prior;
tail->next= NULL;
}
delete q;
}
int Ifexit(int x,int y,CSnake &snake)
{
CSnakeNode *p=new CSnakeNode();
p=snake.head;
int flag=0;
while(p)
{
if(p->x==x&&p->y==y)
{
flag=1;
break;
}
else
p=p->next;
}
return flag;
}
class CFood
{
public:
void GetFood(CSnake &snake);
public:
int food_x;
int food_y;
} food;
class CMove
{
public:
void Moving(CSnake &snake);
void Change_dir(char);
public:
dir direct;
} ;
void CFood::GetFood(CSnake &snake)
{
srand((unsigned int) time(NULL));
food_x=rand()%(LINE-2)+1;
food_y=rand()%(ROW-2)+1;
while(Ifexit(food_x,food_y,snake))
{
food_x=rand()%(LINE-2)+1;
food_y=rand()%(ROW-2)+1;
}
f.point[food_x][food_y]='#';
}
void CMove::Change_dir(char keydown)
{
switch(keydown)
{
case 'w': case 72:
case 'W':direct=up;break;
case 's': case 80:
case 'S':direct=down;break;
case 'A':case 75:
case 'a':direct=lef;break;
case 'D':case 77:
case 'd':direct=rit;break;
}
}
void CMove::Moving(CSnake &snake)
{
int a,b;
a=snake.head->x;
b=snake.head->y;
switch(direct)
{
case up:--a;break;
case down:++a;break;
贪吃蛇的编程代码case lef:--b;break;
case rit:++b;break;
}
direct1=direct;
if(a==0||a==(LINE-1)||b==0||b==(ROW-1)||Ifexit(a,b
,snake))
{
char c;
cout<<"游戏结束,是否继续: 是:y 否:n"<<endl;
cin>>&c;
if(c!='y'&&c!='Y')
{
cout<<"退出游戏!"<<endl;
exit(0);
}
else
{
snake.Clear();
system("cls");
run();
}
system("PAUSE");
exit(0);
}
if(a==food.food_x&&b==food.food_y)
{
snake.AddHead(a,b);
food.GetFood(snake);
}
else
{
snake.AddHead(a,b);
snake.DelTail();
}
}
int IsOpposite(dir d)
{
if(direct1==up&&d==down||direct1==rit&&d==lef||direct1==down&&d==up||direct1==lef&&d==rit)
return 1;
else
return 0;
}
void run()
{
CSnake snake;
CMove m;
f.InFrame();
food.GetFood(snake);
snake.AddHead(3,4);
snake.AddHead(3,5);
snake.AddHead(3,6);
snake.AddHead(3,7);
f.point[snake.tail->x][snake.tail->y]='+';
f.OutFrame();
while(1)
{
char c1,keydown;
c1=getch();
if(c1==-32)
{
keydown=getch();
}
else
keydown=c1;
m.Change_dir(keydown);
if(IsOpposite(m.direct))
m.direct=direct1;
m.Moving(snake);
while(!kbhit())
{
system("cls");
m.Moving(snake);
f.point[snake.tail->x][snake.tail->y]='+';
f.OutFrame();
Sleep(250);
}
}
}
void main()
{
char c;
cout<<"游戏规则:W:上 S:下 A:左 D: 右; 或者用键盘的↑↓←→键控制"<<endl;
cout<<"是否开始游戏 是:Y 否:N"<<endl;
cin>>&c;
if(c!='y'&&c!='Y')
{
cout<<"退出游戏!"<<endl;
exit(0);
}
else
{
system("cls");
run();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论