zoukankan      html  css  js  c++  java
  • ege图形库之简单贪吃蛇(c++)

    第二次做动画显示效果的小程序,心血来潮想做下儿时的经典游戏----贪吃蛇。由于时间有限,只是简单地做了基本功能,有时间后再完善更多功能。

    由于个人水平有限,可能代码有些地方可以改进。不足之处敬请指出。

    注:要运行该代码需要正确配置,连接好ege图形库的头文件,做好准备工作。具体教程请看http://jingyan.baidu.com/article/4b07be3c40d35f48b380f3c7.html

    编程环境:codeblocks   windows10  corei5 cpu

    源代码:

    #include<stdio.h>
    #include<graphics.h>
    #include<windows.h>
    #include<stdlib.h>
    #define Random(x) (rand() % x)
    struct position
    {
    int x;int y;
    position* next;
    };
    class snake
    {
    private:
    int direction;
    public:
    position *tail;
    position *head;
    position* gettail(){return tail;}
    void settail(position* p){tail=p;}
    position* gethead(){return head;}
    void sethead(position* p){head=p;}
    snake()
    {
    tail=new position;head=new position;
    head->x=75;head->y=15;
    tail->x=15;tail->y=15;
    tail->next=new position;
    tail->next->x=45;tail->next->y=15;
    tail->next->next=head;
    direction=4;
    }
    void setdirection(int a){direction=a;}
    int getdirection(){return direction;}
    void move(int x0,int y0,bool &newfood)
    {
    switch(direction)
    {position*p;int xt;int yt;
    case 1:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt;head->y=yt-30;p=tail->next;delete tail;tail=p;break;}
    case 2:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt;head->y=yt+30;p=tail->next;delete tail;tail=p;break;}
    case 3:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt-30;head->y=yt;p=tail->next;delete tail;tail=p;break;}
    case 4:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt+30;head->y=yt;p=tail->next;delete tail;tail=p;break;}
    }
    if(head->x==x0&&head->y==y0)
    {
    newfood=1;
    switch(direction)
    {int xt;int yt;
    case 1:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt;head->y=yt-30;break;}
    case 2:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt;head->y=yt+30;break;}
    case 3:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt-30;head->y=yt;break;}
    case 4:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt+30;head->y=yt;break;}
    }
    }
    }
    void show()
    {
    position *p=gettail();int sum=0;
    while(p!=gethead()->next)
    {sum++;
    if(head->x>0&&head->x<900&&head->y>0&&head->y<900)
    {
    setfillcolor(EGERGB(255,0,0));
    bar(p->x-15,p->y-15,p->x+15,p->y+15);
    setfillcolor(EGERGB(0,0,255));
    bar(p->x-13,p->y-13,p->x+13,p->y+13);
    p=p->next;
    }
    else
    {
    setcolor(EGERGB(255,0,0));
    setfontbkcolor(EGERGB(0x80,0x00,0x80));
    setfont(100,0,"宋体");
    outtextxy(250,360,"GAMEOVER");
    if(sum<20)outtextxy(250,460,"菜鸟水平");
    if(sum>=20&&sum<40)outtextxy(250,460,"高手水平");
    setfont(70,0,"宋体");
    if(sum>=40&&sum<60)outtextxy(100,460,"你骨骼精奇,是个奇才");
    if(sum>=60)outtextxy(200,360,"你已经成仙了");
    Sleep(1000);
    getch();
    }
    int hx,hy;
    hx=head->x;hy=head->y;
    position *p2=gettail();
    while(p2!=gethead())
    {
    if(hx==p2->x&&hy==p2->y)
    {
    setcolor(EGERGB(255,0,0));
    setfontbkcolor(EGERGB(0x80,0x00,0x80));
    setfont(100,0,"宋体");
    outtextxy(250,360,"GAMEOVER");
    if(sum<20)outtextxy(250,460,"菜鸟水平");
    if(sum>=20&&sum<40)outtextxy(250,460,"高手水平");
    setfont(70,0,"宋体");
    if(sum>=40&&sum<60)outtextxy(100,460,"你骨骼精奇,是个奇才");
    if(sum>=60)outtextxy(200,360,"你已经成仙了");
    Sleep(1000);
    getch();
    }
    p2=p2->next;
    }
    }
    }
    };
    int main()
    {
    INITGRAPH(900,900);
    setfillcolor(EGERGB(255,0,0));
    snake s1;
    char k;
    bool newfood=1;
    for(;;)
    {
    delay_fps(8);
    int fx;int fy;
    if(newfood)
    {
    //下面的代码是食物的显示
    L:fx=Random(30)+1;fy=Random(30)+1;//贪吃蛇的食物产生位置是随机的
    fx=fx*30-15;fy=fy*30-15;
    bool bo=1;
    position *p=s1.gettail();
    while(p!=s1.gethead()->next)
    {
    if(p->x==fx&&p->y==fy){bo=0;break;}//食物不能产生在蛇身体的位置;
    p=p->next;
    }
    if(!bo)goto L;
    newfood=0;
    }
    //手动模式:
    if(kbhit())
    {k=getch();
    switch(k)
    {
    case 38:if(s1.getdirection()!=2)s1.setdirection(1);break;//根据键值修改对应的方向
    case 40:if(s1.getdirection()!=1)s1.setdirection(2);break;
    case 37:if(s1.getdirection()!=4)s1.setdirection(3);break;
    case 39:if(s1.getdirection()!=3)s1.setdirection(4);break;
    }
    }
    s1.move(fx,fy,newfood);
    cleardevice();
    s1.show();
    setfillcolor(EGERGB(0,0,255));
    bar(fx-15,fy-15,fx+15,fy+15);
    setfillcolor(EGERGB(0,255,0));
    bar(fx-15,fy-15,fx+15,fy+15);
    //Sleep(80);
    }
    }

    运行效果截图:

  • 相关阅读:
    Delphi XE5 android 蓝牙通讯传输
    Delphi XE5 android toast
    Delphi XE5 android openurl(转)
    Delphi XE5 如何设计并使用FireMonkeyStyle(转)
    Delphi XE5 android 捕获几个事件
    Delphi XE5 android listview
    Delphi XE5 android 黑屏的临时解决办法
    Delphi XE5 android popumenu
    Delphi XE5 android 获取网络状态
    Delphi XE5 android 获取电池电量
  • 原文地址:https://www.cnblogs.com/linruier/p/6919742.html
Copyright © 2011-2022 走看看