Two Player Starter Code
<< How to make a Web Launched Application | FinalProjectsTrailIndex | Gridworld Tile Game >>
A Hockey Example
To keep it simple, we will use the a and z keys for player1 on the left, and the quote and forward slash key for the player 2
Hockey.java
import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * Class Hockey - a Small Demonstration of the Key Listener combined with * an instance of a Timer object that generates ActionListener events. * * The user input is generated from key presses, * and once the puck it hit, it is animated by * starting the timer which causes Action events * at regular time intervals. The ActionListener * can respond to them by drawing the puck in the new * location. * * @author Chris Thiel, OFMCap * @version 8 May 2008 */ public class Hockey extends JApplet implements KeyListener,ActionListener { private int keyCode; char c; int maxX,maxY; private char p1up, p1down, p2up, p2down, hit; String action="none"; Timer timer; Player p1,p2; Puck puck; public void init() { this.addKeyListener(this); p1up='a'; p1down='z'; p2up='\''; p2down=Character.toChars(47)[0]; hit=Character.toChars(32)[0]; maxX=getSize().width; maxY=getSize().height; p1=new Player(Color.RED, 20, 150, maxX, maxY); p2=new Player(Color.BLUE, maxX-40, 150, maxX, maxY); puck=new Puck(40, 150, maxX, maxY); timer = new Timer(10, this); timer.stop(); } public void paint(Graphics g) { // simple text displayed on applet g.setColor(Color.white); g.fillRect(0, 0, 500, 500); g.setColor(Color.black); g.drawString("Hokey Hocky - KeyListener / ActionListener Demo - Press a Key", 20, 20); g.setColor(Color.blue); g.drawString("action is "+action, 20, 60); g.setFont(new Font("Arial", Font.PLAIN, 32)); g.setColor(Color.red); g.drawString("\""+c+"\"", 50, 100); p1.draw(g); p2.draw(g); puck.draw(g); } /** * KeyListener Interface's Implementation */ public void keyTyped(KeyEvent e) {} public void keyPressed(KeyEvent e) { keyCode=e.getKeyCode(); c=e.getKeyChar(); action="none"; if (c==hit) { action="hit puck"; timer.start(); } if (c==p1up) { action="player 1 up"; p1.setY(p1.getY()-4); puck.setY(puck.getY()-4); } if (c==p1down) { action="player 1 down"; p1.setY(p1.getY()+4); puck.setY(puck.getY()+4); } if (c==p2up) { action="player 2 up"; p2.setY(p2.getY()-4); } if (c==p2down) { action="player 2 down"; p2.setY(p2.getY()+4); } repaint(); } public void keyReleased(KeyEvent e) {} /** * ActionListener is an interface and * requires the actionPerformed() method * to be defined..in this case we * look for a timer event */ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == timer) // the passage of time, so we move our puck // if it was hit { if (timer.isRunning()){ puck.move(); if (puck.getX()+30>maxX) { timer.stop(); if (puck.getY()>=p2.getY() && puck.getY()<=p2.getY()+40) { action="goalees wins"; }else{ action="goal!"; } } } } repaint(); } public void update(Graphics g){ paint(g); } }
Player.java
import java.awt.*; /** * class Player * * @author Chris Thiel * @version 9 May 2009 */ public class Player { protected int xLoc; protected int yLoc; protected int maxX; protected int maxY; protected Color c; /** * Constructor for objects of class Player */ public Player (Color color, int x, int y, int maximumX, int maximumY) { xLoc=x; yLoc=y; maxX=maximumX; maxY=maximumY; c=color; } public int getX() { return xLoc; } public int getY() { return yLoc; } public void setX(int x) { if (x>=0 && x<=maxX) xLoc=x; } public void setY(int y) { if (y>=0 && y<=maxY) yLoc=y; } public void draw(Graphics g) { g.setColor(c); g.fillRect(xLoc, yLoc, 10,40); } }
Puck.java
import java.awt.*; /** * Write a description of class Puck here. * * @author Chris Thiel, OFMCap * @version 9 May 2009 */ public class Puck extends Player { public Puck (int x, int y, int maximumX, int maximumY) { super(Color.BLACK, x, y, maximumX, maximumY); } public void move() { setX(xLoc+10); }; public void draw(Graphics g) { g.setColor(c); g.fillOval(xLoc, yLoc, 6,6); } }