package ufo; /** * * A continuation of the game series. * This allows for more than one key to be * pressed at the same time. * We use booleans found in Ufo4 (up, right, left, down)... * ...and the keyPressed / released methods in this class * @author JasonButka * * */ import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.*; public class UfoGame4 extends Applet implements Runnable, KeyListener { static int SCREENWIDTH = 950; static int SCREENHEIGHT = 240; int keyCode; char keyChar; boolean shooting; Ufo4 player1; Ufo4 player2; boolean shoot = false; //usual thread, during game Thread animationLoop; //special move thread (not fully in use yet) Thread thr1; //double buffer objects Image backbuffer; Graphics2D g2d; Image background; //sprite variables //animation variables Image dbImage; // double buffering technique Graphics dbg; // double buffering technique // find & load image private int fx; private int fy; private URL getURL(String filename) { URL url = null; try { url = this.getClass().getResource(filename); } catch (Exception e) { // do nothing } return url; } public void init() {//OPEN init() addKeyListener(this); setFocusable(true); shooting=false; this.setSize(SCREENWIDTH, SCREENHEIGHT); Toolkit tk = Toolkit.getDefaultToolkit(); // puck = new Puck6(120,120); // puck.load(); player1 = new Ufo4(100, 100); player1.load(); player2 = new Ufo4(200, 50); player2.load(); backbuffer= tk.getImage(getURL("space.jpg")); //load the animation strip }//CLOSE init() public void start() { if(shooting==false) { animationLoop = new Thread(this); animationLoop.start(); System.out.println("IN START() Animation Loop"); } if (shooting==true) { thr1 = new Thread(this); thr1.start(); System.out.println("In START() thr1"); } } public void stop() { if(shooting==true) { animationLoop = null; } else { thr1 = null; } } public void run() { Thread t = Thread.currentThread(); while (t == thr1){ try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); player1.shoot(); // puck.shot(600, 200); if(player1.runShoot==false) { shooting=false; t=null; start(); } repaint(); } while (t == animationLoop) { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } repaint(); } } public void update(Graphics g) {// open update() if (dbImage == null) {//open db if // this size of the background dbImage = createImage (SCREENWIDTH, SCREENHEIGHT); dbg = dbImage.getGraphics (); }//close db if // clear screen in background dbg.setColor (getBackground ()); dbg.fillRect (0, 0, SCREENWIDTH, SCREENHEIGHT); dbg.setColor (getForeground()); paint (dbg); // draw image on the screen g.drawImage (dbImage, 0, 0, this); }// close update() public void paint(Graphics g) { //opens paint() //draw the back buffer to the screen g.drawImage(backbuffer, 0, 0, this); //Draw Player/Puck Objects player1.drawPlayer(g); player2.drawPlayer(g); // puck.drawPuck(g, 0); //draws puck and allows possession and shot and pass (eventually) //puck.possessed(player1.playerX,player1.playerY, player1.shot, true); //System.out.println("Left "+player1.left+ " Right "+player1.right+" up " +player1.up+" down "+player1.down); player1.motion(); try { Thread.sleep(75); } catch (InterruptedException e) { e.printStackTrace(); } } //closes paint() @Override public void keyPressed(KeyEvent e){ keyCode=e.getKeyCode(); switch(keyCode) { case KeyEvent.VK_RIGHT: player1.left = false; player1.right= true; /* if(player1.bounds.intersects(puck.bounds)) { puck.setPosessed(true); } */ if(player1.bounds.intersects(player2.bounds)) {//OPENS if touched //nothing happens. a collision System.out.println("BOOM!"); // player1.skateLeft(); }//CLOSES if touched break; case KeyEvent.VK_LEFT: player1.right=false; player1.left = true; if(player1.bounds.intersects(player2.bounds)); { //nothing happens. a collision } if(!player1.bounds.intersects(player2.bounds)) { } break; case KeyEvent.VK_UP: player1.up = true; if(player1.bounds.intersects(player2.bounds)); { //nothing happens. a collision } if(!player1.bounds.intersects(player2.bounds)) { //player1.changeYUp(); } break; case KeyEvent.VK_DOWN: player1.down = true; if(player1.bounds.intersects(player2.bounds)); { //nothing happens. a collision } if(!player1.bounds.intersects(player2.bounds)) { //player1.changeYDown(); } break; case KeyEvent.VK_ENTER: shooting=true; //boolean for thread control start(); //method for thread control break; } repaint(); } @Override public void keyReleased(KeyEvent e) { int keyCode=e.getKeyCode(); switch(keyCode){ // when right is released the walking sound stops and booleans are reset case KeyEvent.VK_RIGHT: // if you want to stop, use line below player1.right = false; break; case KeyEvent.VK_LEFT: // same thing as right player1.left = false; break; case KeyEvent.VK_UP: // nothing really happens after up key is released player1.up = false; break; case KeyEvent.VK_DOWN: // nothing really happens after up key is released player1.down = false; break; } repaint(); } /* * not using this right now * (non-Javadoc) * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent) */ @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } }