| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1004 人关注过本帖
标题:大神,求问为什么我的贪吃蛇动不了,怎么控制啊
只看楼主 加入收藏
啦啦1223
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2022-12-25
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:3 
大神,求问为什么我的贪吃蛇动不了,怎么控制啊
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
//
#include<time.h>
//
#include<unistd.h>
#include<conio.h>
//预定义
#define U 1
#define D 2
#define L 3
#define R 4
//
//定义全局变量区
typedef struct snake
{
    int x;
    int y;
    struct snake* next;
} snake;
snake *head;//
snake *food;//
snake *q;//
int status=R;
//函数声明
void createMap();
//坐标转换
void gotoXY(int a,int b);
//颜色转换
 int color(int x);
 //创建蛇
 void initSnake();
 //
 void creatFood();
 //
 void keyboradControl();
 //
 int snakeMove();
 //
 void screenTips();
  void screenTips()
  {
    color(11);
      gotoXY(64,4);
      printf("最高记录");
      color(11);
      gotoXY(64,6);
    printf("当前记录");
    gotoXY(73,11);
    printf("提示");
  }
 int cantCrossWall();
 int cantCrossWall()
 {
     if(head->x==0||head->x==56||head->y==0||head->y==26)
     {
         system("cls");
         gotoXY(30,6);
         printf("撞墙");
         return 1;
     }
     return 0;
 }
int snakeMove()
{
    snake *newNode;
    newNode = (snake*)malloc(sizeof(snake));
    if(status==U)
    {
    newNode->x=head->x;
    newNode->y=head->y-1;
    /*newNode->next=head;   
    //
    head=newNode;
    //
    q=head;
    if(newNode->x==food->x&&newNode->y==food->y)
    {
        while(q!=NULL)
        {
            //TODO
            gotoXY(q->x,q->y);
            color(14);
            printf("◆");
            q=q->next;
        }
        //
        creatFood();
        }
        else
        {
        while(q->next->next!=NULL)
        {
            //TODO
            gotoXY(q->x,q->y);
            color(14);
            printf("◆");
            q=q->next;
        }   
        //
        gotoXY(q->next->x,q->next->y);
        color(3);
        printf("■");
        //
        free(q->next);
        q->next=NULL;
        }
    }*/
    if(status ==D)
    {
        newNode->x=head->x;
        newNode->y=head->y+1;
    }
    if(status ==L)
        {
            newNode->x=head->x-2;
            newNode->y=head->y;
        }
    if(status ==R)
        {
            newNode->x=head->x+2;
            newNode->y=head->y;
        }

  newNode->next=head;   
    //
    head=newNode;
    //
    if(cantCrossWall())
    {
        status=0;
        return 3;
    }
    q=head;
    if(newNode->x==food->x  &&  newNode->y==food->y)
    {
        while(q!=NULL)
        {
            //TODO
            gotoXY(q->x,q->y);
            color(14);
            printf("◆");
            q=q->next;
        }
        //
        creatFood();
        }
        else
        {
        while(q->next->next!=NULL)
        {
            //TODO
            gotoXY(q->x,q->y);
            color(14);
            printf("◆");
            q=q->next;
        }   
        //
        gotoXY(q->next->x,q->next->y);
        color(3);
        printf("■");
        //
        free(q->next);
        q->next=NULL;
        }
    }}
 void keyboradControl()
   {    status = R;//
          while(1)
       {
           //TODO
           //
           if(GetAsyncKeyState(VK_UP) && status !=D)
           {
               //
               status = U;
               }
           else if(GetAsyncKeyState(VK_DOWN)&&status !=U)//暂停
               {status = D;}
           else if(GetAsyncKeyState(VK_LEFT)&&status !=R)
                     { status = L;}
               else if(GetAsyncKeyState(VK_RIGHT)&&status !=L)
                    {status = R;}
        //
        Sleep (500);
        snakeMove();           
       }
   }
 void creatFood()
 {
     snake *food1;
     food1 = (snake*)malloc(sizeof(snake));
     //
     srand((unsigned)time(NULL));
     //
     food1->x=rand()%53+2;
     while((food1->x%2)!=0)
     {
         //
         food1->x = rand()%53+2;
     }
     //
     food1->y = rand()%23+1;
     //
     q = head;
     while(q->next!=NULL)
     {
         //
         if(q->x==food1->x&&q->y==food1->y)
         {
             free(food1);
             creatFood();//
            
         }
         q=q->next;
         
     }
     //
     gotoXY(food1->x,food1->y);
     color(10);
     printf("★");
     food = food1;
 }
  void initSnake()
  {  int i=1;
  //
      snake *ss;
      //分配内存空间,使用头插法(单链表),以设定的x,y位置开始插入
      ss= (snake*)malloc(sizeof(snake));
      ss->x = 24;
       ss->y = 5;
        ss->next = NULL;
        //
        for(i=1;i<=4;i++)
        {   //
            head=(snake*)malloc(sizeof(snake));
             head -> next = ss;
              head -> x = 24 + i*2;
               head -> y = 5;
                ss = head;//
        }
        //
        color(14);
        while(ss !=NULL){
            //TODO
            gotoXY(ss->x,ss->y);
            printf("◆");
            ss = ss->next;
        }
        
  }
 int  color(int x){
     //更加待打印内容的颜色属性
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x);
     return 0;
 }
 void gotoXY(int a,int b)
 {
     COORD c;
     c.X =a;
     c.Y =b;
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
 }
//函数定义、函数实现
void  createMap(){
    int i,j;
    //因为是中文字符,所有横向占位2个坐位空间
    color(5);
    for(i=0;i<58;i+=2)
    { gotoXY(i,0);
     printf("□");
     gotoXY(i,26);
     printf("□");
    }
    for(j=1;j<26;j++)
    {
        gotoXY(0,j);
         printf("□");
        gotoXY(56,j);
         printf("□");
        
    }
    //打印中间堂哥
    color(3);
    for(i=2;i<56;i+=2)
{
    for(j=1;j<26;j++)//j的增量为一
    {
        gotoXY(i,j);
        printf("■");
    }
}
}
int  main()
{   //设置控制台的宽高
    system("mode con cole=110 lines=30");
    //调用创建
    createMap();
    screenTips();
    initSnake();
    creatFood();
    keyboradControl();
    snakeMove();
    while(1)
    {
        //TODO
    }
    return 0;
}
搜索更多相关主题的帖子: color int printf next status 
2022-12-25 10:12
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1479
专家分:9055
注 册:2010-3-16
收藏
得分:20 
改3个函数内容。
程序代码:
int snakeMove()
{
   int over = 0;
   snake *newNode;
   newNode = (snake *)malloc(sizeof(snake));
   if (status == U)
   {
      newNode->x = head->x;
      newNode->y = head->y - 1;
      /*newNode->next=head;
      //
      head=newNode;
      //
      q=head;
      if(newNode->x==food->x&&newNode->y==food->y)
      {
          while(q!=NULL)
          {
              //TODO
              gotoXY(q->x,q->y);
              color(14);
              printf("◆");
              q=q->next;
          }
          //
          creatFood();
          }
          else
          {
          while(q->next->next!=NULL)
          {
              //TODO
              gotoXY(q->x,q->y);
              color(14);
              printf("◆");
              q=q->next;
          }
          //
          gotoXY(q->next->x,q->next->y);
          color(3);
          printf("■");
          //
          free(q->next);
          q->next=NULL;
          }*/
   }
   if (status == D)
   {
      newNode->x = head->x;
      newNode->y = head->y + 1;
   }
   if (status == L)
   {
      newNode->x = head->x - 2;
      newNode->y = head->y;
   }
   if (status == R)
   {
      newNode->x = head->x + 2;
      newNode->y = head->y;
   }

   newNode->next = head;
   //
   head = newNode;
   //
   if (cantCrossWall())
   {
      status = 0;
      over = 1;
      // return 3;
   }
   q = head;
   if (newNode->x == food->x && newNode->y == food->y)
   {
      while (q != NULL)
      {
         // TODO
         gotoXY(q->x, q->y);
         color(14);
         printf("");
         q = q->next;
      }
      //
      creatFood();
   }
   else
   {
      while (q->next->next != NULL)
      {
         // TODO
         gotoXY(q->x, q->y);
         color(14);
         printf("");
         q = q->next;
      }
      //
      gotoXY(q->next->x, q->next->y);
      color(3);
      printf("");
      //
      free(q->next);
      q->next = NULL;
   }

   return !over;
}

void keyboradControl()
{
   status = R; //
   int key, chInput = 0;
   int isGame = 1;
   while (isGame)
   {

      // TODO
      //
      if (_kbhit() != 0)
      {
         chInput = _getch();
         if (chInput == 0xE0 || chInput == 0) // 方向键读2次
            chInput = _getch();

         switch (chInput) // 控制操作
         {
         case 'a':
         case 'A':
         case 0x4B: // Key_Left:
            if (status != R)
               status = L;
            break;
         case 'd':
         case 'D':
         case 0x4D: // Key_Right:
            if (status != L)
               status = R;
            break;
         case 'w':
         case 'W':
         case 0x48: // Key_Up:
            if (status != D)
               status = U;

            break;
         case 's':
         case 'S':
         case 0x50: // Key_Down:
            if (status != U)
               status = D;

            break;
         case 0x1B: // Key_ESC:
            isGame = 0;
            continue;
            break;
         }
         //
      }
      Sleep(500);
      isGame = snakeMove();
   }
}


int main()
{ // 设置控制台的宽高
   system("mode con cole=110 lines=30");
   // 调用创建
   createMap();
   screenTips();
   initSnake();
   creatFood();
   keyboradControl();
   // snakeMove();
   //  while (1)
   //  {
   //     // TODO
   //  }
   return 0;
}

2022-12-25 10:44
forever74
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:CC
等 级:版主
威 望:58
帖 子:1685
专家分:4252
注 册:2007-12-27
收藏
得分:0 
也许是冬眠呢。

对宇宙最严谨的描述应该就是宇宙其实是不严谨的
2022-12-26 20:44
啦啦1223
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2022-12-25
收藏
得分:0 
回复 2楼 apull
感谢大神
2022-12-30 20:53
快速回复:大神,求问为什么我的贪吃蛇动不了,怎么控制啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017820 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved