用队列实现贪吃蛇游戏逻辑
class form
{
public:
ontimer(); //定时处理
is_die(); //判断蛇是否死亡
move(); // 移动蛇
is_has_food(); //蛇头位置是否有食物
eat_food(); // 蛇吃当前位置的食物
display(); //显示蛇
private:
queueEx queue_; //用队列保存蛇各节的位置
}
ontimer()
{
move();
display();
if (is_die())
{
alert("you die!");
}
else
{
head_pos = queue_.head();
if (is_has_food( head_pos ))
{
eat_food();
}
}
}
move()
{
queue_.pop_tail();
head_pos = queue_.head();
++head_pos;
queue_.enqueue(head_pos);
}
eat_food()
{
head_pos = queue_.head();
++head_pos;
queue_.enqueue(head_pos);
}