Ucigame - Gallery - TimeTrial

Home | Getting Started | Reference | Introduction to Java | Gallery

Your browser cannot show Java applets. TimeTrial shows how the game's overall framerate can be set to one value (20 here), while individual sprites can have different, slower framerates. The four sloMoX sprites are each based on the same set of four frames, all stored in the file images/slomo.png. The three calls to Sprite.framerate() control the frequency at which the frames change. Note that the sprite sloMo1 has a framerate based on the window's framerate, which is the default.

The bouncing balls are mostly just for visual interest.

TimeTrial.java
import ucigame.*;

public class TimeTrial extends Ucigame
{
    Sprite sloMo1;
    Sprite sloMo2;
    Sprite sloMo3;
    Sprite sloMo4;
    Sprite ball1;
    Sprite ball2;
    Sprite ball3;

    public void setup()
    {
        window.size(250, 250);
        window.title("Time Trial");
        framerate(20);
        window.showFPS();

        canvas.background(50, 150, 150);

        sloMo1 = makeSprite(42, 42);
        sloMo1.addFrames(getImage("images/slowmo.png"),
                            0, 0,
                            42, 0,
                            84, 0,
                            126, 0);
        sloMo2 = makeSprite(42, 42);
        sloMo2.addFrames(getImage("images/slowmo.png"),
                            0, 0,
                            42, 0,
                            84, 0,
                            126, 0);
        sloMo3 = makeSprite(42, 42);
        sloMo3.addFrames(getImage("images/slowmo.png"),
                            0, 0,
                            42, 0,
                            84, 0,
                            126, 0);
        sloMo4 = makeSprite(42, 42);
        sloMo4.addFrames(getImage("images/slowmo.png"),
                            0, 0,
                            42, 0,
                            84, 0,
                            126, 0);

        sloMo2.framerate(10);
        sloMo3.framerate(5);
        sloMo4.framerate(1);

        sloMo1.position(50, 50);
        sloMo2.position(150, 50);
        sloMo3.position(50, 150);
        sloMo4.position(150, 150);

        ball1 = makeSprite(getImage("images/BigBall.png", 255));
        ball1.position(canvas.width()/2 - ball1.width()/2 + 1,
                         canvas.height()/2 - ball1.height()/2);
        ball1.motion(4, 3);

        ball2 = makeSprite(getImage("images/LittleBall.png", 255));
        ball2.position(canvas.width() - ball2.width(),
                         canvas.height() - ball2.height());
        ball2.motion(-6, -4);

        ball3 = makeSprite(getImage("images/LittleBall.png", 255));
        ball3.position(0, canvas.height() - ball2.height());
        ball3.motion(7, -4);
    }

    public void draw()
    {
        canvas.clear();

        ball1.move();
        ball2.move();
        ball3.move();

        ball2.bounceIfCollidesWith(ball1, ball3);
        ball3.bounceIfCollidesWith(ball1, ball2);

        ball1.bounceIfCollidesWith(TOPEDGE, BOTTOMEDGE, LEFTEDGE, RIGHTEDGE);
        ball2.bounceIfCollidesWith(TOPEDGE, BOTTOMEDGE, LEFTEDGE, RIGHTEDGE);
        ball3.bounceIfCollidesWith(TOPEDGE, BOTTOMEDGE, LEFTEDGE, RIGHTEDGE);

        ball1.draw();
        ball2.draw();
        ball3.draw();
        sloMo1.draw();
        sloMo2.draw();
        sloMo3.draw();
        sloMo4.draw();
    }
}