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.