Sign in to follow this  
chris_j_pook

Moving sprite ...

Recommended Posts

Hi, I am setting out to make a small game based on an old megadrive game SmashTV. It consists of a static back ground image and a sprite which can move around the screen. At random intervals enemies enter the screen from the edges and attack the player. I am fairly competent at Java coding but I am not sure where to start with this. In perticular how to implement the movable sprite, and how it will overlay the background image. Are there any resources around preferably with example code that show this sort of thing in action ? Any help greatly appreciated ... Cheers Chris

Share this post


Link to post
Share on other sites
I'm not going to write out all the java classes for you, but you basicly have to make a Game class(to load resources, update the game loop and clean up). The make an instance of your Game class in the main() method and call init(), run() and shutdown() where you place the run() in a loop with a breaking condition. Run will clear the background, draw the image, tell all sprites to update and draw themselfs.

Next, make a Sprite class with some parameters(Such as image to load, start (x,y) and initial velocity). Add an update() and render() function to the class.


void update(float time)
{
x += velX * time;
y += velY * time;

// Update the rest
}


The above code is a simple example of timebased movement. As you can see, x,y,velX, velY are floats too. Everytime you update/draw your sprites you time how long it took to render. You pass this to the update function so it multiplies the velocity by time. If the update/render loop took 0.05 seconds, your sprites' position will be update by velX * 0.05. This will make sure the game runs at the same speed on all systems.

And the render function should just draw the image to the screen. This completely depends on your graphics API.

Toolmaker

Share this post


Link to post
Share on other sites
Ok I have made this so far. Is this heading in the right direction ?
Ive come a bit unstuck when it comes to drawing the player sprite from the player class, what do I use for the parameters ?

(Main loop of the Game class)
private void mainLoop()
{
// Get an object representing the area within the window borders.
Container clientArea = this.getContentPane();

// Get the Graphics object associated with the client area.
Graphics g = clientArea.getGraphics();

// Make a new Player for this level.
Player player = new Player();

// The main game loop ...
// Should i put while(true)? It crashes my comp if I do :/
for (int a = 1; a <=1000; a++)
{
// Update player location. move vars are held as booleans ...
if(moveright) player.x += 10;
if(moveleft) player.x -= 10;
if(moveup) player.y += 10;
if(movedown) player.y -= 10;

// draw background
drawBackground(g);

// call the player's draw method
player.draw(g);

// **** Will include buffering later to make graphics smoother.
}
}

(Mouse listener method to get keyboard input, inside Game class)
public void keyPressed(KeyEvent ke)
{
switch(ke.getKeyCode())
{
case KeyEvent.VK_LEFT:
moveleft = true;
break;
case KeyEvent.VK_RIGHT:
moveright = true;
break;
case KeyEvent.VK_UP:
moveup = true;
break;
case KeyEvent.VK_DOWN:
movedown = true;
break;
}
}

(Draw method of the Player class, x,y coord stored within Player class)
public void draw(Graphics g)
{
if (orientation == 0)
{
Image img = Toolkit.getDefaultToolkit().getImage("down.jpg");
}
else if (orientation == 1)
{
Image img = Toolkit.getDefaultToolkit().getImage("left.jpg");
}
else if (orientation == 2)
{
Image img = Toolkit.getDefaultToolkit().getImage("right.jpg");
}
else if (orientation == 3)
{
Image img = Toolkit.getDefaultToolkit().getImage("up.jpg");
}

// Not sure what to do here to draw graphic on screen, do I pass an instance
// of Game to the Player class ?
// g.drawImage(img,x,y,?);
}

Any comments appreciated ...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this