贪吃蛇C语言

合集下载

c语言贪吃蛇代码的算法模块

c语言贪吃蛇代码的算法模块

c语言贪吃蛇代码的算法模块以下是一个简单的 C 语言贪吃蛇代码的算法模块:1. 食物生成:在游戏区域的随机位置生成食物。

```cint x = rand() % SCREEN_WIDTH;int y = rand() % SCREEN_HEIGHT;food.x = x;food.y = y;```2. 蛇的移动:根据蛇的当前方向移动蛇头。

```cswitch (snake.direction) {case UP:snake.y--;break;case DOWN:snake.y++;break;case LEFT:snake.x--;break;case RIGHT:snake.x++;break;}```3. 检查游戏结束条件:检查蛇是否吃到自己或撞到边界。

```cif (snake.x == food.x && snake.y == food.y) {// 吃到食物,蛇身增长addNode(&snake);generateFood(&food);} else {if (snake.x < 0 || snake.x >= SCREEN_WIDTH || snake.y < 0 || snake.y >= SCREEN_HEIGHT) {// 撞墙,游戏结束gameOver = true;} else {removeNode(&snake);}}```4. 控制蛇的移动速度:根据游戏难度控制蛇的移动速度。

```cif (difficulty == EASY) {// 较慢的速度sleep(100);} else if (difficulty == NORMAL) {// 正常的速度sleep(75);} else {// 较快的速度sleep(50);}```以上是一个简单的贪吃蛇游戏算法的核心部分。

你可以根据实际需求对代码进行修改和扩展,以实现更完整的贪吃蛇游戏功能。

贪吃蛇(C语言知识学习)

贪吃蛇(C语言知识学习)

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <windows.h>//蛇头移动方向#define UP 1#define DOWN 2#define LEFT 3#define RIGHT 4//死亡判定(怼墙或怼自己)#define KISSASS 1#define KISSWALL 2//坐标转化#define POINT(x,y) ((y)*80+(x)+1) //将(x,y)坐标转化为一个int类数值#define GETX(x) (((x)-1)%80)//将int类数值提取出原先的x#define GETY(y) (((y)-1)/80)//同理,提取出yHANDLE Console;void Position(int x, int y); //移动光标到(x,y)//Windows.hvoid DrawMap();//画墙void ShowText(char *text);//根据字符串的长短智能打印出包围字符串的笑脸void ResetSnake();//初始化贪吃蛇int RefreshSnake();//更新蛇的移动void CreatFood();//生成食物void Draw();//画出蛇身和食物void RefreshScreen();//屏幕刷新void GameOver(int Type);//游戏结束条件int Gaming();//代码跑起来char play = 0; ////值为1则继续游戏,值为0游戏退出char pause = 0; //值为1则暂停,值为0游戏继续char direction; //蛇头方向int snake[500]; //snake[0]为蛇头int body; //蛇身体长度int newbody;//吃完食物新长的蛇身int addHead,deleteT ail;//增加蛇头擦去蛇尾,使贪吃蛇动起来int food = 0; //食物void main(){CONSOLE_CURSOR_INFO CurrInfo = { sizeof(CONSOLE_CURSOR_INFO), 0 };Console = GetStdHandle(STD_OUTPUT_HANDLE);DrawMap();ShowText("Copyright reserve by 张博元");while(Gaming()){if (play)DrawMap();};return 0;}void Position(int x, int y) //移动光标到(x,y)//Windows.h{COORD coors = { x, y };SetConsoleCursorPosition(Console, coors);}void DrawMap() //画墙{int i;for (i = 3; i < 70; i = i + 12){Position(i, 0);printf("===我是墙===");}for (i = 0; i < 25; i = i + 1){Position(0, i);printf("|| ");}for (i = 3; i < 70; i = i + 12){Position(i, 24);printf("============");}for (i = 0; i < 25; i = i + 1){Position(76, i);printf("|| ");}}void ShowText(char *text) //根据字符串的长短智能打印出包围字符串的笑脸{int i;int strLength = strlen(text); //得到字符串长度Position(40 - (strLength / 2)-1, 11);printf("%c", 1);for (i = 0; i < strLength + 2; i++){printf("%c", 1);}printf("%c", 1);Position(40 - (strLength / 2)-1, 12);printf("%c ", 1);printf(text);printf(" %c", 1);Position(40 - (strLength / 2)-1, 13);printf("%c", 1);for (i = 0; i < strLength + 2; i++){printf("%c", 1);}printf("%c", 1);}void ResetSnake() //初始化贪吃蛇{int x, y;for (x = 39, y = 0; y < 2; y++){snake[y] = POINT(x, 7 - y);}body = 2; //设定蛇身初始长度为2newbody = 6;addHead = 0;deleteTail = 0;direction = LEFT;}int RefreshSnake() //更新蛇的移动//返回值为1游戏结束{int x, y;memcpy(snake + 1, snake, sizeof(int)*body); if (!newbody){deleteTail = snake[body];snake[body] = 0;}else{body++;newbody--;}x = GETX(snake[0]);y = GETY(snake[0]);switch (direction) //控制蛇头移动方向{case UP:y -= 1; //蛇头向上移动一格(对应坐标y-1)snake[0] = POINT(x, y);break;case DOWN:y += 1;snake[0] = POINT(x, y);break;case LEFT:x -= 1;snake[0] = POINT(x, y);break;case RIGHT:x += 1;snake[0] = POINT(x, y);break;}addHead = snake[0];if (x > 75 || x < 3 ||y > 23 || y < 1) //检测是否撞墙{GameOver(KISSWALL);return 1;}int i;for (i = 1; i < body; i++)if (snake[0] == snake[i]) //检测是否撞到自己身体{GameOver(KISSASS);return 1;}if (snake[0] == food){while (1) //食物的位置不与蛇身重合{food = (rand() % (75 * 23)) ;for (i = 0; snake[i]; i++)if (food == snake[i]) //检测是否吃到食物,吃到则reset食物food = 0;if (food) //如果food==0则重新建立一个食物坐标break;}CreatFood(); //吃了?再来一个!newbody = (rand() % 6) + 1; //吃完食物蛇身增长一节}return 0;}void CreatFood() //生成食物{if (GETX(food) > 75 || GETX(food) < 3 || GETY(food) > 23 || GETY(food) < 1) CreatFood;elsePosition(GETX(food), GETY(food));printf("%c",4);}void Draw() //画出蛇身和食物{system("cls");int i;for (i = 0; snake[i]; i++){Position(GETX(snake[i]), GETY(snake[i]));printf("%c",1);}CreatFood();}void RefreshScreen() //屏幕刷新{if (deleteT ail){Position(GETX(deleteTail), GETY(deleteTail));printf(" ");}if (addHead){Position(GETX(addHead), GETY(addHead));printf("%c",1);}addHead = deleteT ail = 0;}void GameOver(int Type) //游戏结束条件{switch (Type){case KISSASS://撞到自己身体ShowText("NOOB!当你以光速绕着一棵树奔跑就会发现自己在怼自己!");break;case KISSWALL://撞墙ShowText("NOOB!你有考虑过墙的感受吗→→");}food = 0;play = 0;memset(snake, 0, sizeof(int) * 500); //内存初始化}int Gaming() //执行int KeyboardInput;Sleep(60);//速度if (kbhit()){KeyboardInput = getch();if (KeyboardInput == 0 || KeyboardInput == 0xE0) {KeyboardInput = getch();switch (KeyboardInput) //方向控制{case 72:if (direction != DOWN)direction = UP;break;case 80:if (direction != UP)direction = DOWN;break;case 75:if (direction != RIGHT)direction = LEFT;break;case 77:if (direction != LEFT)direction = RIGHT;break;}}if (KeyboardInput == '\r') //暂停{if (!play){play = 1;if (pause){Draw();pause = 0;}}else{ShowText("不许暂停,继续怼!!");play = 0;pause = 1;}}else if (KeyboardInput == 0x1B) //退出return 0;}if (play){if (!food){srand(clock());food = (rand() % (75 * 23));ResetSnake();Draw();}else{if (!RefreshSnake()){RefreshScreen();}}}return 1;}// Allrights reserve by 博元。

c语言贪吃蛇课程设计

c语言贪吃蛇课程设计

c语言贪吃蛇课程设计一、教学目标本课程旨在通过学习C语言实现贪吃蛇游戏,让学生掌握C语言编程的基本技能,培养学生的逻辑思维能力和问题解决能力。

具体的教学目标如下:1.理解C语言的基本语法和数据类型。

2.掌握C语言的运算符和表达式。

3.学习C语言的控制结构,包括条件语句和循环语句。

4.了解C语言的函数和数组。

5.能够使用C语言编写简单的程序。

6.能够使用C语言实现贪吃蛇游戏的基本功能,包括蛇的移动、食物的生成和 collision检测。

7.能够优化代码,提高游戏的性能和可读性。

情感态度价值观目标:1.培养学生的团队合作意识和沟通能力,通过小组合作完成游戏项目。

2.培养学生的创新思维和问题解决能力,鼓励学生进行游戏的创意设计和优化。

3.培养学生的学习兴趣和自信心,通过完成游戏项目感受到编程的乐趣和成就感。

二、教学内容根据课程目标,本课程的教学内容主要包括C语言的基本语法和数据类型、运算符和表达式、控制结构、函数和数组。

具体的教学大纲如下:1.第一章:C语言概述•C语言的发展历史和特点•C语言的基本语法和结构2.第二章:数据类型和运算符•基本数据类型和变量•运算符和表达式3.第三章:控制结构4.第四章:函数和数组•函数的定义和调用•一维数组和多维数组5.第五章:贪吃蛇游戏实现•蛇的移动和食物的生成•collision检测和游戏结束条件•游戏界面的设计和美化三、教学方法为了激发学生的学习兴趣和主动性,本课程将采用多种教学方法,包括讲授法、讨论法、案例分析法和实验法。

1.讲授法:通过教师的讲解和演示,向学生传授C语言的基本语法和编程技巧。

2.讨论法:通过小组讨论和分享,培养学生的团队合作意识和沟通能力。

3.案例分析法:通过分析具体的贪吃蛇游戏案例,引导学生思考和解决问题。

4.实验法:通过编写代码和调试程序,培养学生的动手能力和问题解决能力。

四、教学资源为了支持教学内容和教学方法的实施,本课程将准备以下教学资源:1.教材:《C语言程序设计》2.参考书:《C语言编程实例解析》3.多媒体资料:PPT课件、视频教程4.实验设备:计算机、编程环境(如Visual Studio、Code::Blocks等)通过以上教学资源的使用,将丰富学生的学习体验,提高学生的学习效果。

贪吃蛇 C语言代码

贪吃蛇 C语言代码
y=randno();
}
snake[x][y]=FOOD;
draw(snake);
/*---------------------------------------*/
/*--------控制的部分---------------------*/
while(judgeGO(snake))
}
if(x>0&&x<16&&y>0&&y<16)
{
if(a==0)
printf("█");
else printf("□");
return ;
}
}
void draw(int (*sna)[17])
draw(snake);
Sleep(100);
continue;
}
rightmove(snake);
draw(snake);
}
int randno()
{
srand(time(NULL)); //运用随机函数,取随机数,出现食物用
return rand()%15+1;
}
//判断游戏是否结束
bool judgeGO(int (*sna)[17])
{
int x,y,i=0,max=0,count=0;
while(!kbhit()&&key1!=77&&judgeGO(snake))
{
if(judgeF(snake,key))
{
draw(snake);
Sleep(100);

C语言贪吃蛇全部程序及说明Word版

C语言贪吃蛇全部程序及说明Word版

#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <string.h>#include <time.h>const int H = 8; //地图的高const int L = 16; //地图的长char GameMap[H][L]; //游戏地图int key; //按键保存int sum = 1, over = 0; //蛇的长度, 游戏结束(自吃或碰墙)int dx[4] = {0, 0, -1, 1}; //左、右、上、下的方向int dy[4] = {-1, 1, 0, 0};struct Snake //蛇的每个节点的数据类型{int x, y; //左边位置int now; //保存当前节点的方向, 0,1,2,3分别为左右上下}Snake[H*L];const char Shead = '@'; //蛇头const char Sbody = '#'; //蛇身const char Sfood = '*'; //食物const char Snode = '.'; //'.'在地图上标示为空void Initial(); //地图的初始化void Create_Food(); //在地图上随机产生食物void Show(); //刷新显示地图void Button(); //取出按键,并判断方向void Move(); //蛇的移动void Check_Border(); //检查蛇头是否越界void Check_Head(int x, int y); //检查蛇头移动后的位置情况int main(){Initial();Show();return 0;}void Initial() //地图的初始化{int i, j;int hx, hy;system("title 贪吃蛇"); //控制台的标题memset(GameMap, '.', sizeof(GameMap)); //初始化地图全部为空'.' system("cls");srand(time(0)); //随机种子hx = rand()%H; //产生蛇头hy = rand()%L;GameMap[hx][hy] = Shead;Snake[0].x = hx; Snake[0].y = hy;Snake[0].now = -1;Create_Food(); //随机产生食物for(i = 0; i < H; i++) //地图显示{for(j = 0; j < L; j++)printf("%c", GameMap[i][j]);printf("\n");}printf("\n小小C语言贪吃蛇\n");printf("按任意方向键开始游戏\n");getch(); //先接受一个按键,使蛇开始往该方向走Button(); //取出按键,并判断方向}void Create_Food() //在地图上随机产生食物{int fx, fy;while(1){fx = rand()%H;fy = rand()%L;if(GameMap[fx][fy] == '.') //不能出现在蛇所占有的位置{GameMap[fx][fy] = Sfood;break;}}}void Show() //刷新显示地图{int i, j;while(1){_sleep(500); //延迟半秒(1000为1s),即每半秒刷新一次地图Button(); //先判断按键在移动Move();if(over) //自吃或碰墙即游戏结束{printf("\n**游戏结束**\n");printf("你的得分:%d\n",sum=10*(sum-1));getchar();break;}system("cls"); //清空地图再显示刷新吼的地图for(i = 0; i < H; i++){for(j = 0; j < L; j++)printf("%c", GameMap[i][j]);printf("\n");}printf("\n小小C语言贪吃蛇\n");printf("按任意方向键开始游戏\n");}}void Button() //取出按键,并判断方向{if(kbhit() != 0) //检查当前是否有键盘输入,若有则返回一个非0值,否则返回0 {while(kbhit() != 0) //可能存在多个按键,要全部取完,以最后一个为主key = getch(); //将按键从控制台中取出并保存到key中switch(key){ //左case 75: Snake[0].now = 0;break;//右case 77: Snake[0].now = 1;break;//上case 72: Snake[0].now = 2;break;//下case 80: Snake[0].now = 3;break;}}}void Move() //蛇的移动{int i, x, y;int t = sum; //保存当前蛇的长度//记录当前蛇头的位置,并设置为空,蛇头先移动x = Snake[0].x; y = Snake[0].y; GameMap[x][y] = '.';Snake[0].x = Snake[0].x + dx[ Snake[0].now ];Snake[0].y = Snake[0].y + dy[ Snake[0].now ];Check_Border(); //蛇头是否越界Check_Head(x, y); //蛇头移动后的位置情况,参数为: 蛇头的开始位置if(sum == t) //未吃到食物即蛇身移动哦for(i = 1; i < sum; i++) //要从蛇尾节点向前移动哦,前一个节点作为参照{if(i == 1) //尾节点设置为空再移动GameMap[ Snake[i].x ][ Snake[i].y ] = '.';if(i == sum-1) //为蛇头后面的蛇身节点,特殊处理{Snake[i].x = x;Snake[i].y = y;Snake[i].now = Snake[0].now;}else //其他蛇身即走到前一个蛇身位置{Snake[i].x = Snake[i+1].x;Snake[i].y = Snake[i+1].y;Snake[i].now = Snake[i+1].now;}GameMap[ Snake[i].x ][ Snake[i].y ] = '#'; //移动后要置为'#'蛇身}}void Check_Border() //检查蛇头是否越界{if(Snake[0].x < 0 || Snake[0].x >= H|| Snake[0].y < 0 || Snake[0].y >= L)over = 1;}void Check_Head(int x, int y) //检查蛇头移动后的位置情况{if(GameMap[ Snake[0].x ][ Snake[0].y ] == '.') //为空GameMap[ Snake[0].x ][ Snake[0].y ] = '@';elseif(GameMap[ Snake[0].x ][ Snake[0].y ] == '*') //为食物{GameMap[ Snake[0].x ][ Snake[0].y ] = '@';Snake[sum].x = x; //新增加的蛇身为蛇头后面的那个Snake[sum].y = y;Snake[sum].now = Snake[0].now;GameMap[ Snake[sum].x ][ Snake[sum].y ] = '#';sum++;Create_Food(); //食物吃完了马上再产生一个食物}elseover = 1;}。

C语言项目案例之贪吃蛇

C语言项目案例之贪吃蛇

C语⾔项⽬案例之贪吃蛇项⽬案例:贪吃蛇下载链接:1. 初始化墙代码:// 初始化墙void init_wall(void){for (size_t y = 0; y <= HIGH; ++y){for (size_t x = 0; x <= WIDE; ++x){if (x == WIDE || y == HIGH) // 判断是否到墙{printf("=");}else{printf(" ");}}printf("\n");}}效果:2. 定义蛇和⾷物类型typedef struct{int x;int y;}FOOD; // ⾷物typedef struct{int x;int y;}BODY; // ⾝体typedef struct{int size; // ⾝体长度BODY body[WIDE*HIGH];}SNAKE; // 蛇3. 初始化蛇和⾷物// 定义⼀个蛇和⾷物SNAKE snake;FOOD food;// 初始化⾷物void init_food(void){food.x = rand() % WIDE; // 随机⽣成坐标food.y = rand() % HIGH;}// 初始化蛇void init_snake(void){snake.size = 2;// 将蛇头初始化到墙中间snake.body[0].x = WIDE / 2;snake.body[0].y = HIGH / 2;// 蛇⾝紧跟蛇头snake.body[1].x = WIDE / 2 - 1;snake.body[1].y = HIGH / 2;}4. 显⽰UI// 显⽰UIvoid showUI(void){// 显⽰⾷物// 存放光标位置COORD coord;coord.X = food.x;coord.Y = food.y;// 光标定位SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); putchar('$');// 显⽰蛇for (size_t i = 0; i < snake.size; ++i){// 设置光标coord.X = snake.body[i].x;coord.Y = snake.body[i].y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); if (i == 0)}else{putchar('#');}}}效果:最终代码// main.c#define _CRT_SECURE_NO_WARNINGS#include "./snakeGame.h"int main(void){// 取消光标CONSOLE_CURSOR_INFO cci;cci.bVisible = FALSE; // 取消光标cci.dwSize = sizeof(cci);SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cci); system("color 2");printf("欢迎来到贪吃蛇\n准备好了吗?按s/S开始,q/Q退出\n"); char ch = _getch();switch (ch){case 's':case 'S':system("color 0");system("cls");break;default:return 0;}init_wall();init_food();init_snake();showUI();playGame();return 0;}// snakeGame.c#include "./snakeGame.h"// 定义⼀个蛇和⾷物SNAKE snake;FOOD food;// ⽅向增量int dx = 0;int dy = 0;int lx, ly; // 尾节点// 初始化⾷物void init_food(void){food.x = rand() % WIDE; // 随机⽣成坐标food.y = rand() % HIGH;}// 初始化蛇void init_snake(void){snake.size = 2;snake.fraction = 0;// 将蛇头初始化到墙中间snake.body[0].x = WIDE / 2;snake.body[0].y = HIGH / 2;snake.body[1].x = WIDE / 2 - 1;snake.body[1].y = HIGH / 2;}// 初始化墙void init_wall(void){for (size_t y = 0; y <= HIGH; ++y){for (size_t x = 0; x <= WIDE; ++x){if (x == WIDE || y == HIGH) // 判断是否到墙{printf("=");}else{printf(" ");}}printf("\n");}printf("分数:0\n");}// 显⽰UIvoid showUI(void){// 显⽰⾷物// 存放光标位置COORD coord;coord.X = food.x;coord.Y = food.y;// 光标定位SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);putchar('$');// 显⽰蛇for (size_t i = 0; i < snake.size; ++i){// 设置光标coord.X = snake.body[i].x;coord.Y = snake.body[i].y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);if (i == 0){putchar('@');}else{putchar('#');}}// 处理尾节点coord.X = lx;coord.Y = ly;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);putchar(' ');coord.X = WIDE;coord.Y = HIGH;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);printf("\n分数:%d\n",snake.fraction);}void playGame(void){COORD _coord;system("color 7");char key = 'D';// 蛇不能撞墙while (snake.body[0].x >= 0 && snake.body[0].x <= WIDE && snake.body[0].y >= 0 && snake.body[0].y <= HIGH) {// 蛇不能撞⾃⼰for (size_t i = 1; i < snake.size; ++i){if (snake.body[0].x == snake.body[i].x && snake.body[0].y == snake.body[i].y){goto OVER;}}// 撞⾷物if (snake.body[0].x == food.x && snake.body[0].y == food.y){++snake.size;++snake.fraction;// 随机出现⾷物init_food();}// 控制蛇移动// 判断是否按下按键if (_kbhit()){key = _getch(); // 不需要敲回车,按下就⽴马确认}// 判断W A S D中哪个按键按下switch (key){case 'w':case 'W':dx = 0;dy = -1;break;case 'a':case 'A':dx = -1;dy = 0;break;case 's':case 'S':dx = 0;dy = 1;break;case 'd':dx = 1;dy = 0;break;case 'q':case 'Q':_coord.X = WIDE;_coord.Y = HIGH;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), _coord); putchar('\n');return;}// 蛇移动// 记录尾节点位置lx = snake.body[snake.size - 1].x;ly = snake.body[snake.size - 1].y;for (size_t i = snake.size - 1; i > 0; --i){snake.body[i].x = snake.body[i - 1].x;snake.body[i].y = snake.body[i - 1].y;}// 更新蛇头snake.body[0].x += dx;snake.body[0].y += dy;showUI();Sleep(500); // 延时}// 游戏结束OVER:system("color 4");_coord.X = 6;_coord.Y = HIGH + 1;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), _coord); printf("\n游戏结束");printf("按r/R重新开始,按q/Q退出\n");char _key;_key = _getch();switch (_key){case 'r':case 'R':system("cls");init_wall();init_food();init_snake();showUI();playGame();case 'Q':case 'q':default:system("color 7");return;}}// snakeGame.h#pragma once#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <Windows.h>#define WIDE 60 // 长#define HIGH 20 // ⾼typedef struct{int x;int y;}FOOD; // ⾷物typedef struct{int x;int y;}BODY; // ⾝体typedef struct{int size; // ⾝体长度int fraction; // 分数BODY body[WIDE*HIGH];}SNAKE; // 蛇void init_wall(void);void init_food(void);void init_snake(void);void showUI(void);void playGame(void);。

超简单贪吃蛇c语言代码编写

超简单贪吃蛇c语言代码编写

超简单贪吃蛇c语言代码编写贪吃蛇其实就是实现以下几步——1:蛇的运动(通过“画头擦尾”来达到蛇移动的视觉效果)2:生成食物3:蛇吃食物(实现“画头不擦尾”)4:游戏结束判断(也就是蛇除了食物,其余东西都不能碰)#include<stdio.h>#include<stdlib.h>#include<windows.h>#include<conio.h>#include<time.h>#define width 60#define hight 25#define SNAKESIZE 200//蛇身的最长长度int key=72;//初始化蛇的运动方向,向上int changeflag=1;//用来标识是否生成食物,1表示蛇还没吃到食物,0表示吃到食物int speed=0;//时间延迟struct {int len;//用来记录蛇身每个方块的坐标int x[SNAKESIZE];int y[SNAKESIZE];int speed;}snake;struct{int x;int y;}food;void gotoxy(int x,int y)//调用Windows的API函数,可以在控制台的指定位置直接操作,这里可暂时不用深究{COORD coord;coord.X = x;coord.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }//■○void drawmap(){//打印图框for (int _y = 0; _y < hight; _y++){for (int x = 0; x < width; x+=2){if (x == 0 || _y == 0 || _y == hight - 1 || x == width - 2){gotoxy(x, _y);printf("■");}}}//打印蛇头snake.len=3;snake.x[0]=width/2;snake.y[0]=hight/2;gotoxy(snake.x[0],snake.y[0]);printf("■");//打印蛇身for(int i=1;i<snake.len;i++){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1]+1;gotoxy(snake.x[i],snake.y[i]);printf("■");}//初始化食物的位置food.x=20;food.y=20;gotoxy(food.x,food.y);printf("○");}/**控制台按键所代表的数字*“↑”:72*“↓”:80*“←”:75*“→”:77*/void snake_move()//按键处理函数{int history_key=key;if (_kbhit()){fflush(stdin);key = _getch();key = _getch();}if(changeflag==1)//还没吃到食物,把尾巴擦掉{gotoxy(snake.x[snake.len-1],snake.y[snake.len-1]);printf(" ");}for(int i=snake.len-1;i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];}if(history_key==72&&key==80)key=72;if(history_key==80&&key==72)key=80;if(history_key==75&&key==77)key=75;if(history_key==77&&key==75)key=77;switch(key){case 72:snake.y[0]--;break;case 75:snake.x[0]-= 2;break;case 77:snake.x[0]+= 2;break;case 80:snake.y[0]++;break;}gotoxy(snake.x[0],snake.y[0]);printf("■");gotoxy(0,0);changeflag=1;}void creatfood(){if(snake.x[0] == food.x && snake.y[0] == food.y)//只有蛇吃到食物,才能生成新食物{changeflag=0;snake.len++;if(speed<=100)speed+=10;while(1){srand((unsigned int) time(NULL));food.x=rand()%(width-6)+2;//限定食物的x范围不超出围墙,但不能保证food.x 为偶数food.y=rand()%(hight-2)+1;for(int i=0;i<snake.len;i++){if(food.x==snake.x[i]&&food.y==snake.y[i])//如果产生的食物与蛇身重合则退出break;}if(food.x%2==0)break;//符合要求,退出循环}gotoxy(food.x,food.y);printf("○");}}bool Gameover(){//碰到围墙,OVERif(snake.x[0]==0||snake.x[0]==width-2)return false;if(snake.y[0]==0||snake.y[0]==hight-1) return false;//蛇身达到最长,被迫OVERif(snake.len==SNAKESIZE)return false;//头碰到蛇身,OVERfor(int i=1;i<snake.len;i++){if(snake.x[0]==snake.x[i]&&snake.y[0]==snake.y[i])return false;}return true;}int main(){system("mode con cols=60 lines=27");drawmap();while(Gameover()){snake_move();creatfood();Sleep(350-speed);//蛇的移动速度}return 0;}。

贪吃蛇游戏代码(C语言编写)

贪吃蛇游戏代码(C语言编写)

cleardevice(); if (!pause)
nextstatus(); else {
settextstyle(1,0,4); setcolor(12); outtextxy(250,100,"PAUSE"); } draw();
if(game==GAMEOVER) { settextstyle(0,0,6); setcolor(8); outtextxy(101,101,"GAME OVER"); setcolor(15); outtextxy(99,99,"GAME OVER"); setcolor(12); outtextxy(100,100,"GAME OVER"); sprintf(temp,"Last Count: %d",count); settextstyle(0,0,2); outtextxy(200,200,temp); }
place[tear].st = FALSE; tear ++; if (tear >= MAX) tear = 0; } else { eat = FALSE; exit = TRUE; while(exit) { babyx = rand()%MAXX; babyy = rand()%MAXY; exit = FALSE; for( i = 0; i< MAX; i++ ) if( (place[i].st)&&( place[i].x == babyx) && (place[i].y == babyy))
struct SPlace { int x; int y; int st; } place[MAX]; int speed; int count; int score; int control; int head; int tear; int x,y; int babyx,babyy; int class; int eat; int game; int gamedelay[]={5000,4000,3000,2000,1000,500,250,100}; int gamedelay2[]={1000,1};

C语言实现贪吃蛇游戏(命令行)

C语言实现贪吃蛇游戏(命令行)

C语⾔实现贪吃蛇游戏(命令⾏)这是⼀个纯C语⾔写的贪吃蛇游戏,供⼤家参考,具体内容如下#include<stdio.h>#include<stdlib.h>#include<windows.h>#include<time.h>#include<conio.h>#define SNAKE_LENGTH 100//定义蛇的最⼤长度#define SCREEN_WIDETH 80#define SCREEN_HEIGHT 30//定义每⼀节蛇的坐标struct coor{int x;int y;};//枚举⽅向enum CH {right = VK_RIGHT,left = VK_LEFT,up = VK_UP,down = VK_DOWN};//定义蛇的属性struct snake{int len;//当前蛇的长度struct coor coord[SNAKE_LENGTH];//每⼀节蛇的坐标enum CH CH;//定义蛇的⽅向int SPEED;int flag;//定义蛇的状态 1表⽰存活 0表⽰死亡}snake;//光标移动函数void gotoxy(int x, int y){COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);}//初始化游戏界⾯void init_sence(){//初始化上下墙for (int i = 0; i < SCREEN_WIDETH; i += 2){gotoxy(i,0);printf("■");gotoxy(i, SCREEN_HEIGHT);printf("■");}//初始化左右墙for (int i = 0; i <=SCREEN_HEIGHT; i++){gotoxy(0, i);printf("■");gotoxy(SCREEN_WIDETH,i);printf("■");}//打印提⽰信息gotoxy(SCREEN_WIDETH + 5, 2);printf("\t\t贪吃蛇");gotoxy(SCREEN_WIDETH + 5, 6);printf("2018//12//1");gotoxy(SCREEN_WIDETH + 5, 8);printf("作者:⼩⾖芽");gotoxy(SCREEN_WIDETH + 5, 10);printf("F1:加速\tF2:减速");gotoxy(SCREEN_WIDETH + 5, 12);printf("CTRL:继续\t空格:暂停");gotoxy(SCREEN_WIDETH + 5, 14);printf("ESC:退出游戏");gotoxy(SCREEN_WIDETH + 5, 28);printf("建议:QQ:2862841130:::");}struct foodcoord {int x;int y;int flag;//定义⾷物的状态}food;//**这是c程序**#include"snake.h"//蛇的移动void move_snake();//画出蛇void draw_snake();//产⽣⾷物void creatfood();//判断蛇是否吃到⾷物void eatfood();//判断蛇是否死掉void SnakeState();int main(){//设置窗⼝⼤⼩system("mode con cols=110 lines=31");//设置标题SetConsoleTitleA("贪吃蛇");//初始化蛇begin:snake.CH = VK_RIGHT;//初始化⽅向snake.len = 5; //初始化长度snake.SPEED = 300;//初始化蛇的移动速度snake.coord[1].x = SCREEN_WIDETH / 2;//初始化蛇头的坐标 snake.coord[1].y = SCREEN_HEIGHT / 2;snake.coord[2].x = SCREEN_WIDETH / 2-2;//初始化蛇头的坐标 snake.coord[2].y = SCREEN_HEIGHT / 2;snake.coord[3].x = SCREEN_WIDETH / 2-4;//初始化蛇头的坐标 snake.coord[3].y = SCREEN_HEIGHT / 2;//初始化⾷物状态food.flag = 1;//1表⽰吃到⾷物 0表⽰没有吃到⾷物//初始化⾷物状态snake.flag = 1;//1活 0死init_sence();//初始化游戏界⾯while (1){draw_snake();//画蛇Sleep(snake.SPEED);//蛇的移动速度move_snake();//移动蛇if(food.flag)creatfood();//产⽣⾷物eatfood();//判断是否吃到⾷物SnakeState();//判断蛇是否死亡if (!snake.flag)break;}system("cls");gotoxy(SCREEN_WIDETH/2, SCREEN_HEIGHT/2-4);printf(" GAME OVER");gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+2);printf("你的得分是:\t\t\t%d ",snake.len-1);gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+4);printf("我不服再来:\t\t\tCTRL ");gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+6);printf("算了垃圾游戏毁我青春:\t\tESC");while (1){if (GetAsyncKeyState(VK_CONTROL)){system("cls");goto begin;}else if (GetAsyncKeyState(VK_ESCAPE))return 0;}}//蛇的移动void move_snake(){//判断是否有按键操作if (GetAsyncKeyState(up)){if(snake.CH!=down)snake.CH = up;}else if (GetAsyncKeyState(down)){if (snake.CH != up)snake.CH = down;}else if (GetAsyncKeyState(right)){if (snake.CH != left)snake.CH = right;}else if (GetAsyncKeyState(left)){if (snake.CH != right)snake.CH = left;}else if (GetAsyncKeyState(VK_F1)){if(snake.SPEED>=100)snake.SPEED -= 50;}else if (GetAsyncKeyState(VK_F2)){if (snake.SPEED <= 3000)snake.SPEED += 100;}//根据检测到的⽅向改变蛇头的位置switch (snake.CH){case right:snake.coord[1].x += 2; break;case left:snake.coord[1].x -= 2; break;case up:snake.coord[1].y -= 1; break;case down:snake.coord[1].y += 1; break;}}//画出蛇void draw_snake(){//画出蛇头gotoxy(snake.coord[1].x, snake.coord[1].y);printf("□");//画出蛇⾝,直接⼀个for循环实现for (int i = 2; i <= snake.len; i++){gotoxy(snake.coord[i].x, snake.coord[i].y);printf("□");}//擦掉尾巴gotoxy(snake.coord[snake.len].x, snake.coord[snake.len].y); printf(" ");//遍历每⼀节蛇for (int i = snake.len; i >1; i--){snake.coord[i].x = snake.coord[i - 1].x;snake.coord[i].y = snake.coord[i - 1].y;}gotoxy(0, 0);printf("■");gotoxy(85, 25);printf("得分:%d ", snake.len-1);}//产⽣⾷物void creatfood(){//随机种⼦⽣成srand((unsigned)time(NULL));if(food.flag)while (1){food.x = rand() % 80;food.y = rand() % 30;if (food.x % 2 == 0 && food.x >= 2 && food.x <= 78 && food.y > 1 && food.y < 30){int flag = 0;//判断产⽣的⾷物可不可能在蛇的⾝体上for (int i = 1; i <= snake.len; i++){if (snake.coord[i].x == food.x&&snake.coord[i].y == food.y){flag = 1;break;}}if (flag)continue;//绘制⾷物else{gotoxy(food.x, food.y);printf("⊙");food.flag = 0;break;}}}food.flag = 0;}//判断蛇是否吃到⾷物void eatfood(){//只需要判断蛇头是否与⾷物重合if (food.x == snake.coord[1].x&&food.y == snake.coord[1].y){snake.len+=1;food.flag = 1;}}//判断蛇是否死掉void SnakeState(){if (snake.coord[1].x < 2 || snake.coord[1].x>78 || snake.coord[1].y < 1 || snake.coord[1].y>29) snake.flag = 0;for (int i = 2; i <= snake.len; i++){if (snake.coord[1].x == snake.coord[i].x&&snake.coord[1].y == snake.coord[i].y)snake.flag = 0;}}更多有趣的经典⼩游戏实现专题,分享给⼤家:以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

贪吃蛇C语言编码

贪吃蛇C语言编码

#include<bios.h>#include<conio.h>#include<stdio.h>#include<process.h>#include<stdlib.h>#define MAX_LENGTH 100#define LEFT 0x4b00#define RIGHT 0x4d00#define UP 0x4800#define DOWN 0x5000#define PAUSE 0x1970 /* p key */#define ESC 0x11b#define TIME_LIMIT 4 /*Òƶ¯ËÙ¶È*/#define SNAKE 2#define WALL 1#define WCOLOR WHITE#define BKCOLOR BLACK#define SCOLOR WHITE#define FOOD 15#define FCOLOR RED#define X 20#define Y 2struct snake{int x;int y;}snk[MAX_LENGTH];int bkey;int box[22][22];int begin ,end,where_x,where_y,eat;int direction = 0,dead=0,pre_direction;void initbox(){int i,j;for(i=0;i<22;i++)for(j=0;j<22;j++)box[i][j]=0;for(i=0;i<22;i++)box[i][21] = box[21][i] = box[0][i] = box[i][0] = 1;textbackground(BKCOLOR);textcolor(WCOLOR);for(i=0;i<22;i++)for(j=0;j<22;j++)if( box[i][j]==1 ) { gotoxy(X+i,Y+j); putch(W ALL); }}int timeover(){static long int tm1,tm2;tm1=biostime(0,0l);if(tm1 - tm2 >TIME_LIMIT ) { tm2 = tm1; return 1;}return 0;}void go_ahead(){int mid = begin;if(direction == 0 )return;if(begin == 0 ) begin = MAX_LENGTH-1;else begin -= 1;if(direction == UP) { snk[begin].x = snk[mid].x ; snk[begin].y = snk[mid].y-1; }else if(direction == DOWN ){snk[begin].x = snk[mid].x ; snk[begin].y = snk[mid].y+1; } else if(direction == LEFT){snk[begin].x = snk[mid].x-1 ; snk[begin].y = snk[mid].y; } else if(direction == RIGHT){snk[begin].x = snk[mid].x+1 ; snk[begin].y = snk[mid].y; } if(box[snk[begin].x][snk[begin].y] == 1) dead=1;gotoxy(X+snk[begin].x,Y+snk[begin].y);textcolor(SCOLOR);putch(SNAKE);box[snk[begin].x][snk[begin].y] = 1;if(eat != 1){ gotoxy(X+snk[end].x,Y+snk[end].y);textcolor(BKCOLOR);putch('');if(end == 0) end = MAX_LENGTH-1;else end -= 1;box[snk[end].x][snk[end].y] = 0;}}void do_action(){switch(bkey){case UP : if(direction != DOWN) direction = UP; break;case DOWN : if(direction != UP ) direction = DOWN; break;case LEFT : if(direction != RIGHT) direction = LEFT; break;case RIGHT: if(direction != LEFT ) direction = RIGHT; break;case PAUSE : getch();bkey = direction; break;case ESC : exit(0);break;default : return;}}do_eat(){if((snk[begin].x == where_x) && (snk[begin].y == where_y) ) {eat = 1;if(begin == 0 ) begin = MAX_LENGTH -1 ;else begin -= 1;snk[begin].x = where_x ;snk[begin].y = where_y;}}void putfood(){do{where_x = random(20)+1;where_y = random(20)+1;}while(box[where_x][where_y]==1);textcolor(FCOLOR);gotoxy(X+where_x,Y+where_y);putch(FOOD);}main(){do{dead= 0;bkey= 0;direction = 0;clrscr();textcolor(WHITE);gotoxy(1,1);printf("press direction keys for start\n");printf("press p for PAUSE\n");printf("press ESC for EXIT\n ");initbox();begin = end = MAX_LENGTH-1;box[9][9] = 0;snk[begin].x = 9;snk[begin].y = 9;gotoxy(X+snk[begin].x,Y+snk[begin].y);textcolor(SCOLOR);putch(SNAKE);putfood();eat = 0;do{ do_eat();if(eat == 1 ) { putfood(); eat = 0; }while( !timeover() )if( bioskey(1) )bkey = bioskey(0);do_action();go_ahead();}while( !dead );textcolor(RED);gotoxy(1,24);printf("dou you want to try again y/n");do { bkey = bioskey(0); }while(bkey!=0x1579 && bkey!=0x316e); if(bkey == 0x316e )break;}while(1);}。

c语言课程设计贪吃蛇设计

c语言课程设计贪吃蛇设计
物或蛇身
Part Three
C语言基础知识
数据类型和变量
基本数据类型:int、float、char、double等 复合数据类型:数组、结构体、指针等 变量声明:使用关键字"int"、"float"等声明变量 变量赋值:使用"="为变量赋值 变量作用域:局部变量、全局变量等 变量生命周期:从声明到释放的过程
结构体和联合体:包括结构 体定义、结构体初始化、结 构体访问、联合体定义、联 合体初始化、联合体访问等
函数和数组
函数:C语言中的基本单元,用于实现特定功能
数组:C语言中的基本数据类型,用于存储一组相同类型 的数据
数组函数:如strlen()、strcpy()等,用于操作字符串
指针:C语言中的重要概念,用于指向内存地址
Part Four
贪吃蛇游戏设计
游戏逻辑设计
游戏结束:当蛇碰到边界或 自己时结束
游戏循环:不断更新蛇的位 置和方向
游戏开始:初始化蛇的位置 和方向
得分计算:根据吃到的食物 数量计算得分
游戏难度:根据得分调整游 戏难度,如增加蛇的速度或
改变食物的位置
游戏界面:设计游戏界面, 包括蛇、食物、边界等元素
Part Seven
总结和展望
课程设计收获和体会
掌握了C语言的基本语法和编 程技巧
学会了如何设计并实现一个完 整的游戏项目
提高了解决问题的能力和团队 协作能力
对游戏开发有了更深入的了解 和兴趣
C语言在游戏开发中的应用前景
游戏开发中,C语言具有高效、稳定的特点,适合开发大型游戏。 C语言具有广泛的应用领域,可以开发各种类型的游戏,如动作、冒险、策略等。 C语言具有强大的社区支持,可以找到大量的游戏开发资源和教程。 C语言在游戏开发中具有广泛的应用前景,可以开发出更多优秀的游戏作品。

贪吃蛇c语言代码

贪吃蛇c语言代码

贪吃蛇c语言代码#include <graphics.h>#include <conio.h>#include <stdlib.h>#include <dos.h>#define NULL 0#define UP 18432#define DOWN 20480#define LEFT 19200#define RIGHT 19712#define ESC 283#define ENTER 7181struct snake{int centerx;int centery;int newx;int newy;struct snake *next;};struct snake *head;int grade=60; /*控制速度的*******/int a,b; /* 背静遮的位置*/void *far1,*far2,*far3,*far4; /* 蛇身指针背静遮的指针虫子*/int size1,size2,size3,size4; /* **全局变量**/int ch=RIGHT; /**************存按键开始蛇的方向为RIGHT***********/int chy=RIGHT;int flag=0; /*********判断是否退出游戏**************/int control=4; /***********判断上次方向和下次方向不冲突***/int nextshow=1; /*******控制下次蛇身是否显示***************/int scenterx; /***************随即矩形中心坐标***************/int scentery;int sx; /*******在a b 未改变前得到他们的值保证随机矩形也不在此出现*******/int sy;/************************蛇身初始化**************************/void snakede(){struct snake *p1,*p2;head=p1=p2=(struct snake *)malloc(sizeof(struct snake));p1->centerx=80;p1->newx=80;p1->centery=58;p1->newy=58;p1=(struct snake *)malloc(sizeof(struct snake));p2->next=p1;p1->centerx=58;p1->newx=58;p1->centery=58;p1->newy=58;p1->next=NULL;}/*******************end*******************/void welcome() /*************游戏开始界面,可以选择速度**********/ {int key;int size;int x=240;int y=300;int f;void *buf;setfillstyle(SOLID_FILL,BLUE);bar(98,100,112,125);setfillstyle(SOLID_FILL,RED);bar(98,112,112,114);setfillstyle(SOLID_FILL,GREEN);bar(100,100,110,125);size=imagesize(98,100,112,125);buf=malloc(size);getimage(98,100,112,125,buf);cleardevice();setfillstyle(SOLID_FILL,BLUE);bar(240,300,390,325);outtextxy(193,310,"speed:");setfillstyle(SOLID_FILL,RED);bar(240,312,390,314);setcolor(YELLOW);outtextxy(240,330,"DOWN");outtextxy(390,330,"UP");outtextxy(240,360,"ENTER to start..." );outtextxy(270,200,"SNAKE");fei(220,220);feiyang(280,220);yang(340,220);putimage(x,y,buf,COPY_PUT);setcolor(RED);rectangle(170,190,410,410);while(1){ if(bioskey(1)) /********8选择速度部分************/ key=bioskey(0);switch(key){case ENTER:f=1;break;case DOWN:if(x>=240){ putimage(x-=2,y,buf,COPY_PUT);grade++;key=0;}case UP:if(x<=375){ putimage(x+=2,y,buf,COPY_PUT);grade--;key=0;break;}}if (f==1)break;} /********** end ****************/ free(buf);}/*************************随即矩形*****************//***********当nextshow 为1的时候才调用此函数**********/void ran(){ int nx;int ny;int show; /**********控制是否显示***********/int jump=0;struct snake *p;p=head;if(nextshow==1) /***********是否开始随机产生***************/{show=1;randomize();nx=random(14);ny=random(14);scenterx=nx*22+58;scentery=ny*22+58;while(p!=NULL){if(scenterx==p->centerx&&scentery==p->centery||scenterx==sx&&scentery==sy) {show=0;jump=1;break;}elsep=p->next;if(jump==1)break;}if(show==1){putimage(scenterx-11,scentery-11,far3,COPY_PUT);nextshow=0;break;}}}/***********过关动画**************/ void donghua(){ int i;cleardevice();setbkcolor(BLACK);randomize();while(1){for(i=0;i<=5;i++){putpixel(random(640),random(80),13); putpixel(random(640),random(80)+80,2); putpixel(random(640),random(80)+160,3); putpixel(random(640),random(80)+240,4); putpixel(random(640),random(80)+320,1); putpixel(random(640),random(80)+400,14); }setcolor(YELLOW);settextstyle(0,0,4);outtextxy(130,200,"Wonderful!!"); setfillstyle(SOLID_FILL,10);bar(240,398,375,420);feiyang(300,400);fei(250,400);yang(350,400);if(bioskey(1))if(bioskey(0)==ESC){flag=1;break;}}}/*************************end************************//***********************初始化图形系统*********************/ void init(){int a=DETECT,b;int i,j;initgraph(&a,&b,"");}/***************************end****************************/ /***画立体边框效果函数******/void tline(int x1,int y1,int x2,int y2,int white,int black){ setcolor(white);line(x1,y1,x2,y1);line(x1,y1,x1,y2);setcolor(black);line(x2,y1,x2,y2);line(x1,y2,x2,y2);}/****end*********//*************飞洋标志**********/int feiyang(int x,int y){int feiyang[18][18]={ {0,0,0,0,0,0,1,1,1,1,1,1,0,1,1,0,0,0}, {0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,0},{0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,0,0,0},{0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0},{0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0},{0,0,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0},{0,0,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0},{0,0,1,1,1,1,1,0,0,1,0,0,1,1,0,0,0,0},{0,0,1,1,1,0,0,0,0,1,0,1,1,1,0,0,0,0},{0,0,1,1,0,1,1,1,0,1,1,1,0,0,0,0,0,0},{0,0,1,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},{0,0,1,1,0,0,0,1,1,0,0,1,1,0,0,1,0,0},{0,0,1,1,1,0,0,1,1,0,0,1,1,0,0,1,0,0},{0,0,1,1,1,1,0,1,1,1,1,1,1,0,1,1,0,0},{0,0,0,1,1,1,0,1,1,1,1,1,0,0,1,0,0,0},{0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,0,0,0},{0,0,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,0},{0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0}};int i,j;for(i=0;i<=17;i++)for(j=0;j<=17;j++){if (feiyang[i][j]==1)putpixel(j+x,i+y,RED);}}/********"飞"字*************/int fei(int x,int y){int fei[18][18]={{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0},{0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0},{0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0},{0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0},{0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0}};int i,j;for(i=0;i<=17;i++)for(j=0;j<=17;j++){if (fei[i][j]==1)putpixel(j+x,i+y,BLUE);}}/*********"洋"字**************/int yang(int x,int y){int yang[18][18]={{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0}, {1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0},{0,1,1,1,0,0,0,1,1,1,0,1,1,0,0,0,0,0},{0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,1,0,0},{0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0},{0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0},{0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0},{0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},{0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,1,1,0},{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0},{1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},{0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}};int i,j;for(i=0;i<=17;i++)for(j=0;j<=17;j++){if (yang[i][j]==1)putpixel(j+x,i+y,BLUE);}}/******************主场景**********************/ int bort(){ int a;setfillstyle(SOLID_FILL,15);bar(49,49,71,71);setfillstyle(SOLID_FILL,BLUE);bar(50,50,70,70);size1=imagesize(49,49,71,71);far1=(void *)malloc(size1);getimage(49,49,71,71,far1);cleardevice();setfillstyle(SOLID_FILL,12);bar(49,49,71,71);size2=imagesize(49,49,71,71);far2=(void *)malloc(size2);getimage(49,49,71,71,far2);setfillstyle(SOLID_FILL,12);bar(49,49,71,71);setfillstyle(SOLID_FILL,GREEN);bar(50,50,70,70);size3=imagesize(49,49,71,71);far3=(void *)malloc(size3);getimage(49,49,71,71,far3);cleardevice(); /*取蛇身节点背景节点虫子节点end*/ setbkcolor(8);setfillstyle(SOLID_FILL,GREEN);bar(21,23,600,450);tline(21,23,600,450,15,8); /***开始游戏场景边框立体效果*******/ tline(23,25,598,448,15,8);tline(45,45,379,379,8,15);tline(43,43,381,381,8,15);tline(390,43,580,430,8,15);tline(392,45,578,428,8,15);tline(412,65,462,85,15,8);tline(410,63,464,87,15,8);tline(410,92,555,390,15,8);tline(412,94,553,388,15,8);tline(431,397,540,420,15,8);tline(429,395,542,422,15,8);tline(46,386,377,428,8,15);tline(44,384,379,430,8,15);setcolor(8);outtextxy(429,109,"press ENTER ");outtextxy(429,129,"---to start"); /*键盘控制说明*/outtextxy(429,169,"press ESC ");outtextxy(429,189,"---to quiet");outtextxy(469,249,"UP");outtextxy(429,289,"LEFT");outtextxy(465,329,"DOWN");outtextxy(509,289,"RIGHT");setcolor(15);outtextxy(425,105,"press ENTER ");outtextxy(425,125,"---to start");outtextxy(425,165,"press ESC ");outtextxy(425,185,"---to quiet");outtextxy(465,245,"UP");outtextxy(425,285,"LEFT");outtextxy(461,325,"DOWN");outtextxy(505,285,"RIGHT"); /*******end*************/ setcolor(8);outtextxy(411,52,"score");outtextxy(514,52,"left");setcolor(15);outtextxy(407,48,"score");outtextxy(510,48,"left");size4=imagesize(409,62,465,88); /****分数框放到内存********/ far4=(void *)malloc(size4);getimage(409,62,465,88,far4);putimage(500,62,far4,COPY_PUT); /*******输出生命框***********/setfillstyle(SOLID_FILL,12);setcolor(RED);outtextxy(415,70,"0"); /***************输入分数为零**********/outtextxy(512,70,"20"); /*************显示还要吃的虫子的数目*********/ bar(46,46,378,378);feiyang(475,400);fei(450,400);yang(500,400);outtextxy(58,390,"mailto:");outtextxy(58,410,"snake game");outtextxy(200,410,"made by yefeng");while(1){ if(bioskey(1))a=bioskey(0);if(a==ENTER)break;}}/******************gameover()******************/void gameover(){ char *p="GAME OVER";int cha;setcolor(YELLOW);settextstyle(0,0,6);outtextxy(100,200,p);while(1){if(bioskey(1))cha=bioskey(0);if(cha==ESC){flag=1;break;}}}/***********显示蛇身**********************/void snakepaint(){struct snake *p1;p1=head;putimage(a-11,b-11,far2,COPY_PUT);while(p1!=NULL){putimage(p1->newx-11,p1->newy-11,far1,COPY_PUT);p1=p1->next;}}/****************end**********************//*********************蛇身刷新变化游戏关键部分*******************/ void snakechange(){struct snake *p1,*p2,*p3,*p4,*p5;int i,j;static int n=0;static int score;static int left=20;char sscore[5];char sleft[1];p2=p1=head;while(p1!=NULL){ p1=p1->next;if(p1->next==NULL){a=p1->newx;b=p1->newy; /************记录最后节点的坐标************/ sx=a;sy=b;}p1->newx=p2->centerx;p1->newy=p2->centery;p2=p1;}p1=head;while(p1!=NULL){p1->centerx=p1->newx;p1->centery=p1->newy;p1=p1->next;}/********判断按键方向*******/if(bioskey(1)){ ch=bioskey(0);if(ch!=RIGHT&&ch!=LEFT&&ch!=UP&&ch!=DOWN&&ch!=ESC) /********chy为上一次的方向*********/ch=chy;}switch(ch){case LEFT: if(control!=4){head->newx=head->newx-22;head->centerx=head->newx;control=2;if(head->newx<47)gameover();}else{ head->newx=head->newx+22;head->centerx=head->newx;control=4;if(head->newx>377)gameover();}chy=ch;break;case DOWN:if(control!=1){ head->newy=head->newy+22;head->centery=head->newy; control=3;if(head->newy>377)gameover();}else{ head->newy=head->newy-22; head->centery=head->newy;control=1;if(head->newy<47)gameover();}chy=ch;break;case RIGHT: if(control!=2){ head->newx=head->newx+22;head->centerx=head->newx;control=4;if(head->newx>377)gameover();}else{ head->newx=head->newx-22;head->centerx=head->newx;control=2;if(head->newx<47)gameover();}chy=ch;break;case UP: if(control!=3){ head->newy=head->newy-22;head->centery=head->newy;control=1;if(head->newy<47)gameover();}else{ head->newy=head->newy+22;head->centery=head->newy;control=3;if(head->newy>377)gameover();}chy=ch;break;case ESC:flag=1;break;}/* if 判断是否吃蛇*/if(flag!=1){ if(head->newx==scenterx&&head->newy==scentery){ p3=head;while(p3!=NULL){ p4=p3;p3=p3->next;}p3=(struct snake *)malloc(sizeof(struct snake));p4->next=p3;p3->centerx=a;p3->newx=a;p3->centery=b;p3->newy=b;p3->next=NULL;a=500;b=500;putimage(409,62,far4,COPY_PUT); /********** 分数框挡住**************/ putimage(500,62,far4,COPY_PUT); /*********把以前的剩下虫子的框挡住********/ score=(++n)*100;left--;itoa(score,sscore,10);itoa(left,sleft,10);setcolor(RED);outtextxy(415,70,sscore);outtextxy(512,70,sleft);nextshow=1;if(left==0) /************判断是否过关**********/donghua(); /*******如果过关,播放过关动画*********************/}p5=head; /*********************判断是否自杀***************************/ p5=p5->next;p5=p5->next;p5=p5->next;p5=p5->next; /****从第五个节点判断是否自杀************/while(p5!=NULL){if(head->newx==p5->centerx&&head->newy==p5->centery){ gameover();break;}elsep5=p5->next;}}}/************snakechange()函数结束*******************//*****************************主函数******************************************/ int main(){ int i;init(); /**********初始化图形系统**********/ welcome(); /*********8欢迎界面**************/ bort(); /*********主场景***************/ snakede(); /**********连表初始化**********/ while(1){ snakechange();if(flag==1)break;snakepaint();ran();for(i=0;i<=grade;i++)delay(3000);}free(far1);free(far2);free(far3);free(far4);closegraph();return 0;}。

C语言贪吃蛇

C语言贪吃蛇
printf("│ │ │\n");
printf("│ │ │\n");
{
food();
exist_food=1;
}
if(x[0]==xi&&y[0]==yi)
{
exist_food=0;
score[3]++;
case 27:
return false;
}
if(x[0]>60)
x[0]=2;
if(x[0]<2)
x[0]=60;
if(y[0]<1)
printf("│ │ │\n");
printf(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
void InitSnake()
{
x[0]=20;
y[0]=10;
score[3]=0;
len=1;
exist_food=0;
}
void food() // 生成食物 x坐标为偶数
{
int i;
fag=0;
srand((unsigned)time( NULL ));
break;
case 'a': // 纵坐标之间的距离大约是横坐标之间的距离的两倍
x[0]-=2;
break;
case 'd':
x[0]+=2;
break;
SetConsoleCursorInfo(hOut,&cci);

贪吃蛇c语言精简版

贪吃蛇c语言精简版

贪吃蛇c语言精简版//贪吃蛇#include<stdio.h>#include <windows.h>#include <stdlib.h>#include <time.h>#include <conio.h>struct all_xy{POINT point;struct all_xy *next;};int x=2,y=0,key,i,found_time;POINT save_point,save_point2,food_xy={20,10};BOOL end_self=FALSE,flag;struct all_xy *head=NULL,*node1,*node2;void gotoxy(int x, int y){COORD coord;coord.X = x,coord.Y=y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);}void snake_add(){for(node2=head;head!=NULL&&node2->next!=NULL;node2=node2->next);node1=(struct all_xy *)malloc(sizeof(struct all_xy));node1->point=(head==NULL?food_xy:node2->point);node1->next=NULL;if(head==NULL){head=node1;return;}else{for(node2=head;node2->next!=NULL;node2=node2->next);node2->next=node1;} }void make_food(long *x,long *y){for(found_time=0;found_time<500;found_time++){*x=(rand()%38+1)*2,*y=rand()%23+1;for(node1=head,flag=FALSE;node1!=NULL;node1=node1->next)if(*x==node1->point.x&&*y==node1->point.y){flag=TRUE;break;}if(!flag){gotoxy(*x,*y);printf("○");return;}}MessageBox(NULL,"已找不到空点放食物了,程序结束!","你太牛了!",MB_ICONASTERISK);exit(0);}int main(){for(i=0;i<3;i++)snake_add();for(srand(time(NULL)),make_food(&food_xy.x,&food_xy.y);;Sleep(400)){if(kbhit()){if((key=getch())==224)key=getch();switch(key){case 80:y!=-1?(x=0,y=1):printf("\a");break;case 72:y!=1?(x=0,y=-1):printf("\a");break;case 75:x!=2?(x=-2,y=0):printf("\a");break;case 77:x!=-2?(x=2,y=0):printf("\a");break;}}node2=head,node1=node2->next,save_point=node1->point;node1->point=node2->point,node1=node1->next;head->point.x+=x,head->point.y+=y;for(node2=head->next;node2!=NULL;node2=node2->next)//依次检查是否自己撞到自己if(node2->point.x==head->point.x&&node2->point.y==head->point.y)//如果发现头结点和任意一个结点的X Y相同则设置end_self的值并跳出{end_self=TRUE;break;}if(head->point.x<0||head->point.x>=78||head->point.y<0||head->point.y>=25| |end_self==TRUE){gotoxy(32,5);printf("游戏结束!");getch();return 0;}for(;node1!=NULL;node1=node1->next){save_point2=node1->point,node1->point=save_point;node2=node1,save_point=save_point2;}gotoxy(save_point.x,save_point.y);printf(" ");if(head->point.x==food_xy.x&&head->point.y==food_xy.y)snake_add(),make_food(&food_xy.x,&food_xy.y);for(node1=head;node1!=NULL;node1=node1->next)gotoxy(node1->point.x,node1->point.y),node1==head?printf("⊙"):printf("□");}}。

用C实现经典的贪吃蛇游戏

用C实现经典的贪吃蛇游戏

用C实现经典的贪吃蛇游戏经典贪吃蛇游戏的C语言实现贪吃蛇是一款经典的游戏,它具有简单的规则和上瘾的玩法。

本文将介绍如何使用C语言实现经典的贪吃蛇游戏。

一、游戏规则简介在贪吃蛇游戏中,玩家操控一条蛇,通过操纵蛇的移动方向来吃掉食物。

随着蛇吃到食物的数量增加,蛇的身体也会逐渐变长。

玩家需要注意的是,蛇的头部不能碰到自己的身体或游戏边界,否则游戏结束。

二、游戏实现思路要实现贪吃蛇游戏,我们需要解决以下几个主要问题:1. 游戏画面的绘制:使用C语言的图形库,如graphics.h或SDL,来绘制游戏画面。

2. 蛇的移动:通过键盘控制蛇的移动方向,确定蛇每次移动的位置。

3. 食物的生成:在游戏区域中随机生成食物,供蛇吃掉。

4. 身体碰撞检测:判断蛇的头部是否碰到自身的身体或游戏边界,如果是,则游戏结束。

三、游戏实现步骤以下是实现贪吃蛇游戏的简要步骤:1. 初始化游戏:设置游戏窗口大小、颜色和其他必要的初始化操作。

在游戏区域中生成一条初始长度为3的蛇,设置蛇的初始位置和移动方向。

2. 绘制游戏画面:使用图形库提供的函数,在游戏窗口中绘制蛇的身体和食物。

3. 键盘控制移动:使用键盘监听函数,当玩家按下方向键时,改变蛇的移动方向。

4. 蛇的移动:根据蛇的当前位置和移动方向,更新蛇头的位置,并将蛇头加入蛇的身体中,同时删除蛇的尾部,实现蛇的移动。

5. 食物的生成:在游戏区域中随机生成食物,初始位置不能与蛇的位置重叠。

6. 身体碰撞检测:判断蛇的头部是否碰到自身的身体或游戏边界,如果是,则游戏结束。

7. 游戏结束:显示游戏得分和结束信息,释放资源。

四、代码实现示例(部分代码)```c#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <time.h>#include <Windows.h>#include <stdbool.h>// 定义游戏窗口大小和蛇身颜色#define WIDTH 80#define HEIGHT 25#define SNAKE_COLOR 15// 定义蛇的结构体typedef struct _node {int x; // 蛇身坐标xint y; // 蛇身坐标ystruct _node* next; // 指向下一个节点的指针} node;node* head; // 蛇头指针node* tail; // 蛇尾指针int direction; // 蛇的移动方向int score; // 游戏得分// 初始化蛇void initSnake() {// 创建蛇头节点head = (node*)malloc(sizeof(node)); head->x = WIDTH / 2;head->y = HEIGHT / 2;head->next = NULL;// 设置初始移动方向direction = 'w';}// 绘制游戏画面void draw() {// 清空屏幕system("cls");// 绘制蛇身node* p = head;while (p) {gotoxy(p->x, p->y);printf("■");p = p->next;}// 绘制食物// 绘制游戏信息gotoxy(0, HEIGHT + 1);printf("Score: %d", score);}// 键盘控制移动void control() {char ch;if (_kbhit()) {ch = _getch();if (ch == 'w' || ch == 's' || ch == 'a' || ch == 'd') { direction = ch;}}}// 蛇的移动void move() {// 根据移动方向改变蛇头位置// 更新蛇身位置// 判断是否吃到食物 // 判断是否撞墙或自身}int main() {// 初始化游戏// 游戏循环while (true) {// 绘制游戏画面// 键盘控制移动// 蛇的移动// 判断游戏是否结束 }// 游戏结束return 0;}```以上是使用C语言实现经典贪吃蛇游戏的简要介绍。

简单贪吃蛇c语言代码,一个C语言写简单贪吃蛇源代码.doc

简单贪吃蛇c语言代码,一个C语言写简单贪吃蛇源代码.doc

简单贪吃蛇c语⾔代码,⼀个C语⾔写简单贪吃蛇源代码.doc ⼀个C语⾔写简单贪吃蛇源代码#include#include#include#include#include#includeint grade=5,point=0,life=3;voidset(),menu(),move_head(),move_body(),move(),init_insect(),left(),upon(),right(),down(),init_graph(),food_f(),ahead(),crate(); struct bug{int x;int y;struct bug *last;struct bug *next;};struct fd{int x;int y;int judge;}food={0,0,0};struct bug *head_f=NULL,*head_l,*p1=NULL,*p2=NULL;void main(){char ch;initgraph(800,600);set();init_insect();while(1){food_f();Sleep(grade*10);setcolor(BLACK);circle(head_l->x,head_l->y,2);setcolor(WHITE);move_body();if(kbhit()){ch=getch();if(ch==27){ahead();set();}else if(ch==-32){switch(getch()){case 72:upon();break;case 80:down();break;case 75:left();break;case 77:right();break;}}else ahead();}else{ahead();}if(head_f->x==food.x&&head_f->y==food.y) {Sleep(100);crate();food.judge=0;point=point+(6-grade)*10;if(food.x<30||food.y<30||food.x>570||food.y>570)life++;menu();}if(head_f->x<5||head_f->x>595||head_f->y<5||head_f->y>595) {Sleep(1000);life--;food.judge=0;init_graph();init_insect();menu();}for(p1=head_f->next;p1!=NULL;p1=p1->next){if(head_f->x==p1->x&&head_f->y==p1->y){Sleep(1000);life--;food.judge=0;init_graph();init_insect();menu();break;}}if(life==0){outtextxy(280,300,"游戏结束!");getch();return;}move();};}void init_graph(){clearviewport();setlinestyle(PS_SOLID,1,5);rectangle(2,2,600,598);setlinestyle(PS_SOLID,1,1);}void set(){init_graph();outtextxy(640,50,"1、开始 / 返回");outtextxy(640,70,"2、退出");outtextxy(640,90,"3、难度");outtextxy(640,110,"4、重新开始");switch(getch()){case '1': menu();setcolor(GREEN);circle(food.x,food.y,2);setcolor(WHITE);return; case '2': exit(0);break;。

贪吃蛇游戏语言

贪吃蛇游戏语言

-
THANKS
感谢观看
XXX
贪吃蛇游戏c语言
答辩人:xxx
指导教师:xxx
-
引言
目录
游戏实现
1 引言
பைடு நூலகம்言
1
贪吃蛇游戏是一款经典的休闲 游戏,玩家需要控制一条蛇在 屏幕上移动,吃掉食物并避免
撞到墙壁或自己的身体
2
在C语言中实现贪吃蛇游 戏需要使用图形库来绘制 游戏界面和图像,以及处 理游戏逻辑和用户输入
2 游戏实现
游戏逻辑
游戏实现
游戏循环
贪吃蛇游戏的实现需要一个游戏循环,不 断更新游戏状态、渲染游戏界面和处理用 户输入。在C语言中,我们可以使用while 循环来创建游戏循环,不断检查用户的输 入并更新游戏状态,直到游戏结束
游戏实现
代码实现
以下是一个简单的贪吃蛇游戏的C语言代码实现示例
游戏实现
以下是上述代码的完整实现
游戏实现
用户输入处理
为了控制蛇的移动,我们需要从用户获取输入,并根 据输入来改变蛇的移动方向。通常,我们可以使用键 盘上的方向键来控制蛇的移动方向。在C语言中,我 们可以使用getch()函数来获取用户的按键输入
游戏实现
图形库
实现贪吃蛇游戏需要使用图形库来 绘制游戏界面和图像。在C语言中, 常用的图形库有SDL、OpenGL等。 这些图形库提供了绘制图像、处理 事件、渲染等功能,方便我们实现 贪吃蛇游戏
游戏实现
贪吃蛇游戏的核心逻辑包括以下几个部分 蛇的移动:根据用户的输入来控制蛇的移动方向, 每次移动蛇的身体会相应地增长 食物的生成:在游戏区域内随机生成食物的位置, 蛇吃到食物后身体长度增加 碰撞检测:检测蛇是否撞到墙壁或自己的身体,如 果撞到则游戏结束 分数计算:根据蛇吃到的食物数量来计算分数,并 在游戏结束时显示分数
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
typedef struct snake
{
int a;
int b;
struct snake *u;
struct snake *n;
}snake,*snake1;
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd = dd->u;
}
p.a = (p.a%15)+1;
}
else if(c == 'a' && c0 != 'd')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
{
printf("game over!!\n");
exit(0);
}
dd = dd->u;
}
if(p.a == f.a && p.b == f.b)
{
dd = dd2;
at =1;
f.a = (rand()%15+1);
f.b = (rand()%15+1);
for(i=0;i<n;i++)
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.b=(p.b%15)+1;
}
{
if(f.a == dd->a && f.b == dd->b)
{
f.a = dd2->a;
f.b = dd2->b;
break;
}
}
n++;
}
if(kbhit())
{
c = getch();
dd = dd2;
if(c == 'w' && c0 != 's')
{
if(at == 1)
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.a=(p.a%15)+1;
}
else if(c0 == 'a')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
p.b = (p.b%15)+1;
}
else
{
goto qq;
}
c0 = c;
printf("▏");
else if(i == p.a && j == p.b)
printf("■");
else if(i == f.a && j == f.b)
printf("★");
else
{
t = 0;
dd = dd2;
for(k = 0; k < n ;k++)
{
if(i == dd->a && j == dd->b)
}
fflush(stdin);
dd = &q;
_sleep(0);
}
}
dd = dd->u;
}
if(p.b == 1)
p.b = 15; ຫໍສະໝຸດ else p.b=(p.b-1)%15;
}
else if(c0 == 'd')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.a == 1)
p.a = 15;
else
p.a = (p.a-1)%15;
}
else if(c == 's' && c0 != 'w')
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
{
if(at == 1)
{
dd0 =(snake1)malloc(sizeof(snake));
dd0->a = dd2->a;dd0->b = dd2->b;
dd0->n = NULL;dd0->u = dd2;
dd2=dd0;
}
dd = dd2;
for(i = 0; i<n ; i++)
}
dd = dd2;
for(i = 0; i<n ; i++)
{
dd1 = dd->u;
dd->b = dd1->b;
dd->a = dd1->a;
dd = dd->u;
}
if(p.a == 1)
p.a = 15;
else
p.a=(p.a-1)%15;
}
else if(c0 == 's')
p.u = NULL;
p.n = &q;
p.a = 5;p.b = 6;q.a = 5;q.b = 5;
q.u = &p;q.n = NULL;
dd=dd2= &q;
f.a=(rand()%15+1);
f.b=(rand()%15+1);
while(1)
{
srand(time(NULL));
system("cls");
for(i = 0;i < 17;i ++)
{
for(j = 0; j < 17;j++)
{
if(i == 0 )
printf("▁");
else if(i == 16)
printf("▔");
else if(j == 0)
printf("▕");
else if(j == 16)
typedef struct food
{
int a;
int b;
}food;
void main()
{
char c,c0 = 'd';
int i,j,k,n=1,t,at;
相关文档
最新文档