/*********************************************************
********************贪吃蛇(难道可选)********************
**************制作者:Xu Lizi      日期:2012/12/31********
********************部分函数有借鉴************************
**********************************************************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
int snakey[100]={5,4,3,2,1};     /*定义蛇的横坐标*/
int snakex[100]={1,1,1,1,1};     /*定义蛇的纵坐标,蛇头起始位置为(5,1)*/
int life=0;    /*定义蛇的生命,0表示存活,1表示死亡*/
int lenght=5;      /*定义蛇的长度,初始为5节*/
char map[12][24]={"***********************",   /*y*/
"*                     *",
"*                     *",
"*                     *",
"*                     *",
"*                     *",
"*                     *",
"*                     *",
"*                     *",
"*                     *",
"*                     *",
/*x*/    "***********************"};
void put_money(int i,int j)       /*放钱函数,使用随机数,随机出现食物*/
{
int x=0,y=0;
srand(time(NULL));
while ( (map[y][x]==003) || (map[y][x]==002) || (map[y][x]=='*') || ((x==i)&&(y==j)) )
{
x=rand()%21+1;
y=rand()%10+1;
}
map[y][x]='$';
return;
}
void output()        /*输出*/
{
system("cls");
int i,j;
for(i=0; i<12; i++)
{
for(j=0; j<23; j++) printf("%c", map[i][j]);
printf("\n");
}
return;
}
void gameover()        /*游戏结束*/
{
life=1;
printf("笨蛋,输了吧!!!\n");
return;
}
void turn_up()       /*向上移动*/
{
system("cls");
int i;
if ( (snakex[0]==1) || (map[snakex[0]-1][snakey[0]]==003) ) gameover(); else {
if (map[snakex[0]-1][snakey[0]]=='$')
{
put_money( snakey[0], snakex[0]-1 );
lenght++;
map[snakex[lenght-1]][snakey[lenght-1]]=003;
}
for(i=lenght; i>0; i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
map[snakex[lenght]][snakey[lenght]]=' ';
snakex[0]--;
for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003;
map[snakex[0]][snakey[0]]=002;
output();
}
return;
}
void turn_down()         /*向下*/
{
system("cls");
int i;
if ( (snakex[0]==10) || (map[snakex[0]+1][snakey[0]]==003) ) gameover();else {
if (map[snakex[0]+1][snakey[0]]=='$')
{
put_money(snakey[0],snakex[0]+1);
lenght++;
map[snakex[lenght-1]][snakey[lenght-1]]=003;
}
for(i=lenght; i>0; i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
snakex[0]++;
map[snakex[lenght]][snakey[lenght]]=' ';
for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003;
map[snakex[0]][snakey[0]]=002;
output();
}
return;
}
void turn_left()     /*向左*/
{
system("cls");
int i;
if ( (snakey[0]==1) || (map[snakex[0]][snakey[0]-1]==003) ) gameover();else {
if (map[snakex[0]][snakey[0]-1]=='$')
{
put_money(snakey[0]-1,snakex[0]);
lenght++;
map[snakex[lenght-1]][snakey[lenght-1]]=003;
}
for(i=lenght; i>0; i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
map[snakex[lenght]][snakey[lenght]]=' ';
snakey[0]--;
for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003;
map[snakex[0]][snakey[0]]=002;
output();
}
return;
}
void turn_right()        /*向右*/
{
system("cls");
int i;
if ( (snakey[0]==21) || (map[snakex[0]][snakey[0]+1]==003) ) gameover();else {
if (map[snakex[0]][snakey[0]+1]=='$')
{
put_money(snakey[0]+1,snakex[0]);
lenght++;
map[snakex[lenght-1]][snakey[lenght-1]]=003;
}
for(i=lenght; i>0; i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
map[snakex[lenght]][snakey[lenght]]=' ';
snakey[0]++;
for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003;
map[snakex[0]][snakey[0]]=002;
output();
}
return;
}
int main()
{
int i,timeover,hard;
long start;
char name , direcation;
printf("\n    向上移动:W  ;向下移动:S ; 向左移动:A ; 向右移动:D \n");
printf("\t请选择难度(数字)\n\t分1~5级,分别代表\n\t1难,2中上,3中,4中下5,易:\n");
scanf("%d",&hard);
system("cls");
for(i=1;i<5;i++) map[1][i]=003;      /*输出蛇身*/
map[1][5]=002;      /*输出蛇头*/
put_money(0,0);
output();
while(life!=1)    /*当蛇死亡时结束循环*/
{
/*让蛇自动运行的函数******有借鉴*/
timeover=1;
start=clock();
while((timeover=(clock()-start<=hard*100))&&!kbhit());        //难度设定
if(timeover)
{
direcation=getch();
}
/*让蛇自动运行的函数******有借鉴*/
switch(direcation)
{
case 'w':turn_up();break;
case 's':turn_down();break;
case 'a':turn_left();break;
case 'd':turn_right();break;
}
}
return 0;
}

用C语言编写贪吃蛇代码(难度可选)相关推荐

  1. c语言编写贪吃蛇代码无错,刚学C语言,想写一个贪吃蛇的代码

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include #include #include typedef struct snake { int a; int b; stru ...

  2. c语言安卓贪吃蛇代码下载,C语言贪吃蛇代码

    c语言编写贪吃蛇源代码,简单易懂,文件为VC源代码.如果你正在学习c语言,就来下载吧.很经典的 C语言贪吃蛇代码部分 #include #include #include#include #defin ...

  3. c语言字符蛇代码,C语言实现贪吃蛇代码

    本文实例为大家分享了C语言实现贪吃蛇代码的具体代码,供大家参考,具体内容如下 #include"stdafx.h" #include #include #include #incl ...

  4. C语言实现贪吃蛇代码

    C语言实现贪吃蛇,有点难写啊. 我是用VS写的 代码里有注释 我就不多说了 大家也可以直接去下载我的可执行文件 #include<stdio.h> #include<time.h&g ...

  5. 用C语言编写贪吃蛇项目描述,刚学C语言,想写一个贪吃蛇的代码

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include #include #include typedef struct snake { int a; int b; stru ...

  6. c语言编写贪吃蛇难点解析,刚学C语言,想写一个贪吃蛇的代码

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include #include #include typedef struct snake { int a; int b; stru ...

  7. 如何用c 语言编写贪吃蛇,刚学C语言,想写一个贪吃蛇的代码

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include #include #include typedef struct snake { int a; int b; stru ...

  8. c语言字符蛇代码,贪吃蛇游戏c语言源贪吃蛇代码代码学习

    1########蛇的状态,U:上:D:下:L:左R:右蛇身的一个节点12{ 13intx; 14inty; 15structSNAKE*next; 16}snake; 1718//全局变量// 19 ...

  9. 贪吃蛇的c语言程序码,C语言贪吃蛇代码下载_C语言贪吃蛇代码官方下载-太平洋下载中心...

    C语言编写贪吃蛇源代码,简单易懂,文件为VC源代码.如果你正在学习c语言,就来下载吧.很经典的. C语言贪吃蛇代码原理: 产生一个固定大小没有边界的游戏区域,蛇从区域的中心开始,由玩家通过键盘控制蛇的 ...

最新文章

  1. 如何解决CentOS下使用yum安装python-pip失败
  2. 给wmnp升级php和nginx的方法
  3. Mozilla Labs Apps Developer Preview发布了
  4. Java 安装后的检测是否安装成功
  5. Spring(2)——Spring IoC 详解
  6. OpenCASCADE:绘制测试线束之基本命令
  7. android函数未定义,android – 方法findViewById(int)未定义
  8. 你这么爱听歌,一定活得很难过吧 | 今日最佳
  9. 组合数学 —— 卡特兰数列(Catalan)
  10. 956. 最高的广告牌
  11. python 朋友圈leetcode_利特代码0547。朋友圈[python],LeetCode0547FriendCircles,Python
  12. 在线HTML实体转字符串工具
  13. 时机论:早起的鸟儿也要选对“用户”季节
  14. cobbler报错:No such command: --get-loaders解决方案
  15. 【转】高性能服务器架构(High-Performance Server Architecture)
  16. selenium获取文本的几种方法小结(获取源码)
  17. VOD紧急抱团是利是弊?
  18. 用 Webgoat 撬动地球,看安全测试的引路石!
  19. 快播将关闭QVOD服务器 宅男,你心碎了吗?
  20. 怎么用思维导图做会议纪要?MindNow来教你

热门文章

  1. 手动从注册表中删除服务项
  2. Vue前台两级下拉栏分类内容
  3. MT2503芯片处理器平台简介
  4. 51单片机实现简易计算器
  5. Tomcat tomcatlocalhost 中文乱码
  6. stm8s103k3 周期 捕获_STM8S做输入捕获
  7. android安卓手机怎么修改自己的ip地址
  8. 视壮科技:VS-RK3399简单的I2C 功能介绍
  9. c语言中5l是数值或字符常量,C语言中什么叫做正确的数值或字符常量?
  10. 指纹解锁亮屏时间 分析