- Code realistic missile behavior in the onKeysPolled callback.
- Code an inner class.
- Code an anonymous class.
Exercises
Exercise 4A_ScrollingScenery
Make a screen with a scrolling scenery image.
- Extend a SceneryModel and set the View with 'sceneryB-1.png'.
- In enlistEntities, give your scenery a X velocity.
Read about
setXVelocity.
Exercise 4B_MovingShip
Make a ship move left and right at the bottom of the screen in response to keystrokes.
- Use 'ship-3.png'
Exercise 4C_FiringShip
Make a ship move left and right and fire missiles. Implement some kind of timing algorithm to make the ship wait a brief moment between firing missiles, This will make your games more challenging and realistic.
- Watch the video on this page for help on how to time the missiles.
- Here's a hint as to one possible implementation:
private void fireMissile() {
// Check if firing delay has elapsed to avoid cannon blasting
if (System.currentTimeMillis() - firingInstant > firingDelay) {
// locate missile away from the ship to avoid collision
missile = new Missile(// add code here);
missile.setActive(true);
missile.setYVelocity(missileVelocity);
firingInstant = System.currentTimeMillis();
SoundEffects.SHOOT.play();
addEntity(missile);
}
}
Exercise 4D_AnonymousAliens
Add two aliens to what you created in 4C. Keep the firing ship.
- Make the first alien move from right to left across the screen and continue to wrap around.
- Make a second alien that moves right to left. In onOutOfBounds, when the second alien reaches the west side of the screen, it should disappear. Call .dispose() which generates a "disposed" message event, then have onMessagesPolled generate a new alien on the east side at a random Y location, again moving left.
- Use an anonymous inner type SpriteModel to generate the sprites.
- Here's a hint:
private SpriteModel createAlien(String resource, int y) {
SpriteModel alien = new SpriteModel(// more here) {
// add required callbacks here
@Override
protected void setAppearance() {
// add more code here
}
};
return alien;
}
- Here's an object diagram of this exercise showing a snapshot in time:
Exercise 4E_Barrier
On the bottom half of the screen make a barrier down the center and an alien that starts right to left. When the alien hits the barrier for the first time, it should crawl up and around the barrier and continue moving to the left.
There are a couple different ways to do this. You could create an anonymous
inner alien and handle the onBlocked event in updateGameState. Or you could
create a concrete Alien class in its own java file and use updateParameters
to handle the blocked situations.
These design decisions can impact reusability, extensibility, and maintenance,
which all affect cost and development time. What are the advantages and
disadvantages of each design approach?
This type of barrier can be used in numerous ways including being placed horizontally on background scenery to make it look like someone’s walking on a ledge.
Challenges
Challenge 4F_UFOMissileGame
Make a game that displays a UFO at the left side of the screen. The spaceship should respond to up and down arrow keystrokes. Create an alien or aliens that move right to left. When the alien reaches the left edge, a new alien should appear at the right edge at a random Y location.
The ship should fire missiles eastward when the spacebar is pressed. When a missile collides with an alien, make an explosion appear briefly and make the alien disappear with a sound. Make both the alien and the ship disappear when the alien collides with the ship.
Use the ufo-7.png and dot-4.png image files to implement your sprites.
Use a static scenery background with fortresses to hide behind.
Add Barriers. When the alien bumps into a barrier, it should crawl along it and around it.
Challenge 4G_ObjectDiagram
Draw a UML object diagram of the UFO Missile Game. An object diagram depicts a snapshot in time, so not everything in your code will be shown in the diagram. You may want to include your ship, an alien or aliens, the explosion, and/or the missile object(s). Use pencil and paper or UMLet.