贪吃蛇源代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
int x[50]={1,1,1,1};//初始蛇坐标 x 
int y[50]={1,1,1,1};//初始蛇坐标 y 
int n=3;
int t=0;
int score=0;
unsigned  int x1=0;//初始食物坐标 x 
unsigned  int y1=0;//初始食物坐标 y 
void action_y();
void action_x();
void gotoxy();
void paint();
void no_text_cursor();
void snake_birth();
void snake_action();
void array_w();
void array_s();
void array_a();
void array_d();
void game_report();
void (*pfun)(void)=array_d;//移动函数指针控制移动 
int main(void)
{
no_text_cursor();//隐藏光标 
snake_birth();//诞生 
for(int i=0;i<1;)//不断绘制界面 
{
paint();
game_report();
pfun();
snake_action();
gotoxy(0,0);//将光标移动至(0,0)。 
}
return 0;

void paint()//绘制界面函数 
{


if(t%100==0)//让食物定时存在 
{
srand(time(NULL));
x1=1+rand()%18;//随机产生食物的坐标 x 
y1=1+rand()%18;//随机产生食物的坐标 y 
}
    system("color 12");//修改颜色为蓝底绿字 
for(unsigned int i=0;i<20;i++)//绘制游戏界面,i为纵向 ,k为横向 
{
for(unsigned int k=0;k<20;k++)
{
if(i==0||i==19)
{
printf("█");
}
else
{
if(k==0||k==19)
{
printf("█");
}else
{
if(i==y1&&k==x1)
{
printf("○");
}
else
{
  char o=0;
  char time=0;
      for(int j=0;j<=n;j++)
  {
  if(x[j]==k&&y[j]==i)
  {
  if(j==0)
  {
  if(time==0)
  {
  printf("★");
  time++;
  o=1;
  }
  } 
  else
  {
  if(time==0)
  {
  printf("●");
  time++;
  o=1;
    }
  }
          }
}
if(o==0){
printf("  ");
}
}
}
}
}
printf("\n");
}
printf("贪吃蛇三代 控制键:WASD\n");
printf("蛇头坐标%2d,%2d 食物坐标%2d,%2d 节数%2d\n",x[0],y[0],x1,y1,n+1); 
t++;
}
void snake_birth()//初始化函数 
{
x[0]=4;x[1]=3;x[2]=2;x[3]=1;
y[0]=1;y[1]=1;y[2]=1;y[3]=1;
}
void snake_action()//键盘检测函数 
{
char ch=0;
if(kbhit())
{
ch=getch();
switch(ch)
{
case 'd':
if(x[0]!=18)
{
pfun=array_d; 
        }
break;
case 'a':
if(x[0]!=1)
    {
pfun=array_a;
        }
    break;
case 'w':
if(y[0]!=1)
{
pfun=array_w;
    }
break;
case 's':
if(y[0]!=18)
{
pfun=array_s;
    }
break;
}
}
}
void array_w()//上移函数 
{
if(y[0]!=1)
{
action_y();
y[0]--;
action_x();
}
}
void array_s()//下移函数 
{
if(y[0]!=18)
{
action_y();
y[0]++;
action_x();
}
}
void array_a()
{
if(x[0]!=1)
{
action_x();
x[0]--;
action_y();

}
void array_d()
{
if(x[0]!=18)
{

action_x();
x[0]++;
action_y();
}
}
void action_y()
{
for(int i=n;i>=1;i--)
{
y[i]=y[i-1];
}
}
void action_x()
{
for(int i=n;i>=1;i--){
x[i]=x[i-1];
}
}
void game_report()//游戏报告函数用于计算得分处理游戏结果。 
{
if((x[0]==x1&&y[0]==y1)||(x[1]==x1&&y[1]==y1)||(x[2]==x1&&y[2]==y1)||(x[3]==x1&&y[3]==y1))
{
if(n<49)
{
x[++n]=x[n-1];y[n]=y[n-1]; 
}
score+=10;
x1=0;y1=0;

}
printf("YOUR SCORE:%d ",score);
}
void no_text_cursor(void) //隐藏光标函数 
{
  CONSOLE_CURSOR_INFO cursor_info = {1, 0}; 
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)//移动光标函数 
{
    COORD coord = {x, y};   
    /*COORD是Windows API中定义的一种结构,表示一个字符在控制台屏幕上的坐标。其定义为:


    typedef struct _COORD {


    SHORT X; // horizontal coordinate


    SHORT Y; // vertical coordinate
    } COORD;*/
    
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

猜你喜欢

转载自blog.csdn.net/qq_41105058/article/details/80068651