×

windows.h贪吃蛇程序

windows.h贪吃蛇程序(microbit贪吃蛇程序)

admin admin 发表于2023-04-06 20:47:09 浏览59 评论0

抢沙发发表评论

本文目录一览:

如何用visualC++6.0编写一个贪吃蛇程序

#ifndef __COLORCONSOLE__H__

#define __COLORCONSOLE__H__

#include windows.h

#include iostream

#include conio.h

#include time.h

using namespace std;

HANDLE initiate();

BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString);

#endif

#ifndef __FOOD__H__ //防止重复引用该头文件

#define __FOOD__H__

#include "colorConsole.h"

class Food

{

public:

void initfood(int); //出始化食物函数

protected:

WORD goodcolor[1]; //定义正常食物的颜色

WORD badcolor[1]; //定义变质食物的颜色

int numgood,goodx[50],goody[50]; //定义正常食物的个数、坐标

int numbad,badx[30],bady[30]; //定义变质食物的个数、坐标

};

#endif

#ifndef __FOOD__H__ //防止重复引用该头文件

#define __FOOD__H__

#include "colorConsole.h"

class Food

{

public:

void initfood(int); //出始化食物函数

protected:

WORD goodcolor[1]; //定义正常食物的颜色

WORD badcolor[1]; //定义变质食物的颜色

int numgood,goodx[50],goody[50]; //定义正常食物的个数、坐标

int numbad,badx[30],bady[30]; //定义变质食物的个数、坐标

};

#endif

#ifndef __SNAKE__H__ //防止重复引用该头文件

#define __SNAKE__H__

#include "colorConsole.h"

class Snake

{

public:

void initsnake(); //初始化蛇的函数

protected:

WORD bColor[1]; //定义蛇身的颜色

WORD hColor[1]; //定义蛇头的颜色

int headx,heady,bodyx[100],bodyy[100]; //定义蛇头和蛇身的坐标

int node; //定义蛇身的节数

};

#endif

#include "colorConsole.h"

HANDLE initiate()

{

HANDLE hOutput;

hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

return hOutput;

}

BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString)

{

DWORD cWritten;

BOOL fSuccess;

COORD coord;

coord.X = x; // start at first cell

coord.Y = y; // of first row

fSuccess = WriteConsoleOutputCharacter(

hOutput, // screen buffer handle

lpszString, // pointer to source string

lstrlen(lpszString), // length of string

coord, // first cell to write to

cWritten); // actual number written

if (! fSuccess)

cout"error:WriteConsoleOutputCharacter"endl;

for (;fSuccess coord.X lstrlen(lpszString)+x; coord.X += nColors)

{

fSuccess = WriteConsoleOutputAttribute(

hOutput, // screen buffer handle

wColors, // pointer to source string

nColors, // length of string

coord, // first cell to write to

cWritten); // actual number written

}

if (! fSuccess)

cout"error:WriteConsoleOutputAttribute"endl;

return 0;

}

#include "Food.h"

HANDLE handle;

void Food::initfood(int lev) //初始化食物

{

numgood=lev*5;

numbad=lev*2;

goodcolor[0]=FOREGROUND_RED|FOREGROUND_BLUE; //正常食物的颜色

badcolor[0]=FOREGROUND_RED|FOREGROUND_GREEN; //变质食物的颜色

srand((unsigned) time(NULL));

textout(handle,64,6,goodcolor,1,"正常食物为★"); //输出图例

textout(handle,64,8,badcolor,1,"变质食物为★");

for(int fgood=0;fgoodnumgood;fgood++) //输出正常食物

{

goodx[fgood]=((rand()%55+4)/2)*2;

goody[fgood]=rand()%26+4;

textout(handle,goodx[fgood],goody[fgood],goodcolor,1,"★");

}

for(int fbad=0;fbadnumbad;fbad++) //输出变质食物

{

badx[fbad]=((rand()%56+4)/2)*2;

bady[fbad]=rand()%26+4;

textout(handle,badx[fbad],bady[fbad],badcolor,1,"★");

}

}

#include "Game.h"

HANDLE handle;

Game::Game()

{

node=3; //蛇的出始默认节数

headx=44,heady=4; //蛇头的出始默认坐标

controlx=-2,controly=0; //蛇的出始默认方向为向左

start=0;

scores=0;

self=0,wall=0,died=0; //游戏结束条件默认为否

sColor[0]=FOREGROUND_GREEN|FOREGROUND_INTENSITY; //围墙颜色

head[0]=FOREGROUND_RED|FOREGROUND_INTENSITY; //开始界面文字颜色

}

int Game::starting()

{

while(1)

{

textout(handle,22,16,head,1,"控制键: W-上 S-下 A-左 D-右"); //开始界面

textout(handle,16,18,head,1,"吃完所有正常食物升级 千万不要吃到变质的食物哦!");

textout(handle,30,20,head,1, "选择级数 1-9 ");

if(_kbhit())

{

int v=_getch(); //选择级数

switch(v)

{

case 49:

level=1;

break;

case 50:

level=2;

break;

case 51:

level=3;

break;

case 52:

level=4;

break;

case 53:

level=5;

break;

case 54:

level=6;

break;

case 55:

level=7;

break;

case 56:

level=8;

break;

default:

level=9;

break;

}

textout(handle,16,18,head,1," "); //清屏

textout(handle,30,20,head,1, " ");

textout(handle,22,16,head,1," ");

break;

}

}

//设置四周围墙

for(int wideup=2;wideup60;wideup+=2) //上边

{

textout(handle,wideup,2,sColor,1,"■");

}

for(int widedown=2;widedown60;widedown+=2) //下边

{

textout(handle,widedown,30,sColor,1,"■");

}

for(int longr=2;longr30;longr++) //左边

{

textout(handle,2,longr,sColor,1,"■");

}

for(int longl=2;longl31;longl++) //右边

{

textout(handle,60,longl,sColor,1,"■");

}

levelup=5*level;

return level;

}

void Game::ifstart() //由用户判断是否开始游戏

{

textout(handle,24,0,sColor,1,"按 y 开始游戏");

int v=_getch();

switch(v)

{

case 'y': start=1;break;

default : start=0;break;

}

textout(handle,24,0,sColor,1," "); //清屏

}

bool Game::getstart() //获得start

{

return start;

}

void Game::moventurn() //关于移动和转向的函数

{

int len1=0; //设置一个消除原有图形的计数器

for(int len=0;lennode;len++) //消除原有图形

{

textout(handle,bodyx[len],bodyy[len],bColor,1," ");

}

for(int len6=node-1;len60;len6--) //显示移动以后的图形

{

len1=len6-1;

bodyx[len6]=bodyx[len1]; //获得前一节body的坐标

bodyy[len6]=bodyy[len1];

textout(handle,bodyx[len6],bodyy[len6],bColor,1,"●"); //移动后的图形

}

bodyx[0]=headx;

bodyy[0]=heady;

textout(handle,bodyx[0],bodyy[0],bColor,1,"●"); //显示移动后的第一节

headx+=controlx;

heady+=controly;

textout(handle,headx,heady,hColor,1,"◎"); //显示移动后的头部

Sleep(165-level*17); //速度

if(kbhit()) //转向

{

char direction=getch(); //获取方向

if(direction=='w'direc!='s')

{

textout(handle,headx,heady,hColor,1," ");

textout(handle,headx,heady,hColor,1,"◎");

controlx=0;

controly=-1;

direc='w';

}

if(direction=='s'direc!='w')

{

textout(handle,headx,heady,hColor,1," ");

textout(handle,headx,heady,hColor,1,"◎");

controlx=0;

controly=1;

direc='s';

}

if(direction=='a'direc!='d')

{

textout(handle,headx,heady,hColor,1," ");

textout(handle,headx,heady,hColor,1,"◎");

controlx=-2;

controly=0;

direc='a';

}

if(direction=='d'direc!='a')

{

textout(handle,headx,heady,hColor,1," ");

textout(handle,headx,heady,hColor,1,"◎");

controlx=2;

controly=0;

direc='d';

}

}

}

bool Game::ifgameover() //判断是否结束游戏

{

if(headx==2||headx==60||heady==2||heady==30) //撞墙

{

wall=1;

return 1;

}

for(int sbody=1;sbodynode;sbody++) //咬到自己

{

if(headx==bodyx[sbody]heady==bodyy[sbody])

{

self=1;

return 1;

}

}

for(int countbad=0;countbadnumbad;countbad++) //吃到变质食物

{

if(headx==badx[countbad]heady==bady[countbad])

{

died=1;

return 1;

}

}

return 0;

}

void Game::gameover() //输出游戏结束界面

{

//sndPlaySound("DOWN.wav",SND_ASYNC);

if(wall==1)

textout(handle,20,14,sColor,1,"撞到墙了 游戏结束");

if(self==1)

textout(handle,20,14,sColor,1,"咬到自己了 游戏结束");

if(died==1)

textout(handle,19,14,sColor,1,"吃到变质食物了 游戏结束");

}

void Game::scorenlevelup()

{

char marks[5],Level[5]; //准备输出级数和分数

for(int eatgood=0;eatgoodnumgood;eatgood++) //判断是否咬到正常食物以加分

{

if(headx==goodx[eatgood]heady==goody[eatgood])

{

//sndPlaySound("LASER.wav",SND_ASYNC);

goodx[eatgood]=74; //防止以后走到已吃过食物点的时候会判断为吃到而加分以影响升级

goody[eatgood]=6;

node++; //长一截

scores+=10; //加十分

itoa(scores,marks,10); //转换数据类型,准备输出

itoa(level,Level,10);

textout(handle,64,11,hColor,1,"分数:"); //输出分数

textout(handle,70,11,hColor,1,marks);

textout(handle,64,13,hColor,1,"级数:"); //输出级数

textout(handle,70,13,hColor,1,Level);

levelup--;

if(levelup==4)

{

for(int fgood=0;fgoodnumgood;fgood++) //再次输出正常食物

{

textout(handle,goodx[fgood],goody[fgood],goodcolor,1,"★");

}

}

}

}

if(levelup==0) //升级

{

level++;

levelup=5*level;

for(int i=4;i58;i+=2) //清屏

{

for(int j=4;j30;j++)

{

textout(handle,i,j,sColor,1," ");

}

}

initfood(level); //输出食物

textout(handle,headx,heady,hColor,1,"◎"); //打印蛇头

for(int j=0;jnode;j++) //打印蛇身

{

textout(handle,bodyx[j],bodyy[j],bColor,1,"●");

}

textout(handle,18,0,hColor,1,"按任意键开始更高的等级");

itoa(level,Level,10);

textout(handle,70,13,hColor,1,Level); //输出新的级数

while(1) //由用户控制是否开始更高等级

{

if (_kbhit())

{

textout(handle,18,0,sColor,1, " ");

break;

}

}

}

}

#include "Snake.h"

extern HANDLE handle;

void Snake::initsnake() //初始化蛇

{

hColor[0]=FOREGROUND_RED|FOREGROUND_INTENSITY; //蛇头颜色

bColor[0]=FOREGROUND_BLUE|FOREGROUND_INTENSITY; //蛇身颜色

for(int i=0;inode;i++) //蛇身各节的坐标定义

{

bodyx[i]=(i+1)*2+headx;

bodyy[i]=heady;

}

textout(handle,headx,heady,hColor,1,"◎"); //打印蛇头

for(int j=0;jnode;j++) //打印蛇身

{

textout(handle,bodyx[j],bodyy[j],bColor,1,"●");

}

}

#include "Snake.h"

#include "Food.h"

#include "Game.h"

Game game;

HANDLE handle; //窗口句柄

void main()

{

//sndPlaySound("BACKGROUND.wav",SND_LOOP|SND_ASYNC);

handle=initiate(); //初始化窗口句柄

game.initfood(game.starting()); //初始化食物

game.initsnake(); //初始化蛇

game.ifstart(); //玩家是否开始游戏

if(game.getstart())

{ while(1)

{

game.moventurn(); //蛇的移动和转向

game.scorenlevelup(); //得分和升级

if(game.ifgameover()) //判断是否中止游戏

break;

}

game.gameover(); //输出游戏结束原因

}

}

这是一个控制台的贪吃蛇 比较简单

只要楼主了解几个API函数就可以 至于算法 有好多 我这个不怎么样

我建议用链表来表示蛇体 下面是我的一个snake 类 函数体没给 楼主自己想吧

struct node

{node* pre;

Cpoint point;

node* next;

};

node::node()

{ point(0,0);

pre=NULL;

next=NULL;

};

class snake

{ private:

node* snakehead;

node snakebody[100];

int length

int direction;

public:

void go();

bool fail();

void textout();

} ;

贪吃蛇为什么需要包含windows.h

Win32程序的开头都需要。

WINDOWS.H是一个最重要的头文件,它包含了其他Windows头文件,这些头文件的某些也包含了其他头文件。这些头文件中最重要的和最基本的是:

WINDEF.H 基本数据类型定义。

WINNT.H 支持Unicode的类型定义。

WINBASE.H Kernel(内核)函数。

WINUSER.H 用户界面函数。

WINGDI.H 图形设备接口函数。

c语言贪吃蛇的速度控制程序怎么编

可以Sleep函数设置延时来控制贪吃蛇的速度。

函数名: Sleep

功 能: 执行挂起一段时间

用 法: void Sleep(DWORD dwMilliseconds);

在VC中使用带上头文件

#include windows.h

( Sleep函数存放头文件:WinBase.h)

在gcc编译器中,使用的头文件因gcc版本的不同而不同

#include unistd.h

Sleep()单位为毫秒,sleep()单位为秒(如果需要更精确可以用usleep单位为微秒)

返回值

若进程/线程挂起到参数所指定的时间则返回0,若有信号中断则返回剩余秒数。

例程:

/ *--------------------snake.h --------------------*/

#ifndef SNAKE_H

#define SNAKE_H

 

#define LEFT 'a'

#define RIGHT 'd'

#define DOWN 's'

#define UP 'w'

#define ESC 27

 

#define N 200               /*蛇的最大长度*/

 

char key;                   /*控制按键*/

 

struct Food

{

     int x;                   /*食物的横坐标*/

     int y;                   /*食物的纵坐标*/

     int yes;                 /*判断是否要出现食物的变量*/

}food;                        /*食物的结构体*/

 

struct Snake

{

     int x[N];

     int y[N];

     int node;                /*蛇的节数*/

     int direction;           /*蛇移动方向*/

     int life;                /* 蛇的生命,0活着,1死亡*/

}snake;

#endif

/ *--------------------snake.c --------------------*/

#include graphics.h

#include stdlib.h

#include conio.h

#include time.h

#include stdio.h

#include "snake.h"

 

int score = 0;

int gamespeed = 100;                //蛇运行速度

 

static void Init(void);             /*图形驱动*/

static void Close(void);            /*图形结束*/

static void Game_interface(void);   /*游戏界面*/

static void GameOver(void);         /*结束游戏*/

static void GamePlay(void);         /*游戏过程*/

static void PrScore(void);          /*输出成绩*/

 

/*主函数*/

int main(void)

{

     Init();                

     Game_interface();                

     GamePlay();             

     Close();                

     return 0;

}

 

/*图形驱动*/

static void Init(void)

{

    int gd=9,gm=2;

 

    initgraph(gd,gm," ");

    cleardevice();

}

 

/* 开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙 */

static void Game_interface(void)

{

     int i;

 

     setcolor(LIGHTCYAN);                           /*setbkcolor(LIGHTGREEN);*/

     setlinestyle(PS_SOLID,0,1);                    /*设置线型*/

     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);                 /*右边框*/

     }

}

 

/* 游戏主函数 */

static void GamePlay(void)

{

     int i;

      

     srand(time(NULL));                             /*随机数发生器*/

     food.yes = 1;                                  /*1表示需要出现新食物,0表示已经存在食物*/

     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)                                       /*可以重复玩游戏,压ESC键结束*/

     {

         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;i0;i--)          /*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/

             {

                 snake.x[i] = snake.x[i-1];

                 snake.y[i] = snake.y[i-1];

             }

             /*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/

             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;isnake.node;i++)

             {

                 if((snake.x[i] == snake.x[0])  (snake.y[i] == snake.y[0]))

                 {

                     GameOver();                  /*显示失败*/

                     snake.life = 1;

                     break;

                 }

             }

             if((snake.x[0]  55) || (snake.x[0]  595) || (snake.y[0]  55) || (snake.y[0]  455))     /*蛇是否撞到墙壁*/-windows.h贪吃蛇程序

             {

 

 

                 GameOver();                                            /*本次游戏结束*/

                 snake.life = 1;                                        /*蛇死*/

             }

             if(snake.life == 1)                                        /*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/-windows.h贪吃蛇程序

                 break;

             if((snake.x[0] == food.x)  (snake.y[0] == food.y))       /*吃到食物以后*/

             {

                 setcolor(BLACK);                                       /*把画面上的食物东西去掉*/

                 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(RED);                         /*画出蛇*/

 

 

             for(i=0;isnake.node;i++)

                 rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);

             Sleep(gamespeed);                        /*用延迟控制贪吃蛇速度*/                

             setcolor(BLACK);                        /*用黑色去除蛇的的最后一节*/

             rectangle(snake.x[snake.node-1],snake.y[snake.node-1],

                       snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);

         }        /*endwhile(!kbhit)*/

          

         if(snake.life == 1)                        /*如果蛇死就跳出循环*/

             break;

          

         key=getch();                          /*接收按键*/

         if (key == ESC) break;                  /*按ESC键退出*/

          

         switch(key)

         {                                

             case UP:

                 if(snake.direction != 4)           /*判断是否往相反的方向移动*/

                     snake.direction = 3;

                 break;

             case RIGHT:

                 if(snake.direction != 2)

                     snake.direction = 1;

                 break;

             case LEFT:

                 if(snake.direction != 1)

                     snake.direction = 2;

                 break;

             case DOWN:

                 if(snake.direction != 3)

                     snake.direction = 4;

                 break;

         }

 

     }/*endwhile(1)*/

}

 

/*游戏结束*/

static void GameOver(void)

{

     cleardevice();

     PrScore();

     setcolor(RED);

     setfont(56,0,"黑体");

     outtextxy(200,200,"GAME OVER");

     getch();

}

 

/*输出成绩*/

static void PrScore(void)

{

     char str[10];

 

     setfillstyle(YELLOW);

     bar(50,15,220,35);

     setcolor(BROWN);

     setfont(16,0,"宋体");

     sprintf(str,"score:%d",score);

     outtextxy(55,16,str);

}

 

static void Close(void)

{

     closegraph();

}

c语言 贪吃蛇 程序

基本思路:

蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。

#include stdio.h

#include conio.h

#include windows.h

#define BEG_X 2

#define BEG_Y 1

#define WID 20

#define HEI 20

HANDLE 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("┓");

else

printf("━");

}

else if(i == HEI - 1)//最后一行

{

if(j == 0)

printf("┗");

else if(j == WID - 1)

printf("┛");

else

printf("━");

}

else if(j == 0 || j == WID - 1)//第一列或最后一列

printf("┃");

else

printf("  ");

}

++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;

else

return;

break;

case DOWN:

if(head-pos.Y BEG_Y + HEI - 2)

++pos.Y;

else

return;

break;

case LEFT:

if(head-pos.X BEG_X + 2)

pos.X -= 2;

else

return;

break;

case RIGHT:

if(head-pos.X BEG_X + (WID - 2) * 2)

pos.X += 2;

else

return;

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,可以设置光标,就能实现制定位置打印制定符号。

2,涉及一个结构体,包含两个元素坐标元素和一个结构体指针。

3,结构体串联形成链表,遍历获取成员坐标,打印符号得到蛇身。

4,不断的加头,去尾,重新遍历坐标,再打印形成蛇的移动。

5,食物产生的位置判定,不能越界,也不能与蛇身体重合。

6,蛇的转向判定,一条规则,不允许倒退。

7,转向的实现,跟行进方向决定新的关节坐标(当前头的上下左右)

8,死亡检测,是否头节点坐标是否与墙壁重合,是否与身体其他关节重合。

9,加速减速,设置刷新休眠时间实现。

参考资料来源:百度百科-C语言