当精灵“掉落”时如何在 Java 游戏中调用 gameOver 方法 屏幕?
我正在使用 BlueJ 用 Java 开发一个简单的平台游戏。
在接下来的几周内,我将发布一些问题,寻求有关该项目的帮助,因为我不太精通编程。 所以我希望我不会变得烦人,如果你能帮助我,我将非常感激。
在我的游戏中,我有一个 gameOver 方法,它可以结束游戏并向控制台打印一条语句。 我想要的是当我的玩家精灵“从屏幕上掉下来”时调用此方法。
现在,我在屏幕底部放置了一个矩形形状来代表某种危险(例如水),当玩家与其碰撞时,它会调用 gameOver 方法,游戏结束。 然而,我仍然希望如果玩家跌落到屏幕的某个高度以下,游戏就会结束。
这是我的 gameOver 方法(来自“Game”类) -
/** End the game. */
public void gameOver()
{
world.terminate();
isOver = true;
}
这是我的玩家精灵(当前使用多边形绘制) -
super(Utils.createPolygon(170, 102,
155, 118, 137, 118, 151, 134, 146,
140, 167, 140, 170, 135, 174, 140,
193, 140, 188, 134, 202, 118, 186,
118, 170, 102));
setColor(new Color(254, 167, 184));
这是代码(来自“Enemy”类,类似的代码也用于 Hazard 类)当玩家与敌人碰撞时结束游戏 -
if (e.contact.involves(player) && (player.getLives() < 1)) {
System.out.println("YOU ARE DEAD!");
game.gameOver();
}
我想要一个类似的或至少简单的解决方案,当我的玩家精灵掉落或超出屏幕的某个坐标时,它将调用我的 gameOver 方法。
如果您能提供示例/示例代码以及帮助我理解它的注释,以便让我了解我应该做什么来实现这一目标,我将不胜感激。
非常感谢您,我期待您的答复。
I am developing a simple platform game in Java using BlueJ.
Over the next few weeks I will be posting a few questions asking for help with this project as am not very proficient at programming. So I hope I don't become annoying and I would be very grateful if you can kindly help me.
In my game I have a gameOver method which ends the game and prints a statement to the console. What I would like is to call upon this method when my player sprite "falls off" the screen.
Right now I have a rectangle shape placed at the bottom of the screen to represent some kind of hazard (like water) and when the player collides with it, it calls the gameOver method and the game ends. However, I would still prefer if the game ended if the player fell below a certain height of the screen.
This is my gameOver method (from the 'Game' class)-
/** End the game. */
public void gameOver()
{
world.terminate();
isOver = true;
}
This is my player sprite (currently drawn using a polygon)-
super(Utils.createPolygon(170, 102,
155, 118, 137, 118, 151, 134, 146,
140, 167, 140, 170, 135, 174, 140,
193, 140, 188, 134, 202, 118, 186,
118, 170, 102));
setColor(new Color(254, 167, 184));
This is the code (from the 'Enemy' class, similar code is used in a Hazard class also) that ends the game when player collides with an enemy-
if (e.contact.involves(player) && (player.getLives() < 1)) {
System.out.println("YOU ARE DEAD!");
game.gameOver();
}
I would like a similar, or at least simple solution that will call on my gameOver method when my player sprite falls or goes beyond a certain coordinate of the screen.
I would appreciate it if you could kindly provide a sample/example code, with comments to help me understand it, in order to give an idea of what I should do to achieve this.
Thank you very much and I look forward to your answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
入门指南:
检查边界:
player.getPosition().Y > world.getMaxAllowedHeight()
);供参考:请发布您已经尝试过的内容以及遇到问题的地方。 寻求一个填鸭式的解决方案对任何人都没有帮助——你必须自己考虑一下这个问题。 如果您不知道从哪里开始,请告诉我们; 我们可以尝试引导您走向正确的方向。
更新:听起来你已经做了一些思考,这太棒了! 抱歉,如果我太严厉地跳下你的喉咙,但你原来的帖子暗示了相反的情况。 不过,下一次,一定要尝试问一个更多“帮助我帮助自己”的问题,而不是“为我解决”的问题。 一个简单的“有人可以告诉我如何开始解决这个问题吗?” 会做。
To get you started:
To check the boundries:
player.getPosition().Y > world.getMaxAllowedHeight()
);For reference: Please post what you've tried already and where you're having problems. Asking for a spoonfed solution isn't going to help anyone - you're going to have to think about this a bit yourself. If you have no idea where to start, tell us that instead; we can try to steer you in the right direction.
Update: Sounds like you've done a bit of thinking already, which is great! Sorry if I was harsh to jump down your throat, but your original post suggested otherwise. Next time, though, do try to ask a more help-me-help-myself question than a solve-it-for-me one. A simple "Can someone give me an idea of how to start tackling this problem?" will do.
为了帮助你做功课:
假设玩家有某种边界区域并且游戏本身有某种区域......
To help you do your homework:
assuming that the player has some kind of bounding area and the game itself has some kind of area...
一个很好的问题是,“我的精灵在哪里?”。
下一个问题是“我的世界的边界是什么?”。
如果你能回答这两个问题,那么你应该能够回答你自己的问题。
假设你有一个二维世界,只要精灵的位置在水平边界内以及垂直边界内,那么你的精灵仍然在地图上,否则,他不在地图上。
A good question to ask yourself is, "Where is my sprite?".
The next question is, "What are the boundaries of my world?".
If you can answer those two questions, then you should be able to answer your own question.
Assuming that you have a two-dimensional world, as long as the location of the sprite is within the horizontal boundaries as well as within the vertical boundaries, then your sprite is still on the map, otherwise, he's not.
你有游戏循环吗?
通常在游戏中,有某种紧密循环来控制逻辑和渲染过程,诸如此类。
您需要扩展 isPlayerDead() 方法来检查碰撞以及精灵的位置是否大于屏幕的高度。
Do you have a gameloop?
Usually in a game there is somekind of tightloop that controls the logic and rendering process, something like.
You will need to expand on the isPlayerDead() method to check for collisions and if the location of your sprite is greater than the height of the screen.