I am writing a pacman game and it has to be an applet not a JApplet.
The problem is the pacman's movement. He will only go a very short distance before he locks up. He is supposed to be following the laws of the walls according to the text characters in maze.txt although he does not. If pacspeed is changed it will affect the movement of pacman. He just seems to be restricted within a smaller box than the actual maze and he doesn't follow the laws of the walls. Can anyone help or give some pointers?
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Image;
import java.io.*;
import java.net.*;
public class PacMan extends Applet implements KeyListener, Runnable
{
private static final long serialVersionUID = 1L;
String fileToRead = "maze1.txt";
StringBuffer strBuff;
String mazefix;
private char []mazecnv;
private char [][] maze;
Image myPic, Space, TopLeft, TopRight, Vertical, Dot, PowerDot1,botLeft, botRight, Horizontal,
Pacman, PacR1, PacR2, PacR3, PacR4, PacL1, PacL2, PacL3, PacL4, PacU1, PacU2, PacU3, PacU4,
PacD1, PacD2, PacD3, PacD4;
URL base;
MediaTracker mt;
boolean left = false;
boolean right = false;
boolean up = false;
boolean down = false;
int FontSize = 30;
int pacmanx = 213;
int pacmany = 267;
int pacmandx = 0;
int pacmandy = 0;
int AnimPactimer = 1;
int pacspeed = 8;
int speed = 75;
int width = 28;
int height = 31;
public void init()
{
maze = new char[height][width];
addKeyListener(this);
//*************************conveting text maze for images******************
String pacMaze = this.getParameter("fileToRead");
if (pacMaze != null) fileToRead = new String(pacMaze);
readFile();
mazefix = strBuff.toString();
mazecnv = new char[mazefix.length()];
mazefix.getChars(0, mazefix.length(), mazecnv, 0);
int ind = 0;
for(int r = 0; r < height; r++)
{
for(int c = 0; c < width; c++)
{
maze[r][c] = mazecnv[ind];
ind = ind + 1;
while (ind < mazecnv.length && (mazecnv[ind] == '\r' || mazecnv[ind] == '\n'))
{
ind = ind + 1;
}
}
}
//**************************************************************************
resize(451,510); // sets window size.
//********************************load up images****************************
mt = new MediaTracker(this);
try
{
base = getDocumentBase();
}
catch (Exception e)
{
}
botLeft = getImage(base,"Tile-BotLeft.jpg");
mt.addImage(myPic,1);
botRight = getImage(base,"Tile-BotRight.jpg");
mt.addImage(myPic,2);
Horizontal = getImage(base,"Tile-Horizontal.jpg");
mt.addImage(myPic,3);
Space = getImage(base,"Tile-Space.jpg");
mt.addImage(myPic,4);
TopLeft = getImage(base,"Tile-TopLeft.jpg");
mt.addImage(myPic,5);
TopRight = getImage(base,"Tile-TopRight.jpg");
mt.addImage(myPic,6);
Vertical = getImage(base,"Tile-Vertical.jpg");
mt.addImage(myPic,7);
Dot = getImage(base,"Tile-Dot.jpg");
mt.addImage(myPic,9);
PowerDot1 = getImage(base,"Tile-PowerDot1.jpg");
mt.addImage(myPic,10);
Pacman=getImage(base,"Pac-Pacman.jpg");
mt.addImage(myPic, 11);
PacR1=getImage(base,"Pac-PacR1.jpg");
mt.addImage(myPic, 12);
PacR2=getImage(base,"Pac-PacR2.jpg");
mt.addImage(myPic, 13);
PacR3=getImage(base,"Pac-PacR3.jpg");
mt.addImage(myPic, 14);
PacR4=getImage(base,"Pac-PacR4.jpg");
mt.addImage(myPic, 15);
PacL1=getImage(base,"Pac-PacL1.jpg");
mt.addImage(myPic, 16);
PacL2=getImage(base,"Pac-PacL2.jpg");
mt.addImage(myPic, 17);
PacL3=getImage(base,"Pac-PacL3.jpg");
mt.addImage(myPic, 18);
PacL4=getImage(base,"Pac-PacL4.jpg");
mt.addImage(myPic, 19);
PacU1=getImage(base,"Pac-PacU1.jpg");
mt.addImage(myPic, 20);
PacU2=getImage(base,"Pac-PacU2.jpg");
mt.addImage(myPic, 21);
PacU3=getImage(base,"Pac-PacU3.jpg");
mt.addImage(myPic, 22);
PacU4=getImage(base,"Pac-PacU4.jpg");
mt.addImage(myPic, 23);
PacD1=getImage(base,"Pac-PacD1.jpg");
mt.addImage(myPic, 24);
PacD2=getImage(base,"Pac-PacD2.jpg");
mt.addImage(myPic, 25);
PacD3=getImage(base,"Pac-PacD3.jpg");
mt.addImage(myPic, 26);
PacD4=getImage(base,"Pac-PacD4.jpg");
mt.addImage(myPic, 27);
try
{
mt.waitForAll();
}
catch (InterruptedException e)
{
}
//***************************************************************
}
//**********************************reading in file and buffering for no flicker****************
public void readFile()
{
String line;
URL url = null;
try
{
url = new URL(getDocumentBase(), fileToRead);
}
catch(MalformedURLException e)
{
}
try
{
InputStream in = url.openStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
strBuff = new StringBuffer();
while((line = bf.readLine()) != null)
{
strBuff.append(line + "\n");
}
}
catch(IOException e)
{
e.printStackTrace();
}
//****************************************************************************************
}
//***************************************keyListner******************************************
public void keyReleased(KeyEvent evt) { }
public void keyTyped(KeyEvent evt) { }
public void keyPressed(KeyEvent evt)
{
int key = evt.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
right = false;
left = true;
up = false;
down = false;
}
else if (key == KeyEvent.VK_RIGHT)
{
right = true;
left = false;
up = false;
down = false;
}
else if (key == KeyEvent.VK_UP)
{
right = false;
left = false;
up = true;
down = false;
}
else if (key == KeyEvent.VK_DOWN)
{
right = false;
left = false;
up = false;
down = true;
}
}
//*********************************************************
//***************************update pacmans' mouth*********
public void update(Graphics g)
{
AnimPactimer++;
paint(g);
}
//*********************************************************
//**************************Paint all images***************
public void paint(Graphics g)
{
for (int x = 0; x < height; x++)
{
for (int y = 0; y < width; y++)
{
switch (maze[x][y])
{
case '[': g.drawImage(botLeft, y * 16, x * 16, this);
break;
case ']': g.drawImage(botRight, y * 16, x * 16, this);
break;
case '-': g.drawImage(Horizontal, y * 16, x * 16, this);
break;
case ' ': g.drawImage(Space, y * 16, x * 16, this);
break;
case '(': g.drawImage(TopLeft, y * 16, x * 16, this);
break;
case ')': g.drawImage(TopRight, y * 16, x * 16, this);
break;
case '|': g.drawImage(Vertical, y * 16, x * 16, this);
break;
case '.': g.drawImage(Dot, y * 16, x * 16, this); // maze[pacposx / 16][pacposy/ 16] = ' ';
break;
case 'O': g.drawImage(PowerDot1, y * 16, x * 16, this);
break;
default: g.drawImage(Space, y * 16, x * 16, this);
break;
}
}
}
if (left) //Left
{
if(AnimPactimer > 4)
AnimPactimer = 1;
if (AnimPactimer == 1)
Pacman = PacL1;
else if (AnimPactimer == 2)
Pacman = PacL2;
else if (AnimPactimer == 3)
Pacman = PacL3;
else if (AnimPactimer == 4)
Pacman = PacL4;
}
if (right) //Right
{
if(AnimPactimer > 4)
AnimPactimer = 1;
if (AnimPactimer == 1)
Pacman = PacR1;
else if (AnimPactimer == 2)
Pacman = PacR2;
else if (AnimPactimer == 3)
Pacman = PacR3;
else if (AnimPactimer == 4)
Pacman = PacR4;
}
if (up) //Up
{
if(AnimPactimer > 4)
AnimPactimer = 1;
if (AnimPactimer == 1)
Pacman = PacU1;
else if (AnimPactimer == 2)
Pacman = PacU2;
else if (AnimPactimer == 3)
Pacman = PacU3;
else if (AnimPactimer == 4)
Pacman = PacU4;
}
if (down) //Down
{
if(AnimPactimer > 4)
AnimPactimer = 1;
if (AnimPactimer == 1)
Pacman = PacD1;
else if (AnimPactimer == 2)
Pacman = PacD2;
else if (AnimPactimer == 3)
Pacman = PacD3;
else if (AnimPactimer == 4)
Pacman = PacD4;
}
g.drawImage(Pacman, pacmanx, pacmany, this);
}
public void start()
{
Thread th = new Thread (this);
th.start ();
}
//***********************Move the pacman depending on what key is being pressed**************
public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
int px = pacmanx/16 - pacspeed/16;
int py = pacmany/16;
char posXY = maze[px][py];
boolean condition = posXY != '|' && posXY != '(' && posXY != ')' && posXY != ']' && posXY != '[' && posXY != '-'; //Laws of the walls
if (left) //Left
{
if (condition)
{
pacmanx -= pacspeed; //decrease x position to make image go left
pacmany -= 0;
System.out.println(pacmanx);
repaint();
}
else if(!condition)
return;
}
if (right) //Right
{
if (condition)
{
pacmanx += pacspeed; // increase x position to make image go right
pacmany += 0;
System.out.println(pacmanx);
repaint();
}
else if(!condition)
return;
}
if (up) //Up
{
if (condition)
{
pacmanx -= 0;
pacmany -= pacspeed; // decrease y postition to make image go up
System.out.println(pacmany);
repaint();
}
else if(!condition)
return;
}
if (down) //Down
{
if (condition)
{
pacmanx += 0;
pacmany += pacspeed; // increase y position to make image go down
System.out.println(pacmany);
repaint();
}
else if(!condition)
return;
}
try
{
Thread.sleep(speed);
}
catch (InterruptedException e)
{
}
}
}
//*******************************************************
}
All image files included in zip and maze.txt file also included. html file won't upload? Anything wrong with files let me know.
Thanks everyone for the help.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.