Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Game Development and the fly likes Question about sprite movement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Question about sprite movement" Watch "Question about sprite movement" New topic

Question about sprite movement

Joseph Tulowiecki

Joined: Jun 10, 2011
Posts: 25
Hi I'm trying to write a pacman game for a cellphone ( Java 2D J2ME ). I am fairly new to java and I am trying to brain storm ideas on how to structure this program without any repetitive code.
I have a tiled background for the layout. I want the ghosts to travel in random directions on their own. First off I'm organizing the program like this. I have an abstract Character class. Then underneath that I have a PacmanCharacter, GhostCharacter, and FruitCharacter class. Now back to my initial question... There are many locations on the background where I want the ghost to have to make a decision whether it be keep going straight or make a turn I was going to do this by just randomly selecting a direction at each intersection however in order to do this I would have to have an array of variables that contains the information on each pixel location where a turn is possible, and which direction it is possible to turn in. Then I would have to write a for loop and scroll through all of this information every time the game updates. As you can see this is a ton of unnecessary code that will no doubt slow down my game. SO! My next idea is to have a a track line through the paths the (Sprite) ghost can travel on. Any just make it so the ghost can not leave this track line. Then for each level all I would have to do is create a new track line and not have to set all new variables... (As you can see I'm quite new so these solutions arent coming to me naturally yet) I was wondering, Is this latest idea the best way of going about something like this? And if so, how would I implement it? I don't even know where to begin. Thanks in advance.
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4616

Hi Joseph,

I suggest you start with a Maze class. It simply provides methods that say whether a particular cell is a wall or not. Now, your Game class has one such Maze, and it also keeps track of the players. Game will map 2D coordinates to Maze coordinates. It can determine in which cells a player is located by converting the coordinates of the corners of the bounding box of a player to maze coordinates. Now it can determine whether it's possibly for a player to move further up, down, left or right.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Joseph Tulowiecki

Joined: Jun 10, 2011
Posts: 25
cooooool ok im going to look in to this right now. this forum is great thanks a lot man appreciate it!
I agree. Here's the link:
subject: Question about sprite movement
It's not a secret anymore!