贪吃蛇游戏代码

#include<stdio.h>
  2 #include<time.h>
  3 #include<windows.h>
  4 #include<stdlib.h>
  5 
  6 #define U 1
  7 #define D 2
  8 #define L 3 
  9 #define R 4       //蛇的状态,U:上 ;D:下;L:左 R:右
 10 
 11 typedef struct SNAKE //蛇身的一个节点
 12 {
   
 13     int x;
 14     int y;
 15     struct SNAKE *next;
 16 }snake;
 17 
 18 //全局变量//
 19 int score = 0, add = 10;//总得分与每次吃食物得分。
 20 int status, sleeptime = 200;//每次运行的时间间隔
 21 snake *head, *food;//蛇头指针,食物指针
 22 snake *q;//遍历蛇的时候用到的指针
 23 int endGamestatus = 0; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。
 24 
 25 //声明全部函数//
 26 void Pos();
 27 void creatMap();
 28 void initSnake();
 29 int biteSelf();
 30 void createFood();
 31 void cantCrossWall();
 32 void snakeMove();
 33 void pause();
 34 void runGame();
 35 void initGame();
 36 void endGame();
 37 void gameStart();
 38 
 39 void Pos(int x, int y)//设置光标位置
 40 {
   
 41     COORD pos;
 42     HANDLE hOutput;
 43     pos.X = x;
 44     pos.Y = y;
 45     hOutput = GetStdHandle(STD_OUTPUT_HANDLE);//返回标准的输入、输出或错误的设备的句柄,也就是获得输入、输出/错误的屏幕缓冲区的句柄
 46     SetConsoleCursorPosition(hOutput, pos);
 47 }
 48 
 49 void creatMap()//创建地图
 50 {
   
 51     int i;
 52     for (i = 0; i<58; i += 2)//打印上下边框
 53     {
   
 54         Pos(i, 0);
 55         printf("■");//一个方块占两个位置
 56         Pos(i, 26);
 57         printf("■");
 58     }
 59     for (i = 1; i<26; i++)//打印左右边框
 60     {
   
 61         Pos(0, i);
 62         printf("■");
 63         Pos(56, i);
 64         printf("■");
 65     }
 66 }
 67 
 68 void initSnake()//初始化蛇身
 69 {
   
 70     snake *tail;
 71     int i;
 72     tail = (snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//
 73     tail->x = 24;
 74     tail->y = 5;
 75     tail->next = NULL;
 76     for (i = 1; i <= 4; i++)//初始长度为4
 77     {
   
 78         head = (snake*)malloc(sizeof(snake));
 79         head->next = tail;
 80         head->x = 24 + 2 * i;
 81         head->y = 5;
 82         tail = head;
 83     }
 84     while (tail != NULL)//从头到为,输出蛇身
 85     {
   
 86         Pos(tail->x, tail->y);
 87         printf("■");
 88         tail = tail->next;
 89     }
 90 }
 91 //??
 92 int biteSelf()//判断是否咬到了自己
 93 {
   
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X W F

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值