Java2 Moving sprites

In this part of the Java 2D games tutorial we will work with sprites.

The term sprite has several meanings. It is used to denote an image or an animation in a scene. It is also used to represent any movable object in a game. Also one of the meanings is the code that encapsulates a character in a game. In our tutorial by using sprite we refer to a movable object or its java class.

R-Type
In the first example we will have a spacecraft. We can move the spacecraft on the board using the cursor keys.

Craft.java
package rtype;

import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Craft {

private String craft = "craft.png";

private int dx;
private int dy;
private int x;
private int y;
private Image image;

public Craft() {
ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
image = ii.getImage();
x = 40;
y = 60;
}


public void move() {
x += dx;
y += dy;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public Image getImage() {
return image;
}

public void keyPressed(KeyEvent e) {

int key = e.getKeyCode();

if (key == KeyEvent.VK_LEFT) {
dx = -1;
}

if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}

if (key == KeyEvent.VK_UP) {
dy = -1;
}

if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}

public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();

if (key == KeyEvent.VK_LEFT) {
dx = 0;
}

if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}

if (key == KeyEvent.VK_UP) {
dy = 0;
}

if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}
}

This class represents a spacecraft. In this class we keep the image of the sprite and the coordinates of the sprite. The keyPressed() and keyReleased() methods control whether the sprite is moving or is in stanstill.

public void move() {
x += dx;
y += dy;
}

The move() method changes the coordinates of the sprite. These x, y values are used in the paint() method to draw the image of the sprite.

if (key == KeyEvent.VK_LEFT) {
dx = 0;
}

When we release the left cursor key, we set the dx variable to zero. The spacecraft will stop moving.

Board.java
package rtype;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;
import javax.swing.Timer;


public class Board extends JPanel implements ActionListener {

private Timer timer;
private Craft craft;

public Board() {

addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);

craft = new Craft();

timer = new Timer(5, this);
timer.start();
}


public void paint(Graphics g) {
super.paint(g);

Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);

Toolkit.getDefaultToolkit().sync();
g.dispose();
}


public void actionPerformed(ActionEvent e) {
craft.move();
repaint();
}


private class TAdapter extends KeyAdapter {

public void keyReleased(KeyEvent e) {
craft.keyReleased(e);
}

public void keyPressed(KeyEvent e) {
craft.keyPressed(e);
}
}

}

This is the Board class.

g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);

In the paint() method, we draw the spacecraft. We get the image and the coordinates from the sprite class.

public void actionPerformed(ActionEvent e) {
craft.move();
repaint();
}

The actionPerformed() method is called every 5ms. We move the sprite and repaint the board.

RType.java
package rtype;

import javax.swing.JFrame;

public class RType extends JFrame {

public RType() {

add(new Board());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setTitle("R - Type");
setResizable(false);
setVisible(true);
}

public static void main(String[] args) {
new RType();
}
}

This is the main class.



Figure: R-Type
Shooting missiles
In the next example we will add another sprite type to our example. A missile. We can launch missiles with the space key.

Missile.java
package rtype;

import java.awt.Image;

import javax.swing.ImageIcon;

public class Missile {

private int x, y;
private Image image;
boolean visible;

private final int BOARD_WIDTH = 390;
private final int MISSILE_SPEED = 2;

public Missile(int x, int y) {

ImageIcon ii =
new ImageIcon(this.getClass().getResource("missile.png"));
image = ii.getImage();
visible = true;
this.x = x;
this.y = y;
}


public Image getImage() {
return image;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public boolean isVisible() {
return visible;
}

public void move() {
x += MISSILE_SPEED;
if (x > BOARD_WIDTH)
visible = false;
}
}

Here we have a new sprite called Missile.

public void move() {
x += MISSILE_SPEED;
if (x > BOARD_WIDTH)
visible = false;
}

The missile moves at constant speed. When it hits the right border of the Board, it becomes invisible. It is then removed from the ArrayList of missiles.

Craft.java
package rtype;

import java.awt.Image;
import java.awt.event.KeyEvent;

import java.util.ArrayList;

import javax.swing.ImageIcon;

public class Craft {

private String craft = "craft.png";

private int dx;
private int dy;
private int x;
private int y;
private Image image;

private ArrayList missiles;

private final int CRAFT_SIZE = 20;

public Craft() {
ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
image = ii.getImage();
missiles = new ArrayList();
x = 40;
y = 60;
}


public void move() {
x += dx;
y += dy;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public Image getImage() {
return image;
}

public ArrayList getMissiles() {
return missiles;
}

public void keyPressed(KeyEvent e) {

int key = e.getKeyCode();

if (key == KeyEvent.VK_SPACE) {
fire();
}

if (key == KeyEvent.VK_LEFT) {
dx = -1;
}

if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}

if (key == KeyEvent.VK_UP) {
dy = -1;
}

if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}

public void fire() {
missiles.add(new Missile(x + CRAFT_SIZE, y + CRAFT_SIZE/2));
}

public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();

if (key == KeyEvent.VK_LEFT) {
dx = 0;
}

if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}

if (key == KeyEvent.VK_UP) {
dy = 0;
}

if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}
}

The Craft.java has changed a bit.

if (key == KeyEvent.VK_SPACE) {
fire();
}

If we press the space key, we fire.

public void fire() {
missiles.add(new Missile(x + CRAFT_SIZE, y + CRAFT_SIZE/2));
}

The fire() method creates a new Missile object and adds it to the missiles ArrayList.

public ArrayList getMissiles() {
return missiles;
}

The getMissiles() method returns the ArrayList of missiles. It is called from the Board class.

Board.java
package rtype;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.Timer;


public class Board extends JPanel implements ActionListener {

private Timer timer;
private Craft craft;

public Board() {

addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);

craft = new Craft();

timer = new Timer(5, this);
timer.start();
}


public void paint(Graphics g) {
super.paint(g);

Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);

ArrayList ms = craft.getMissiles();

for (int i = 0; i < ms.size(); i++ ) {
Missile m = (Missile) ms.get(i);
g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
}

Toolkit.getDefaultToolkit().sync();
g.dispose();
}


public void actionPerformed(ActionEvent e) {
ArrayList ms = craft.getMissiles();

for (int i = 0; i < ms.size(); i++) {
Missile m = (Missile) ms.get(i);
if (m.isVisible())
m.move();
else ms.remove(i);
}

craft.move();
repaint();
}


private class TAdapter extends KeyAdapter {

public void keyReleased(KeyEvent e) {
craft.keyReleased(e);
}

public void keyPressed(KeyEvent e) {
craft.keyPressed(e);
}
}
}

This is the Board.java file.

ArrayList ms = craft.getMissiles();

for (int i = 0; i < ms.size(); i++ ) {
Missile m = (Missile) ms.get(i);
g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
}

In the paint() method we draw all missiles from the array list.

ArrayList ms = craft.getMissiles();
for (int i = 0; i < ms.size(); i++) {
Missile m = (Missile) ms.get(i);
if (m.isVisible())
m.move();
else ms.remove(i);
}

In the actionPerformed() method we parse all missiles from the array list. Depending on the visible flag, we move the missile or remove it from the container.

RType.java
package rtype;

import javax.swing.JFrame;

public class RType extends JFrame {

public RType() {

add(new Board());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setTitle("R - Type");
setResizable(false);
setVisible(true);
}

public static void main(String[] args) {
new RType();
}
}

Finally, this is the main class.



Figure: Shooting missiles

Niciun comentariu:

Trimiteți un comentariu