基于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 7181
struct 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;
   break;
    }
 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)      /***********是否开始随机产生***************/
  while(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;
  }
    else
  p=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:jiangzhiliang002@tom.com");
  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;
  }
   else
  p5=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;
}
(0)

相关推荐

  • C语言链表实现贪吃蛇游戏

    阅读学习了源代码,并做了简单的注释和修改,里面只用了链表数据结构,非常适合C语言入门者学习阅读. 程序可在VS2013下编译运行. #include<stdio.h> #include<time.h> #include<windows.h> #include<stdlib.h> #define U 1 #define D 2 #define L 3 #define R 4 //蛇的状态,U:上 :D:下:L:左 R:右 typedef struct SNAK

  • C语言结构数组实现贪吃蛇小游戏

    一.设计思路 蛇身本质上就是个结构数组,数组里存储了坐标x.y的值,再通过一个循环把它打印出来,蛇的移动则是不断地刷新重新打印.所以撞墙.咬到自己只是数组x.y值的简单比较. 二.用上的知识点 结构数组Windows API函数 三.具体实现 先来实现静态页面,把地图.初始蛇身.食物搞定. 这里需要用到Windows API的知识,也就是对控制台上坐标的修改 //这段代码来自参考1 void Pos(int x, int y) { COORD pos; HANDLE hOutput; pos.X

  • 基于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 #defi

  • JS实现的贪吃蛇游戏完整实例

    本文实例讲述了JS实现的贪吃蛇游戏.分享给大家供大家参考,具体如下: 思想: 1.设计蛇:属性有宽.高.方向.状态(有多少节),方法:显示,跑 2.设计食物:属性宽.高 3.显示蛇:根据状态向地图里加元素 4.蛇跑起来:下一节到前一节的位置,蛇头根据方向变,删除原来的蛇,新建蛇:当出界时,死亡,初始化:当蛇头吃到自己的时候,死亡,初始化 5.食物被吃掉,蛇加一节,去掉原来的食物,生成新的食物 6.添加定时器,绑定按键 完整示例: <!doctype html> <html lang=&q

  • C语言循环链表实现贪吃蛇游戏

    本文实例为大家分享了C语言表实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 总体思想 利用循环链表将一条蛇的坐标进行存储,然后利用gotoxy()函数(可以将光标定位到指定的位置),此时根据蛇的坐标进行输出"@",输出多几个既可以产生一条蛇.通过遍历循环链表进行蛇的移动,对循环链表的插入元素,产生蛇变长的效果.下面为各功能实现的函数 1.贪吃蛇地图函数map() 2.蛇的移动move(),up(),left()等函数 3.产生食物food()和吃到食物eat_food() 4.蛇吃

  • C语言开发实现贪吃蛇游戏

    本文实例为大家分享了C语言实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 1.最好用VS运行 2.用到的函数有:_kbhit _getch EasyX图形库内一系列函数 3.蛇身与食物用矩形画的 代码如下: #include<stdio.h> #include<graphics.h> //easyx头文件 #include<conio.h> #include<time.h> /********************设置各种属性*************

  • 基于Pygame实现简单的贪吃蛇游戏

    目录 导入相关的包 设置屏幕大小以及基本参数 设置贪吃蛇的位置,以及移动的大小 绘制蛇 让蛇动起来 实现贪吃蛇拐弯 实现随机食物 吃食物 完整代码  导入相关的包 import pygame, sys, random from pygame.locals import * 设置屏幕大小以及基本参数 设置屏幕大小为400*400,mainClock = pygame.time.Clock()用来设置时间同步,不会根据计算机的运行来决定运行多少次, mainClock.tick(1) 一秒只会运行一

  • C语言实现双人贪吃蛇游戏实例代码

    贪吃蛇双人小游戏,每局游戏两分钟,死亡则直接失败,若时间结束,则分高者获胜.   上源代码: ​ #include <stdio.h> #include <stdlib.h> #include <Windows.h> #include <time.h> #include<stdbool.h> #include <conio.h> #define SNAKESIZE 100 #define MAPWIDTH 118 #define MA

  • C语言实现简单的贪吃蛇游戏的示例代码

    目录 运行效果 代码 一个简单的贪吃蛇游戏本来代码就不多,在保证可读性的情况下,很容易就控制在100以内了. 运行效果 代码 #include <Windows.h> #include <stdio.h> #include <conio.h> #include <time.h> #define PANIC(err) (fprintf(stderr,"PANIC Line %d : %s",__LINE__,err),exit(-1),1)

  • Python实现智能贪吃蛇游戏的示例代码

    目录 前言 基本环境配置 实现效果 实现代码 前言 我想大家都玩过诺基亚上面的贪吃蛇吧,本文将带你一步步用python语言实现一个snake小游戏. 基本环境配置 版本:Python3 系统:Windows 相关模块:pygame pip install pygame安装即可 实现效果 实现代码 import random, pygame, sys from pygame.locals import * import time ''' ''' FPS = 1 ##WINDOWWIDTH = 64

  • Java实现贪吃蛇游戏的示例代码

    目录 项目演示 项目实战 1. 游戏的主启动类 2. 游戏的面板 3. 数据中心 4. 绘制静态面板 5. 绘制静态小蛇 6. 绘制动态小蛇 7. 设置游戏状态 8. 让蛇动起来 9. 绘制食物布局 10. 游戏失败判定 11. 积分获取系统 12. 游戏优化 项目演示 项目演示地址 项目实战 1. 游戏的主启动类 作为贪吃蛇游戏的主启动类,构建了顶级窗口,可以容纳各种面板, package Snake; import javax.swing.*; /** * 游戏的主启动类 */ public

  • JavaScript TypeScript实现贪吃蛇游戏完整详细流程

    目录 项目背景及简介 多模块需求分析 场景模块需求 食物类模块需求 记分牌模块需求 蛇类模块需求 控制模块需求 项目搭建 ts转译为js代码 package.json包配置文件 webpack.config.js打包工具配置 项目结构搭建 html文件 css文件(这里使用的是less) 项目页面 多模块搭建 完成Food(食物)类 完成ScorePanel(记分牌)类 完成Snake(蛇)类 完成GameControl(控制)类 完成index类(启动项目) 项目启动 总结 项目背景及简介 t

随机推荐