当精灵“掉落”时如何在 Java 游戏中调用 gameOver 方法 屏幕?

发布于 2024-07-13 04:47:29 字数 1115 浏览 5 评论 0原文

我正在使用 BlueJ 用 Ja​​va 开发一个简单的平台游戏。

在接下来的几周内,我将发布一些问题,寻求有关该项目的帮助,因为我不太精通编程。 所以我希望我不会变得烦人,如果你能帮助我,我将非常感激。

在我的游戏中,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

东走西顾 2024-07-20 04:47:29

入门指南:

  1. 考虑如何获得玩家当前的位置。
  2. 想想是什么让玩家跌倒(应该有一些方法/类来实现重力和跌倒?)
  3. 检查玩家跌落到你的世界边界时的位置。 如果它们超出了边界,就该调用 GameOver 方法了。

检查边界:

  1. 你的世界应该有一个最低高度,玩家必须保持在该高度以上才能生存。 记住这个值。
  2. 如果您获得玩家的垂直位置并与这个最小高度进行比较,您应该得到一个布尔结果(安全与死亡)。
  3. 请注意,屏幕位置从左上角开始,并随着您向右和向下移动而增加,因此您的玩家位置可能反映了这一点。 因此,您很可能正在寻找测试 (player.getPosition().Y > world.getMaxAllowedHeight());

供参考:请发布您已经尝试过的内容以及遇到问题的地方。 寻求一个填鸭式的解决方案对任何人都没有帮助——你必须自己考虑一下这个问题。 如果您不知道从哪里开始,请告诉我们; 我们可以尝试引导您走向正确的方向。

更新:听起来你已经做了一些思考,这太棒了! 抱歉,如果我太严厉地跳下你的喉咙,但你原来的帖子暗示了相反的情况。 不过,下一次,一定要尝试问一个更多“帮助我帮助自己”的问题,而不是“为我解决”的问题。 一个简单的“有人可以告诉我如何开始解决这个问题吗?” 会做。

To get you started:

  1. Think about how you might get the player's current position.
  2. Think about what makes the player fall (there should be some method/class that implements gravity and falling?)
  3. Check the player's position as they fall against the boundries of your world. If they're outside the boundries, it's time to call your GameOver method.

To check the boundries:

  1. Your world should have a minimum height that the player has to keep above to stay alive. Remember this value.
  2. If you get your player's vertical position and compare to this minimum height, you should get a boolean result (safe vs. dead).
  3. Note that screen position starts at the top left and increases as you go right and down, so you're player position probably reflects this. Thus, you're most likely looking for the test (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.

人疚 2024-07-20 04:47:29

为了帮助你做功课:

   // if the player goes out of bounds, the game is over
   if (player.getBounds().outside(game.getBounds()) 
   {
        System.out.println("YOU WENT OUT!");
        game.gameOver();
    }

假设玩家有某种边界区域并且游戏本身有某种区域......

To help you do your homework:

   // if the player goes out of bounds, the game is over
   if (player.getBounds().outside(game.getBounds()) 
   {
        System.out.println("YOU WENT OUT!");
        game.gameOver();
    }

assuming that the player has some kind of bounding area and the game itself has some kind of area...

燃情 2024-07-20 04:47:29

一个很好的问题是,“我的精灵在哪里?”。

下一个问题是“我的世界的边界是什么?”。

如果你能回答这两个问题,那么你应该能够回答你自己的问题。

假设你有一个二维世界,只要精灵的位置在水平边界内以及垂直边界内,那么你的精灵仍然在地图上,否则,他不在地图上。

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.

风柔一江水 2024-07-20 04:47:29

你有游戏循环吗?

通常在游戏中,有某种紧密循环来控制逻辑和渲染过程,诸如此类。

while(stillRunning)
{

   if(isPlayerDead()) 
   { 
       showGameOverScreen(); 
   }
   else
   {
       PerformLogic();
       RenderToScreen();
   }

   sleep(0);
}

您需要扩展 isPlayerDead() 方法来检查碰撞以及精灵的位置是否大于屏幕的高度。

Do you have a gameloop?

Usually in a game there is somekind of tightloop that controls the logic and rendering process, something like.

while(stillRunning)
{

   if(isPlayerDead()) 
   { 
       showGameOverScreen(); 
   }
   else
   {
       PerformLogic();
       RenderToScreen();
   }

   sleep(0);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文