Home > Hitchhiker's Guide to Games, Software Development > Lab[2] = Make it Collide

Lab[2] = Make it Collide

Objective

  • Run the Jaalaga starting code on a device
  • Detect collisions between missiles and enemies
  • Establish good values for the ship’s speed and missile speed with the expanded game loop
  • Pick better sprites for the enemy ships

What You Will Need

Lab Version

  • Version 0.5

Prerequisite

In Lab[0] we setup a development environment. Our development environment at this point includes several software tools, the AndEngine source code and our Android or Kindle Device. Among the code was a project with a wide variety of AndEngine examples. You should have reached a point where you ran these examples on your device. If you did not reach this point you should get Lab[0] working before tackling this lab because it requires a working AndEngine setup.

Introduction

In this lab we move beyond what we learned in Lab[1]. A new red button has been added that causes ‘missiles’ to fire from the player’s ship to the newly added enemies. Some basic collision detection has already been added to the code restricting the ships movement to the camera bounds. What is missing right now is the collision detection between the player’s missiles and the enemy ships.

The image below displays the type of thing you should see when first running the code for this lab.

Screenshot_2013-11-11-00-00-08

Steps

Run the Starting Code for Lab 2

First you will need to get the starting code from Git Hub at the link described above. Add this project to your working setup from Lab[0]. Note, if you changed the ship sprite by finishing the last lab, you can update just the “src” folder and the “AndroidManifest.xml” file.  Compile and run the code and you should see something like the screen above.

Examine TODO Items

The source code for this lab and future labs will be marked with comments that have TODO at the beginning. This lab has six such comments

lab_2_todo

Collision Detection for Missiles and Enemy Ships

First, as be sure that you have tried to shoot a ship. What happens? A less than interesting  event happens as the graphic for the missile travels right over the enemy sprite. For this part of the lab will actually try to write some code. Step by step instructions will be given, so don’t panic.

The general algorithm, or approach to solving a problem, we will implement is as follows:

For each missile, iterate over every enemy ship and determine if a collision has occurred. When a collision has occurred, remove both the missile and enemy from the screen.

The procedure above is represented in the flow chart below. Note that this flow also includes hiding rockets that have left the screen. The code you need to add to accomplish this flow is provided below.

lab_2_procedure

lab_2_code

You should add the code above to your project and run the game to see that missile impacts now cause the enemy ships to disappear. Some notes on the code above:

  • A “for loop” is a way to do things in a repeated fashion. It is of the form “for( initial value; condition; end of loop action)”. The normal practice is for loops to count upwards, but this loop is a specific scenario where we may need to remove items and we don’t want to lose our place.
    1. We set the variable j to the last item in the enemies list
    2. We check to see if j is greater than or equal to zero. In other words we check to see that if we have more enemies.
    3. If we still have an enemy, we we execute all instructions in the loop, else we exit
    4. At the end of the loop we reduce the value of j so we can access the next enemy
    5. Return to step 2
  • The recycle call in theory should return the object used for the missile to the pool to be reused. This is because making too many objects can slow down your program as “garbage” builds up and must be cleaned up. That is the theory, but in reality there is a bug in the RocketPool right now that means that the recycling/reuse is not happening. Expect this to be fixed in the next lab.
  • You effectively “remove” something from the screen by hiding it and turning off updates.

Change Ship and Missile Speed

At this point, you may be saying, “I already did that in lab 1!” The need to repeat the exercise is three fold.

  1. The code changed in a fairly dramatic fashion. We could have had a lesson on using a difference tool, but I figured you did not have the time or interest.
  2. The game loop has now grown and the old values may no longer work well.
  3. Repetition strengthens neural pathways

Change Sprites

Something as simple as MS Paint can be used to change graphics. You can also use more advanced image editing software like GIMP (http://www.gimp.org/)

  • /assets/gfx/button01.png (Width: 512 px, Height: 64 px)
    • Normal and pressed graphic
  • /assets/gfx/menu/logo.png (Width: 240 px, Height: 240 px)
  • /assets/gfx/game/ship.png (Width: 64 px, Height 64 px)
  • /assets/gfx/game/enemy1.png (Width: 32px, Height: 32px)
  • /assets/gfx/game/enemy2.png (Width: 32px, Height: 32px)
  • /assets/gfx/game/enemy3.png (Width: 32px, Height: 32px)
  • /assets/gfx/game/playerRocket.png (Width: 5px, Height: 15px)
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: