Java Block Game

broken image


last modified July 20, 2020

In this part of the Java 2D games tutorial we will talk about collision detection.

Java+You, Download Today! Java Download » What is Java? » Uninstall About Java. Games for tomorrow's programmers. Block-based programming lessons for beginners. Free from Google. An implementation of the popular Snake vs Block game in Java using JavaFX and FXML - Ashwin-19/SnakeVSBlock-Game.

Many games need to handle collisions, especially arcade games. Simply said, we need to detect when two objects collide on screen.

Block

In the next code example, we will expand the previous example. We add a new Alien sprite. We will detect two types of collisions: when the missile hits an alien ship and when our spacecraft collides with an alien.

Shooting aliens

In the example, we have a spacecraft and aliens. We can move the spacecraft on the board using the cursor keys. Missiles destroying aliens are launched with the spacebar key.

Sprite.java

The code that can be shared by all sprites (a craft, an alien, and a missile)is placed in the Sprite class.

The getBounds() method returns the bounding rectangle of the sprite image. We need the bounds in collision detection.

This class represents a spacecraft.

All the missiles fired by the spacecraft are storedin the missiles list.

When we fire a missile, a new Missile object isadded to the missiles list. It is retained in the listuntil it collides with an alien or goes out of the window.

Board.java

This is the Board class.

These are the initial positions of alien ships.

The initAliens() method creates a list of alien objects.The aliens take their initial positions from the pos array.

Inside the paintComponent() method, we either draw game sprites orwrite the game over message. This depends on the ingame variable.

The drawObjects() method draws game sprites on the window.First, we draw the craft sprite.

Block

In the next code example, we will expand the previous example. We add a new Alien sprite. We will detect two types of collisions: when the missile hits an alien ship and when our spacecraft collides with an alien.

Shooting aliens

In the example, we have a spacecraft and aliens. We can move the spacecraft on the board using the cursor keys. Missiles destroying aliens are launched with the spacebar key.

Sprite.java

The code that can be shared by all sprites (a craft, an alien, and a missile)is placed in the Sprite class.

The getBounds() method returns the bounding rectangle of the sprite image. We need the bounds in collision detection.

This class represents a spacecraft.

All the missiles fired by the spacecraft are storedin the missiles list.

When we fire a missile, a new Missile object isadded to the missiles list. It is retained in the listuntil it collides with an alien or goes out of the window.

Board.java

This is the Board class.

These are the initial positions of alien ships.

The initAliens() method creates a list of alien objects.The aliens take their initial positions from the pos array.

Inside the paintComponent() method, we either draw game sprites orwrite the game over message. This depends on the ingame variable.

The drawObjects() method draws game sprites on the window.First, we draw the craft sprite.

In this loop we draw all aliens; they are drawn only if they have not been previously destroyed. This is checked by the isVisible() method.

In the top-left corner of the window, we draw how many aliens are left.

The drawGameOver() draws a game over message in themiddle of the window. The message is displayed at the end of the game,either when we destroy all alien ships or when we collide with one ofthem.

Each action event represents one game cycle. The game logic is factored into specific methods. For instance, the updateMissiles() moves all the available missiles.

Inside the updateAliens() method, we first check if there are any alien objects left in the aliens list. The game is finishedif the list is empty. If it is not empty, we go trough the list and moveall its items. The destroyed aliens are removed from the list.

The checkCollisions() method checks for possible collisions. First, we check if the craft object collides with any of the alien objects.We get the rectangles of the objects with the getBounds() method.The intersects() method checks if the two rectangles intersect.

This code checks the collisions between missiles and aliens.

This is the Alien class.

Java Block Games

Aliens return to the screen on the right side after they have disappeared on the left.

Java Block Game Online

Missile.java

This is the Missile class.

Missiles move in one direction only. They disappear after they reachthe right window border.

Finally, this is the main class.

Java Block Application

This chapter was about collision detection.





broken image