I NEED YOUR HELP with my game PLEASE! It runs slow!!
I asked this question in "Java Programming" but I did not get a good answer.
This is the second time I am writing this same code, which is for a pacman game, (I converted my code to active rendering) but I still have the same problem. I am totally frustrated, please help me.
The problem is this:
If I try to run my game on a fairly fast computer (Generally above 800Mhz) my game runs at an accebtable rate. But If I run it on lets say a 500Mhz PC my game runs really slowly, and I get a low FPS. I don't think that this should happen since Pacman is a fairly simple game.
I've triedeverything but without success please help me.
My code is downloadable in the site below (its a 34kb zip file)
PLEASE take a look at the code if you have time (you can also run it), and let me know what you think and any advices you have.
The site URL is: http://homepage.ntlworld.com/pars/pacman/pac.zip
The main file is "Game.java"
SleepTimer.java is a timer class and Maze.java generates a maze for the game.
Thank you in advance
[1100 byte] By [
pars40a] at [2007-9-28 11:03:04]

I ran it on a PII 400 MHz laptop with 4 megs of VRAM and it ran fine. Maybe there is something else wrong on your slow machine (background tasks, etc.).
P.S. I am assuming that the data printed to the console is the FPS. I'm averaging 31.
runs fine on my comp (and i have sucha 500mhz machine (98se/128mb/gf2mx)) :x
[...]
while (running)
{
requestFocus(); //added this thingy
[...]
i had to add a requestFocus() in the mainloop... to get the keys to work. dunno if there is a better place... well at least it works there :>
oh... and add:
//overwrite the update function to avoid flicker
public void update(Graphics g)
{
paint(g);
}
and do u really need all these sprites? (if u need sprites it's better to use one big image with all animations inside... then slice it up to get your sprites/tiles - this way u have the file-header only once. that saves a lot of space if u have tons of tiny images)
btw in this case u could create em with drawing funktions to save space (me points at the 4k contest) ;)
btw?the input handling needs some adjustment... the original was different (let's say u walk up... and hit then left but u cant walk left at this place... if u hold it u walk left at the next possible position. try to implement it this way)
oNyxa at 2007-7-12 1:20:38 >

Really, in that case its a relief, The thing is I ran my code on some college computer (400Mhz) and I got around 13-14 fps. They probably have loads of background processes (as rgeimer suggested).
oNyx, my main concern here was the speed of the application, I know there are problems with collision detection, and the controls (as you mentioned). But I will definitely sort them out. Incidentally can you tell me what fps you got when you ran my code (its displayed on the console window).
Now that you are here, can I ask you some questions please? If I use one big image instead of all those little gif images would it have any effect on the speed of the game?
If I use java code to actually create the images (eg "drawArc" to draw pacman etc), would that help to run the game faster than using .gif images.
Thanx alot
it would probably be faster,but it should be possible to get it fast enough with images too.
zuloa at 2007-7-12 1:20:38 >

i get 30-33fps wich looks fast enough for this kind of game.
>If I use one big image instead of all those
>little gif images would it have any effect
>on the speed of the game?
u shouldnt notice any difference.
>If I use java code to actually create the
>images (eg "drawArc" to draw pacman etc),
>would that help to run the game faster
>than using .gif images
should also be as fast as before (approx).
no speed... just filesize.
but u can speed the whole drawing routine up a lot. with clipping.
atm u draw everything into your buffer... and flush then everything onto the screen.
it's obviously much faster if u only redraw the parts wich needs to be redrawn in your buffer... and copy then only the needed parts onto the screen.
u have 19*19 tiles wich are X*X pixles. and atm u need only to redraw 1 or 2 of em (with the ghosts u'll have to redraw 5-10... but thats still much faster than 361 tiles ;))
check the documentation of drawImage... u can pass clipping coords through it :)
oNyxa at 2007-7-12 1:20:38 >

Thanx alot for all your replies :-)
if you realy want to improve performance, switch to VolatileImages (if you only need Opaque backgrounds) or automatic Images(if you need Bitmask transparency), and use Fullscreen exclusive mode and a page flipping BufferStrategy.
Yeah it would definitely be a good thing if I can get my game into Full Screen exclusive, Is there anywhere in particular that you have good tutorials of the topic(s) you mentioned (I mean other than the ones on Sun).Thanx
javadoc covers it all pretty thoroughly :S
something like this... (this wont compile, its off the top of my head)
[code]
class FullScreenFrame extends Frame implements Runnable
{
BufferedImage automaticImage;
VolatileImage volatileImage;
public FullScreenFrame(DisplayMode dm)
{
GraphicsConfiguration gc = getGraphicsConfiguration();
GraphicsDevice gd = gc.getDevice();
gd.setFullScreenWindow(this); //theres your fullscreen window...
gd.setDisplayMode(dm); //change resolution
createBufferStrategy(2); //creates your BufferStrategy
//it will default to the best available strategy
//(which, in fullscreen will be a page flipping strategy)
automaticImage = gc.createCompatibleImage(32,32,Transparency.BITMASK); //creates an automatic image
volatileImage = gc.createCompatibleVolatileImage(32,32); //creates a volatile image
//*misc image init code*
}
public void run()
{
while(true)//your game loop
{
Graphics g = getBufferStrategy.getDrawGraphics();
//gets a graphics context for drawing onto the back Buffer
//*misc rendering code*
//note, you'll have to add code for dealing with the VolatileImage losing its surface
g.dispose();
getBufferStrategy().show(); // causes the buffer to flip
//maybe have a sleep here
}
}
}
>Is there anywhere in particular that you have
>good tutorials of the topic(s) you mentioned
>(I mean other than the ones on Sun).
some src:
http://www.javagaming.org/Code/code.html
some txt:
http://www.javagaming.org/Documents/Understanding_AWT_Image_Types/understanding_awt_image_types.html
oNyxa at 2007-7-12 1:20:38 >

> P.S. I am assuming that the data printed to the
> console is the FPS. I'm averaging 31.
You should not print that stuff to the console, printing stuff to the console all the time can seriously decrease performance(well maybe only when you want debugging info, define a boolean variable DEBUG).
There is also a capital letter problem with your gifs: pacxx.GIF. You should
rename them all to pacxx.gif.
jpwinne
maybe you should remove that timer, it has no benefit. Calling System.currentTimeMillis() all the time also hurts performance.
public void run()
{
addKeyListener(this);
requestFocus();
offscreen = getGraphicsConfiguration().createCompatibleImage(665,665);
buff = offscreen.getGraphics();
//frameTimer = new SleepTimer();
//frameTimer.setDelay(1000 / fps);
//frameTimer.setAutoCorrection(true, fps);
//frameTimer.startTimer();
try
{
while (running)
{
// .... your stuff
// call the paint method to draw the offscreen image
// to the screen
paint(this.getGraphics());
Thread.sleep(1000/fps);
// counting the number of frames per second
counter++;
//if ( (System.currentTimeMillis() - start) >= 1000)
//{
//System.out.println(counter);
//start = System.currentTimeMillis();
//counter = 0;
//}
}
}
catch (Exception e) {}
long globalFps = counter / ((System.currentTimeMillis() - start) / 1000);
System.out.println("fps = " + globalFps);
}
You should also get the logic out of your keyPressed methods (put them in the game loop)
Your code should read:
// setting up the keyboard controls
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_LEFT)
currentDirection = LEFT;
else if (e.getKeyCode()==KeyEvent.VK_RIGHT)
currentDirection = RIGHT;
else if (e.getKeyCode()==KeyEvent.VK_UP)
currentDirection = UP;
else if (e.getKeyCode()==KeyEvent.VK_DOWN)
currentDirection = DOWN;
}