import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Rectangle;
import java.awt.image.*;
import java.util.*;
import sun.audio.*; //import the sun.audio package
import java.io.*;
public class PacMaze2 extends JApplet implements Runnable, KeyListener
{
static Font gameFont = new Font("SanSerif", Font.BOLD, 72);
int width;
int height;
int xcordPink = 150;
int ycordBlue = 133;
int xcordOrange =253;
ArrayList <Rectangle> walls;
Image backgroundBuf;
Image screenBuf;
Image img;
Image cherry;
Image pinkGhost;
Image blueGhost;
Image orangeGhost;
Image finishLineCongratulations;
boolean collision;
JLabel player;
Thread runner;
int x = 355;
int y = 255;
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();
collisions();
img = getImage (getCodeBase(),"Animated_PacMan_Left.gif");
pinkGhost = getImage (getCodeBase(),"Pink_Ghost.gif");
blueGhost = getImage (getCodeBase(),"Blue_Ghost.gif");
orangeGhost = getImage (getCodeBase (),"Orange_Ghost.gif");
cherry = getImage (getCodeBase (),"Pac_Cherry.gif");
finishLineCongratulations = getImage (getCodeBase(), "PacMan_Congrats.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 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-60, wallThickness ) ); //bottom horizontal piece
walls.add( new Rectangle( 50,height-wallThickness-250, width-200, wallThickness ) ); //top horizontal piece (left)
walls.add( new Rectangle( 285,height-wallThickness-250, width-340, wallThickness ) ); //top horizontal piece (right)
walls.add( new Rectangle( 180,height-wallThickness-150, width-20, wallThickness ) );//middle piece (across)
walls.add( new Rectangle( 250,height-wallThickness-100, width-20, wallThickness ) ); //lower middle piece across
walls.add( new Rectangle( 180,height-205, wallThickness, 100 ) ); //middle piece
walls.add( new Rectangle( 120,height-205, wallThickness, 100 ) );
}
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 (cherry,64,55,this);
g.drawImage (finishLineCongratulations,358,155,this);
// g.drawImage (img,256,42,this); //MY TESTING PACMAN
g.setColor (Color.BLUE);
g.drawString("START" , 350, 250);
g.setColor (Color.BLUE);
g.drawString("FINISH" ,350, 150);
/////////////////////////////////////////////CIRCLES FOR FOOD/////////////////////////////////////////////////////////
g.setColor(Color.YELLOW);
g.fillOval (340,265,10,10);
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.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.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); //1
g.fillOval (261,21,10,10);
g.fillOval (300,21,10,10);
g.fillOval (338,21,10,10);
g.fillOval (371,21,10,10);
g.setColor(Color.GREEN);
g.fillOval (369,57,15,15);
g.setColor(Color.YELLOW);
g.fillOval (338,61,10,10);
g.fillOval (300,61,10,10); //2
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);
g.fillOval (371,120,10,10);
g.fillOval (338,120,10,10);
g.fillOval (300,120,10,10);
g.fillOval (261,120,10,10); //4
g.fillOval (221,120,10,10);
g.fillOval (371,90,10,10);
g.fillOval (338,90,10,10); //3
g.fillOval (300,90,10,10);
g.fillOval (261,90,10,10);
g.fillOval (221,90,10,10);
g.fillOval (150,90,10,10); ///ANGLED FOOD CIRCLE
g.fillOval (101,95,10,10);
g.fillOval (371,215,10,10);
g.fillOval (338,215,10,10);
g.fillOval (300,215,10,10);
g.fillOval (260,215,10,10);
g.fillOval (218,215,10,10);
g.fillOval (178,215,10,10);
g.fillOval (138,215,10,10);
g.fillOval (103,215,10,10);
g.fillOval (103,175,10,10);
g.fillOval (103,135,10,10);
g.fillOval (65,215,10,10);
g.fillOval (65,175,10,10);
g.fillOval (65,135,10,10);
g.fillOval (65,95,10,10);
g.fillOval (338,165,10,10);
g.fillOval (300,165,10,10); //5
g.fillOval (260,165,10,10);
g.fillOval (218,165,10,10);
g.fillOval (150,90,10,10);
g.fillOval (150,130,10,10);
g.fillOval (150,170,10,10);
g.drawImage (img,x,y,this);//345,255
g.drawImage (pinkGhost,xcordPink,255,this);//pink ghost sprite
xcordPink--;
if (xcordPink <-1)
{
xcordPink = 150;
}
g.drawImage (blueGhost,13,ycordBlue,this);
ycordBlue--;
if (ycordBlue <-1)
{
ycordBlue = 133; //blue ghost sprite
}
g.drawImage (orangeGhost,xcordOrange,84,this);
xcordOrange--;
if (xcordOrange <-1)
{
xcordOrange = 253; //blue ghost sprite
}
}
public void keyReleased (KeyEvent key) {} //have to be here (all 3 methods)
public void keyTyped (KeyEvent key) {}
public void keyPressed (KeyEvent key) {
int code = key.getKeyCode();
if (code == KeyEvent.VK_UP)
y-=speed;
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 boolean checkCollisionWalls (Rectangle entityAura) //?????????????????
//{
// for (int i= 0; i<walls.size (); i++)
// {
// if (entityAura.intersects (walls.get(i)))
// return true;
// }
// return false;
//}
public Rectangle boundariesPacMan ()
{
return (new Rectangle (x,y,28,28));
}
public Rectangle boundariesFood ()
{
return (new Rectangle (x,y,340,265));
}
public void collisions ()
{
// if ()
}
public void run ()
{
paintWalls ();
screenBuf = createImage (width, height);
Graphics gapplet = (Graphics2D) screenBuf.getGraphics();
while (true)
{
long time = System.currentTimeMillis();
gapplet.drawImage (backgroundBuf, 0,0, this);
repaint();
time = (1000) - (System.currentTimeMillis() - time);
try {
Thread.sleep (10);
} catch (Exception ex) { stop (); }
}
}
}
|