Basic Games (with Swing and AWT UI)

Make a character to control

import java.awt.Image; public class Hero { private Image sprite; private int x = 0; private int y = 0; private int xVelocity = 0; private int yVelocity = 0; public Hero(Image sprite, int x, int y) { this.sprite = sprite; this.x = x; this.y = y; } public void update () { this.x += this.xVelocity; this.y += this.yVelocity; } public int getX() { return this.x; } public int getY() { return this.y; } public void setXVel(int setTo) { this.xVelocity = setTo; } public void setYVel(int setTo) { this.yVelocity = setTo; } public Image getSprite() { return this.sprite; } }

- This keeps track of the position and velocity, and stores its image

- The game class (defined below) will create a hero object and make it move with keyboard inputs

Make an item to collect

import java.awt.Image; public class Collectable { private Image sprite; private int x = 0; private int y = 0; public Collectable(Image sprite, int x, int y) { this.sprite = sprite; this.x = x; this.y = y; } public int getX() { return this.x; } public int getY() { return this.y; } public void setX (int setTo) {this.x = setTo;} public void setY (int setTo) {this.y = setTo;} public Image getSprite() { return this.sprite; } }

- This keeps track of its position, and stores its image

Define the game

public class SimpleGame { private Hero player1; private ArrayList<Collectable> collectables; public SimpleGame () { try { // Initialize player 1 Image heroSprite = new ImageIcon("demoImage.png").getImage(); player1 = new Hero(heroSprite,100,100); // Initialize 2 collectables collectables = new ArrayList<Collectable>(); Image colSprite = new ImageIcon("collectable.png").getImage(); collectables.add( new Collectable(colSprite,40,30)); collectables.add( new Collectable(colSprite,140,230)); } catch (Exception e) { e.printStackTrace(); } } // Define the game loop public void update () { player1.update(); // If player 1 reaches a collectable, reset its position for (Collectable coll : collectables) { if ( Math.abs(player1.getX() - coll.getX()) < 64 && Math.abs(player1.getY() - coll.getY()) < 64) { coll.setX( (int)(Math.random() * 350) ); coll.setY( (int)(Math.random() * 350) ); } } } // Display game objects and player 1 public void display (Graphics2D g) { for (Collectable obj : collectables) { g.drawImage(obj.getSprite(), obj.getX(), obj.getY(), null); } g.drawImage(player1.getSprite(), player1.getX(), player1.getY(), null); } // Define actions for direction keys public void onLeftKeyPress() { player1.setXVel(-6); player1.setYVel(0); } public void onRightKeyPress() { player1.setXVel(6); player1.setYVel(0); } public void onUpKeyPress() { player1.setXVel(0); player1.setYVel(-6); } public void onDownKeyPress() { player1.setXVel(0); player1.setYVel(6); } }

- Import java.awt.Graphics2D, java.awt.Image, java.util.ArrayList, and javax.swing.ImageIcon

- For every step of the game, the hero's position will be updated; if he reaches a collectable, it will jump to a new position

- For better encapsulation, the game display function and key action functions are defined here

Set up the UI

public class SimpleGamePanel extends JPanel implements ActionListener { SimpleGame game; public SimpleGamePanel(SimpleGame game) { this.game = game; try { this.setBackground(Color.blue); Timer timer = new Timer (100, this); timer.start(); } catch (Exception e) { e.printStackTrace(); } PressRightAction pressRightAction = new PressRightAction(); this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0, false), "pressRight"); this.getActionMap().put("pressRight", pressRightAction ); PressLeftAction pressLeftAction = new PressLeftAction(); this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0, false), "pressLeft"); this.getActionMap().put("pressLeft", pressLeftAction ); PressUpAction pressUpAction = new PressUpAction(); this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0, false), "pressUp"); this.getActionMap().put("pressUp", pressUpAction ); PressDownAction pressDownAction = new PressDownAction(); this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0, false), "pressDown"); this.getActionMap().put("pressDown", pressDownAction ); } @Override public void actionPerformed (ActionEvent event) { game.update(); repaint(); } public void paint (Graphics gra) { Graphics2D g = (Graphics2D) gra; super.paint(g); game.display(g); } // Define the action to be performed when key is pressed public class PressRightAction extends AbstractAction { @Override public void actionPerformed (ActionEvent ae) { game.onRightKeyPress(); } } public class PressLeftAction extends AbstractAction { @Override public void actionPerformed (ActionEvent ae) { game.onLeftKeyPress(); } } public class PressUpAction extends AbstractAction { @Override public void actionPerformed (ActionEvent ae) { game.onUpKeyPress(); } } public class PressDownAction extends AbstractAction { @Override public void actionPerformed (ActionEvent ae) { game.onDownKeyPress(); } } }

- Import these from java.awt: Color, Graphics, Graphics2D, event.ActionEvent, event.ActionListener, event.KeyEvent;

- Import these from javax.swing: AbstractAction, JPanel, KeyStroke, Timer

- The keyboard actions are defined here and connected with the functions defined in the game class

- The graphics object is passed to the game's display function to handle it

Completed