What is a game without Sprite?
Sprite is a graphical object, that have its own behaviour such as movement, animation, etc.
see Sprite API documentation
Displaying Sprite in GTGE is pretty straight forward:
BufferedImage heroImage = getImage("hero.gif"); int posx = 10, posy = 10; Sprite hero = new Sprite (heroImage, posx, posy);
hero.update ();
hero.render (g);
That's it, very simple !
It display a sprite with image hero.gif as the graphical object at coordinate 10, 10.
To move a sprite, simply use one of these two methods:
// move the sprite two pixel to the right from its origin location Sprite.move (2, 0); // set new sprite location at coordinate 20, origin y location Sprite.setLocation (20, getY());
import java.awt.*; import java.awt.image.BufferedImage; import java.awt.event.KeyEvent; import com.golden.gamedev.*; import com.golden.gamedev.object.Sprite; public class Tutorial3 extends Game { Sprite sprite; public void initResources() { BufferedImage img = getImage("ship.png"); sprite = new Sprite(img, 100, 100); } public void update() { sprite.update(); // move sprite based on arrow key if (keyDown(KeyEvent.VK_LEFT)) sprite.moveX(-1); if (keyDown(KeyEvent.VK_RIGHT)) sprite.moveX(1); if (keyDown(KeyEvent.VK_UP)) sprite.moveY(-1); if (keyDown(KeyEvent.VK_DOWN)) sprite.moveY(1); } public void render(Graphics2D g) { // clear background g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, getWidth(), getHeight()); sprite.render(g); } }See the moving sprite in action !
Sprite behaviour is what a sprite will do after some time.
For example: in shooter game, enemy ship (sprite) shoots after 10 ticks (behaviour).
In GTGE, there is a helper class namely Timer to design behaviour of a sprite.
Timer class is a timer that will trigger an action after specified delay time has been elapsed.
To create sprite behaviour you need to create Timer object inside a sprite.
For example: creating shoot behaviour on Ship class.
import com.golden.gamedev.object.*; public class Ship extends Sprite { Timer shoot; public Ship() { // the ship will shoot every 10 ticks shoot = new Timer(10); } public void update() { if (shoot.action()) { // time to shoot!!! } } }
There is one built-in behaviour in Sprite class, it is movement behaviour.
Movement behaviour is a behaviour to move the sprite after movement delay time elapsed.
Let's see how to use it.
You need to set the sprite speedX, speedY
variables to move the sprite.
Sprite s = new Sprite(getImage("ship.png"), 10, 10); // move the sprite 1 pixel to right, // every movement delay time elapsed s.speedX = 1;
Another sprite behaviour example is on AnimatedSprite, a behaviour to animated after some time.
Okay, now you have display a sprite (graphical object), and move it across the screen.
Do you satisfy enough? No? You want to display many sprites?
Okay, we do it just like the old way.
Create the whole sprites, and then update and render it.....
err...you want to display 100 sprites and add more and more at runtime???
Then it's time for SpriteGroup come to the rescue !
This tutorial is far from finished, and also the API documentation still empty everywhere !
But we hope all the API is self-explanatory.
I have lack of time to do all these documentation, but that does not mean GTGE API would be static without any improvement.
The engine is actively developed since we also used this engine to create our games (see by yourself games built by GTGE here). It's just the tutorial is our second priority, because our English is horrible (hope you not recognized this much in this tutorial), so if anyone would like to help the documentation, e-mail us as soon as possible! We will be very very happy to hear it! We'll explain how GTGE works personally and you could help us documented this baby right away.
If you have any trouble, please visit our forum, we will try our best to answer your questions.
And don't forget to see sample code in folder <GTGE path>\sample (run PLAY.BAT to see how it works). It will be enough to start creating a decent game.
Also many games in game section is released along with the source code, it worth to take a look.
Finally, we want this tutorial as simple as possible, if you feel this tutorial is hard or confusing, drop me a line, where should I fix it or which one is the hardest, so i could simplify it a bit.
Thank you
Next issue: SpriteGroup, PlayField, Background, Collision, Tiling.
Till we meet again......
Thank you for using Golden T Game Engine
Happy Coding!
Regards,
Paul'
1 2 3 4 5 6 7 |
<< Previous Page |