贪吃蛇游戏(printf输出C语言版本)

您所在的位置:网站首页 c语言贪吃蛇怎么写 贪吃蛇游戏(printf输出C语言版本)

贪吃蛇游戏(printf输出C语言版本)

2024-07-09 22:49| 来源: 网络整理| 查看: 265

这一次我们应用printf输出实现一个经典的小游戏—贪吃蛇,主要难点是小蛇数据如何存储、如何实现转弯的效果、吃到食物后如何增加长度。

1 构造小蛇

首先,在画面中显示一条静止的小蛇。二维数组canvas[High][Width]的对应元素,值为0输出空格,-1输出边框#,1输出蛇头@,大于1的正数输出蛇身*。startup()函数中初始化蛇头在画布中间位置(canvas[High/2][Width/2] = 1;),蛇头向左依次生成4个蛇身(for (i=1;i1) 60 printf("*"); // 输出蛇身* 61 } 62 printf("\n"); 63 } 64 } 65 66 void updateWithoutInput() // 与用户输入无关的更新 67 { 68 } 69 70 void updateWithInput() // 与用户输入有关的更新 71 { 72 } 73 74 int main() 75 { 76 startup(); // 数据初始化 77 while (1) // 游戏循环执行 78 { 79 show(); // 显示画面 80 updateWithoutInput(); // 与用户输入无关的更新 81 updateWithInput(); // 与用户输入有关的更新 82 } 83 return 0; 84 } 2 小蛇自动移动

实现小蛇的移动是贪吃蛇游戏的难点,下图列出了小蛇分别向右、向上运动后,对应二维数组元素值的变化,从中我们可以得出实现思路。

 

假设小蛇元素为54321,其中1为蛇头、5432为蛇身、最大值5为蛇尾。首先将所有大于0的元素加1,得到65432;将最大值6变为0,即去除了原来的蛇尾;再根据对应的移动方向,将2对应方向的元素由0变成1;如此即实现了小蛇的移动。小蛇向上移动的对应流程如图所示。

 

定义变量int moveDirection表示小蛇的移动方向,值1、2、3、4分别表示小蛇向上、下、左、右方向移动,小蛇移动实现在moveSnakeByDirection()函数中。

1 #include 2 #include 3 #include 4 #include 5 //C语言自学网 6 #define High 20 // 游戏画面尺寸 7 #define Width 30 8 9 // 全局变量 10 int moveDirection; // 小蛇移动方向,上下左右分别用1,2,3,4表示 11 int canvas[High][Width] = {0}; // 二维数组存储游戏画布中对应的元素 12 // 0为空格0,-1为边框#,1为蛇头@,大于1的正数为蛇身* 13 14 void gotoxy(int x,int y) //光标移动到(x,y)位置 15 { 16 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); 17 COORD pos; 18 pos.X = x; 19 pos.Y = y; 20 SetConsoleCursorPosition(handle,pos); 21 } 22 23 // 移动小蛇 24 // 第一步扫描数组canvas所有元素,找到正数元素都+1 25 // 找到最大元素(即蛇尾巴),把其变为0 26 // 找到=2的元素(即蛇头),再根据输出的上下左右方向,把对应的另一个像素值设为1(新蛇头) 27 void moveSnakeByDirection() 28 { 29 int i,j; 30 for (i=1;i1) 108 printf("*"); // 输出蛇身* 109 } 110 printf("\n"); 111 } 112 Sleep(100); 113 } 114 115 void updateWithoutInput() // 与用户输入无关的更新 116 { 117 moveSnakeByDirection(); 118 } 119 120 void updateWithInput() // 与用户输入有关的更新 121 { 122 } 123 124 int main() 125 { 126 startup(); // 数据初始化 127 while (1) // 游戏循环执行 128 { 129 show(); // 显示画面 130 updateWithoutInput(); // 与用户输入无关的更新 131 updateWithInput(); // 与用户输入有关的更新 132 } 133 return 0; 134 } 3 玩家控制小蛇移动

这一步的实现比较简单,在updateWithInput()函数中按asdw键改变moveDirection的值,然后调用moveSnakeByDirection()实现小蛇向不同方向的移动,如图所示。

 

1 void updateWithInput() // 与用户输入有关的更新 2 //C语言自学网 3 { 4 char input; 5 if(kbhit()) // 判断是否有输入 6 { 7 input = getch(); // 根据用户的不同输入来移动,不必输入回车 8 if (input == 'a') 9 { 10 moveDirection = 3; // 位置左移 11 moveSnakeByDirection(); 12 } 13 else if (input == 'd') 14 { 15 moveDirection = 4; // 位置右移 16 moveSnakeByDirection(); 17 } 18 else if (input == 'w') 19 { 20 moveDirection = 1; // 位置上移 21 moveSnakeByDirection(); 22 } 23 else if (input == 's') 24 { 25 moveDirection = 2; // 位置下移 26 moveSnakeByDirection(); 27 } 28 } 29 } 4 判断游戏失败

当小蛇和边框或自身发生碰撞时,游戏失败,如图所示。

1 void moveSnakeByDirection() 2 //C语言自学网 3 { 4 int i,j; 5 for (i=1;i1) 150 printf("*"); // 输出蛇身* 151 else if (canvas[i][j]==-2) 152 printf("F"); // 输出食物F 153 } 154 printf("\n"); 155 } 156 Sleep(100); 157 } 158 159 void updateWithoutInput() // 与用户输入无关的更新 160 { 161 moveSnakeByDirection(); 162 } 163 164 void updateWithInput() // 与用户输入有关的更新 165 { 166 char input; 167 if(kbhit()) // 判断是否有输入 168 { 169 input = getch(); // 根据用户的不同输入来移动,不必输入回车 170 if (input == 'a') 171 { 172 moveDirection = 3; // 位置左移 173 moveSnakeByDirection(); 174 } 175 else if (input == 'd') 176 { 177 moveDirection = 4; // 位置右移 178 moveSnakeByDirection(); 179 } 180 else if (input == 'w') 181 { 182 moveDirection = 1; // 位置上移 183 moveSnakeByDirection(); 184 } 185 else if (input == 's') 186 { 187 moveDirection = 2; // 位置下移 188 moveSnakeByDirection(); 189 } 190 } 191 } 192 193 int main() 194 { 195 startup(); // 数据初始化 196 while (1) // 游戏循环执行 197 { 198 show(); // 显示画面 199 updateWithoutInput(); // 与用户输入无关的更新 200 updateWithInput(); // 与用户输入有关的更新 201 } 202 return 0; 203 } 6 思考题

1. 增加道具,吃完可以加命或减速;

2. 尝试实现双人版贪吃蛇

感谢你的阅读,请用心感悟!希望可以帮到爱学习的你!!分享也是一种快乐!!!请接力。。。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3