Greedy Snake 3.0

#include <easyx.h>
#include <conio.h>
#include <list>
using namespace std;

int sx;
int sy;
int x;
int y;
char ch;
char score[12];
int m = (rand()%64)*10+5;
int n = (rand()%46)*10+35;
int length;
list<char> lc;
list<char>::iterator lci;
bool destroy;
int td;        
char choice;

int main()
{
    initgraph(640,480);

    settextcolor(RED);
    settextstyle(60,30,_T("΢ÈíÑźÚ"));
    outtextxy(100,170,"Greedy Snake");
    settextcolor(WHITE);
    settextstyle(20,10,_T("΢ÈíÑźÚ"));
    outtextxy(130, 270, "set of key: 'w':up 's':down 'a':left 'd':right");
    outtextxy(140, 300,"any of other keys to pause or retrieve");
    outtextxy(170, 240, "press any key to start the game");
    getch();

    do{
    cleardevice();
    sx = 25;
    sy = 25;
    ch = 'd';
    length=3;
    lc.clear();
    destroy=false;
    settextstyle(20,10,_T("΢ÈíÑźÚ"));
    lc.push_back(ch);
    lc.push_back(ch);
    lc.push_back(ch);
    setfillcolor(WHITE);
    while(true)
    {
        //detect eat or not
        if(sx==m&&sy==n)
        {
            length+=1;
            m = (rand()%64)*10+5;
            n = (rand()%47)*10+15;
        }
        else
            lc.pop_front();
        //draw random circle
        solidcircle(m,n,5);
        //draw the snake
        solidcircle(sx,sy,5);
        x=sx;
        y=sy;
        for(int i=0; i<length-1 ;++i)
        {
            if(i==0)
                lci=--lc.end(); 
            else
                --lci;
            switch(*lci)
            {
            case 'w':
                solidcircle(x,y+=10,5);
                break;
            case 'a':
                solidcircle(x+=10,y,5);
                break;
            case 's':
                solidcircle(x,y-=10,5);
                break;
            case 'd':
                solidcircle(x-=10,y,5);
                break;
            }
            if(sx==x&&sy==y)
                destroy=true;
        }
        //destroy the snake 
        if(destroy==true)
        {
            setfillcolor(BLACK);
            solidcircle(m,n,5);
            Sleep(100);
            x=sx;
            y=sy;
            for(int i=0; i<length-1 ;++i)
            {
                td=(800-100)/(length-1);
                Sleep(800-i*td);
                if(i==0)
                    lci=--lc.end(); 
                else
                    --lci;
                switch(*lci)
                {
                case 'w':
                    solidcircle(x,y+=10,5);
                    break;
                case 'a':
                    solidcircle(x+=10,y,5);
                    break;
                case 's':
                    solidcircle(x,y-=10,5);
                    break;
                case 'd':
                    solidcircle(x-=10,y,5);
                    break;
                }
            }
            settextcolor(YELLOW);
            settextstyle(50,30,_T("΢ÈíÑźÚ"));
            outtextxy(130,170,"GAME OVER");
            break;
        }
        //detect keybord input
        if(kbhit())
            ch=getch();
        switch(ch)
        {
        case 'w':
            if(lc.back()!='s')
                sy-=10;
            else
            {
                ch='s';
                sy+=10;
            }
            break;
        case 'a':
            if(lc.back()!='d')
                sx-=10;
            else
            {
                ch='d';
                sx+=10;
            }
            break;
        case 's':
            if(lc.back()!='w')
                sy+=10;
            else
            {
                ch='w';
                sy-=10;
            }
            break;
        case 'd':
            if(lc.back()!='a')
                sx+=10;
            else
            {
                ch='a';
                sx-=10;
            }
            break;
        default:
            getch();
            ch=lc.back();
            switch(ch)
            {
            case 'w':
                sy-=10;
                break;
            case 'a':
                sx-=10;
                break;
            case 's':
                sy+=10;
                break;
            case 'd':
                sx+=10;
                break;
            }
            break;
        }
        //put direction into the list
        lc.push_back(ch);
        //dectect the collision with the border
        if(sx<0 || sx>635 || sy<25 || sy>475)
        {
            setfillcolor(BLACK);
            solidcircle(m,n,5);
            x=sx;
            y=sy;
            for(int i=0; i<length ;++i)
            {
                td=(800-100)/(length-1);
                Sleep(800-i*td);
                if(i==0)
                    lci=--lc.end(); 
                else
                    --lci;
                switch(*lci)
                {
                case 'w':
                    solidcircle(x,y+=10,5);
                    break;
                case 'a':
                    solidcircle(x+=10,y,5);
                    break;
                case 's':
                    solidcircle(x,y-=10,5);
                    break;
                case 'd':
                    solidcircle(x-=10,y,5);
                    break;
                }
                if(i==0)
                    line(0,20,640,20);
            }
            settextcolor(YELLOW);
            settextstyle(50,30,_T("΢ÈíÑźÚ"));
            outtextxy(130,170,"GAME OVER");
            break;
        }
        //display the picture
        Sleep(100);
        cleardevice();
        //draw the score
        sprintf(score,"score: %04d",length-3);
        outtextxy(265,0,score);
        line(0,20,640,20);
    }
    settextcolor(WHITE);
    settextstyle(20,10,_T("΢ÈíÑźÚ"));
    outtextxy(260, 230, "'r': restart");
    outtextxy(260, 260, "'q': quit");
    choice=getch();
    }while(choice=='r');
    
    closegraph();
    return 0;
}

 

上一篇:深度(迷宫)1


下一篇:洛谷 9.19 月赛部分题解