|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是原码的一部分
struct Snake
{
Snake():next(NULL), before(NULL)
{
pt.x = 0; pt.y = 0;
}
Snake(const POINT &npt, Snake *, Snake *);
POINT pt;
Snake * next, * before;
} snake; //定义蛇头
Snake * psLast = NULL, * food = NULL;
void NewFood(HWND hwnd)
{
food = new Snake(); 原码在这儿用new申请了内存
SYSTEMTIME st;
GetLocalTime(&st);
srand(st.wMilliseconds);
food->pt.x = rand() % XWIDTH, food->pt.y = rand() % YHEIGHT;
while(GameClient[food->pt.x][food->pt.y])
{
GetLocalTime(&st);
srand(st.wMilliseconds);
food->pt.x = rand() % XWIDTH, food->pt.y = rand() % YHEIGHT;
}
GameClient[food->pt.x][food->pt.y] = true;
InvalidateRect(hwnd, NULL, TRUE);
}
//判断前方是否是食物
inline bool IsFood()
{
if(snake.pt.x == food->pt.x && snake.pt.y == food->pt.y)
return true;
else
return false;
}
//判断是否撞墙或者撞到自己身体
bool TouchWall()
{
Snake * temp = psLast;
//超越边界判断
if(snake.pt.x >= XWIDTH ||
snake.pt.x < 0 ||
snake.pt.y < 0 ||
snake.pt.y >= YHEIGHT)
return true;
//碰撞自身身体判断
while(temp != &snake)
{
if(snake.pt.x == temp->pt.x &&
snake.pt.y == temp->pt.y)
return true;
temp = temp->before;
}
return false;
}
void Move(HWND hwnd)
{
int x, y; //用于记录蛇头的当前位置(未前进时)
//标记前方是否为食物,默认为不是食物,表示系通常的前进
Snake * temp = psLast;
//记录蛇头当前坐标
x = snake.pt.x;
y = snake.pt.y;
//测试前进的地方是否是食物,同时将蛇头前进
switch(direct)
{
case UP:
--snake.pt.y;
break;
case DOWN:
++snake.pt.y;
break;
case LEFT:
--snake.pt.x;
break;
case RIGHT:
++snake.pt.x;
break;
default:
break;
}
//如果不是食物,作如下处理
if(!IsFood())
{
//最后一节不是蛇头
if(temp != &snake)
{
GameClient[temp->pt.x][temp->pt.y] = false;
while(temp != snake.next)
{
temp->pt.x = temp->before->pt.x;
temp->pt.y = temp->before->pt.y;
temp = temp->before;
}
temp->pt.x = x;
temp->pt.y = y;
GameClient[temp->pt.x][temp->pt.y] = true;
}
else //psLast == &snake,即蛇只有蛇头
GameClient[x][y] = false;
if(TouchWall())
IsOver = true; //则标记游戏结束
else
GameClient[snake.pt.x][snake.pt.y] = true; //不然移动后的蛇头位置标记为有方块了
InvalidateRect(hwnd, NULL, TRUE); //刷新游戏区
}
//如果是食物,作如下处理
else
{
++Score;
food->before = psLast; 把new申请的内存加入链表中
psLast->next = food;
if(temp != &snake)
{
food->pt.x = psLast->pt.x;
food->pt.y = psLast->pt.y;
while(temp != snake.next)
{
temp->pt.x = temp->before->pt.x;
temp->pt.y = temp->before->pt.y;
temp = temp->before;
}
//temp=snake.next;
temp->pt.x = x;
temp->pt.y = y;
}
else //psLast == &snake,即蛇只有蛇头
{
food->pt.x = x; food->pt.y = y;
}
psLast = food; 把申请的内存地址赋给别一个指针
NewFood(hwnd); 刷新食物,又会用到new
}
}
整个原码过程都不曾出现delete,这会不会造成内存泄露?
|
|