大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang

/*贪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
char qipan[20][80];
int snake = 1;
int change(int *head, int *tail, int *score, char *direction, int zuobiao[2][80])
{
int x, y;
switch(*direction)
{
case 72:
x = zuobiao[0][*head]-1;
y = zuobiao[1][*head];
break;
case 87:
x = zuobiao[0][*head]-1;
y = zuobiao[1][*head];
break;
case 80:
x = zuobiao[0][*head]+1;
y = zuobiao[1][*head];
break;
case 83:
x = zuobiao[0][*head]+1;
y = zuobiao[1][*head];
break;
case 75:
x = zuobiao[0][*head];
y = zuobiao[1][*head]-1;
break;
case 65:
x = zuobiao[0][*head];
y = zuobiao[1][*head]-1;
break;
case 68:
x = zuobiao[0][*head];
y = zuobiao[1][*head]+1;
break;
default:
x = zuobiao[0][*head];
y = zuobiao[1][*head]+1;
}
if(qipan[x][y] == '_' || qipan[x][y] == '|' || qipan[x][y] == '*')
return 1;
if(qipan[x][y] == ' ')
{
qipan[zuobiao[0][*tail]][zuobiao[1][*tail]]=' ';
*tail=(*tail+1)%80;
qipan[zuobiao[0][*head]][zuobiao[1][*head]]='*';
*head=(*head+1)%80;
zuobiao[0][*head]=x;
zuobiao[1][*head]=y;
qipan[x][y]='#';
return 0;
}
if(qipan[x][y] == '@')
{
qipan[zuobiao[0][*head]][zuobiao[1][*head]]='*';
*head=(*head+1)%80;
zuobiao[0][*head]=x;
zuobiao[1][*head]=y;
qipan[x][y]='#';
*score += 1;
return 0;
}
}
void gotoxy(int x,int y)//位置函数
{
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void show_start()
{
gotoxy(20,0);
color(1);
int u;
for(u = 0; u < 18; u ++)
printf("* ");
gotoxy(20, 12);
for(u = 0; u < 18; u ++)
printf("* ");
gotoxy(28,2);
color(13);
printf("S N A K E  G A M E");
gotoxy(31, 5);
color(14);
printf("#");
gotoxy(34, 5);
color(11);
printf("One  snake");
gotoxy(34, 7);
printf("Two  snakes");
gotoxy(28,9);
printf("The speed you want: ");
gotoxy(31, 5);
char temp = 'a';
color(14);
while(1)
{
while(!kbhit());
temp = getch();
if(temp == 72 || temp == 80)
{
gotoxy(31,5+2*(snake-1));
printf(" ");
snake = snake % 2 + 1;
gotoxy(31,5+2*(snake-1));
printf("#");
gotoxy(31,5+2*(snake-1));
}
if(temp == 13)
break;
}
gotoxy(27,10);
color(10);
printf("GOOD  LUCK  TO  YOU  !");
}
int main()
{
srand(time(0));
int head1 = 3, tail1 = 0, score1 = 0;
int head2 = 3, tail2 = 0, score2 = 0;
int zuobiao1[2][80];
int zuobiao2[2][80];
/*棋盘 20×80 初始化*/
for(int i = 0; i < 20; i++)
{
for(int j = 0; j < 80; j++)
{
qipan[i][j] = ' ';
}
}
for(int i = 0; i < 20; i++)
{
qipan[i][0] = qipan[i][79] = '|';//第一列、最后一列是墙20*1
}
for(int i = 0; i < 80; i++)
{
qipan[0][i] = qipan[19][i] = '_';//第一行、最后一行是墙1*80
}
/*蛇的坐标 2×8 初始化, 将蛇放到棋盘上*/
int x1 = 1, x2 = 18, y, temp = 0;
for(int m = tail1; m < head1; m++)
{
zuobiao1[0][m] = x1;//初始行号
y = zuobiao1[1][m] = ++temp;//初始列号
qipan[x1][y] = '*';
}
zuobiao1[0][head1] = x1;//初始行号
y = zuobiao1[1][head1] = ++temp;//初始列号
qipan[x1][y] = '#';//蛇头
show_start();
if(snake == 2)
{
temp = 0;
for(int m = tail2; m < head2; m++)
{
zuobiao2[0][m] = x2;//初始行号
y = zuobiao2[1][m] = ++temp;//初始列号
qipan[x2][y] = '*';
}
zuobiao2[0][head2] = x2;//初始行号
y = zuobiao2[1][head2] = ++temp;//初始列号
qipan[x2][y] = '#';//蛇头
}
clock_t start;
int timeover;
char direction1 = 77;//72 88 75 77
char direction2 = 68;//87 83 65 68
char new_direction;
char new_direction1 ;
char new_direction2 ;
int gamespeed;
gotoxy(48, 9);
color(14);
scanf("%d", &gamespeed);
int rand_i = rand()%18 + 1;
int rand_j = rand()%78 + 1;
qipan[rand_i][rand_j] = '@';
system("cls");
gotoxy(10, 1);
color(10);
printf("Your present score: ");
gotoxy(30, 1);
color(11);
printf("%d", score1);
gotoxy(0,4);
for(int i = 0; i < 20; i++)//打印出棋盘
for(int j = 0; j < 80; j++)
{
if(qipan[i][j] == '*' || qipan[i][j] == '#')
color(13);
else if(qipan[i][j] == '@')
color(12);
else
color(15);
printf("%c", qipan[i][j]);
}
while(1)//87 83 65 68
{
for(int i = 0; i < 20; i++)//打印出棋盘
for(int j = 0; j < 80; j++)
{
if(qipan[i][j] == '*' || qipan[i][j] == '#')
{
gotoxy(j, i+4);
color(13);
}
if(qipan[i][j] == '@')
{
gotoxy(j, i+4);
color(12);
}
if(qipan[i][j] == '_' || qipan[i][j] == '|')
{
gotoxy(j, i+4);
color(15);
}
printf("%c", qipan[i][j]);
}
for(int q = 0; q < 3; q++)
{
gotoxy(rand()%78 + 1, rand()%18 + 5 );
color(14);
printf("+");
}
start = clock();
timeover = 1;
while(!kbhit() && (timeover = clock() - start/CLOCKS_PER_SEC <= gamespeed));
if(timeover)//有按键
{
if(snake == 1)
getch();
new_direction = getch();
if(snake == 2)
{
if(new_direction == 87 || new_direction ==83 ||new_direction == 65||new_direction == 68)
{
new_direction2 = new_direction;
new_direction1 = direction1;
if(new_direction2 + direction2 == 170 || new_direction2 + direction2 == 133)
{
change(&head1, &tail1, &score1, &direction1, zuobiao1);
change(&head2, &tail2, &score2, &direction2, zuobiao2);
continue;
}
}
if(new_direction == 72 || new_direction ==80 ||new_direction == 75||new_direction == 77)
{
new_direction1 = new_direction;
new_direction2 = direction2;
if(new_direction1 + direction1 == 152)
{
change(&head1, &tail1, &score1, &direction1, zuobiao1);
change(&head2, &tail2, &score2, &direction2, zuobiao2);
continue;
}
}
}
if(snake == 1 && (new_direction == 72 || new_direction ==80 ||new_direction == 75||new_direction == 77))
{
new_direction1 = new_direction;
if(new_direction1 + direction1 == 152)
{
change(&head1, &tail1, &score1, &direction1, zuobiao1);
continue;
}
}
}
else
{
if(snake == 2)
{
new_direction2 = direction2;
new_direction1 = direction1;
}
else
{
new_direction1 = direction1;
}
}
if(snake == 1)
{
direction1 = new_direction1;
if(change(&head1, &tail1, &score1, &direction1, zuobiao1) == 1)
{
system("cls");
gotoxy(30,10);
color(7);
printf("G A M E  O V E R !");
gotoxy(0,20);
color(1);
return 0;
}
}
if(snake == 2)
{
direction1 = new_direction1;
if(change(&head1, &tail1, &score1, &direction1, zuobiao1) == 1)
{
system("cls");
gotoxy(30,8);
color(7);
printf("G A M E  O V E R !");
gotoxy(30,10);
printf("Snake Two is the Hero!");
gotoxy(0,20);
color(1);
return 0;
}
direction2 = new_direction2;
if(change(&head2, &tail2, &score2, &direction2, zuobiao2) == 1)
{
system("cls");
gotoxy(30,8);
color(7);
printf("G A M E  O V E R !");
gotoxy(30,10);
printf("Snake One is the Hero!");
gotoxy(0,20);
color(1);
return 0;
}
}
int randnew = 0;
for(int i = 0; i < 20; i++)//打印出棋盘
for(int j = 0; j < 80; j++)
if(qipan[i][j] == '@')
{
randnew ++;
}
if(randnew == 0)
{
rand_i = rand()%18 + 1;
rand_j = rand()%78 + 1;
qipan[rand_i][rand_j] = '@';
}
if(snake == 2 && randnew == 1)
{
rand_i = rand()%18 + 1;
rand_j = rand()%78 + 1;
qipan[rand_i][rand_j] = '@';
}
gotoxy(30, 1);
printf("%d", score1);
if(snake == 2)
{
gotoxy(30,2);
printf("%d", score2);
}
}
}

转载于:https://www.cnblogs.com/xiaoyajiang/p/5950310.html

双人贪吃蛇小游戏C++原创相关推荐

  1. 单双人贪吃蛇小游戏(控制台)

    本代码参考另一位博主的贪吃蛇的思想.单双人贪吃蛇模式. 编译已通过的平台:VS2019. 如有问题,请伙伴们一起讨论哟. 1. 程序界面功能以及运行截图如下: (1)程序主界面运行截图: (2)单人游 ...

  2. Python实现贪吃蛇小游戏(双人模式)

    这篇文章主要为大家详细介绍了Python实现双人模式的贪吃蛇小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 简单用py写了一个贪吃蛇游戏,有单人.双人模式,比较简 ...

  3. Python贪吃蛇小游戏_完整源码免费分享

    文章目录 Python 贪吃蛇小游戏 1. 导包 2. 配置初始化参数 3. 主函数及运行主体 4. 画食物的函数 5. 画贪吃蛇的函数 6. 画网格的函数(非必选,觉得多余的可以忽略此项) 7. 操 ...

  4. 原生JavaScript抒写——贪吃蛇小游戏

    原生JavaScript抒写--贪吃蛇小游戏 文章目录 原生JavaScript抒写--贪吃蛇小游戏 前言 一.需求分析 二.效果展示 三.具体逻辑代码分析 1.首先创建一个html文件,然后我们利用 ...

  5. 100行代码,使用 Pygame 制作一个贪吃蛇小游戏!

    作者 | 周萝卜 来源 | 萝卜大杂烩 相信我们大家都玩过贪吃蛇游戏,今天我们就从头一起来写一个贪吃蛇小游戏,只需要100多行的代码就完成了. 用到的 Pygame 函数 贪吃蛇小游戏用到的函数 功能 ...

  6. GUI编程---贪吃蛇小游戏开发

    学习链接:狂神说Java–1小时开发贪吃蛇小游戏 ①初识理论 帧:时间片足够小=>就是动画,1秒30帧.连起来是动画,拆开就是静态的图片! 键盘监听 定时器Timer 游戏图片素材:GUI之贪吃 ...

  7. python 贪吃蛇小游戏代码_10分钟再用Python编写贪吃蛇小游戏

    Python编写贪吃蛇 前不久我们公众号发布了一篇C++编写贪吃蛇小游戏的推文,反响空前.看来大家对这类简单易上手小游戏还是很喜爱的. 恰逢2018年IEEE Spectrum编程语言排行榜新鲜出炉, ...

  8. python100行代码程序-100行python代码,轻松完成贪吃蛇小游戏

    大家小时候都玩过贪吃蛇吧?小编小时候可喜欢拿爸妈的手机玩了,厉害着呢!今天,小编就来用100行代码实现一个简易版的贪吃蛇.在网上,贪吃蛇教程蛮多的,但要安装蛮多库的,而且也不够清晰,今天的代码比较短, ...

  9. python小游戏编程实例-10分钟教你用Python写一个贪吃蛇小游戏,适合练手项目

    另外要注意:光理论是不够的.这里顺便总大家一套2020最新python入门到高级项目实战视频教程,可以去小编的Python交流.裙 :七衣衣九七七巴而五(数字的谐音)转换下可以找到了,还可以跟老司机交 ...

  10. python100行代码-怎样写贪吃蛇小游戏?用100行python代码轻松解决!

    大家小时候都玩过贪吃蛇吧?小编小时候可喜欢拿爸妈的手机玩了,厉害着呢!今天,小编就来用100行代码实现一个简易版的贪吃蛇.在网上,贪吃蛇教程蛮多的,但要安装蛮多库的,而且也不够清晰,今天的代码比较短, ...

最新文章

  1. AI一分钟 | 阿里与南洋理工成立AI联合研究院;传蔚来汽车拟赴美IPO,融资20亿美元
  2. Linux网络服务基础
  3. 配置 influxDB 鉴权及 HTTP API 写数据的方法
  4. Diagram of Spring 3.0 module dependencies--转载
  5. 生吃蔬菜健康轻松瘦身 - 健康程序员,至尚生活!
  6. mods文件夹怎么创建_TCL电视怎么安装蜜蜂视频通用教程
  7. php debug_print_backtrace,php中debug_backtrace、debug_print_backtrace和匿名函数用法实例
  8. C++ Under the Hood
  9. mysql事务的acid、三种并发问题与四种隔离级别
  10. 绝大部分博士目前都无法进入大学
  11. 华为专利全球第一:哪里跌倒,哪里爬起!
  12. MYSQL连接出现Auth_连接MySQL数据库出现时Authentication plugin 'caching_sha2_password' cannot be loaded的解决办法...
  13. 基于java SpingBoot框架的企业办公管理系统
  14. 订阅消息 data.thing1.value is emtpy
  15. call方法 java_漫谈JS中的call和apply方法
  16. utf8字符集中漢字默認占三個字節
  17. cont在c语言用法,在S7-1500中指令TSEND_C and TRCV_C如何使用?
  18. 超市商品管理系统java
  19. [‘1‘,‘2‘,‘3‘].map(parseInt)结果讲解
  20. 使用Python将mat文件转换为npy文件

热门文章

  1. 有趣的计算机黑科技,7个让你欲罢不能的电脑黑科技神器!每一款都让人爱不释手...
  2. 光影精灵4黑苹果_台式机技嘉主板黑苹果EFI引导文件分享amp;2020.12.4
  3. 2022秋招面经(C++软开)
  4. 软考—软件设计师(中级)第5版
  5. proteus三输入与非门名字_proteus常用元件中英文对照表
  6. java后台管理项目策划书_12款适合做Java后台管理系统的项目
  7. linux办公软件wps字体,linux版wps-office安装缺少的字体
  8. 微信公众号+web后台的工资条发放功能的实现
  9. 大数据之路之交通大数据应用总体架构设计
  10. 华为虚拟机eNSP命令大全