Example Sprite Following Mouse (Action Sprite Game) GTGE

now i will share for all if you need and confused with following sprite with mouse. ok let’s try this code.. this code build with GTGE(Golden T studio) Framework.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PlaneMambo.game;

import com.golden.gamedev.Game;
import com.golden.gamedev.GameLoader;
import com.golden.gamedev.object.GameFont;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import com.golden.gamedev.object.sprite.AdvanceSprite;
/**
 *
 * @author me
 */
public class MoveOverMouse extends Game{

    AdvanceSprite    sprite;
    GameFont          font;
    @Override
    public void initResources() {

        BufferedImage[] images = getImages("../image/plane1.png", 1, 1);
        sprite = new AdvanceSprite(images,-150,-150);

        font = fontManager.getFont(getImages("../image/font.png", 20, 3),
                                   " !            .,0123" +
                                   "456789:   -? ABCDEFG" +
                                   "HIJKLMNOPQRSTUVWXYZ ");

        
    }

    @Override
    public void update(long elapsedTime) {
        sprite.update(elapsedTime);

        int tileX = (int) (getMouseX() / 24);
	int tileY = (int) (getMouseY() / 24);

        if (keyPressed(KeyEvent.VK_ESCAPE)) {
            finish();
        }
    }

    @Override
    public void render(Graphics2D g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        int tileX = (int) (getMouseX() / 24);
	int tileY = (int) (getMouseY() / 24);
        font.drawString(g, "Koordinat: "+tileX+", "+tileY,tileX*24-25, (tileY*24)+2);
        sprite.render(g, tileX*24-25, (tileY*24)+2);
 
    }
    public static void main(String[] args) {
        GameLoader game = new GameLoader();
        game.setup(new MoveOverMouse(), new Dimension(640,480), false);
        game.start();
    }
}

Hasilnya:

source code diatas digunakan untuk membuat sprite mengikuti mouse di framework GTGE..semoga membantu..:D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s