• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Question about sprite movement

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 15725
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joseph Tulowiecki
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cooooool ok im going to look in to this right now. this forum is great thanks a lot man appreciate it!
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic