A Galaga game developed in Java. GitHub Gist: instantly share code, notes, and snippets. A simple shooting game /. Essential Java 3D Fast Ian Palmer Publisher: Springer-Verlag ISBN: 1-85233-394-4./ import java.awt.AWTEvent; import java.awt.BorderLayout; import java.awt.Button. ปลื้ม ณัฐดนัย พุ่มพุทธ ADI God IS Gaming#AlienAttack #Nokia6280 #Funmobile #JavaGames #phonegame #mobilegame #oldgames #PranBuri #PrachuapKhiriKhan. Download free mobile game Cowboys & Aliens. Download java game on your mobile phone. All games 2011 year released. Many interesting games from the producer - Gameloft Interesting game Adventures and Action. Page Information: Download Ben 10 Alien Force game for mobiles - one of the best Java games! At PHONEKY Free Java Games Market, you can download mobile games for any phone absolutely free of charge. Nice graphics and addictive gameplay will keep you entertained for a very long time.
last modified July 20, 2020
In this part of the Java 2D games tutorial we will talk about collision detection.
Alien Quarantine Java Game
Many games need to handle collisions, especially arcade games. Simply said, we need to detect when two objects collide on screen.
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.Alien Shooter Java Game
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.
Alien Shooter Java Game 320x240
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. Aliens return to the screen on the right side after they have disappeared on the left.
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.
This chapter was about collision detection.