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 <Rectangle> 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 <Rectangle> ();
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<walls.size(); i++)
{
Rectangle r = (Rectangle) walls.get (i);
background.fillRect (r.x, r.y, r.width, r.height);
}
}
public void paint (Graphics g)
{
g.drawImage (screenBuf,0,0, this);
g.drawImage (img,345,255,this);
// g.drawImage (img,50,15,this); MY TESTING PACMAN
g.setColor (Color.BLUE);
g.drawString("START" , 340, 250);
g.setColor (Color.BLUE);
// g.drawString("FINISH" , 0, 20);
/////////////////////////////////////////////CIRCLES FOR FOOD/////////////////////////////////////////////////////////
g.setColor(Color.YELLOW);
g.fillOval (300,265,10,10);
g.fillOval (260,265,10,10);
g.setColor(Color.GREEN); //LOWER LEVEL
g.fillOval (218,262,15,15);
g.setColor(Color.YELLOW);
g.fillOval (180,265,10,10);
g.fillOval (140,265,10,10);
g.fillOval (100,265,10,10);
g.fillOval (60,265,10,10);
g.fillOval (21,265,10,10);
g.drawImage (pinkGhost,15,255,this);//pink ghost sprite
g.fillOval (21,225,10,10);
g.setColor(Color.GREEN);
g.fillOval (19,180,15,15);
g.setColor(Color.YELLOW);
g.fillOval (21,140,10,10);
g.fillOval (21,100,10,10); //left moving upward
g.fillOval (21,60,10,10);
g.fillOval (21,21,10,10);
g.drawImage (blueGhost,13,133,this);//blue ghost sprite
g.fillOval (61,21,10,10);
g.fillOval (101,21,10,10);
g.fillOval (141,21,10,10); //moving right at top
g.fillOval (181,21,10,10);
g.fillOval (221,21,10,10);
g.fillOval (261,21,10,10);
g.fillOval (261,61,10,10);
g.fillOval (221,61,10,10);
g.fillOval (181,61,10,10);
g.fillOval (141,61,10,10); //second line of food
g.fillOval (101,61,10,10);
}
public void run ()
{
paintWalls ();
screenBuf = createImage (width, height);
Graphics gapplet = (Graphics2D) screenBuf.getGraphics();
while (true)
{
gapplet.drawImage (backgroundBuf, 0,0, this);
repaint();
try {
Thread.sleep (10);
} catch (Exception ex) { stop (); }
}
}
}
|