
-----------------------------------
huskiesgoaler34
Thu Jun 02, 2011 12:40 pm

Sprite Movement when key is pressed
-----------------------------------
Hey, I am trying to create a similar game to PacMan called PacMaze. I was able to put all the Sprites i want onto the screen but I am having trouble getting my sprite, the yellow PacMan to move. I put in code that I thought would move the sprite when each arrow key was pressed but it isn't working. However, there are no errors. The program works fine but the sprite doesn't move. (Note: I know that it flickers (I'll fix that later) and I don't want the ghosts to move yet, just the PacMan.) Can someone help me out. I inserted the if statements but the image isn't moving. Thanks in advance.

[code]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import sun.audio.*;    //import the sun.audio package
import java.io.*;


public class PacMaze extends JApplet implements Runnable, KeyListener 
{
  static Font gameFont = new Font("SanSerif", Font.BOLD, 72);
  int width;
  int height;
  ArrayList  walls;
  Image backgroundBuf;
  Image screenBuf;
  Image img;
  Image pinkGhost;
  Image blueGhost;
  JLabel player;
  int x = 0;
  int y = 0;
  int speed = 10;
  Graphics2D background;
  Thread thread;
  
  public static void Music (){
  
//  InputStream in = new FileInputStream("Opening_Sound.mp3");
//// Create an AudioStream object from the input stream.
//AudioStream as = new AudioStream(in);         
//// Use the static class member "player" from class AudioPlayer to play
//// clip.
//AudioPlayer.player.start(as);            
//// Similarly, to stop the audio.
//AudioPlayer.player.stop(as); 

  }



  
  public void init ()
  {
    Music();
    img = getImage (getCodeBase(),"Animated_PacMan_Left.gif");
    pinkGhost = getImage (getCodeBase(),"Pink_Ghost.gif");
    blueGhost = getImage (getCodeBase(),"Blue_Ghost.gif");
    player = new JLabel (new ImageIcon (img));
    player.setSize (img.getWidth (this), img.getHeight (this));
    addKeyListener (this);
    setFocusable (true);
    width = this.getWidth();
    height = this.getHeight();
    walls = new ArrayList  ();
    buildWalls();
        
  }
  public void keyReleased (KeyEvent ke) {}   //have to be here (all 3 methods)
  public void keyTyped (KeyEvent ke) {}
  public void keyPressed (KeyEvent ke) {
    int code = ke.getKeyCode();
    if (code == KeyEvent.VK_UP)
      y-=speed;
    else if (code == KeyEvent.VK_DOWN)
      y+=speed;
    else if (code == KeyEvent.VK_LEFT)
      x-=speed;
    else if (code == KeyEvent.VK_RIGHT)
      x+=speed;
    player.setLocation (x,y);
    
    
  }
  
  public void start ()
  {
    
    
    if (thread == null )
      thread = new Thread (this);
    thread.start();
   
  }
  
  public void stop ()
  {
   if (thread != null ) 
    thread.interrupt();
   thread = null;
  }
  

  
  
public void buildWalls( )
  
{   // create Rectangle objects and store in ArrayList walls
  
  int wallThickness = 10;
      
  walls.add (new Rectangle (0,0, width, wallThickness));//top border
  walls.add (new Rectangle (0,0, wallThickness, height)); //left border     /BORDERS AROUND APPLET FRAME
  walls.add (new Rectangle (width-wallThickness,0, wallThickness,height));//right border
  walls.add (new Rectangle (0, height-wallThickness,width,wallThickness));//down border
  
  walls.add( new Rectangle( 50,height-wallThickness-240, wallThickness, 65 ) ); //top space divider on left side
  walls.add( new Rectangle( 50,height-wallThickness-120, wallThickness, 70 ) );  //bottom space divider on left side      
  walls.add( new Rectangle( 50,height-wallThickness-50, width-120, wallThickness ) ); //bottom horizontal piece
  walls.add( new Rectangle( 50,height-wallThickness-250, width-200, wallThickness ) ); //top horizontal piece (left)
  walls.add( new Rectangle( 50,height-wallThickness-250, width-220, wallThickness ) ); //top horizontal piece (right)
  walls.add( new Rectangle( 180,height-205, wallThickness, 100 ) );  //middle piece
  
  }

public void paintWalls ()
{
  backgroundBuf = createImage (width, height);
  background = (Graphics2D) backgroundBuf.getGraphics();
  background.setColor (Color.black);
  background.fillRect (0,0,width, height);
  background.setColor (Color.RED);
  
  for (int i=0; i