Hello Moving World
From Victory Engine
This is an extended version of Hello World. In this version, the GameLoop is introduced and used to animate the world.
package org.victoryengine.tutorials.helloworld;
import org.victoryengine.core.AbstractEntity;
import org.victoryengine.core.EntityList;
import org.victoryengine.core.GameLoop;
import org.victoryengine.geom.Point;
import org.victoryengine.graphics2d.Colour;
import org.victoryengine.graphics2d.Display2D;
import org.victoryengine.graphics2d.DisplayListener;
import org.victoryengine.graphics2d.Drawable;
import org.victoryengine.graphics2d.GC;
import org.victoryengine.graphics2d.Image;
import org.victoryengine.graphics2d.j2d.JDisplay;
import org.victoryengine.graphics2d.j2d.JDisplayParameters;
/**
* Simple game which loads an image of the world and displays it.
* Example of the Victory Engine.
* @author Bart van Heukelom
*/
public class HelloMovingWorldGame extends AbstractEntity implements Drawable, DisplayListener {
private Image world;
private Display2D display;
private Point pos = new Point();
public static void main(String[] args) {
new HelloMovingWorldGame().start();
}
/**
* Set up the display.
*/
public HelloMovingWorldGame() {
// set up a 2d window of 300 x 300 pixels
display = new JDisplay(new JDisplayParameters(300, 300));
display.setBackgroundColour(Colour.BLACK);
display.setWindowTitle("Hello Moving World");
// set this as listener, so the game can be exited
// when the window is closed. see windowClosed()
display.addListener(this);
// add this to the drawing loop, to draw the world
// see draw()
display.addDrawable(this);
// load a 150x150 image of the world
// which is placed directly in the classpath
world = display.getImageIO().loadImage(
ClassLoader.getSystemResource("world.png")
);
// add this to the entity list
// so step() will be executed every game step
EntityList.add(this);
}
/**
* Start the display and game loop.
*/
public void start() {
display.start();
GameLoop.get().start();
}
@Override
public void step() {
final double step = GameLoop.get().getStep() * 0.25;
pos.set(
75 + Math.cos(step) * 40,
75 + Math.sin(step) * 40
);
}
/**
* Draw the earth.
*/
public void draw(GC context) {
context.drawImage(world, pos);
}
/**
* Exit the game when the window is closed.
*/
public void windowClosed() {
System.exit(0);
}
public void frameStart(GC context) {
// not used, but neccessary to implement DisplayListener
}
public double getDepth() {
// not used, part of Drawable
return 0;
}
public void setDepth(double depth) {
// not used, part of Drawable
}
}
