早期作品......
#include <windows.h>
#include <list>
#include <cstdio>
#include <ctime>
using namespace std;
class CMap
{
public:
CMap();
int GetMapWidth() const ;
int GetMapHeight() const;
public:
int m_mapWith;
int m_mapHeiht;
int m_map[20][20];
};
CMap::CMap():m_mapWith(15), m_mapHeiht(15)
{
for(int i=0; i<m_mapWith; i++)
{
for(int j=0; j<m_mapHeiht; j++)
{
m_map[0][0] = 0;
}
}
}
int CMap::GetMapWidth() const
{
return this->m_mapWith ;
}
int CMap::GetMapHeight() const
{
return this->m_mapHeiht ;
}
class CFood
{
public:
CFood(CMap &map, int typeNum);
void GrowFood( CMap &map, int foodNum, int typeNum);
public:
int m_x;
int m_y;
int m_typeOfFood;
int m_numOfFood;
};
CFood::CFood(CMap &map, int typeNum)
{
srand( time(0) );
GrowFood(map, 5, typeNum);
}
void CFood::GrowFood(CMap &map, int foodNum, int typeNum)
{
m_numOfFood = foodNum;
for(int i=0; i<foodNum*2; i++)
{
int x = map.GetMapWidth();
int y = map.GetMapHeight();
m_x = rand() % x;
m_y = rand() % y;
m_x > map.GetMapWidth() - 4 ? m_x -= 2: m_x;
m_x < 3? m_x = 2 : m_x;
m_y > map.GetMapWidth() - 4 ? m_y -= 2: m_y;
m_y < 3? m_y = 2 : m_y;
map.m_map[m_x][m_y] = 1; //食物
m_typeOfFood = rand() % typeNum;
}
}
class CSnake
{
public:
CSnake();
void SnakeMove(HWND hWnd, CMap &map,CFood &food, WPARAM key);
int GetSnakeNodeNum();
void SnakeMoveLoop(CMap map);
void InitSnake();
void GetMoveSpeed(HWND hWnd);
struct stSnakeNode
{
int x;
int y;
int color;
int direction;
};
protected:
public:
bool m_isAutoMove;
bool m_isMove;
list<stSnakeNode> m_head;
list<stSnakeNode>::iterator m_snakeIter;
list<stSnakeNode>::iterator iter;
UINT m_curDirection;
int m_moveSpeed;
};
void CSnake::InitSnake()
{
srand(time(0));
m_head.clear();
int snakeLen = 8;
stSnakeNode stSN1;
for(int i=0; i<snakeLen; i++)
{
stSN1.x = snakeLen - i - 1;
stSN1.y = 0;
stSN1.direction = VK_RIGHT ;
stSN1.color = i % 5;
m_head.push_back(stSN1);
}
m_curDirection = VK_LEFT;
}
void CSnake::SnakeMove(HWND hWnd, CMap &map,CFood &food, WPARAM key)
{
list<stSnakeNode>::iterator i;
static UINT d;
list<UINT> direction;
list<UINT>::iterator iter;
switch( key )
{
case VK_LEFT:
case VK_RIGHT:
case VK_DOWN:
case VK_UP:
i = m_head.begin();
//检测是否能移动
if( (((m_head.begin()->direction == VK_RIGHT && key != VK_LEFT ) ) ||
((m_head.begin()->direction == VK_LEFT && key != VK_RIGHT ) ) )||
(((m_head.begin()->direction == VK_DOWN && key != VK_UP) ) ||
((m_head.begin()->direction == VK_UP && key != VK_DOWN) ) )
)
{
static int snakeColor = 0;
d = key;
//是否吃到食物
if( map.m_map[m_head.begin()->x][ m_head.begin()->y])
{
map.m_map[m_head.begin()->x ][ m_head.begin()->y] = 0;
stSnakeNode stSnake;
stSnake.color = (snakeColor % 5) ;
#include <windows.h>
#include <list>
#include <cstdio>
#include <ctime>
using namespace std;
class CMap
{
public:
CMap();
int GetMapWidth() const ;
int GetMapHeight() const;
public:
int m_mapWith;
int m_mapHeiht;
int m_map[20][20];
};
CMap::CMap():m_mapWith(15), m_mapHeiht(15)
{
for(int i=0; i<m_mapWith; i++)
{
for(int j=0; j<m_mapHeiht; j++)
{
m_map[0][0] = 0;
}
}
}
int CMap::GetMapWidth() const
{
return this->m_mapWith ;
}
int CMap::GetMapHeight() const
{
return this->m_mapHeiht ;
}
class CFood
{
public:
CFood(CMap &map, int typeNum);
void GrowFood( CMap &map, int foodNum, int typeNum);
public:
int m_x;
int m_y;
int m_typeOfFood;
int m_numOfFood;
};
CFood::CFood(CMap &map, int typeNum)
{
srand( time(0) );
GrowFood(map, 5, typeNum);
}
void CFood::GrowFood(CMap &map, int foodNum, int typeNum)
{
m_numOfFood = foodNum;
for(int i=0; i<foodNum*2; i++)
{
int x = map.GetMapWidth();
int y = map.GetMapHeight();
m_x = rand() % x;
m_y = rand() % y;
m_x > map.GetMapWidth() - 4 ? m_x -= 2: m_x;
m_x < 3? m_x = 2 : m_x;
m_y > map.GetMapWidth() - 4 ? m_y -= 2: m_y;
m_y < 3? m_y = 2 : m_y;
map.m_map[m_x][m_y] = 1; //食物
m_typeOfFood = rand() % typeNum;
}
}
class CSnake
{
public:
CSnake();
void SnakeMove(HWND hWnd, CMap &map,CFood &food, WPARAM key);
int GetSnakeNodeNum();
void SnakeMoveLoop(CMap map);
void InitSnake();
void GetMoveSpeed(HWND hWnd);
struct stSnakeNode
{
int x;
int y;
int color;
int direction;
};
protected:
public:
bool m_isAutoMove;
bool m_isMove;
list<stSnakeNode> m_head;
list<stSnakeNode>::iterator m_snakeIter;
list<stSnakeNode>::iterator iter;
UINT m_curDirection;
int m_moveSpeed;
};
void CSnake::InitSnake()
{
srand(time(0));
m_head.clear();
int snakeLen = 8;
stSnakeNode stSN1;
for(int i=0; i<snakeLen; i++)
{
stSN1.x = snakeLen - i - 1;
stSN1.y = 0;
stSN1.direction = VK_RIGHT ;
stSN1.color = i % 5;
m_head.push_back(stSN1);
}
m_curDirection = VK_LEFT;
}
void CSnake::SnakeMove(HWND hWnd, CMap &map,CFood &food, WPARAM key)
{
list<stSnakeNode>::iterator i;
static UINT d;
list<UINT> direction;
list<UINT>::iterator iter;
switch( key )
{
case VK_LEFT:
case VK_RIGHT:
case VK_DOWN:
case VK_UP:
i = m_head.begin();
//检测是否能移动
if( (((m_head.begin()->direction == VK_RIGHT && key != VK_LEFT ) ) ||
((m_head.begin()->direction == VK_LEFT && key != VK_RIGHT ) ) )||
(((m_head.begin()->direction == VK_DOWN && key != VK_UP) ) ||
((m_head.begin()->direction == VK_UP && key != VK_DOWN) ) )
)
{
static int snakeColor = 0;
d = key;
//是否吃到食物
if( map.m_map[m_head.begin()->x][ m_head.begin()->y])
{
map.m_map[m_head.begin()->x ][ m_head.begin()->y] = 0;
stSnakeNode stSnake;
stSnake.color = (snakeColor % 5) ;