1. 首页 > 综合百科

如何用慧编程做贪吃蛇代码

如何用慧编程做贪吃蛇代码

如何用慧编程做贪吃蛇代码

用慧编程做贪吃蛇代码过程如下:1.我们需要建立四个头文件,然后分别设置蛇的状态,上下左右,这是蛇能够有方向可走的前提,然后我们再设置蛇身的节点,定义一个简单的函数,这样蛇的全身以及他的行走方向就弄完了。2.贪吃蛇不能穿墙代码。

贪吃蛇随机生成一个食物。4.设置一个随机函数。这样贪吃蛇代码就做好了。慧编程是一款面向STEAM教育领域的积木式和代码编程软件,基于图形化编程开发。

c语言贪吃蛇代码

基本思路:蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。#include #include #include #define BEG_X2#define BEG_Y1#define WID20#define HEI20HANDLE hout;typedef enum {UP, DOWN, LEFT, RIGHT} DIR;typedef struct Snake_body{COORD pos;//蛇身的位置struct Snake_body *next;//下一个蛇身struct Snake_body *prev;//前一个蛇身}SNAKE, *PSNAKE;PSNAKE head = NULL;//蛇头PSNAKE tail = NULL;//蛇尾//画游戏边框的函数void DrawBorder(){int i, j;COORD pos = {BEG_X, BEG_Y};for(i = 0; i < HEI; ++i){SetConsoleCursorPosition(hout, pos);for(j = 0; j < WID; ++j){if(i == 0)//第一行{if(j == 0)printf("┏");else if(j == WID - 1)printf("┓");elseprintf("━");}else if(i == HEI - 1)//最后一行{if(j == 0)printf("┗");else if(j == WID - 1)printf("┛");elseprintf("━");}else if(j == 0 || j == WID - 1)//第一列或最后一列printf("┃");elseprintf("  ");}++pos.Y;}}//添加蛇身的函数void AddBody(COORD pos){PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));pnew->pos = pos;if(!head){head = tail = pnew;}else{pnew->next = head;//新创建蛇身的next指向原先的蛇头head->prev = pnew;//原先的蛇头的prev指向新创建的蛇身head = pnew;//把新创建的蛇身作为新的蛇头}SetConsoleCursorPosition(hout, head->pos);printf("◎");}//蛇身移动的函数void MoveBody(DIR dir){PSNAKE ptmp;COORD pos = head->pos;switch(dir){case UP:if(head->pos.Y > BEG_Y + 1)--pos.Y;elsereturn;break;case DOWN:if(head->pos.Y < BEG_Y + HEI - 2)++pos.Y;elsereturn;break;case LEFT:if(head->pos.X > BEG_X + 2)pos.X -= 2;elsereturn;break;case RIGHT:if(head->pos.X < BEG_X + (WID - 2) * 2)pos.X += 2;elsereturn;break;}AddBody(pos);//添加了一个新的蛇头ptmp = tail;//保存当前的蛇尾tail = tail->prev;if(tail)tail->next = NULL;SetConsoleCursorPosition(hout, ptmp->pos);printf("  ");free(ptmp);}int main(){int ctrl;DIR dir = RIGHT;//初始蛇的方向是向右的COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};system("color 0E");system("mode con cols=90 lines=30");hout = GetStdHandle(STD_OUTPUT_HANDLE);printf("    ------------贪吃蛇的移动------------");DrawBorder();//自定义几个蛇的身体AddBody(pos);pos.X += 2;AddBody(pos);pos.X += 2;AddBody(pos);pos.X += 2;AddBody(pos);pos.X += 2;AddBody(pos);pos.X += 2;AddBody(pos);pos.X += 2;AddBody(pos);//控制蛇的移动while(ctrl = getch()){switch(ctrl){case 'w':if(dir == DOWN)continue;dir = UP;break;case 's':if(dir == UP)continue;dir = DOWN;break;case 'a':if(dir == RIGHT)continue;dir = LEFT;break;case 'd':if(dir == LEFT)continue;dir = RIGHT;break;case 'q':return 0;}MoveBody(dir);}return 0;}扩展资料:实现逻辑1,可以设置光标,就能实现制定位置打印制定符号。

3,结构体串联形成链表,遍历获取成员坐标,打印符号得到蛇身。4,不断的加头,去尾,重新遍历坐标,再打印形成蛇的移动。5,食物产生的位置判定,不能越界,也不能与蛇身体重合。6,蛇的转向判定,一条规则,不允许倒退。

7,转向的实现,跟行进方向决定新的关节坐标(当前头的上下左右)8,死亡检测,是否头节点坐标是否与墙壁重合,是否与身体其他关节重合。9,加速减速,设置刷新休眠时间实现。

C语言的贪吃蛇源代码

 /int gamespeed=50000;struct Food{ int x; int y; int yes;}food;struct Snake{ int x[N]; int y[N]; int node; int direction; int life;}snake;void Init(void);void Close(void);void DrawK(void);void GameOver(void);void GamePlay(void);void PrScore(void);void main(void){ Init(); DrawK(); GamePlay(); Close();}void Init(void){ int gd=DETECT,gm; initgraph(&gd,&gm,"c:tc"); cleardevice();}void DrawK(void){ setcolor(11); setlinestyle(SOLID_LINE,0,THICK_WIDTH); for(i=50;i<=600;i+=10) { rectangle(i,40,i+10,49); rectangle(i,451,i+10,460); } for(i=40;i<=450;i+=10) { rectangle(50,i,59,i+10); rectangle(601,i,610,i+10); }}void GamePlay(void){ randomize(); food.yes=1; snake.life=0; snake.direction=1; snake.x[0]=100;snake.y[0]=100; snake.x[1]=110;snake.y[1]=100; snake.node=2; PrScore(); while(1) { while(!kbhit()) { if(food.yes==1) { food.x=rand()%400+60; food.y=rand()%350+60; while(food.x%10!=0) food.x++; while(food.y%10!=0) food.y++; food.yes=0; } if(food.yes==0) { setcolor(GREEN); rectangle(food.x,food.y,food.x+10,food.y-10); } for(i=snake.node-1;i>0;i--) { snake.x[i]=snake.x[i-1]; snake.y[i]=snake.y[i-1]; } switch(snake.direction) { case 1:snake.x[0]+=10;break; case 2: snake.x[0]-=10;break; case 3: snake.y[0]-=10;break; case 4: snake.y[0]+=10;break; } for(i=3;i595||snake.y[0]<55|| snake.y[0]>455) { GameOver(); snake.life=1; } if(snake.life==1) break; if(snake.x[0]==food.x&&snake.y[0]==food.y) { setcolor(0); rectangle(food.x,food.y,food.x+10,food.y-10); snake.x[snake.node]=-20;snake.y[snake.node]=-20; snake.node++; food.yes=1; score+=10; PrScore(); } setcolor(4); for(i=0;i

贪吃蛇代码

求用C++写的贪吃蛇代码

C++贪吃蛇代码问题

本文由“血色素人”发布,不代表“写客百科”立场,转载联系作者并注明出处:https://www.ixieke.com/zonghe/171246.html


联系我们

在线咨询: 点击这里给我发消息

工作日:9:30-18:30,节假日休息