C++贪吃蛇源代码

#include <iostream>
#include <Windows.h>
#include <time.h>
#include <conio.h>
using namespace std;

//界面初始化
inline void Refresh(char qp[][22], int grade, int gamespeed)
{
	system("cls");
	int i, j;
	cout<<endl;//第一行不显示

	for(i = 0; i < 22; i++)
	{
		cout<<"\t";
		for(j = 0; j < 22; j++)
		{
			cout<<qp[i][j]<<' ';//界面横杆- - - -
		}
		if(i == 0) cout<<"\t等级为:"<<grade;
		if(i == 4) cout<<"\t自动前进时间";
		if(i == 6) cout<<"\t间隔为:"<<gamespeed<<"ms";
		cout<<endl;
	}
}

int main()
{
	char tcsQiPan[22][22];
	int i, j;
	for(i = 1; i<= 20; i++)
	{
		for(j = 1; j <= 20; j++)
		{
			tcsQiPan[i][j] = ' ';
		}
	}
	for(i = 0; i <= 21; i++)
	{
		tcsQiPan[0][i] = tcsQiPan[21][i] = '-';
	}
	for(i = 0; i <= 21; i++)
	{
		tcsQiPan[i][0] = tcsQiPan[i][21] = '|';
	}

	int tcsZuoBiao[2][200];
	for(i = 0; i < 4; i++)
	{
		tcsZuoBiao[0][i] = 1;
		tcsZuoBiao[1][i] = i + 1;
	}

	int head = 3, tail = 0;//头和尾
	//初始化蛇身 ***#
	for(i = 1; i <= 3; i++)
	{
		tcsQiPan[1][i] = '*';
	}
	tcsQiPan[1][4] = '#';

	int x1, y1;

	srand(time(0));//选取当前时间出随机数
	//产生1~20的随机数
	do 
	{
		x1 = rand()%20 + 1;
		y1 = rand()%20 + 1;
	} while (tcsQiPan[x1][y1] != ' ');
	tcsQiPan[x1][y1] = '*';

	cout<<"\n\n\t\t游戏即将开始!"<<endl;
	long start;

	int grade = 1, length = 4;
	int gamespeed = 500;
	for(i = 3; i >= 0; i--)
	{
		start = clock();//存入当前时间
		while(clock() - start <= 1000);
		system("cls");
		if(i > 0)
		{
			cout<<"\n\n\t\t进入倒计时:"<<i<<endl;//倒计时 3 2 1
		}
		else
			Refresh(tcsQiPan, grade, gamespeed);

	}
	int timeover;
	char direction = 77;//键盘right的键值
	int x, y;
	while(1)
	{
		timeover = 1;
		start = clock();
		while((timeover = (clock() - start <= gamespeed)) && !kbhit());
		//没键按下则一直往右走,有键按下则改变方向
		if(timeover)
		{
			getch();
			direction = getch();
		}
		switch(direction)
		{
			//向上
			case 72: x = tcsZuoBiao[0][head] - 1; y = tcsZuoBiao[1][head];break;
			//向下
			case 80: x = tcsZuoBiao[0][head] + 1; y = tcsZuoBiao[1][head];break;
			//向左
			case 75: x = tcsZuoBiao[0][head]; y = tcsZuoBiao[1][head] - 1;break;
			//向右
			case 77: x = tcsZuoBiao[0][head]; y = tcsZuoBiao[1][head] + 1;
		}

		if(!(direction == 72 || direction == 80 || direction == 75 || direction == 77))
		{
			cout<<"\tGameOver!"<<endl; 
			return 0;
		}
		if(x == 0 || x == 21 || y == 0 || y == 21)
		{
			cout<<"\tGameOver!"<<endl; 
			return 0;
		}
		if(tcsQiPan[x][y] != ' ' && !(x == x1 && y == y1))//棋盘为空代表没碰到身子,但是有可能为刚出的米
		{
			cout<<"\tGameOver!"<<endl; 
			return 0;
		}
		//吃到米
		if(x == x1 && y == y1)
		{
			length++;
			if(length >= 8)
			{
				length -= 8;
				grade++;//吃到8个,游戏级别加1
				if(gamespeed >= 200)
				{
					gamespeed = 550 - grade * 50; //前进的速度,也就是刷新的时间间隔
				}
			}
			tcsQiPan[x][y] = '#';
			tcsQiPan[tcsZuoBiao[0][head]][tcsZuoBiao[1][head]] = '*';
			head = (head + 1) % 100;
			tcsZuoBiao[0][head] = x;
			tcsZuoBiao[1][head] = y;
			do 
			{
				x1 = rand()%20 + 1;
				y1 = rand()%20 + 1;
			} while (tcsQiPan[x1][y1] != ' ');
			tcsQiPan[x1][y1] = '*';
			Refresh(tcsQiPan, grade, gamespeed);
		}
		else
		{
			tcsQiPan[tcsZuoBiao[0][tail]][tcsZuoBiao[1][tail]] = ' ';
			tail = (tail + 1) % 100;
			tcsQiPan[tcsZuoBiao[0][head]][tcsZuoBiao[1][head]] = '*';
			head = (head + 1) % 100;
			tcsZuoBiao[0][head] = x;
			tcsZuoBiao[1][head] = y;
			tcsQiPan[tcsZuoBiao[0][head]][tcsZuoBiao[1][head]] = '#';
			Refresh(tcsQiPan, grade, gamespeed);
		}
		return 0;
	}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值