贪吃蛇小游戏

回复帖子

@神•于跃 2019-07-12 17:27 回复

哪位大佬找一下错误,私信我。贪食蛇小游戏,非原创……改完后在c++上运行就珂以玩啦(@-@) 上代码:

#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
using namespace std;
const int N=21;
void Get_xy(int x,int y)
   { 
        HANDLE hout; 
        COORD pos; 
        pos.X=x*2; 
        pos.Y=y; 
        hout=GetStdHandle(STD_OUTPUT_HANDLE); 
        SetConsoleCursorPosition(hout,pos); 
   }

void Color(int num) 
{
    HANDLE hout;
     hout=GetStdHandle(STD_OUTPUT_HANDLE);
     SetConsoleTextAttribute(hout,num); 
}
void Initial()
{ int i,j;
 int wall[N+2][N+2]={{0}};
  for(i=1;i<=N;i++)
   for(j=1;j<=N;j++)
    wall[i][j]=1; 
    Color(11); 
    for(i=0;i<N+2;i++)
     { 
            for(j=0;j<N+2;j++)
             {
                     if(wall[i][j]) 
                     cout<<"■"; 
                     else cout<<"□"; 
             } cout<<endl; 
     } 
     Get_xy(N+3,1);
     Color(20);
     cout<<"按'W','S','A','D'进行操作"<<endl;
     Get_xy(N+3,2); 
     Color(20); 
     cout<<"按任意键暂停"<<endl; 
     Get_xy(N+3,3); 
     Color(20); 
     cout<<"得分:"<<endl; 
}
void game()
{ 
    int snake=NULL; 
    int len=1; 
    int i; 
    int score=0; 
    int apple[2]; 
    int tail[2]; 
    char ch='p'; 
    Initial(); 
    srand((unsigned)time(NULL)); 
    apple[0]=rand()%N+1; 
    apple[1]=rand()%N+1; 
    Get_xy(apple[0],apple[1]); 
    Color(12); 
    cout<<"●"<<endl; 
    snake=(int)realloc(snake,sizeof(int)len); 
    for(i=0;i<len;i++) 
    snake[i]=(int)malloc(sizeof(int)2); 
    snake[0][0]=N/2; 
    snake[0][1]=N/2+1; 
    Get_xy(snake[0][0],snake[0][1]); 
    Color(14); 
    cout<<"⊙"<<endl; 
    int flag=1; 
    while(1) 
    { if(flag)
     { 
            tail[0]=snake[len-1][0];
             tail[1]=snake[len-1][1]; 
             Get_xy(tail[0],tail[1]);
              Color(11); 
              cout<<"■"<<endl; 
     } 
     flag=1;
     for(i=len-1;i>0;i--) 
     { 
            snake[i][0]=snake[i-1][0]; 
            snake[i][1]=snake[i-1][1]; 
            Get_xy(snake[i][0],snake[i][1]); 
            Color(14); 
            cout<<"★"<<endl; 
     } 
     if(kbhit()) 
     { 
            Get_xy(0,N+3); 
            ch=getche(); 
     } 
     switch(ch) 
     { 
            case 'W': 
            case 'w':
            snake[0][1]--;
            break; 
            case 'S':
            case 's': 
            snake[0][1]++; 
            break;
            case 'A': 
            case 'a': 
            snake[0][0]--; 
            break; 
            case 'D': 
            case 'd': 
            snake[0][0]++; 
            break; 
            default :
            break; 
     } 
     for(i=1;i<len;i++) 
     { 
         if(snake[0][0]==snake[i][0] && snake[0][1]==snake[i][1]) 
         { 
             Get_xy(N/2,N/2); 
             Color(30); 
             cout<<"Game over!"<<endl; 
             exit(0); 
         } 
     } 
     Get_xy(snake[0][0],snake[0][1]); 
     Color(14); 
     cout<<"⊙"<<endl; 
     Sleep(abs(200-0.5*score)); 
     if(snake[0][0]==a1. 1. pple[0] && snake[0][1]==apple[1]) 
     { 
      flag=0; 
      score++; 
      len++; 
      srand((unsigned)time(NULL)); 
      snake=(int*)realloc(snake,sizeof(int)len); 
      snake[len-1]=(int)malloc(sizeof(int)2); 
      Get_xy(N+6,3); 
      Color(20); 
      cout<<score<<endl; 
      apple[0]=rand()%N+1; 
      apple[1]=rand()%N+1; 
      Get_xy(apple[0],apple[1]); 
      Color(12); 
      cout<<"●"<<endl; 
     } 
     if(snake[0][0]==0 || snake[0][0]==N || snake[0][1]==0 || snake[0][1]==N)
     { 
        Get_xy(N/2,N/2); 
        Color(30); 
        cout<<"Game Over!"<<endl; 
        for(i=0;i<len;i++) 
        free(snake[i]); 
        Sleep(INFINITE); 
        exit(0); 
     } 
     if(len>=NN/20) 
     { 
        Get_xy(N/2,N/2); 
        Color(30); 
        cout<<"Win!"<<endl; 
        for(i=0;i<len;i++) 
        free(snake[i]); 
        Sleep(INFINITE); 
        exit(0); 
     }
    } 
} 
int main() 
{ 
    game(); 
    return 0; 
}
@0AND1STORY 2019-07-12 17:35 回复 举报

其实用链表存蛇的位置会好很多,那个realloc改起来太麻烦了orz

@wyw666 2019-07-12 21:45 回复 举报

不能这么说额,学窗口编程的最好还是从底层语言学起(听别人说的)

反馈
如果你认为某个帖子有问题,欢迎向洛谷反馈,以帮助更多的同学。



请具体说明理由,以增加反馈的可信度。