Chapter 1: Setting up a basic "game"

Creating the player

The basics of the Player Class

Any actors you create should always be a subclass of Actor, so start with instantiating the Player class as a subclass of Actor. Make sure you have a constructor for the class because we're creating an object! The constructor should have nothing more than setting an image to the player object. Remember, we're adding attributes to Player, nothing more!

To add an image to the player, replace //filetype.png// with the image file you're putting to the actor. Make sure to size it to the player (ex, 50px player should have a 50x50 image). All you need to do is add the file to the folder containing all of the code as well!

Game loops

The act() method (inherited from Actor) is the game loop. Each time Main is run, the game loop runs at an extremely high rate. This is why it's important to have conditionals, delays, and smart approaches to code so you don't end up crashing your computer! Switching pictures via animations or playing audio through the game loop can cause extreme lag. For now, we've put our custom movementViaKey() method that we'll move on to next. This won't lag/crash the game because there are conditionals that need to be met to have a repeating action (Ex. Key Press).

Implementing player game controls

Finally, we have our movementViaKey() method which checks what keys are pressed and does an action as a result. Our key method (pun intended) is the keyIsDown(char x) method. Input a character (Ex. X) and perform a result using a conditional.

Player movement

Inside the conditionals, we can add movement using the move(int x) or turn(double y) methods. Using a positive movement integer like move(2) moves the character at the rate of 2, in the direction it's facing. Having a negative integer in move causes you to go the opposite direction of what you're facing. Finally, a positive turn (double, not integer!) makes you turn right. Having a negative turn makes you turn left.

Demo Code

Check out the code below to see how we're combining all of this.


import ActiverseEngine.*; //required to retrieve packages from main files
public class Player extends Actor {
    public Player() {
        // Creates new Player Object
        setImage(new ActiverseImage("filetype.png")); // Set image for Player
    }
    
    @Override
    public void act() { // Game loop
        movementViaKey();
    }

    public void movementViaKey() {
        if (keyIsDown('W')) {
            if (keyIsDown('M')) {
                move(8);
            } else {
                move(5); // Move forward
            }
        }

        if (keyIsDown('S')) {
            move(-5); // Move backward
        }
        
        if (keyIsDown('A')) {
            turn(-0.5); // Turn left
        }
        
        if (keyIsDown('D')) {
            turn(0.5); // Turn right
        }
    }
}

    

Adding the Actor to the World

Creating a new World

To create a new world, you're creating a subclass of World.
We'll use MyWorld in this scenario. First, create the subclass. Next, create the constructor and make sure it matches (or match) to the Main.java code.


import ActiverseEngine.*;
public static void main(String[] args) {
     Activerse.start(new %WORLDNAME%()); // Replace %WORLDNAME% with the world you're editing
}

    

After that, you want to make sure you're having the correct size for your world. I recommend starting with a 400x400 grid and seeing how much you want to modify from there. The final code in the call to super is the cellSize or pixel size. To start, you should always have 1.

You can create a new Background image via setBackgroundImage(), and finally add your desired actor object using addObject(actor, locationX, locationY);.


import ActiverseEngine.*;
public class MyWorld extends World {  // Making a subclass of World named MyWorld
    public MyWorld() {
        super(400, 400, 1); // Setting the size (window size) to a 400x400 size with pixel or cellSize of 1
        setBackgroundImage("world.png"); // Creating the background image for the World via world.png
        addObject(new Player(), 100, 100); // Adding a new player to the world at location X100, Y100
    }
}

    

That's all there is to creating and adding a controllable actor! You can always add on more features to your actor from here and make a more interactive game overall. If you find any bugs or suggestions on improving the code, please open up an Issue via Github. Thanks!