Fork me on GitHub

双向链表与easyx实现简易贪吃蛇

/**
文件:snake.cpp 编写者:image
编写日期:2017年1月28号 简要描述:贪吃蛇游戏,可加速
**/

#include “stdafx.h”

#include #include #include #include #define FRAME_WIDTH 50

#define FRAME_HEIGHT 50
using namespace std;
enum direction { t, b, l, r };
class Snake;
struct food{
int pos_x, pos_y;
};
class section {
section front;
section
rear;
direction direct;
int pos_x, pos_y;
friend class Snake;
public:
section(section fr=NULL,direction dir=t, section re = NULL ) {
front=fr;
rear = re;
direct = dir;
pos_x = pos_y = 25;
}
section makenext() {
rear = new section(this,this->direct);
switch (this->direct) {
case 0: {
rear->pos_x = this->pos_x;
rear->pos_y = this->pos_y + 1;
break;
}
case 1: {
rear->pos_x = this->pos_x;
rear->pos_y = this->pos_y - 1;
break;
}
case 2: {
rear->pos_x = this->pos_x + 1;
rear->pos_y = this->pos_y;
break;
}
case 3: {
rear->pos_x = this->pos_x - 1;
rear->pos_y = this->pos_y;
break;
}
}
return rear;
}
void move() {
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(pos_x
480 / FRAME_WIDTH, pos_y 480 / FRAME_HEIGHT, 480 / FRAME_WIDTH / 2);
switch (this->direct) {
case 0: {
this->pos_x = this->pos_x;
this->pos_y = this->pos_y - 1;
break;
}
case 1: {
this->pos_x = this->pos_x;
this->pos_y = this->pos_y + 1;
break;
}
case 2: {
this->pos_x = this->pos_x - 1;
this->pos_y = this->pos_y;
break;
}
case 3: {
this->pos_x = this->pos_x + 1;
this->pos_y = this->pos_y;
break;
}
}
if (this->front == NULL)
setfillcolor(RED);
else
setfillcolor(GREEN);
fillcircle(pos_x
480/FRAME_WIDTH, pos_y * 480/FRAME_HEIGHT, 480 / FRAME_WIDTH/2);
if(this->rear!=NULL)
this->rear->move();
if (this->front != NULL&&this->direct != this->front->direct)
this->direct = this->front->direct;
}

};
class Snake {
section head;
section
tail;
int length;
int eatentimes;
public:
food foo;
Snake() {
this->head =this->tail= new section();
this->length = 1;
eatentimes = 0;
}
void Add() {
tail=tail->makenext();
length++;
}
bool is_alive() {
section p = head->rear;
while(p != NULL) {
if (p->pos_x == head->pos_x&&p->pos_y == head->pos_y) {
return false;
}
p = p->rear;
}
return head->pos_x != 0 && head->pos_y != 0 && head->pos_x != FRAME_WIDTH && head->pos_y != FRAME_HEIGHT;
}
void move() {
head->move();
if (is_Food()) {
if (eatentimes == 5) {
for (int i = 0; i < 5; i++)
Add();
eatentimes = 0;
setfillcolor(BLACK);
fillcircle(foo.pos_x
480 / FRAME_WIDTH, foo.pos_y * 480 / FRAME_HEIGHT, 480 / FRAME_WIDTH);
makeFood();

        }
        else {
            Add();
            makeFood();
        }
    }
}
void turn(direction dir) {
    head->direct = dir;
}
int getSpeed() {
    return 5000/length;
}
void makeFood() {
    int random\_X = rand() % (FRAME\_WIDTH-1)+1;
    int random\_Y = rand() % (FRAME\_HEIGHT-1)+1;
    section *p = head;
    while (p != NULL) {
        if (p->pos\_x == random\_X&&p->pos\_y == random\_Y) {
            random\_X = rand() % FRAME\_WIDTH;
            random\_Y = rand() % FRAME\_HEIGHT;
    }
        p = p->rear;
    }
    foo.pos\_x = random\_X;
    foo.pos\_y = random\_Y;
    setfillcolor(YELLOW);
    if(eatentimes==4)
        fillcircle(foo.pos\_x * 480 / FRAME\_WIDTH, foo.pos\_y * 480 / FRAME\_HEIGHT, 480 / FRAME_WIDTH );
    else
        fillcircle(foo.pos\_x * 480 / FRAME\_WIDTH, foo.pos\_y * 480 / FRAME\_HEIGHT, 480 / FRAME_WIDTH /2);
    eatentimes++;

}
bool is_Food() {
    return head->pos\_x == foo.pos\_x&&head->pos\_y == foo.pos\_y;
}
void Init() {
    initgraph(480, 480);
    char ch;
    makeFood();
    for (int i = 0; i < 5; i++) {
        Add();
    }
    while(true) {
        int speed = getSpeed();
        if (_kbhit()) {
            if (_kbhit()) {
                speed /= 2;
            }
            ch = _getch();
            switch (ch) {
            case 'w': turn(t); break;
            case 's': turn(b); break;
            case 'a':turn(l); break;
            case 'd':turn(r); break;
            }

        }
        Sleep(speed);
        move();
        if (!is_alive())
            break;
    }
    settextcolor(WHITE);
    outtext(_T("蛇死掉了 按任意键退出游戏"));
    _getch();
}

};
int main() {
srand(time(0));
Snake snake;
snake.Init();

}