http://blog.sina.com.cn/s/blog_641f90190100k6yo.html
模仿这个写的
基于dos
想要实现空格键暂停任意键开始(包括方向键)
因为方向键需要两个getch()获取所以没成功
有空再来重写
方向键(←): 0xe04b
方向键(↑): 0xe048
方向键(→): 0xe04d
方向键(↓): 0xe050
换算成10进制
方向键(←): 75
方向键(↑): 72
方向键(→): 77
方向键(↓): 80
//this is sample greedyshake game in dos
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
using namespace std;
#define ROW 22
#define COL 22
#define SHAKEQUE 200
#define PRESHAKELEN 3//初始的长度
inline void refresh(char q[ROW][COL], int grade, int gamespeed,int p)
{
int i, j;
system("cls");
cout << endl;
for (i = 0; i < ROW; i++){
for (j = 0; j < COL; j++)
cout << q[i][j]<<' ';
if (i == 0)
cout << "\t\t等级" << grade;
if (i == 4)
cout << "\t\t分数" << p;
if (i == 8)
cout << "\t\t间隔" << gamespeed << "ms";
cout << endl;
}
}//每次重新画出贪吃蛇棋盘
int main(){
char direction;
char newdirection;
int i,j;
int head, tail;
int point=0;
int greedyshakelocation[2][SHAKEQUE];//贪吃蛇队列中每个点的位置
char greedyboard[ROW][COL];//贪吃蛇棋盘
int grade=1, gamespeed=500;//等级和速度
//初始化棋盘
for (i = 0; i<COL; i++)
greedyboard[0][i] = greedyboard[ROW-1][i] = '-';
for (j = 1; j < ROW - 1; j++)
greedyboard[j][0] = greedyboard[j][COL-1] = '|';
//棋盘的边界
for (i = 1; i < ROW-1;i++)
for (j = 1; j < COL-1; j++)
greedyboard[i][j] = ' ';//初始化棋盘空白部分
//初始化贪吃蛇坐标
head = PRESHAKELEN-1, tail = 0;
for (i = 0; i < PRESHAKELEN; i++){
greedyshakelocation[0][i] = 1;
greedyshakelocation[1][i] = i + 1;
}
for (i = 1; i < PRESHAKELEN; i++)
greedyboard[1][i] = '*';
greedyboard[1][PRESHAKELEN] = '@';
//随机出点
int x1, y1;
srand(time(0));
do{
x1 = rand() % (ROW-2) + 1;
y1 = rand() % (COL-2 )+ 1;
}while (greedyboard[x1][y1] != ' ');
greedyboard[x1][y1] = '*';
long start;
//倒计时界面
/*cout<< "\n\n\t\t游戏即将开始" << endl;
for (i = 3; i > 0; i--){
start = clock();
while (clock() - start < 1000);
system("cls");
cout << "\n\n\t\t进入倒计时" << i << endl;
}*/
//进入游戏
refresh(greedyboard, grade, gamespeed,point);
direction = 77;
newdirection = direction;
int time;
int x=0, y=0;//用来确定头移向的下一个位置
int count = PRESHAKELEN;//用于晋级
while (1){
time = 1;
start = clock();
while ((time = (clock() - start) <= gamespeed) && !_kbhit());
if (time){
_getch();
newdirection = _getch();
switch (newdirection){//这是相反按键无效的处理
case 72:
if (direction == 80)
newdirection = direction;
else direction = newdirection;
break;
case 80:
if (direction == 72)
newdirection = direction;
else direction = newdirection;
break;
case 75:
if (direction == 77)
newdirection = direction;
else direction = newdirection;
break;
case 77:
if (direction == 75)
newdirection = direction;
else direction = newdirection;
break;
}
}
switch (direction){
case 72:
x = greedyshakelocation[0][head] - 1;
y = greedyshakelocation[1][head];//向上
break;
case 80:
x = greedyshakelocation[0][head] + 1;
y = greedyshakelocation[1][head];//向下
break;
case 75:
x = greedyshakelocation[0][head];
y = greedyshakelocation[1][head] - 1;//向左
break;
case 77:
x = greedyshakelocation[0][head];
y = greedyshakelocation[1][head] + 1;//向右
break;
}
//如果撞到墙
if (x == 0 || x == (ROW - 1) || y == 0 || y == (COL - 1)){
cout << "\t Game over!" << endl;
return 0;
}
//如果撞到自己
if (greedyboard[x][y] != ' '&&!(x == x1&&y == y1)){
cout << "\t Game over!" << endl;
return 0;
}
//如果没有意外的话
if (x == x1&&y == y1){//吃到的话别忘记重新生成一个点
count++;
point++;
if (count>=8){
grade++;
count-=8;
cout << count << endl;
if (gamespeed >= 100){//设置一个限制
gamespeed = 500 - grade * 50;
}
}
greedyboard[x][y] = '@';
greedyboard[greedyshakelocation[0][head]][greedyshakelocation[1][head]] = '*';
head++;
head = head % 100;
greedyshakelocation[0][head] = x;
greedyshakelocation[1][head] = y;
do{
x1 = rand() % (ROW - 2) + 1;
y1 = rand() % (COL - 2) + 1;
} while (greedyboard[x1][y1] != ' ');
greedyboard[x1][y1] = '*';
refresh(greedyboard, grade,gamespeed,point);
}//吃到
else{
greedyboard[x][y] = '@';
greedyboard[greedyshakelocation[0][head]][greedyshakelocation[1][head]] = '*';
greedyboard[greedyshakelocation[0][tail]][greedyshakelocation[1][tail]] = ' ';
head = (head++) % 100;
tail = (tail++) % 100;
greedyshakelocation[0][head] = x;
greedyshakelocation[1][head] = y;
refresh(greedyboard,grade,gamespeed,point);
}//没吃到
}
return 0;
}

4961

被折叠的 条评论
为什么被折叠?



