Skip to main content
Department of Information Technology

Computer games development I (Summer 2008)

Lab assignment

As a preparation for your game project you must complete this lab.

Purpose
The objective of this lab is that you learn the basics of animation. This knowledge will be useful for the project. After completing this lab you should be familiar with sprites and collision detection. You will also learn to automate the build process with Java ANT.
Method
You will implement an animation in a Java programming framework provided by Brackeen (the course book author) and us.
Preparation
Get your hands on a copy of the course book "Developing Games in Java" by David Brackeen". Read through the introduction, chapter one and chapter two. Make sure you have a working Java environment and Java ANT (build tool) installed. If you are having trouble installing ANT on Windows, see the section about ANT here and read this page.
Examination
You will solve the task in groups of 2 students and demonstrate your solutions (programs) to one of us course assistants. When solving these tasks, it is allowed to use code from the book. Regardless of whether you use code from the book, you should at the examination know how to solve these tasks, and related tasks. You should be able to explain to us how to implement variants of the program.
photo_of_the_hero.jpg

Instructions

The task is split up in the following parts:

  1. Show one animated object in a window, e.g. a winking smiley, or the images below.
  2. Automate the build process with Java ANT.
  3. Make the object move around and bounce off the window boundaries.
  4. Add more moving animated objects, and make them bounce off each other.

Part 1: Get started

NOTE: If you are working in the IT PC Lab (P1312 or P1313) read this before you continue:

Background material: The course book p. 22-55 (especially p. 47-55).

  • Download the following zip file containing the code you need to get started. lab1.zip
  • Extract the zip file and open a console and place yourself in the resulting lab1 directory.
  • It's a good habit to separate .java files and the .class files that the java compiler creates. Hence make a new directory called build:
$>mkdir build
  • Now you can use the java compiler to compile all the necessary files:
$>javac -sourcepath src -g:none -d build src\*.java
Inside the build directory you should now have the following class files:

     Animation$AnimFrame.class
     Animation.class
     Main.class
     ScreenManager$1.class
     ScreenManager.class

(Do you remember why there are two class files with $-signs in their names?)
  • To run the program:
$>java -classpath build Main
  • Before you make any changes to the code, a test run should look like this:

test_run.jpg

Note how the little blue hero in the upper left corner twinkles his eye and how the wind fly through his hair (animation).

Part 2: Java ANT

  • Once you got a working ANT you can compile by issuing the following command at the promt:
$>ant

Now ANT will get to work and after some output telling you what is going on, you will hopefully see:

BUILD SUCCESSFUL

Now you can run the program:

$>java -classpath build Main
  • You can use ANT to do more than just compile your code. Have a look at the ANT build file build.xml. To make it simpel to test and run your code you can add the following to build.xml:

    <!-- =================================================================== -->
    <!-- Run								     -->
    <!-- =================================================================== -->
    <target name="run">
      <java classpath="build" classname="Main" fork="true"/>
    </target>

    <!-- =================================================================== -->
    <!-- Test                                                                -->
    <!-- =================================================================== -->
    <target name="test" depends="compile">
      <antcall target="run"/>
    </target>

From the promt you can now use ant to compile and runt your program:
$>ant test
If you want to learn more about ANT, the ANT manual might be useful.
  • Familiarize yourself with the code (not ScreenManager.java). During the lab examination, you will be expected to know why and how it works.
  • Experiment with the animation loop and animation set-up.

Part 3: One Sprite

Background material: The course book p. 71-75.

  • Your task is to extend your the code in part 1, so that the animated object moves around and "bounces" off the window boundaries. See "Sprite" in the course book and in the code from the book.
  • Make the necessary changes to the code from Part 1 so that the Animation/Sprite can move across the screen.
  • Next implement the bouncing off window boundaries. Some leading questions to help you: What are the window boundaries? How do you know when a sprite hits them? How should a sprite change after a collision with a window edge to model "bouncing off the edge"?

Part 4: Many Sprites and Collision Detection

Background material: Parts of chapter 5 and 11 of the course book

  • Your task is to extend your animation in part 2, so that there is more than one sprite and the sprites bounce off each other when they collide.

Many_Sprites.jpg

  • Your program should be designed such that it it easy to add another sprite (or remove one).
  • Collision detection:
    • If you use the images provided for this lab, I recommend simple bounding circles - however, you may use other detection schemes, using a simple bounding box is NOT OK here.
  • Collision handling:
    • You are free to model the collision as you want, but the result should look realistic.
    • If you need inspiration, here you can find a discussion of how to model the collision.
  • Image transforms: Look at the image above. Depending on whether a sprite is moving to the right or to the left, the sprite image is facing either left or right. If you want to improve your program, have a look at page 81 (Listing 2.1) and make your sprites facing in direction they move. (This is not required to pass the assignment).

Extra assignment

This is the extra assignment for those who missed one or more mandatory lectures or completed this lab after the deadline (the last scheduled lab).

  • Use the code from part 3 and add keyboard control.
  • Using the arrow keys it should be possible to direct the movements of one sprite, that one sprite will start with zero velocity.
  • Pressing for example the "up" key, should accelerate the sprite in the "up" direction.
  • Note: The controlled sprite should handle collisions in the same way as the others.

The extra assignment is to be handed in via a runnable jar-file which *includes* source code.

Updated  2008-07-04 10:13:12 by Karl Marklund.