- Explain the purpose of the game loop's activities.
- Create a game controller and a model class.
- Code the callbacks: enlistEntities, setAppearance, onCollisionsPolled, and onOutOfBounds.
- Demonstrate the use of inheritance and abstract classes.
Exercises
Exercise 1A_SimpleSprite
In this exercise, you'll make a game window with a simple sprite in it.
- Extend the GameController and SpriteModel classes.
public class Exercise1A extends GameController {}
public class Alien extends SpriteModel {}
public Exercise1A() {
setTitle("Exercise 1A");
}
public Alien(int x, int y) {
super(x, y);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(()->new Exercise1A());
}
private Alien alien;
@Override
protected void enlistEntities() {
alien = new Alien(400, 300);
addEntity(alien);
}
private final String imageFile = "alien-3.png";
@Override
protected void setAppearance() {
setView(imageFile, 3);
}
The sprite's image file is named with a '-3.png'
indicating there are three animation images in the sprite sheet.
Explore res.drawable to examine the sprites available to you.The second parameter in the setView() method indicates how many sprite image frames you want to be animated.
Read about the
GameController
and
SpriteModel
in the API.
If your IDE autogenerates exception handling code for the abstract methods, you may get runtime errors. Keep the abstract methods but delete the exception code that is being triggered.
Exercise 1B_AnimatedSprite
In this exercise, you'll place a sprite on the screen and make it disappear when the spacebar is pressed. Also, make it so the escape key exits the game.
- Code the methods in the sequence diagram:
- Use
.setActive()to animate the sprite. - Implement AWT's KeyListener interface:
public class Exercise1B extends GameController implements KeyListener {}
public Exercise1B() {
setTitle("Exercise 1B");
addKeyListener(this);
}
@Override
public void keyTyped(KeyEvent e) {
int key = e.getExtendedKeyCode();
if (key == KeyEvent.VK_SPACE)
removeEntity(alien);
else if (key == KeyEvent.VK_ESCAPE)
System.exit(0);
}
Read about KeyListener in the API.
Exercise 1C_BouncingSprite
In this exercise, you'll place a sprite on the screen, set its x and y velocities, and start it moving. When it reaches the edge of the window, make it bounce and change direction.
- Use the sequence diagram:
- Use .setXVelocity() and .setYVelocity() on the sprites in your enlistEntities method.
- For each out of bounds event, find out which entity it is, which screen boundary it hit, then make it rebound.
@Override
protected void onOutOfBounds(OutOfBoundsEvent[] events) {
for (OutOfBoundsEvent oe : events) {
EntityModel em = oe.getEntity();
ScreenBoundary sb = oe.getBoundary();
Physics.rebound(em, sb);
}
}
Read about
outOfBoundsEvent,
ScreenBoundary, and
Physics
in the API.
Exercise 1D_CollidingSprite
In this exercise, place two randomly-moving sprites on the screen and make them bounce off the walls. When they collide with each other, reverse their velocities to make them bounce away from each other.
- Activate a bounce sound when they collide:
SoundEffects.BOUNCE.play();
This can be done in the onCollisionsPolled overridden method.
@Override
protected void onCollisionsPolled(CollisionEvent[] events) {
for (CollisionEvent ev : events) {
// add your code here
}
}
@Override
protected void onOutOfBounds(OutOfBoundsEvent[] events) {
for (OutOfBoundsEvent event : events) {
switch (event.getEdge()) {
case North:
case South:
// add your code here
break;
case East:
case West:
// add your code here
break;
default:
break;
}
}
}
Read about
CollisionEvent,
.getA() and .getB(),
Configuration.Direction, and
.getEdge()
in the API.
Challenges
Challenge 1E_SpriteLab
Make two randomly-moving sprites that disappear with a sound when they collide.