鱼C论坛

 找回密码
 立即注册
查看: 3159|回复: 42

[技术交流] 坦白说,这是一条颜值超高的贪吃蛇!

[复制链接]
发表于 2018-8-19 08:40:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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;
}
贪吃蛇.JPG
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-19 08:46:08 | 显示全部楼层
看一看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-19 11:38:21 | 显示全部楼层
看一看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-19 11:43:06 From FishC Mobile | 显示全部楼层
有点花
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-20 20:56:48 From FishC Mobile | 显示全部楼层
学习一波
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-21 08:31:35 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-21 08:31:56 | 显示全部楼层
666666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-21 08:35:46 | 显示全部楼层
那里看得出来是高颜值啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-22 09:03:23 | 显示全部楼层
.......
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-22 23:31:09 From FishC Mobile | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-23 17:21:36 From FishC Mobile | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-23 20:36:23 | 显示全部楼层
优秀!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-24 07:55:52 | 显示全部楼层
转基因贪吃蛇
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-29 18:56:12 | 显示全部楼层
我要玩蛇蛇
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-29 22:42:19 | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-5 01:33:42 | 显示全部楼层
666666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-5 14:02:26 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-5 17:23:12 | 显示全部楼层
666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-5 19:32:14 | 显示全部楼层
如果是图片这样控制台也可以
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-5 21:58:07 | 显示全部楼层
666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 05:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表