长夜漫漫,圣诞节那天没事写了个贪吃蛇,本来不想发的,但觉得发了的话可能会让一些人学到些东西,就发吧。代码不长,200来行,画面比较流畅。其实还可以更短的,因为我不知道line可以设置宽度,所以我自己想了个每次把5条line并排输出的算法,还有的地方也可以精简,不过懒得弄了,有兴趣的玩玩吧,地址:http://wysaid.googlecode.com/files/snake.rar 如何使用graphics.h 请参见 http://tieba.baidu.com/f?kz=778031710
下面是代码:第一个文件 snake.h
#ifndef _snake_n_
#define _snake_n_
#include<vector>
#include<conio.h>
#include<graphics.h>
#include<cmath>
#include<ctime>
#include<cstdlib>
using std::vector;
class Snake
{
public:
Snake(){direction = 'd';x_head = 100;y_head = 240;x_corner.push_back(0),y_corner.push_back(240);}
void operator++();
friend void drawline(int,int,int,int,bool);
friend class Fruit;
void draw(int);
void ifhit();
void longer();
char getdir(){return direction;}
void move_up(char direct,int speed){if(direction != direct) {x_corner.push_back(x_head);y_corner.push_back(y_head);}y_head -= speed;direction = direct;}
void move_down(char direct,int speed){if(direction != direct) {x_corner.push_back(x_head);y_corner.push_back(y_head);}y_head += speed;direction = direct;}
void move_left(char direct,int speed){if(direction != direct) {x_corner.push_back(x_head);y_corner.push_back(y_head);}x_head -= speed;direction = direct;}
void move_right(char direct,int speed){if(direction != direct) {x_corner.push_back(x_head);y_corner.push_back(y_head);}x_head += speed;direction = direct;}
private:
char direction;
int x_head;
int y_head;
vector<int> x_corner;
vector<int> y_corner;
};
class Fruit
{
public:
Fruit(){x = 0;y = 0;radius = 4;score = 0;}
Fruit(int x_first,int y_first){x = x_first;y = y_first;radius = 4;score = 0;}
void drawfruit(Snake&,int);
bool eaten(Snake&);
private:
int x;
int y;
int radius;
int score;
};
void gameover();
#endif
下面是代码:第一个文件 snake.h
#ifndef _snake_n_
#define _snake_n_
#include<vector>
#include<conio.h>
#include<graphics.h>
#include<cmath>
#include<ctime>
#include<cstdlib>
using std::vector;
class Snake
{
public:
Snake(){direction = 'd';x_head = 100;y_head = 240;x_corner.push_back(0),y_corner.push_back(240);}
void operator++();
friend void drawline(int,int,int,int,bool);
friend class Fruit;
void draw(int);
void ifhit();
void longer();
char getdir(){return direction;}
void move_up(char direct,int speed){if(direction != direct) {x_corner.push_back(x_head);y_corner.push_back(y_head);}y_head -= speed;direction = direct;}
void move_down(char direct,int speed){if(direction != direct) {x_corner.push_back(x_head);y_corner.push_back(y_head);}y_head += speed;direction = direct;}
void move_left(char direct,int speed){if(direction != direct) {x_corner.push_back(x_head);y_corner.push_back(y_head);}x_head -= speed;direction = direct;}
void move_right(char direct,int speed){if(direction != direct) {x_corner.push_back(x_head);y_corner.push_back(y_head);}x_head += speed;direction = direct;}
private:
char direction;
int x_head;
int y_head;
vector<int> x_corner;
vector<int> y_corner;
};
class Fruit
{
public:
Fruit(){x = 0;y = 0;radius = 4;score = 0;}
Fruit(int x_first,int y_first){x = x_first;y = y_first;radius = 4;score = 0;}
void drawfruit(Snake&,int);
bool eaten(Snake&);
private:
int x;
int y;
int radius;
int score;
};
void gameover();
#endif

