11 c++版本的贪吃蛇

发布于:2024-04-27 ⋅ 阅读:(23) ⋅ 点赞:(0)

前言

呵呵 这大概是 大学里面的 c++ 贪吃蛇了吧 

有一些 面向对象的理解, 但是不多 

最近 因为想要 在单片机上面移植一下 贪吃蛇, 所以 重新拿出了一下 这份代码 

然后 将它更新为 c 版本, 还是 用了一些时间 

这里 具体的实现 就不赘述, 仅仅是 发一下代码 以及 具体的使用 

然后 貌似 放在 win10 上面执行 还有一些问题, 渲染的, 应该很好调整 

 

 

贪吃蛇

#include<iostream>
#include<Windows.h>
#include<ctime>
#include<string>
using namespace std;

void position(int ,int );
class Node	//蛇身的结点
{	
public:
	int x;
	int y;
	Node *next;
	Node(int x=0,int y=0){	this->x=x;this->y=y;next=NULL;	}
};

class hero		//英雄榜的结点
{
public:string name;
	   int score;
	   hero(string s="anonymous",int sc=0){	name=s;score=sc;	}
};

class Snake
{
private:
	Node *head;		//头结点
	Node *food;		//食物结点
	bool flag;		//是否吃到食物
	int grade;		//所得分数
	int level;		//游戏级别
	int tot;		//蛇身节点的个数
	int direction;	//蛇的方向
	int herotot;	//英雄榜人物个数
	hero HERO[11];	//英雄榜数据

public:
	Snake()
	{		head=new Node(20,10);flag=false;direction=2;tot=1;grade=0;level=0;herotot=0;
	}
	void setmap();		  //screen's length = 78 ; width = 24;打印出地图
	void menu();			//菜单
	bool isover();			//是否游戏结束
	void go();				//蛇的行动
	void running();			//运行程序
	void init();			//初始化蛇身
	void createfood();		//创建食物
	bool getfood();			//吃到食物
	bool isfoodcover();		//食物是否出现在蛇体上
	bool issuccess();		//是否是成功退出
	void showinfo();		//看文件信息
	void lookhero();		//看英雄榜
	void print();			//打印蛇体
};

int main()
{	Snake snake;
	char ch;

	system("cls");
	do
	{	snake.menu();
	cin>>ch;
	if(ch=='1')	snake.running();
	if(ch=='2')	snake.showinfo();
	if(ch=='3')	snake.lookhero();
	if(ch=='4')	break;
	}while(true);
	return 0;
}




void position(int x,int y)
{	HANDLE handle;
	handle=GetStdHandle(STD_OUTPUT_HANDLE);
	COORD position={x,y};								 // position.x=x;position.y=y
	SetConsoleCursorPosition(handle,position);
}
  
void Snake::setmap()	  	// →	←	↑	↓	⊙	□	■	※
{
	int i;
	
	system("cls");
	for(i=0;i<=39;i++)
	{	position(2*i,0);
		cout<<"※";
		position(2*i,24);
		cout<<"※";
		Sleep(10);
	}

	for(i=23;i>0;i--)
	{	position(0,i);
		cout<<"※";
		position(60,i);
		cout<<"※";
		position(78,i);
		cout<<"※";
		Sleep(10);
	}

	position(64,4);	cout<<"grade :"<<grade;
	position(64,6);	cout<<"level :"<<level;
	position(64,8);	cout<<"length :"<<tot;
	position(64,10);	cout<<"pause: right ";
	position(64,12);	cout<<"key of mouse .";
	position(64,16);	cout<<"snake";
	position(64,18);	cout<<"direction:";
	position(68,20);	cout<<"↑";
	position(64,22);	cout<<"←  ↓  →";
	position(0,0);
}  
  
void Snake::menu()	// ∞	○	Θ
{	system("cls");
	for(int i=0;i<=39;i++)
	{	position((2*i),0);	cout<<"※";
		position((2*i),1);	cout<<"№";
		position((2*i),2);	cout<<"∞";
		position((2*i),3);	cout<<"⊙";
		Sleep(20);
		position((2*i),24);	cout<<"○";
		position((2*i),23);	cout<<"∞";
		position((2*i),22);	cout<<"‰";
		position((2*i),21);	cout<<"※";
	}
	position(20,8);	cout<<"GLUTTONOUS SNAKE"<<endl;
	position(18,10);	cout<<"1.START";
	position(40,10);	cout<<"2.ABOUT GAME";
	position(18,14);	cout<<"3.HERO ";
	position(40,14);	cout<<"4.EXIT";
	position(18,18);	cout<<"PLEASE INPUT :  ";
}

void Snake::init()
{	Node *p=head;
	srand((int )time(NULL));
	for(int i=1;i<=3;i++)
	{	p->next=new Node(20-2*i,10);
		tot++;
		p=p->next;
	}
}  
  
bool Snake::isover()
{	Node *p=head->next->next->next;
	if(head->x>58 || head->x<=0 || head->y>23 || head->y<=0 )	return true;
	while(p)
	{	if( head->x==p->x && head->y==p->y )	return true;
		p=p->next;
	}
	if(level>=10)	return true;
	return false;
}  
  
void Snake::createfood()
{	food=new Node;
	do	
	{	food->x=rand()%58+2;
		food->y=rand()%23+1;
	}while(isfoodcover());
	if(food->x%2==1)	food->x=food->x-1;
	position(food->x,food->y);	cout<<"□";
}  
  
bool Snake::getfood()
{	Node *p;

	if(flag==true)
	{	if((direction+2)%2==1)
		{	
			p=new Node(head->x,head->y+direction);
			p->next=head;
			head=p;
		}
		else
		{	p=new Node(head->x+direction,head->y);
			p->next=head;
			head=p;
		}
		flag=false;
	}

	if( head->x == food->x && head->y == food->y )
	{	flag=true;
		if(grade>=100*(level+1)*(level+1))
		{	level++;
			position(71,6);cout<<level;
		}
		grade=grade+5*(level+1);
		position(71,4);cout<<grade;
		position(72,8);cout<<++tot;
		return true;
	}
	return false;
}

bool Snake::isfoodcover()
{	Node *p=head;
	while(p)
	{	if(food->x==p->x && food->y==p->y )	return true;
		p=p->next;
	}
	return false;
}

void Snake::go()
{	if(GetAsyncKeyState(VK_UP) && direction!=1 )	direction=-1;
	else if(GetAsyncKeyState(VK_LEFT) && direction!=2 )	direction=-2;
	else if(GetAsyncKeyState(VK_DOWN) && direction!=-1 )	direction=1;
	else if(GetAsyncKeyState(VK_RIGHT) && direction!=-2 )	direction=2;
	if(GetAsyncKeyState(VK_LBUTTON))
	{	while(true)
		{if(GetAsyncKeyState(VK_LBUTTON))	break;
		}
	}

	Node *p;
	if((direction+2)%2==1)
	{	p=new Node(head->x,head->y+direction);
		p->next=head;
		head=p;
		while(p->next->next)
		{	p=p->next;
		}
		position(p->next->x,p->next->y);	cout<<" ";
		delete p->next;
		p->next=NULL;
		print();
	}
	else 
	{	p=new Node(head->x+direction,head->y);
		p->next=head;
		head=p;
		while(p->next->next)
		{	p=p->next;
		}
		position(p->next->x,p->next->y);	cout<<" ";
		delete p->next;
		p->next=NULL;
		print();
	}
	Sleep(300-30*level);
}    

void Snake::running()
{	Node *p;
	system("cls");
	init();
	setmap();
	createfood();
	int num=0;
	  while(true)
	  {
		print();
		go();
	  	if(isover())	break;
	  	if(getfood())
		{	delete food;
			createfood();
		}
	  }
	position(0,25);
	if(issuccess())	cout<<"CONGRATULATIONS YOU WIN !"<<endl;
	else cout<<"SORRY YOU LOSE"<<endl;
	cout<<"YOU GOT "<<grade<<" POINT"<<endl;
	cout<<"PLEASE INPUT YOUR BIG NAME: ";
	if(herotot<10)
	{	cin>>HERO[herotot].name;HERO[herotot].score=grade;	herotot++;
	}
	else	{	cin>>HERO[10].name;HERO[10].score=grade;	}
	hero temp=HERO[herotot-1];
	for(int j=herotot-2;j>=0;j--)
	{	if(HERO[herotot-1].score>HERO[j].score)	num++;
		else break;
	}
	for(j=0;j<num;j++)
		HERO[herotot-j-1]=HERO[herotot-j-2];
	HERO[herotot-num-1]=temp;

							// 还回原状
	  p=head->next;
	  while(p)
	  {	p=p->next;
		delete head->next;
		head->next=p;
	  }
	  head->x=20;head->y=10;head->next=NULL;
	direction=2;tot=1;grade=0;level=0;
}

bool Snake::issuccess()
{	if(level>=10)	return true;
	return false;
}

void Snake::showinfo()
{	system("cls");
	position(16,8);	cout<<"WELCOME TO GLUTTONOUS SNAKE";
	position(12,12);	cout<<"THIS IS MADE BY JERRY "<<endl<<"	  AT 2013-11-08	FRIDAY AFTERNOON";
	position(12,16);	system("pause");
}

void Snake::lookhero()
{	system("cls");
	position(25,3);	cout<<"HERO BANG ";
	for(int i=0;i<herotot;i++)
	{	position(12,i+7);
		cout<<"THE  "<<i+1<<"  TH"<<"   NAME :"<<HERO[i].name; position(47,i+7); cout<<"	SCORE :"<<HERO[i].score<<endl;
	}
	position(25,20);	system("pause");
}

void Snake::print()
{
	Node *p=head;
	while(p)
	{	position(p->x,p->y);	cout<<"■";
		p=p->next;
	}
}
  

 

 

程序截图

 首页

470631a91777471daeeb2ecbc707d396.png

 

 

程序执行之后如下  

c798887ae8ec4f85ac461c5c4bef1eca.png

 

 

以前在 win7 上面的截图 

a29938fe50014ccf996661df3817445d.png

 

 

 

 

 


网站公告

今日签到

点亮在社区的每一天
去签到