这是头文件block.h
#ifndef BLOCK_H
#define BLOCK_H #define SNAKE_DOWN DVector(0,1)
#define SNAKE_UP DVector(0,-1)
#define SNAKE_RIGHT DVector(1,0)
#define SNAKE_LEFT DVector(-1,0)
#define SNAKE_DRECTION DVector #define GAME_RESULT int
#define GAME_OVER 0
#define GAME_SUCCEED 1
#define GAME_CONTINUE 2 #define Egg Block
#define MAX_SNAKE_SIZE 25 #include "windows.h"
#include <vector>
#include <time.h> using namespace std; class DVector{
public:
int x;
int y;
public:
DVector(int x = 0,int y = 0):x(x),y(y){}
bool operator==(const DVector dve) const{
if(x == dve.x && y == dve.y)
return true;
return false;
}
bool operator!=(const DVector dve) const{
return !(*this == dve);
}
const DVector operator+(const DVector dve) const{
int x1 = x +dve.x;
int y1 = y +dve.y;
return DVector(x1,y1);
}
const DVector operator-(const DVector dve) const{
int x1 = x - dve.x;
int y1 = y - dve.y;
return DVector(x1,y1);
}
}; class Block{
public:
DVector position;
int side;
public:
Block(DVector position = DVector()):position(position){
side = 20;
}
void SetSide(int side = 20){
this->side = side;
}
int bottom(){
return (position.y+1) * side;
}
int top(){
return position.y*side;
}
int left(){
return position.x*side;
}
int right(){
return (position.x+1)*side;
}
void show(HDC hdc){
Rectangle(hdc,left(),top(),right(),bottom());
Rectangle(hdc,left()+5,top()+5,right()-5,bottom()-5);
}
}; class Snake{
private:
HDC hdc;
vector<Block> veb;
DVector snakehead;
Egg egg; //Egg为Block类型
bool movestate;
SNAKE_DRECTION drection;
int size;
private:
bool isSnakeBody(DVector dve,int k){
vector<Block>::iterator bit;
for(bit = veb.begin()+k; bit != veb.end(); bit++){
if(bit->position == dve)
return true;
}
return false;
}
bool overBound(){
if( snakehead.x >= 20 || snakehead.x < 0 ||
snakehead.y >= 20 || snakehead.y < 0)
return true;
return false;
}
bool SnakeDead(){
if(overBound()||isSnakeBody(snakehead,1))
return true;
return false;
}
public:
Snake(){
srand(time(0));
}
void InitSnake(){
movestate = true;
drection = SNAKE_RIGHT; //初始方向为SNAKE_RIGHT
veb.clear(); //清空蛇
veb.push_back(Block(DVector(3,0)));
veb.push_back(Block(DVector(2,0)));
veb.push_back(Block(DVector(1,0))); //初始长度为3
snakehead = veb.front().position;
getEgg();
size = 3;
}
void SetHDC(HDC hdc){
#ifndef BLOCK_H
#define BLOCK_H #define SNAKE_DOWN DVector(0,1)
#define SNAKE_UP DVector(0,-1)
#define SNAKE_RIGHT DVector(1,0)
#define SNAKE_LEFT DVector(-1,0)
#define SNAKE_DRECTION DVector #define GAME_RESULT int
#define GAME_OVER 0
#define GAME_SUCCEED 1
#define GAME_CONTINUE 2 #define Egg Block
#define MAX_SNAKE_SIZE 25 #include "windows.h"
#include <vector>
#include <time.h> using namespace std; class DVector{
public:
int x;
int y;
public:
DVector(int x = 0,int y = 0):x(x),y(y){}
bool operator==(const DVector dve) const{
if(x == dve.x && y == dve.y)
return true;
return false;
}
bool operator!=(const DVector dve) const{
return !(*this == dve);
}
const DVector operator+(const DVector dve) const{
int x1 = x +dve.x;
int y1 = y +dve.y;
return DVector(x1,y1);
}
const DVector operator-(const DVector dve) const{
int x1 = x - dve.x;
int y1 = y - dve.y;
return DVector(x1,y1);
}
}; class Block{
public:
DVector position;
int side;
public:
Block(DVector position = DVector()):position(position){
side = 20;
}
void SetSide(int side = 20){
this->side = side;
}
int bottom(){
return (position.y+1) * side;
}
int top(){
return position.y*side;
}
int left(){
return position.x*side;
}
int right(){
return (position.x+1)*side;
}
void show(HDC hdc){
Rectangle(hdc,left(),top(),right(),bottom());
Rectangle(hdc,left()+5,top()+5,right()-5,bottom()-5);
}
}; class Snake{
private:
HDC hdc;
vector<Block> veb;
DVector snakehead;
Egg egg; //Egg为Block类型
bool movestate;
SNAKE_DRECTION drection;
int size;
private:
bool isSnakeBody(DVector dve,int k){
vector<Block>::iterator bit;
for(bit = veb.begin()+k; bit != veb.end(); bit++){
if(bit->position == dve)
return true;
}
return false;
}
bool overBound(){
if( snakehead.x >= 20 || snakehead.x < 0 ||
snakehead.y >= 20 || snakehead.y < 0)
return true;
return false;
}
bool SnakeDead(){
if(overBound()||isSnakeBody(snakehead,1))
return true;
return false;
}
public:
Snake(){
srand(time(0));
}
void InitSnake(){
movestate = true;
drection = SNAKE_RIGHT; //初始方向为SNAKE_RIGHT
veb.clear(); //清空蛇
veb.push_back(Block(DVector(3,0)));
veb.push_back(Block(DVector(2,0)));
veb.push_back(Block(DVector(1,0))); //初始长度为3
snakehead = veb.front().position;
getEgg();
size = 3;
}
void SetHDC(HDC hdc){