文字游戏角色碰撞检测问题
我正在制作一款游戏,当你向下移动屏幕时,你要避开由“X”组成的墙壁,如果你击中了“X”,你就会失去一个盾牌,并继续穿过“X”而不会失去更多的盾牌,直到你弹出到一个角色空间中。 但是,当运行下面的代码
以及其他一些不必要的代码时,如果我在 Visual Studio 中放置了停止符,那么它会完美地工作,但是当我在代码运行时开始碰到“X”墙时,墙壁移动得非常快,它不起作用,你最终会进行犁耕,但你会在这个过程中失去所有护盾,并且当你“犁”过它们时,X不会消失。
这是代码:
char coll; // the buffer for the collided char
if (theScreen.check_collision(player, xMove, yMove, coll) != true) {
// no collision
// get rid of old player placing
theScreen.Insert(' ', player.get_location()[0], player.get_location()[1]);
// put in new charater placing
player.move(xMove, yMove);
theScreen.Insert(player);
numb_coll = 0;
} else {
// collision
if (coll == 'X' && player.get_shield() > 0) {
for (int I = 0; I < numb_coll && numb_coll < player.get_location()[1]; I++) {
theScreen.Insert(' ', player.get_location()[0],
player.get_location()[1] - (I + 1));
}
if (numb_coll == 0)
player.set_shield(player.get_shield() - 1);
numb_coll++;
theScreen.Insert(player);
}
if (coll == 'X' && player.get_shield() <= 0)
dead = true;
};
I'm making a game where you avoid walls made of 'X's as you move down the screen, and if you hit an X you lose a shield and keep plowing through the X's without losing any more shields until you pop out into a character space where there isn't an X.
However, when running the code below along with some other unneccesary code it woks perfectly if I have stops placed in Visual Studio, but when I start to hit an 'X' wall while the code is running and walls moving quite fast it doesn't work right and you end up doing the plowing, but you lose all shields in the proccess and the X's don't disappear as you "plow" through them.
Here's the code:
char coll; // the buffer for the collided char
if (theScreen.check_collision(player, xMove, yMove, coll) != true) {
// no collision
// get rid of old player placing
theScreen.Insert(' ', player.get_location()[0], player.get_location()[1]);
// put in new charater placing
player.move(xMove, yMove);
theScreen.Insert(player);
numb_coll = 0;
} else {
// collision
if (coll == 'X' && player.get_shield() > 0) {
for (int I = 0; I < numb_coll && numb_coll < player.get_location()[1]; I++) {
theScreen.Insert(' ', player.get_location()[0],
player.get_location()[1] - (I + 1));
}
if (numb_coll == 0)
player.set_shield(player.get_shield() - 1);
numb_coll++;
theScreen.Insert(player);
}
if (coll == 'X' && player.get_shield() <= 0)
dead = true;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议对代码进行彻底的重新组织。此例程似乎涉及在屏幕上绘制玩家和更改玩家的护盾等级。我相信,如果您将播放器的显示与播放器的修改分开,您的代码将更易于阅读和维护。
考虑一下:
在您的
player
对象中:然后这段代码可能看起来更像是:
这些修改的想法(以伪代码呈现,因为我很懒,而且我不知道到底是什么其余代码看起来像)是将屏幕显示与游戏机制分开。 (您以前可能听说过“模型-视图-控制器”这个术语——这种模式准确地描述了我的建议。将业务规则移出您的视图。: )
I suggest a drastic re-organization of the code. This routine appears to be concerned with both drawing the player on the screen and changing the player's shield rating. I believe if you separate the display of the player from the modification of the player, your code will be easier to read and maintain.
Consider this:
In your
player
object:And then this code could look more like:
The idea of these modifications (rendered in pseudo-code, because I'm lazy and because I don't know exactly what the rest of your code looks like) is to separate the screen display from the game mechanics. (You've probably heard the term Model-View-Controller before -- this pattern accurately describes what I'm suggesting. Move the business rules out of your views. :)