Skip to content

Using Game Objects

Zen1th edited this page Jun 10, 2020 · 1 revision

Using Game Objects

GameObject is the vital class of a game, it represent an entity (monster, player), an image, an intro, a video, etc. and much more

Let's add a rectangle to our test game:

New Imports:

import org.powerhigh.objects.Rectangle;
import org.powerhigh.utils.Color;

At the end of init()

win.setViewportManager(new SizedViewport(500, 400));
Rectangle rect = new Rectangle(100, 100, 100, 100, Color.YELLOW);
win.add(rect);

Here is the result:

Result on Windows10

How it work ?

We create a new rectangle called "rect", at position 100, 100 and with a size of 100x100px then it is added to the window container. win.setViewportManager(...) is used the object that manage what the users view. It got added to not render out-of-screen objects in nothing. And to have a game with same field of view for everyone.

Adding a Sprite

First of all, you should have a texture:

Texture tex = TextureLoader.getTexture("MyImage.png");

The image must be placed at project root (not "src"),example on eclipse below
Eclipse Example
And just make a new Sprite with the Texture as argument. Example:

Texture tex = TextureLoader.getTexture("MyImage.png");
Sprite sprite = new Sprite(tex);

And of course, don't forget the win.add!

If you did all correctly you should get (Note: the result again uses SizedViewport, you can find more details here: Viewport Managers)
Result on Windows10

<< Making a simple game | Inputs (+ events) >>

Clone this wiki locally