如何解决如何编写代码来显示谁赢得了比赛
我为两个玩家制作了一个大炮射击游戏,目的是打破城堡,然后尝试将炮弹落在对手身上,但问题是如何编写代码来显示谁赢得了比赛游戏结束了,我只知道代码需要在游戏结束部分编写,我也是代码新手
void CMyGame::OnUpdate()
{
if (m_mode == MODE_SHOOT)
{
/////////////////////////////////////////////////
// this code will only be executed while shooting
// Todo: 1. Add the gravitation
m_ball.Accelerate(0,-8);
// Todo: 2. Check if cannon hit? game over?
if (m_ball.HitTest(m_cannons[1 - m_turn]))
{
GameOver();
}
// Todo: 3. Check if the ball is outside the screen. Test the following cases:
// - ball to the left of the screen
if (m_ball.GetX() < 0)
NextTurn();
// - ball to the right of the screen (screen width = 800)
if (m_ball.GetX() > 800)
NextTurn();
// - ball below the screen
if (m_ball.GetY() < 0)
NextTurn();
// Todo: 4. Check if the ball hit the castle?
for each (CSprite * pSprite in m_castle)
{
if (m_ball.HitTest(pSprite))
NextTurn();
}
}
// Check if the castle has taken damage
for (CSprite* pSprite : m_castle)
{
if (m_ball.HitTest(pSprite))
pSprite->Delete();
}
m_castle.delete_if(deleted);
// Update the ball's position
m_ball.Update(GetTime());
// called when Game is Over
void CMyGame::OnGameOver()
{
m_cannons[1 - m_turn]->Setimage("fire");
m_mode = MODE_GAMEOVER;
}
解决方法
如果没有看到您的其余代码和有关您的游戏如何运作的信息,很难说,但您可以按照以下步骤操作:
假设您可以检查大炮何时降落在某个地方,并且可以跟踪每个玩家的生命百分比(我相信您可以这样做,因为您已经在给定的代码中规划了重力):
编写一个函数,每次炮弹落地时都会调用该函数,检查炮弹是否对对手的角色造成了最后一击。根据你的说法,在城堡被摧毁之前,你可能不需要做这个检查。因此,函数开头的 if(!castleBroken) {return;}
行只会在城堡被破坏时检查对手的 HP(再次假设对手只能在城堡被破坏时受到伤害)。在这里, CastleBroken 是一个布尔值,当大炮破坏了对手的城堡时,它开始为假并变为真。
这与我过去写的井字游戏类似,每次移动后,我都会检查棋盘状态。如果游戏结束,游戏结束屏幕上会显示获胜者,或者在某些情况下是平局。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。