Lesson 5: Composite Movement

Learning Objectives:
  • Code a cluster of sprites that behaves as a group.
  • Demonstrate the use of aggregation.

Exercises

Exercise 5A_RotatingShip


Make a ship in the center of the screen rotate when the left and right arrow keys are pressed.

Static: Rotating ship
Animated: Rotating ship

Exercise 5B_FiringRotating


Make the ship fire a missile in the direction it is facing. You’ll have to use vector dynamics for this.

Static: Firing ship
Animated: Firing ship

Exercise 5C_SpaceRockCluster


Make two separate space rocks that appear and move in a random direction wrapping around the screen. Make a cluster of four space rocks that bounces as a group off the edge walls.

ShowCollisionBounds on the rock in the cluster that is closest to the ship at all times.

Static: Ship firing at space rock cluster
Animated: Ship firing at space rock cluster

Exercise 5D_RockExplosions


When a missile hits a lone space rock or a space rock in the cluster, make an explosion appear and the single rock disappear. When the ship gets hit by a space rock, make the ship and rock both explode, then disappear.

During the entire game, showCollisionBounds on the rock in the cluster that is closest to the ship.

Static: Ship firing at and exploding rocks
Animated: Ship firing at and exploding rocks

Challenges

Challenge 5E_SpaceAliensTest


Code a Space Aliens game from the UML class diagram provided.

Space Aliens class diagram

The game should have the following features:

  • A spaceship that can move left/right along the baseline using the A/D and arrow keys. It should fire missiles when the space bar is pressed.
  • An army of aliens that march towards the baseline (bottom of the screen). The army of aliens should march horizontally, stepping forward when it gets to the edge of the screen. When an alien is hit by a spaceship missile, it should explode and disappear.
  • ShowCollisionBounds on the “frontline” of aliens. The frontline is always the highest ranking in each file. Note that the “front line” is not necessarily a straight line:
Frontline in alien army
  • Test this out by trying to shoot an alien behind the frontline. Make sure the frontline remains unchanged.
  • Challenge 5F_SpaceAliens


    Add the following features to your 5E_SpaceAliensTest:

    • The frontline aliens should fire ‘fireballs’ back at the spaceship. Only the front line aliens should fire. In addition, an alien should not fire at another alien.
    • The spaceship should explode if hit by a fireball thrown by an alien. It should also explode if bumped by an alien.
    Static: Space Aliens game
    Animated: Space Aliens game

    More

    Top