| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
贪吃蛇升级了,贪吃蛇不仅要好玩,当然也要好看,在这个夏天不如来试一试冰激凌色的贪吃蛇,让你的心情凉爽一夏!!! 
经过精心挑选的的冰激凌色,让贪吃蛇颜值大增, 废话不多说,先看图。 
这是用EasyX图形库,在VC++6.0平台下完成的, 所以你必须要安装了EasyX图形库才行,具体安装方法大家可以百度。(大家玩的时候打开大写键盘,用W, A, S, D控制方向) 
大家试过以后可以反馈一下,无论是好的,还是有什么建议和遇到了bug之类的都可以留言反映一下。 
 
 
#include <iostream> 
#include <graphics.h> 
#include <string> 
#include <cstdlib> 
#include <conio.h> 
#include <time.h> 
 
using namespace std; 
 
//定义方向 
#define UP 'W' 
#define DOWN 'S' 
#define LEFT 'A' 
#define RIGHT 'D' 
 
//定义最小正方形的长度,以及图形界面的长宽,以及长宽的单位 
const int minLength = 12; 
const int length = 480; 
const int width = 480; 
const int numberOfx = 40; 
const int numberOfy = 40; 
 
//定义食物的颜色增加乐趣 
COLORREF colors[10]; 
void setcolor(COLORREF *colors) 
{ 
        colors[0] = RGB(188, 70, 57); 
        colors[1] = RGB(243, 224, 220); 
        colors[2] = RGB(214, 206, 21); 
        colors[3] = RGB(6, 119, 161); 
        colors[4] = RGB(231, 227, 212); 
        colors[5] = RGB(248, 233, 161); 
        colors[6] = RGB(161, 195, 209); 
        colors[7] = RGB(59, 148, 94); 
        colors[8] = RGB(153, 115, 142); 
        colors[9] = RGB(195, 141, 158); 
} 
 
class backGround 
{ 
public: 
        void setGround(); 
}; 
 
void backGround::setGround() 
{ 
        int x = 0; 
        int y = 1; 
 
        //test 
        COLORREF color = RGB(65, 179, 163); 
 
        setbkcolor(color); 
        cleardevice(); 
        setfillcolor(BLACK); 
        for(  ; x < numberOfx ; x++ ) 
        { 
                solidrectangle(minLength * x, 0, minLength * (x + 1), minLength - 1); 
        } 
         
        x = 0; 
        for(  ; x < numberOfx ; x++ ) 
        { 
                solidrectangle(minLength * x, minLength * (numberOfy - 1) + 1, minLength * (x + 1), minLength * numberOfy); 
        } 
         
        for(  ; y < numberOfy - 1 ; y++ ) 
        { 
                solidrectangle(0 ,minLength * y, minLength - 1, minLength * (y + 1)); 
        } 
         
        y = 1; 
        for(  ; y < numberOfy - 1 ; y++ ) 
        { 
                solidrectangle(minLength * (numberOfx - 1) + 1, minLength * y, minLength * numberOfx, minLength * (y + 1)); 
        } 
 
        LOGFONT font; 
        COLORREF color_1 = RGB(188, 70, 57); 
        gettextstyle(&font); 
        font.lfHeight = 30; 
        _tcscpy(font.lfFaceName, _T("黑体")); 
        font.lfQuality = ANTIALIASED_QUALITY; 
        settextcolor(color_1); 
        settextstyle(&font); 
        outtextxy(520, 120, "游戏信息"); 
        font.lfHeight = 20; 
        settextstyle(&font); 
        outtextxy(520, 170, "名称:贪吃蛇"); 
        outtextxy(520, 200, "长度:"); 
        outtextxy(520, 230, "分数:"); 
 
        setlinecolor(color_1); 
        rectangle(480, 0, 680, 480); 
        rectangle(481, 1, 679, 479); 
        rectangle(482, 2, 678, 478); 
        rectangle(483, 3, 677, 477); 
        rectangle(484, 4, 676, 476); 
         
} 
 
class Food 
{ 
public: 
        //定义食物物的位置 
        int xpoSition; 
        int ypoSition; 
        COLORREF color; 
         
        void productFood();   //产生食物的函数 
        void productFoods(); 
        COLORREF colorset();        //设置填充颜色 
}; 
 
void Food::productFood() 
{ 
        time_t t = time(NULL); 
        srand(t); 
         
        xpoSition = rand() % (numberOfx - 2) + 1; 
        ypoSition = rand() % (numberOfy - 2) + 1; 
        color = colorset(); 
        setfillcolor(color); 
        solidrectangle(xpoSition * minLength, ypoSition * minLength, (xpoSition + 1) * minLength, (ypoSition + 1) *minLength); 
} 
 
void Food::productFoods() 
{ 
        xpoSition = rand() % (numberOfx - 2) + 1; 
        ypoSition = rand() % (numberOfy - 2) + 1; 
        color = colorset(); 
        setfillcolor(color); 
        solidrectangle(xpoSition * minLength, ypoSition * minLength, (xpoSition + 1) * minLength, (ypoSition + 1) *minLength); 
} 
 
COLORREF Food::colorset() 
{ 
        time_t t = time(NULL); 
        srand(t); 
        int choice = rand() % 10; 
        return colors[choice]; 
} 
 
class bodyOfSnake 
{ 
public: 
        char direction; 
        char lastdirection; 
        COLORREF color; 
         
        int xposition; 
        int yposition; 
        class bodyOfSnake *next; 
         
        bodyOfSnake() 
        {} 
         
        void start();   //初始化的构造 
        void uMove(); 
        void dMove(); 
        void lMove(); 
        void rMove(); 
        void Move(char Direc); 
}; 
 
void bodyOfSnake::start() 
{ 
        setfillcolor(color); 
        solidrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
} 
 
void bodyOfSnake::uMove() 
{ 
        clearrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
        yposition -= 1; 
        setfillcolor(color); 
        solidrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
         
} 
 
void bodyOfSnake::dMove() 
{ 
        clearrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
        yposition += 1; 
        setfillcolor(color); 
        solidrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
         
} 
 
void bodyOfSnake::lMove() 
{ 
        clearrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
        xposition -= 1; 
        setfillcolor(color); 
        solidrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
         
} 
 
void bodyOfSnake::rMove() 
{ 
        clearrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
        xposition += 1; 
        setfillcolor(color); 
        solidrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
         
} 
 
void bodyOfSnake::Move(char Direc) 
{ 
        switch(Direc) 
        { 
        case UP:uMove();break; 
        case DOWN:dMove();break; 
        case LEFT:lMove();break; 
        case RIGHT:rMove();break; 
        } 
        cout<<direction<<endl; 
} 
 
class headOfSnake 
{ 
public: 
        char direction;   //蛇的运动方向 
        char lastdirection; 
         
        //蛇头的位置坐标 
        int xposition; 
        int yposition; 
        COLORREF color; 
         
        headOfSnake() 
        { 
                color = RGB(71, 75, 70); 
        } 
        void start();   //初始化函数 
        void uMove();   //移动函数 
        void dMove(); 
        void lMove(); 
        void rMove(); 
        void Move(char Direc); 
         
}; 
 
void headOfSnake::start() 
{ 
        time_t t = time(NULL); 
        srand(t + 10); 
        xposition = rand() % (numberOfx - 2) + 1; 
        yposition = rand() % (numberOfy - 2) + 1; 
         
        setlinecolor(WHITE); 
        setfillcolor(color); 
        fillrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
} 
 
void headOfSnake::uMove() 
{ 
        Sleep(150); 
        clearrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
        yposition -= 1; 
        setlinecolor(WHITE); 
        setfillcolor(color); 
        fillrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
         
} 
 
void headOfSnake::dMove() 
{ 
        Sleep(150); 
        clearrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
        yposition += 1; 
        setlinecolor(WHITE); 
        setfillcolor(color); 
        fillrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
         
} 
 
void headOfSnake::lMove() 
{ 
        Sleep(150); 
        clearrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
        xposition -= 1; 
        setlinecolor(WHITE); 
        setfillcolor(color); 
        fillrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
         
} 
 
void headOfSnake::rMove() 
{ 
        Sleep(150); 
        clearrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
        xposition += 1; 
        setlinecolor(WHITE); 
        setfillcolor(color); 
        fillrectangle(xposition * minLength, yposition * minLength, (xposition + 1) * minLength, (yposition + 1) * minLength); 
         
} 
 
void headOfSnake::Move(char Direc) 
{ 
        switch(Direc) 
        { 
        case UP:uMove();break; 
        case DOWN:dMove();break; 
        case LEFT:lMove();break; 
        case RIGHT:rMove();break; 
        } 
} 
 
class Snake 
{ 
public: 
        int length; 
        int score; 
        headOfSnake head; 
        bodyOfSnake *body; 
         
        Snake() 
        { 
                length = 1; 
                score = 0; 
                body = NULL; 
        } 
        ~Snake() 
        { 
                if(body) 
                         
                { 
                        bodyOfSnake *temp; 
                        temp = body; 
                        bodyOfSnake *temps; 
                        while(temp) 
                        { 
                                temps = temp; 
                                temp = temp->next; 
                                delete temps; 
                        } 
                } 
                 
        } 
         
         
        int iseat(Food food);   //判断是否吃到失物 
        void eating(bodyOfSnake **body, Food food);   //吃到食物后的处理工作 
        int foodjudge(int x, int y); 
        int gameover();   //游戏结束的监测 
        //显示长度和分数 
        void showscore(); 
         
}; 
 
int Snake::iseat(Food food) 
{ 
        if(head.xposition == food.xpoSition && head.yposition == food.ypoSition) 
                return 1; 
        return 0; 
} 
 
void Snake::eating(bodyOfSnake **body, Food food) 
{ 
        if(length == 1) 
        { 
                (*body) = new bodyOfSnake; 
                (*body)->color = food.color; 
                switch(head.direction) 
                { 
                case UP: 
                        { 
                                (*body)->xposition = head.xposition; 
                                (*body)->yposition = head.yposition + 1; 
                                (*body)->next = NULL; 
                                (*body)->start(); 
                        };break; 
                case DOWN: 
                        { 
                                (*body)->xposition = head.xposition; 
                                (*body)->yposition = head.yposition - 1; 
                                (*body)->start(); 
                                (*body)->next = NULL; 
                        };break; 
                case LEFT: 
                        { 
                                (*body)->xposition = head.xposition + 1; 
                                (*body)->yposition = head.yposition; 
                                (*body)->next = NULL; 
                                (*body)->start(); 
                        };break; 
                case RIGHT: 
                        { 
                                (*body)->xposition = head.xposition - 1; 
                                (*body)->yposition = head.yposition; 
                                (*body)->next = NULL; 
                                (*body)->start(); 
                        };break; 
                } 
        } 
        else 
        { 
                bodyOfSnake *temp; 
                temp = (*body); 
                while(temp->next) 
                { 
                        temp = temp->next; 
                } 
                bodyOfSnake *temps = new bodyOfSnake; 
                temps->color = food.color; 
                 
                //temp指向最后一节身体 
                switch(temp->direction) 
                { 
                case UP: 
                        { 
                                temps->xposition = temp->xposition; 
                                temps->yposition = temp->yposition + 1; 
                                temps->next = NULL; 
                                temps->start(); 
                        };break; 
                case DOWN: 
                        { 
                                temps->xposition = temp->xposition; 
                                temps->yposition = temp->yposition - 1; 
                                temps->next = NULL; 
                                temps->start(); 
                        };break; 
                case LEFT: 
                        { 
                                temps->xposition = temp->xposition + 1; 
                                temps->yposition = temp->yposition; 
                                temps->next = NULL; 
                                temps->start(); 
                        };break; 
                case RIGHT: 
                        { 
                                temps->xposition = temp->xposition - 1; 
                                temps->yposition = temp->yposition; 
                                temps->next = NULL; 
                                temps->start(); 
                        };break; 
                } 
                 
                temp->next = temps; 
        } 
 
        length++; 
        score +=10; 
         
} 
 
void Snake::showscore() 
{ 
        LOGFONT font; 
        COLORREF color = RGB(128, 44, 193); 
        gettextstyle(&font); 
        font.lfHeight = 30; 
        _tcscpy(font.lfFaceName, _T("黑体")); 
        font.lfQuality = ANTIALIASED_QUALITY; 
        settextcolor(color); 
        font.lfHeight = 20; 
        settextstyle(&font); 
        char s1[5]; 
        sprintf(s1, "%d", length); 
        char s2[10]; 
        sprintf(s2, "%d", score); 
        outtextxy(620, 200, s1); 
        outtextxy(620, 230, s2); 
 
} 
 
int Snake::gameover() 
{ 
        //因为有身体时才会咬到自己 
        bodyOfSnake *temp = body; 
        while(temp != NULL) 
        { 
                if(head.xposition == temp->xposition && head.yposition == temp->yposition) 
                { 
                        setfillcolor(RED); 
                        fillrectangle(head.xposition * minLength, head.yposition * minLength, (head.xposition + 1) * minLength, (head.yposition + 1) * minLength); 
                        return 0; 
                } 
                temp = temp->next; 
        } 
         
        if(head.xposition == 0 || head.xposition == numberOfx - 1 || head.yposition == 0 || head.yposition == numberOfy - 1) 
                return 0; 
        return 1; 
         
         
} 
 
int Snake::foodjudge(int x, int y) 
{ 
        if(body) 
        { 
                bodyOfSnake *temp = body; 
                while(temp) 
                { 
                        if(temp->xposition == x && temp->yposition == y) 
                                return 0; 
                        temp = temp->next; 
                } 
        } 
        if(head.xposition == x && head.yposition == y) 
                return 0; 
        return 1; 
} 
 
int main() 
{ 
        system("color 3F"); 
        initgraph(length + 200, width); 
         
        //背景初始化 
        backGround Back; 
        Back.setGround(); 
 
        //颜色初始化 
        setcolor(colors); 
         
        //食物初始化 
        Food food; 
        food.productFood(); 
         
        //贪吃蛇初始化 
        Snake snake; 
        snake.head.start(); 
        snake.showscore(); 
         
         
         
        int flag = 1; 
        char direction = getch(); 
        char tempdirection; 
        snake.head.direction = snake.head.lastdirection = direction; 
        bodyOfSnake *temp; 
        while(flag) 
        { 
                if(snake.iseat(food)) 
                { 
                        snake.eating(&snake.body, food); 
                        food.productFood(); 
                        while(!snake.foodjudge(food.xpoSition, food.ypoSition)) 
                        { 
                                food.productFoods(); 
                        } 
                        snake.showscore(); 
                } 
                temp = snake.body; 
                snake.head.Move(snake.head.direction); 
                tempdirection = snake.head.lastdirection; 
                snake.head.lastdirection = snake.head.direction; 
                while(temp != NULL) 
                { 
                        temp->direction = tempdirection; 
                        temp->Move(temp->direction); 
                        tempdirection = temp->lastdirection; 
                        temp->lastdirection = temp->direction; 
                        temp = temp->next; 
                } 
                if(!snake.gameover()) 
                { 
                        LOGFONT font; 
                        gettextstyle(&font); 
                        font.lfHeight = 30; 
                        _tcscpy(font.lfFaceName, _T("黑体")); 
                        font.lfQuality = ANTIALIASED_QUALITY; 
                        settextcolor(RED); 
                        settextstyle(&font); 
                        outtextxy(520, 280, "游戏结束"); 
                        flag = 0; 
                } 
                 
                if(_kbhit()) 
                { 
                        direction = getch(); 
                        if(direction == 'Q') 
                                flag = 0; 
                        switch(direction) 
                        { 
                        case UP: 
                                { 
                                        if(snake.head.lastdirection != DOWN) 
                                                snake.head.direction = direction; 
                                };break; 
                        case DOWN: 
                                { 
                                        if(snake.head.lastdirection != UP) 
                                                snake.head.direction = direction; 
                                };break; 
                        case LEFT: 
                                { 
                                        if(snake.head.lastdirection != RIGHT) 
                                                snake.head.direction = direction; 
                                };break; 
                        case RIGHT: 
                                { 
                                        if(snake.head.lastdirection != LEFT) 
                                                snake.head.direction = direction; 
                                };break; 
                        } 
                } 
        } 
         
        getch(); 
        closegraph(); 
 
        return 0; 
} |   
- 
 
 
 
 
 
 
 
 |