Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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 1625 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.
1  
2  
3  
4  
//constructor
public Pacman(Game game) {
    this.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. 
1  
2  
3  
4  
// in class Game
public void paintComponent(Graphics g) {
    pacman.drawObject(g);
}

1  
2  
3  
4  
// in class Pacman
public void drawObject(Graphics g) {
    g.fillRect(x,y,20,20);
}


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.
1  
2  
3  
4  
5  
6  
7  
public void run() {
    while(running) {
        // update x, y
      game.repaint();
       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:
-logic
(like checking the moving-direction flags and responding to that, eating pills, moving ghosts, colliding with ghosts... stuff like that)

-drawing
(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:
http://www.java-gaming.org/forums/index.php?topic=11640.30

弾幕 ☆ @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:
http://www.cokeandcode.com/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 Member




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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (43 views)
2014-09-23 14:38:19

radar3301 (25 views)
2014-09-21 23:33:17

BurntPizza (62 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (37 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!