#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;
}
}
C++贪吃蛇源代码
最新推荐文章于 2024-07-31 21:04:25 发布