vc++6.0贪吃蛇

vc++6.0贪吃蛇

 图片

#include<stdio.h>

#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
//
int m=0;
//int flag=0;
char ch = 's', di;
int score = 0;  //分数
long gamespeed =100 ;//游戏速度
int a[400][400];//蛇身长度
struct Food    //食物
{
    int X;     //食物坐标
    int Y;
    int yes;   //判断食物是否被干掉
}food;
struct Snake   //定义蛇的结构体
{
    int fx[205];//蛇身子数组储存各节所在的坐标
    int fy[205];
    int node;  //蛇的节数
    int life;  //设置蛇的死活
}snake;
//
int map();
void gamePlay();
//
int map()
{
 system("cls");
 int i,j,k=0;

 for(i=0;i<20;i++)
 { 
  printf("/t/t");
  for(j=0;j<20;j++)
  {
   int flag = 0;
   //flag
   
  
   
    for(int z=0;z<snake.node;z++)
    {
     if(i==snake.fx[z]&&j==snake.fy[z])
     {
      if(z==0)
       printf("¤");
      else
       printf("Θ");
      flag = 1;
      break;
     }
    } 
   
  
   if (flag == 1)
   {
    continue;
   }
   if(i==0)
   {
    printf("劉");
   }
   else if(i==19)
   {
    printf("澎");
   }
   else if(i!=0&&i!=19&&j==0)
    printf("鑄");
   else if(i!=0&&i!=19&&j==19)
    printf("鑄");//▕
   else if(i==food.X&&j==food.Y&&food.yes==0)
    printf("★");
   else
    printf("  ");
  }
  printf("/n");
 }
 _sleep(gamespeed);
 printf("/t/t/t/t/t/t/t得分:%d/n",score);
 return 0;
}
void gamePlay()

 int n;
 food.yes = 1;
 snake.life = 0; //蛇是活着的
    snake.fx[0] = 10;//前三节蛇的坐标
    snake.fy[0] = 10;
    snake.fx[1] = 11;
    snake.fy[1] = 10;
    snake.fx[2] = 12;
    snake.fy[2] = 10;
 snake.node = 3;
 srand((int)time(NULL));;
  
 while(1)
 { 
   if(food.yes==1)//食物出现随机出现位置
   {  
    m=0;
    while (m==0)
    {
     food.X=rand()%(18-1+1)+1; 
     food.Y=rand()%(18-1+1)+1;
     for(int f=0;f<snake.node;f++)//判断食物是否出现在蛇身,有则返回从先产生随机
     {
      if(food.X==snake.fx[f]&&food.Y==snake.fy[f])
      {
       m=0;
       break;
      }
      else
      {
       m=1;
      }
     }
    }
    food.yes = 0;
   }
   di = ch;
   if(kbhit())//控制 接受
   ch = getch();//接受 方向键 
   if(di =='w' && ch == 's' )
    ch = 'w';
   else if(di == 'a'&&ch == 'd')
    ch = 'a';
   else if(di == 'd'&&ch == 'a')
    ch = 'd';
   else if(di == 's'&&ch == 'w')
    ch = 's';
   else if(di =='W' && ch == 'S' )
    ch = 'W';
   else if(di == 'A'&&ch == 'D')
    ch = 'A';
   else if(di == 'D'&&ch == 'A')
    ch = 'D';
   else if(di == 'S'&&ch == 'W')
    ch = 'S';
   else if(di == 's'&&ch == 'S')
    ch = 's';     //以上是控制蛇不能走相反的方向
   
   if(ch=='w'||ch=='W')
   {
    snake.fx[0] = (snake.fx[0]-1);
   }
   if(ch=='s'||ch == 'S')
   {
    snake.fx[0] = (snake.fx[0]+1);
   }
   if(ch=='a'||ch == 'A')
   {
    snake.fy[0] = (snake.fy[0]-1);

   }
   if(ch=='d'||ch == 'D')
   {
    snake.fy[0] = (snake.fy[0]+1);
   }          //蛇转弯的控制
   if(snake.fx[0]==0||snake.fx[0]==19||snake.fy[0]==0||snake.fy[0]==19)//是否撞墙
   {
    snake.life=1; //蛇死
    
   }
         for(int i=3;i<snake.node;i++)//判断蛇是否撞到自己
   {
    if(snake.fx[i]==snake.fx[0]&&snake.fy[i]==snake.fy[0])
    {
     snake.life=1;
     break;
    }
    }
   if(snake.fx[0]==food.X && snake.fy[0]==food.Y)
   {
    snake.node++;  //蛇增加一节                      
    food.yes=1;  //食物没有了
    score+=5;//分数增加
    if(gamespeed>=0)
    {
     gamespeed-=2;//速度提高两个单位
    }
   }
   if(snake.life==1)
   {
    printf("/n/t/t/t/t游戏结束!!!/n/t/t/t/t 得分为%d/n",score);
    break;
   }
   map();
   for(n=snake.node;n>0;n--) //控制蛇身总体前进一步
   {
    snake.fx[n]=snake.fx[n-1];
    snake.fy[n]=snake.fy[n-1];
   }
 }
}
int main()
{
 system("title 贪吃蛇 Powered by 刘铸澎");//标题
 system("color fd");//颜色 红
 food.yes = 0;
 gamePlay();
 return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值