New Game Template
Sam edited this page Mar 20, 2024
·
9 revisions
A luna2d game requires two parts:
- A Game file
- At least one Scene file
The Game file has a background scene manager. You start with creating a Game class/file extending from luna2d's Game file. This file then initialize the game, make a scene, add the scene to the scene manager and begin luna2d's Scene Engine (in other words, boot up the game).
Here is an example of a Game file:
public class MyGame extends Game
{
public static final int WIDTH = 800;
public static final int HEIGHT = 640;
public static void main(String[] args)
{
// Create and initialize the game
Game g = new Game();
g.init(WIDTH, HEIGHT, "MyGame", Color.black, "src/my_game/res/");
// Create a scene from 'MyScene' - a extension of the Scene class
MyScene myScene = new MyScene("MyScene");
// Add the newly created scene to luna2d's scene manager
g.sceneManager.addScene(myScene);
// Starts the game (on with the scene named "MyScene")
g.beginSceneEngine("MyScene");
}
}
Here is an example of the scene file used above:
public class Sandbox extends Scene
{
public Sandbox(String name)
{
super(name);
}
public void start()
{
Log.println("My Scene started.");
Color textColor = ColorHandler.getColor("GrassGreen");
new TextDisplay(this, "[Q]uit", 200, 400, textColor, 1);
}
public void end()
{
Log.println("My Game Scene ended.");
}
public void update()
{
if (this.isKeyPressed(KeyEvent.VK_Q))
{
this.endGame();
}
}
@Override
protected void onMouseClick(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
protected void onMousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
protected void onMouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}