显示方式:清屏打印二位数组,数组即游戏地图,包括墙面(用‘W’表示),蛇(‘H’表蛇头,‘B’表身体)和食物(用‘f’表示)。

const int MaxMap = ;
char map[MaxMap][MaxMap];

边缘为墙面:

    for (int i = ; i < MaxMap; i++){
map[][i] = 'W';
map[MaxMap - ][i] = 'W';
}
for (int i = ; i < MaxMap-; i++){
map[i][] = 'W';
map[i][MaxMap - ] = 'W';
}

蛇的身体用结构体连接:

struct snake{
int x;
int y;
struct snake *next;
}head,body1,body2,tail;

初始蛇长为4,head表示蛇头,其next指向NULL;tail表示蛇尾,进食后增加的身体加到tail之前。

    int centerpoint = MaxMap / ;
head = { centerpoint, centerpoint, NULL };
body1 = { centerpoint, centerpoint-, &head };
body2 = { centerpoint, centerpoint - , &body1 };
tail = { centerpoint, centerpoint - , &body2 }; struct snake *p = &tail;
while (p != NULL){
map[p->x][p->y] = 'B';
p = p->next;
}
map[head.x][head.y] = 'H';

引入一个随机种子,生成食物:

void food(){
int x = , y = ;
srand(time());
while (map[x][y]!=' '){
x = rand() % (MaxMap - ) + ;
y = rand() % (MaxMap - ) + ;
}
map[x][y] = 'f';
return;
}

蛇的移动:

首先定义蛇的默认移动方向,这里默认为右(WSAD)。

char direction = 'd';

接收键盘输入的移动方向,并将其替换为默认方向;如果键盘未输入,则为默认方向。

(蛇不能向身体方向移动,此方向无效。)

(nextX,nextY表示蛇头下一个位置与此时的相对位置。)

计算蛇头应出现的下一个位置。蛇的身体跟随移动,清除原蛇尾。

    char letter = direction;
struct snake *p = &tail;
int nextX = , nextY = , tailX = p->x, tailY = p->y;
if (_kbhit()){
letter = _getch();
if (letter == 'w' || letter == 's' || letter == 'a' || letter == 'd'){
if ( != (direction - letter)){
if (!((letter == 'w' && direction == 's') ||
(letter == 's' && direction == 'w') ||
(letter == 'a' && direction == 'd') ||
(letter == 'd' && direction == 'a')))
direction = letter;
}
}
}
switch (direction){
case 'w':
nextX = -;
nextY = ;
break;
case 's':
nextX = ;
nextY = ;
break;
case 'a':
nextX = ;
nextY = -;
break;
case 'd':
nextX = ;
nextY = ;
break;
default:
break;
}
map[p->x][p->y] = ' ';

蛇身的移动,蛇头位置的计算

    while (p->next != NULL){

        p->x = p->next->x;
p->y = p->next->y;
map[p->x][p->y] = 'B';
p = p->next;
} map[head.x][head.y] = 'B';
head.x += nextX;
head.y += nextY;

吃食和游戏结束判定:

蛇头出现的下一个位置如果为墙面或自己的身体,游戏结束。

bool gameover(int x,int y){
if (map[x][y] == 'W' || map[x][y] == 'B')
return ;
else
return ;
}

蛇头出现的下一个位置如果有食物,蛇长加1。

bool eat(int x,int y){
if (map[x][y] == 'f'){
food();
return ;
}
else{
return ;
}
}

进食后,new一个新身体:

if (eat(head.x, head.y)){
snake *newBody = new snake;
newBody->x = tail.x; newBody->y = tail.y; newBody->next = tail.next;
tail.x = tailX, tail.y = tailY;
tail.next = newBody;
map[tailX][tailY] = 'B';
snakeLength++;
}
map[head.x][head.y] = 'H';

显示:

循环执行蛇的移动过程,并清屏打印地图。

完整代码:https://github.com/shuiguai/games/blob/master/snake.cpp

环境:win64,VS2010

最新文章

  1. java Io文件输入输出流 复制文件
  2. [NHibernate]查看NHibernate生成的SQL语句
  3. C# onverride、abstract、vitrtual、new、sealed
  4. ArcGIS Server开发实践之【Search Widget工具查询本地地图服务】
  5. 随机数组&amp;大数相加
  6. LightOj1137 - Expanding Rods(二分+数学)
  7. 纵观minecraft 游戏作者的世界观
  8. 手势识别官方教程(4)在挑划或拖动手势后view的滚动用ScrollView和 HorizontalScrollView,自定义用Scroller或OverScroller
  9. BZOJ 1640: [Usaco2007 Nov]Best Cow Line 队列变换
  10. 转:Javascript的10个设计缺陷
  11. docker 报错:x509: certificate has expired or is not yet valid
  12. 【Python】excel读写操作 xlrd &amp; xlwt
  13. 《AutoCAD Civil 3D .NET二次开发》勘误1
  14. SVN 问题:None of the environment variables SVN_EDITOR, VISUAL or EDITOR are set, and no &#39;editor-cmd&#39; run-time
  15. 创建表结构的sql语句
  16. Go语言学习笔记(三) [控制结构、内建函数]
  17. ExtJS 4.2 教程-06:服务器代理(proxy)
  18. iOS中Date和NString的相互转换
  19. benthos 通过rest api 配置 stream 说明
  20. Spring系列之——springboot解析resources.application.properties文件

热门文章

  1. AVPython:Python Support for ArcView
  2. vue子路由设置、全局组件、局部组件的原生写法
  3. ETL工具-KETTLE教程专栏1----术语和定义
  4. Spring——顾问封装通知
  5. CSS 中蒙版相关设置二三事
  6. Codeforces 385C Bear and Prime Numbers(素数预处理)
  7. js 获取地址栏信息,可以传递多个参数
  8. HDX Insight Installation &amp; Configuration
  9. [Java]算术表达式组建二叉树,再由二叉树得到算式的后序和中序表达式
  10. 技术选型之Docker容器引擎