The most popular PC games, as well as retro free online computer games, are available to download here at Games.lol. Fun free PC games to download date from today all the way back to 2009 when casual F2P games started to boom. Free Windows games always present itself as the 'comfort food' for casual players. Sep 23, 2020 Free Fire is the ultimate survival shooter game available on mobile. Each 10-minute game places you on a remote island where you are pit against 49 other players, all seeking survival. Players freely choose their starting point with their parachute, and aim to stay in the safe zone for as long as possible.
In this tutorial we will learn how to detect when a sprite collides with another. In our game we will make the ball bounce on the racquet. We will also make the game finish if the ball gets to the lower border of the canvas, showing a popup window with the classic message 'Game Over'.
Game Over
Below we can see our class Game, which is exactly the same as the previous one, with the only difference that this one has a gameOver() method;
The gameOver() method launches a popup using JOptionPane.showMessageDialog with the message 'Game Over' and an 'Accept' button. After the popup, System.exit(ABORT) makes the program finish. The gameOver() method is public, because it will be called from the sprite 'Ball' when it detects that it has got to the lower border of the canvas.
Sprite collision
To detect the collision between the ball and the racquet we will use rectangles. In the case of the ball we will use a square around the ball as you can see in the figure 2.
The class java.awt.Rectangle has an intersects method(Rectangle r) which returns true when two rectangles occupy the same space, like in the case of the figure 3 or 4. This method is not quite exact, because as you can see in the figure 4, the ball doesn´t touch the racquet, but for our example it it more than enough.
In this tutorial we will learn how to detect when a sprite collides with another. In our game we will make the ball bounce on the racquet. We will also make the game finish if the ball gets to the lower border of the canvas, showing a popup window with the classic message 'Game Over'.
Game Over
Below we can see our class Game, which is exactly the same as the previous one, with the only difference that this one has a gameOver() method;
The gameOver() method launches a popup using JOptionPane.showMessageDialog with the message 'Game Over' and an 'Accept' button. After the popup, System.exit(ABORT) makes the program finish. The gameOver() method is public, because it will be called from the sprite 'Ball' when it detects that it has got to the lower border of the canvas.
Sprite collision
To detect the collision between the ball and the racquet we will use rectangles. In the case of the ball we will use a square around the ball as you can see in the figure 2.
The class java.awt.Rectangle has an intersects method(Rectangle r) which returns true when two rectangles occupy the same space, like in the case of the figure 3 or 4. This method is not quite exact, because as you can see in the figure 4, the ball doesn´t touch the racquet, but for our example it it more than enough.
Below we can see the Racquet class, with the only difference that we have added a getBounds() method, which returns a rectangle type of object, indicating the position of the racquet. This method will be used by the sprite 'Ball', to know the position of the racquet and in this way to detect the collision.
Another small change is the inclusion of constants:
As we said before, the value of the 'y' position, was fixed to 330. This value is used both in the paint method as in getBounds. When we create a constant, the good thing is that if we want to change the value, we only have to change it in one place. In this way we avoid the possible error of changing it in one place and not changing it in another.
The way of defining a constant is declaring a 'static final' property and writing it in upper case. The compiler allows us to use lower case, but the standar says we use upper case for the constants.
Lastly, the Ball class:
In a similar way, we have included the getBounds() method and the DIAMETER constant to the class 'Racquet'.
More interesting is the inclusion of the new method called collision() which returns true, if the rectangle occupied by the racquet 'game.racquet.getBounds()' intersects with the rectangle occupied by the ball 'getBounds()'.
If the collision takes place, we will change the direction and the position of the ball. If the collision occurs on the side (figure 1), the ball could be several pixels below the upper side of the racquet. In the following game loop, even if the ball moved upwards (figure 2), it could still be in collision with the racquet.
To avoid this, we will place the ball on top of the racquet (figure 3) using:
Download Tai Game Beginner Smart Dots Online
The 'Racquet' getTopY() method gives us the position in the 'y' axis of the upper part of the racquet, and if we discount the DIAMETER, we obtain the exact position where to put the ball so that it is on top of the ball.
Download Tai Game Beginner Smart Dots App
Lastly, it is the move() method of the 'Ball' class which uses the new methods collision() and gameOver() of the 'Game' class. The bounce when it gets to the lower border is replaced by a call to game.gameOver().
And including a new conditional using the collision() method, we get the ball to bounce upwards if it collides with the racquet:
If we run the example we can see:
<< Adding the sprite 'racquet' | Adding sound to our game >> |