• 贪吃蛇,C++


    好久之前写的,登陆博客看了下好久没更新了,就随手发出来吧。
    用这份代码,QT套了个界面设置速度和地图大小。QT的代码就不发了hhh,毕竟源代码分好几个文件

    用了Windows.h 所以只能在Windows下运行。

    #include<Windows.h>
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<ctime>
    #include<cstdlib>
    #include<conio.h>
    #include<deque>
    using namespace std;
    
    const int MAXC=100,MAXR=100;
    typedef pair<int,int> pii;
    
    class Game{
    public:
        int mat[MAXC][MAXR];
        int dx[5],dy[5];
        deque<pii> q;
        int d,col,row;
        bool food;
        HANDLE hOut;
        void pt(int x, int y,char c){
            COORD pos;
            pos.X=x*3+2;
            pos.Y=y*2+1;
            SetConsoleCursorPosition(hOut, pos);
            putchar(c);
        }
        int dir(){
            char c1,c2;
            if(!_kbhit())
                return 4;
            c1=getch(),c2=getch();
            switch(c2){
                case 72:return 1;//up
                case 75:return 0;//left
                case 77:return 3;//right
                case 80:return 2;//down
            }
        }
        void topblock(){
            printf("┌");
            for(int i=0;i<col-1;++i)
                printf("──┬");
            puts("──┐");
        }
        void buttonblock(){
            printf("└");
            for(int i=0;i<col-1;++i)
                printf("──┴");
            puts("──┘");
        }
        void commonblock(){
            printf("├");
            for(int i=0;i<col-1;++i)
                printf("──┼");
            puts("──┤");
        }
        void feed()
        {
            while(!food){
                int xx=rand()%col,yy=rand()%row;
                if(!mat[xx][yy]){
                    mat[xx][yy]=2;
                    food=1;
                    pt(xx,yy,'*');
                }
            }
        }
        Game(){
            row=11,col=11;//settings
            dx[0]=dy[1]=-1,dx[3]=dy[2]=1;
            dx[1]=dx[2]=dy[0]=dy[3]=dx[4]=dy[4]=0;
            d=4;
            food=0;
            hOut=GetStdHandle(STD_OUTPUT_HANDLE);
        }
        void start(){
            system("chcp 437");
            system("mode con:cols=36 lines=24");
            system("chcp 65001");
            print_init();
            while(1){
                if(!food){
                    feed();
                }
                int t=dir(),nx,ny;
                pii tmp=q.front();
                if(t==4||(t==0&&d==3)||(t==3&&d==0)||(t==1&&d==2)||(t==2&&d==1)){
                    nx=tmp.first+dx[d],ny=tmp.second+dy[d];
                }
                else{
                    nx=tmp.first+dx[t],ny=tmp.second+dy[t];
                    d=t;
                }
                bool death=0;
                if(nx>=0&&ny>=0&&nx<row&&ny<col){
                    if(mat[nx][ny]==1){
                        death=1;
                    }
                    else if(mat[nx][ny]==2){
                        pt(tmp.first,tmp.second,'O');
                        q.push_front(pii(nx,ny));
                        pt(nx,ny,'@');
                        mat[nx][ny]=1;
                        food=0;
                    }
                    else{
                        pii tt=q.back();
                        q.pop_back();
                        mat[tt.first][tt.second]=0;
                        pt(tt.first,tt.second,' ');
                        q.push_front(pii(nx,ny));
                        pt(tmp.first,tmp.second,'O');
                        pt(nx,ny,'@');
                        mat[nx][ny]=1;
                    }
                }
                else{
                    death=1;
                }
                if(death){
                   system("cls");
                   puts("你输了");
                   system("pause");
                }
                Sleep(200);
            }
        }
        void print_init(){
            system("cls");
            topblock();
            for(int i=0;i<row;++i){
                printf("│");
                for(int j=0;j<col;++j){
                    printf("  │");
                }
                puts("");
                if(i==row-1)
                    buttonblock();
                else commonblock();
            }
            int xx=rand()%col,yy=rand()%row;
            q.push_front(pii(xx,yy));
            mat[xx][yy]=1;
            pt(xx,yy,'@');
            while(1){
                int td=rand()%4;
                int nx=xx+dx[td],ny=yy+dy[td];
                if(nx>=0&&ny>=0&&nx<row&&ny<col){
                    q.push_back(pii(nx,ny));
                    mat[nx][ny]=1;
                    pt(nx,ny,'O');
                    break;
                }
            }
            feed();
            while(1){
                int ttt;
                if((ttt=dir())!=4){
                    d=ttt;
                    break;
                }
            }
        }
    };
    
    int main(){
        srand((unsigned)time(0));
        Game().start();
    	return 0;
    }
    
  • 相关阅读:
    web中缓存的使用
    .net1.1 Read Byte Array From File
    数据库连接的多种方式(二)
    存储过程和SQL语句比较及存储过程在C#中调用方法(转)
    exec与sp_executesql语法的区别详解(转)
    SQL Server存储过程入门案例详解
    asp.net结合aspnetpager用sql语句分页
    数据库连接的多种方式(一)
    分页存储过程1
    asp.net结合aspnetpager使用SQL2005的存储过程分页(转)
  • 原文地址:https://www.cnblogs.com/chwhc/p/13411767.html
Copyright © 2020-2023  润新知