/* * Game 5 * A continuation of the game series. * This allows for automation and smooth * animation of sprite during a shot * and a puck (Shape) * * As programmed... only left key works (shoot) * * Unfortunately...the action of the Player (to shoot) * is being handled in this class... * Works for now but we will need to change it * * Inclusion of a Puck class * * * @author JasonButka * * */ package shootOut; import java.awt.*; import java.awt.event.KeyEvent; import java.applet.*; import java.util.*; import java.awt.image.*; import java.net.*; public class Game5 extends Applet implements Runnable { static int SCREENWIDTH = 500; static int SCREENHEIGHT = 500; Player5 joe; Puck5 puck; boolean shoot = false; Thread animationLoop; //double buffer objects Image backbuffer; Graphics2D g2d; Image background; //sprite variables //animation variables KeyEvent o; String info; // help clarify Animation numbers Image dbImage; // double buffering technique Graphics dbg; // double buffering technique // find & load image private int fx; private int fy; public boolean keyDown (Event e, int key) { // user presses left cursor key if (key == Event.LEFT) { //keyFrame = 0; shoot = true; } // user presses right cursor key else if (key == Event.RIGHT) { // changing x - speed so that ball moves to the right side (x_speed positive) // keyFrame = 0; //currentFrame++; gameUpdate(0); } if (key == 1004) { // changing x - speed so that ball moves to UP // keyFrame = 0; gameUpdate(0); } // user presses right cursor key else if (key == 1005) { // changing x - speed so that ball moves to the right side (x_speed positive) // keyFrame = 0; gameUpdate(0); } else { // Additionally the method prints out the ASCII - value if an other key is pressed. This is not necessary but a possibility for you to test which value a key has. // System.out.println ("Character: " + (char)key + " Integer Value: " + key); } if (key == 114) { } if (key == 115) { } // DON'T FORGET to return a boolean! return true; } private URL getURL(String filename) { URL url = null; try { url = this.getClass().getResource(filename); } catch (Exception e) { // do nothing } return url; } public void init() { this.setFocusable(true); this.setSize(SCREENWIDTH, SCREENHEIGHT); Toolkit tk = Toolkit.getDefaultToolkit(); joe = new Player5(); joe.load(); // construct puck object at joe's stick puck=new Puck5(joe.getX() +17, joe.getY()+10); puck.load(); //create the back buffer for smooth graphics backbuffer= tk.getImage(getURL("HockeyRink.png")); //load the animation strip } public void start() { animationLoop = new Thread(this); animationLoop.start(); } public void stop() { animationLoop = null; } public void run() { Thread t = Thread.currentThread(); while (t == animationLoop) { try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } repaint(); } } public void gameUpdate(int x) { //see if it's time to animate repaint(); } public void update(Graphics g) { if (dbImage == null) { // this size of the background dbImage = createImage (SCREENWIDTH, SCREENHEIGHT); dbg = dbImage.getGraphics (); } // clear screen in background dbg.setColor (Color.white); dbg.fillRect (0, 0, SCREENWIDTH, SCREENHEIGHT); // draw elements in background dbg.setColor (getForeground()); paint (dbg); // draw image on the screen g.drawImage (dbImage, 0, 0, this); /* Handling the shoot animation *Need to move this to Player at *some point */ if (shoot == true) { for(int i=0;i< 8; i++ ) { //draw the back buffer to the screen g.drawImage(backbuffer, 0, 0, this); joe.drawShoot(g, i); puck.drawShape(g); try { Thread.sleep(150); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } gameUpdate(0); } shoot = false; System.out.println("HEYYY"); } } public void paint(Graphics g) { //draw the back buffer to the screen g.drawImage(backbuffer, 0, 0, this); Graphics2D g2d=(Graphics2D) g; //draw the current frame of animation // drawFrame(ball, g2d, ballX, ballY, 8, currentFrame, 20, 20); g2d.setColor(Color.black); if(shoot==false) { joe.drawShape(g); puck.drawShape(g); } } //draw a single frame of animation public void drawFrame(Image source, Graphics2D dest, int x, int y, int cols, int frame,int width, int height) { fx = 0; fy=0; fx = (frame % cols) * width; fy = (frame / cols) * height; dest.drawImage(source, x, y, x+width, y+height,fx, fy, fx+width, fy+height, this); appletInfo(x, y, x+width, y+height,fx, fy, fx+width, fy+height); /** * img - the specified image to be drawn. This method does nothing if img is null. dx1 - the x coordinate of the first corner of the destination rectangle. dy1 - the y coordinate of the first corner of the destination rectangle. dx2 - the x coordinate of the second corner of the destination rectangle. dy2 - the y coordinate of the second corner of the destination rectangle. sx1 - the x coordinate of the first corner of the source rectangle. sy1 - the y coordinate of the first corner of the source rectangle. sx2 - the x coordinate of the second corner of the source rectangle. sy2 - the y coordinate of the second corner of the source rectangle. observer - object to be notified as more of the image is scaled and converted. */ } // Just used to print info about the drawFrame method (help to understand it a bit more). public String appletInfo(int a, int b, int c, int d, int e, int f, int g, int h) { info = "X "+a+" Y "+b+" C "+c+" D "+d+" Current column X "+e+" Current column Y "+f+" Right side "+g+" bottom side "+h; return info; } }