您的位置:首页 > 编程语言 > C语言/C++

C++写的简易贪吃蛇

2012-10-10 21:53 134 查看
在VC++ 6.0下编译通过

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>

#define W 30
#define H 20

char symbol[3] = {'#', '*', '@'};  //分别代表:篱笆、蛇身、食物
enum direction {left, up, right, down}; //方向

using namespace std;

char game[H][W];
unsigned speed = 300;
unsigned int count = 0;

class Fence
{
public:
Fence();
void display();
};

Fence::Fence()
{
for(int i=0; i < H; ++i)
for(int j=0; j<W; ++j)
{
if(i==0 || i==H-1 || j==0 || j==W-1)
game[i][j]=symbol[0];
else
game[i][j]=' ';
}
}

void Fence::display()
{
for(int i=0; i < H; ++i)
{
for(int j=0; j<W; ++j)
cout << game[i][j] << ' ';
cout << endl;
}
}

class SnakeNode
{
public:
SnakeNode *prior, *next;
int x,y;
SnakeNode(){}
SnakeNode(int a, int b): x(a), y(b), prior(NULL),next(NULL)
{
game[x][y] = symbol[1];
}
};

class Food
{
public:
int x,y;
void produce();
} food;

void Food::produce()
{
srand(time(NULL));
int a = rand() % (H-2) + 1;
int b = rand() % (W-2) + 1;
while(game[a][b] != ' ')
{
a = rand() % (H-2) + 1;
b = rand() % (W-2) + 1;
}
x=a;
y=b;
game[x][y] = symbol[2];
}

class Snake
{
public:
Snake():dir(::right),score(0)
{
head = new SnakeNode(4,5);
tail = new SnakeNode(4,4);
head->next = tail;
head->prior= tail;
tail->next = head;
tail->prior = head;
food.produce();

}
void move();
bool change_dir(direction);
void add_node(int, int);
unsigned int get_score() { return score;}
private:
SnakeNode *head, *tail;
direction dir;
unsigned int score;
};

void Snake::move()
{
int a = head->x;
int b = head->y;

//计算前进一步的位置
switch(dir)
{
case ::left: --b; break;
case ::up:  --a; break;
case ::right: ++b; break;
case ::down: ++a; break;
}

//判断是否撞墙
if(a==0 || a==H-1 || b==0 || b==W-1 || game[a][b] == symbol[1])
{
cout << "Game Over !" << endl;
exit(0);
}

//判断是否吃到食物
if(a==food.x && b==food.y)
{
//重新生成食物
game[a][b]=' ';
food.produce();

//为蛇增加一个节点
this->add_node(a, b);

//加分
score += 10;
//调速度
++count;
if(count%5==0 && speed > 50)
{
speed -= 50;
}
}
else
{
game[tail->x][tail->y] = ' ';
head = tail;
tail = tail->prior;
head->x = a;
head->y = b;
game[a][b]=symbol[1];
}
}

bool Snake::change_dir(direction d)
{
if((dir % 2) != (d % 2))
{
dir=d;
return true;
}
return false;
}

void Snake::add_node(int x, int y)
{
SnakeNode *p = new SnakeNode(x,y);
p->next = head;
head->prior = p;
head = p;
tail->next = head;
head->prior = tail;
}

int main()
{

Fence fence;
Snake snake;
cout << "Press the direction key to start game!" << endl;
fence.display();

int ch;
bool flag = false;
while(true)
{
ch = getch();
while(ch != 0xE0) ch = getch();
ch = getch();
switch(ch)
{
case 72 : flag =snake.change_dir(::up); break;
case 75 : flag =snake.change_dir(::left); break;
case 80 : flag =snake.change_dir(::down); break;
case 77 : flag =snake.change_dir(::right); break;
}
while(!kbhit() || flag)
{
flag = false;
snake.move();
system("cls");
cout << "Score : " << snake.get_score() << "            speed : " << (7-speed/50) << endl;
fence.display();
Sleep(speed);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: