Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (592)
Games in Android Showcase (168)
games submitted by our members
Games in WIP (646)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Pacman Design Issues  (Read 1855 times)
0 Members and 1 Guest are viewing this topic.
Offline ravenger

Senior Newbie

« Posted 2006-01-12 14:39:35 »

Hi, im quite new to java games, so i thought i'd post here.
Im working on a pacman game, which has to have some pathfinding to it later on, but for now im just concentrating on movement of pacman itself.
I have a working movable pacman, but in my opinion it doesnt run as smooth as it should be, and i think its the way i come up with to get it working.

've got a class, Game, which is an extention of JPanel. Furthermore i've implemented KeyListener to make pacman move by passing the direction it is supposed to be moving to, to the Pacman object. (ie, pressing left will do something like pacman.setDirection(Pacman.LEFT)).

So i've got a class Pacman, which implements Runnable, and has a constructorargument Game.
public Pacman(Game game) {;

I also made a method drawObject, which i call from within the Game object its paintComponent method which i have overridden. This method draws the pacman on the screen at the right position. 
// in class Game
public void paintComponent(Graphics g) {

// in class Pacman
public void drawObject(Graphics g) {

I initialize the Pacman object in the Game object, and let pacman run by creating a new Thread and starting it. In the run method of the Pacman object i have a while loop, and each time it passes it checks the direction of the pacman and then updates its coordinates. Last in the whileloop i call game.repaint(); to redraw.
public void run() {
    while(running) {
        // update x, y
       Thread.sleep(50); // if i dont do this, it goes too fast ;) (and yes i know i have to catch an exception here ;))

It works, but like i said, its not running as smooth as it should be, and i've yet to paint the 30x30 tiles maze Wink. What could i consider to make it run more smooth than it does now?
Offline oNyx

JGO Coder

Medals: 2

pixels! :x

« Reply #1 - Posted 2006-01-12 15:04:54 »

Hm. Its unneccessarly complicated.

You only need one thread - the main thread. And since its java2d there is another one... the event dispatcher thread, which just happens to be there.

You need some KeyListener methods, which are called from that dispatcher thread (therefore they are asynchronously). The only thing you do there is setting flags for the moving-direction.

And over in the main thread you only need three things:
(like checking the moving-direction flags and responding to that, eating pills, moving ghosts, colliding with ghosts... stuff like that)

(the board, the pills, the ghosts, pacman some hud...)

and finally some throtteling code for ensuring that it runs at a fixed speed.

You can find some simple throtteling code here:

弾幕 ☆ @mahonnaiseblog
Offline ravenger

Senior Newbie

« Reply #2 - Posted 2006-01-12 15:56:00 »

allright, it sounds nice, but that would imply that i somehow have to implement a loop somewhere that needs to be running till the game ends?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder

Medals: 2

pixels! :x

« Reply #3 - Posted 2006-01-12 16:01:11 »

Yes. (Its the so called "main loop")

Check kev's tutorials:

弾幕 ☆ @mahonnaiseblog
Offline ravenger

Senior Newbie

« Reply #4 - Posted 2006-01-13 10:09:50 »

wow, excelent, thx alot Smiley
The performance is so much better now than it was Smiley
Offline ravenger

Senior Newbie

« Reply #5 - Posted 2006-01-25 11:20:56 »

so i've now come to the point where i've got about 900 entities because i made each wall and each dot an entity (30x30 grid), and the performance degraded alot when painting. Logicaly, because it paints everything each loop, just like in Kevin Glass his tutorial on space invaders. So i figured, why should i paint everything each loop, so i came up with the idea that when a entity doesnt move, it doesnt have te be repainted. The only problem with this problem is that i dont clear the screen, causing everything that does move to have a trail, which i obviously dont want.

How should i solve this?

Edit: using buffered images like the space invaders tutorial instead of doing g.fillRect does seem to improve performance alot, but is it still possible to redraw only what is needed, without having to clear the screen?
Offline Ask_Hjorth_Larsen

Junior Devvie

Java games rock!

« Reply #6 - Posted 2006-01-25 14:17:40 »

You can have a boolean on each thing which can be drawn, specifying whether that thing is 'dirty' or not. Then, when something moves, everything which has a bounding box intersecting the the bounding box of the moving sprite (actually, the union of the bounding box before and after move, since otherwise there may be small areas where the paint "lingers") must be set to dirty and thus repainted. As long as you remember to dirtify ANYTHING that has been in touch with the bounding box of your moving object, you're safe.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

deepthought (17 views)
2015-06-30 15:39:44

deepthought (20 views)
2015-06-30 15:39:09

deepthought (26 views)
2015-06-30 15:36:52

Za\'Anzabar (20 views)
2015-06-29 05:44:54

TritonDreyja (36 views)
2015-06-24 17:10:40

CopyableCougar4 (33 views)
2015-06-23 00:34:45

BurntPizza (36 views)
2015-06-21 20:36:46

cookiecompiler (80 views)
2015-06-11 15:42:53

cookiecompiler (44 views)
2015-06-11 15:41:14

NegativeZero (71 views)
2015-06-11 09:49:18
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!