?? greedysnake.cpp
字號:
// GreedySnake.cpp: implementation of the CGreedySnake class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Snake.h"
#include "GreedySnake.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGreedySnake::CGreedySnake()
{
m_currentLen=START_LEN; //初始化蛇長
}
CGreedySnake::~CGreedySnake()
{
}
void CGreedySnake::MoveUp()
{
if(m_down) return;
m_up=true;
m_down=false;
m_left=false;
m_right=false;
}
void CGreedySnake::MoveDown()
{
if(m_up) return;
m_up=false;
m_down=true;
m_left=false;
m_right=false;
}
void CGreedySnake::MoveLeft()
{
if(m_right) return;
m_up=false;
m_down=false;
m_left=true;
m_right=false;
}
void CGreedySnake::MoveRight()
{
if(m_left) return;
m_up=false;
m_down=false;
m_left=false;
m_right=true;
}
void CGreedySnake::KeepMoving()
{
if(!m_up&&!m_down&&!m_left&&!m_right) //不移動
return;
m_preEnd=m_body[m_currentLen-1]; //將移動前的尾坐標保存
for(int i=m_currentLen-1;i>0;i--)
{
m_body[i]=m_body[i-1]; //后一節=前一節坐標(蛇頭另外處理)
}
//-----處理蛇頭坐標
//向上走
if(m_up)
{
m_body[0].y-=1;
}
//向下走
if(m_down)
{
m_body[0].y+=1;
}
//向左走
if(m_left)
{
m_body[0].x-=1;
}
//向右走
if(m_right)
{
m_body[0].x+=1;
}
}
bool CGreedySnake::IsTouch(int max_x,int max_y)
{
for(int i=1;i<m_currentLen;i++)
{
if( m_body[0].x>=max_x||m_body[0].x<0|| //檢查是否撞到墻
m_body[0].y>=max_y||m_body[0].y<0|| //
(m_body[i].x==m_body[0].x&&m_body[i].y==m_body[0].y) //檢查是否撞到身體
)
return false;
}
return true;
}
bool CGreedySnake::IsEat(CPoint food)
{
if(m_body[0]==food) //吃到食物
return true;
return false;
}
int CGreedySnake::GetBodyLen()
{
return m_currentLen; //返回當前身長
}
void CGreedySnake::InitSnake(int max_x, int max_y)
{
//初始 不動
m_up=false;
m_down=false;
m_left=false;
m_right=true;
m_currentLen=START_LEN;
m_preEnd=0;
//計算一個合適的位置放置蛇
CPoint head;
head.x=max_x-(max_x-START_LEN)/2-1;
head.y=max_y/2;
for(int i=0;i<START_LEN;i++)
{
m_body[i].x=head.x-i;
m_body[i].y=head.y;
}
}
void CGreedySnake::Grow()
{
//生長,新增的一節保存之前的尾巴坐標
m_body[m_currentLen]=m_preEnd;
m_currentLen++; //蛇身長計數+1
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -