7

Edit: Totally forgot to mention I'm coding in Java

I'm having a real hard time making some kind of detection system or some way to make my pacman sprite/character move smoothly through my board in the game. I did not make the board it's a image.

I had tried colour detection first which worked the best yet was not smooth at all and pretty choppy.

I then tried to manual input coordinates of location not allowed to be entered. This also did not work out so well.

I'm currently trying now to have the program use colour detection and check a separate unseen board to see if I'm still on the path. This has failed by far the most. It seems like it would be the smartest but the corners are just alful and hard to fix by adjusting the images.

I'm wondering what method you guys would suggest for such a task.

Kara
  • 6,115
  • 16
  • 50
  • 57
ComputerLocus
  • 3,448
  • 10
  • 47
  • 96
  • It would help to let us know what language you are using. – sosborn Nov 14 '11 at 05:09
  • Is your board a uniform grid? – skyuzo Nov 14 '11 at 05:15
  • I'm not sure what that is so I'm assuming not? Should I be using this? – ComputerLocus Nov 14 '11 at 05:16
  • What do you mean you did not make the board? The easiest way to implement it is a 2d array. – Austin Henley Nov 14 '11 at 05:18
  • @Fogest, he's taking about a grid. What you can do is take your image and overlay a grid on it, this won't be in the final product, but you can then see how many pixels wide each coordinate needs to be. You can keep track of the path and walls by using a 2D array. Heres an example: http://home.comcast.net/~machaira/Pac-Man.png – Matt Nov 14 '11 at 05:24
  • I used a image off the internet. I'm assuming that was a bad idea and I should instead make my own. Now if I do make my own should I draw them from the 2D array then? Or how should I do it? And here is the image in case it helps [image](http://imgur.com/IG3Fh) – ComputerLocus Nov 14 '11 at 05:24
  • 1
    @Matt Okay so using that how would I be checking if pacman is still on the path or only allow him on the path? – ComputerLocus Nov 14 '11 at 05:25
  • @Fogest another suggestion is just to forget about the images and just program it first. You can create a toString method to print out a board or something. Create your methods and test them. The images just makes it look pretty, but the programming will actually do the work. Is this your first year programming? – Matt Nov 14 '11 at 05:27
  • @Matt I will keep this in mind. Please take a look at my edit above^ – ComputerLocus Nov 14 '11 at 05:28
  • @Fogest well to check you would create a 2D array and fill it info. So you could say that each space is an object and if it's null then it's wall. So if someone tries to move up or down and that space is null, then pacman can just keep going. To do the gui you will have to use listeners, which is a completely different thing. – Matt Nov 14 '11 at 05:29
  • @Matt Well we have not learnt about objects yet. – ComputerLocus Nov 14 '11 at 05:31
  • @Fogest: It seems like the fundamental way you're departing from traditional Pacman is that you're using a (relatively) continuous coordinate system rather than discrete. Even though the motions in Pacman are fluid and continuous, there are still discrete points at which Pacman can stop. It's just smoothing out the animation each time he moves so it looks continuous. – Mark Peters Nov 14 '11 at 05:32
  • @Fogest you could probably get away with creating a 2D array with 0's and 1's or whatever you want, where a 0 would be a valid path, but it has no dot and a 1 is also valid, but it has a dot to eat. – Matt Nov 14 '11 at 05:34
  • Yes my method is not the best I know. Which is why I need a new one. @MarkPeters – ComputerLocus Nov 14 '11 at 05:35
  • @Matt How do I get to this "chat". I understand what you mean by having it check basically if it's valid area or not but what would it be a area with the coordinates of the walls and that array is = 1 and everywhere else is = 0? – ComputerLocus Nov 14 '11 at 05:37
  • 1
    @Fogest yea i was going to send this to chat, but you need 20 rep. I really dislike that. I wish there was a way to at least override it when someone with rep creates the room. A wall could be -1 if you want. You will check this every time he moves basically to make sure he can move into that location. If it's an int array, then you could just do -1 for a wall, 0 for no dot, 1 for a dot. – Matt Nov 14 '11 at 05:42
  • @matt So I'm still not understanding how am I telling the program that -1 is a wall 0 is dot and 0 is nothing? – ComputerLocus Nov 14 '11 at 05:45
  • @Fogest first create a 2D array. Just create a grid. Don't do anything to it yet. Find out what size you want. Do you want a 20x20? 20x30? etc.. Then, you can fill in the data to that array. So you need to figure out where the walls are going to be. Let me see if i can draw a picture real quick. – Matt Nov 14 '11 at 05:57
  • @Matt a picture would surely help. – ComputerLocus Nov 14 '11 at 06:00
  • @Matt: This is fairly interesting question. So I've +1'ed so that the OP has enough rep to send this to chat. – Mysticial Nov 14 '11 at 09:04

1 Answers1

5

A typical approach to storing "old school" game boards is to use a char or int multidimensional array. Using Matt's excellent little graphic you can see there are 21 by 21 squares in the board:

int board[21][21] = {{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
                     {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},
                     {1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1},
                     /* ... and so on, for all 21 lines .. */                      }};

It doesn't really matter which numbers you pick for walls and pathways. The "pathway" positions initially contain a code for "contains a dot". As paccy consumes the dots, store a new value into the board at the position to indicate that the dot has been consumed but it is still a pathway square. Matt recommended -1 for walls, 0 for no dot, and 1 for a dot -- that's a pretty plan, as it lets your "wall collision" routines simply look for

if (board[pac.x][pac.y] > 0) {
    /* still in bounds */
} else {
    /* collided against a wall */
}

The downside is the -1 is more awkward looking in your array initializer.

If this were done in C, it'd be easy enough to "improve" this using char board[21][21] instead of int board[21][21] and store the game board as a C string:

char board[21][21] = " XXXXXXXXXXXXXXXXXXX "
                     " X        X        X "
                     " X XX XXX X XXX XX X "
                     " X                 X "
                     " X XX X XXXXX X XX X "
                     " X    X   X   X    X "
                     " XXXX XXX X XXX XXXX "
                     "    X X       X X    "
                     "XXXXX X XXXXX X XXXXX"
                     "        X   X        "
                     "XXXXX X XXXXX X XXXXX"
                     "    X X       X X    "
                     " XXXX X XXXXX X XXXX "
                     " X        X        X "
                     " X XX XXX X XXX XX X "
                     " X  X           X  X "
                     " XX X X XXXXX X X XX "
                     " X    X   X   X    X "
                     " X XXXXXX X XXXXXX X "
                     " X                 X "
                     " XXXXXXXXXXXXXXXXXXX";

This is far easier to read in the source code, takes less memory, and your wall-collision routines can look like this:

if (board[pac.x][pac.y] == 'X') {
    /* collided with a wall */
} else {
    /* still in bounds */
}

(Though the trailing NUL that the compiler will insert at the end of the string means that lower-right-hand square can never be used for pathway or wall -- a little more effort can work around that, but it isn't as beautiful.)

I don't remember enough Java to make this work in Java -- but I'm sure you can figure out something if this looks compelling enough.

Community
  • 1
  • 1
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Okay so I for the most part understand what you are saying. Though I must use Java for this. The part I'm stuck on is how to implement this. So lets say I start with a empty 500 by 500 board. How do I make the grid and have an array that some how knows the coordinates of each box. Because the first code you posted how does it know the size of each grid item? I'm getting really confused with that part. – ComputerLocus Nov 14 '11 at 06:16
  • Looks good. yea i just suggested int's since it's easier i think to work with int's in java. But obviously anything would work. You can access a string in java as if it was an array. – Matt Nov 14 '11 at 06:19
  • I feel like such a noob. I really am getting confused. I'm trying to understand this but I'm still stuck on this grid. – ComputerLocus Nov 14 '11 at 06:21
  • @Fogest like i said, don't worry about the gfx right now. You want to just get it working before you make it pretty. When you do add graphics you need to match up how many pixels each coordinate is. For now get your grid going, create a game board. Create your pacman, don't even worry about the ghosts yet. Make your pacman move. Make him eat. You only want to move him one coordinate at a time. – Matt Nov 14 '11 at 06:22
  • The size of the grid item is unrelated to the size of the logical game board -- standard pacman is 21x21 as Matt's awesome image shows. You can have each square take up 2x2 pixels for a tiny 42x42 game (probably unplayable, but might be fun to put on a wrist-watch or something), or have each square take up 20x20 pixels for a medium-sized 420x420 pixel game board, or have each square take up 100x100 pixels for a _huge_ 2100 x 2100 pixel game board. The _display_ routines won't actually care about the underlying size of the board -- and the board doesn't care about the graphical representation. – sarnold Nov 14 '11 at 06:22
  • The cool thing is, when you separate out the _display_ of the game from the _rules_ of the game, you can simply swap out the graphical components and go from a simple text-based game to a full-blown 3d-rendered game without touching any of the core pacman logic: eat, run, slam into walls and wait for more directions. – sarnold Nov 14 '11 at 06:24
  • @sarnold well my problem still persist. In the array you have shown in your post it tells the program it's 21 by 21 squares correct? Or is that 21 by 21 telling it how big each square is and the lines after are for each square? At your level of knowledge this is most likely a breeze but it's pretty hard to understand this at such a low level. – ComputerLocus Nov 14 '11 at 06:28
  • @Fogest it's a 21x21 grid. This has nothing to do with how big each square is. The grid just holds data, but doesn't represent the actual representation of how the game will look. This is why i suggested to create a toString method to print out the game board. So, you can see where pacman moved, what he ate, where the walls, etc.. – Matt Nov 14 '11 at 06:29
  • @matt it's hard to explain what I don't understand. What I mean is when I'm checking the grid if it's a 1 or 0 how does it know that the 1 or 0 corresponds to a whole chunk of area in a specific grid location? Also I never learnt toString methods. – ComputerLocus Nov 14 '11 at 06:31
  • 1
    @Fogest o, that's easy. You just check the coordinate what it is. So, say pacman is currently at (1,5) and he wants to the move to the right to (2,5), where it's (x,y). You could flip it if you want. Anyway, you need to make sure (2,5) is not a wall. So you check the array, like if(board[1,4] == 0) pacman.move(). pacman would be an object that would update itself when you call move. This is another topic though. But you just directly call the coordinate before you even move to make sure you can. If it's a -1 then you simply stay in the same coordinate. You can do the same thing with strings. – Matt Nov 14 '11 at 06:37
  • @Fogest, I expanded the C example a lot larger to show the entire game board. (I'm afraid I'm too lazy to do the same with the integers in the Java version -- it took a fair amount of careful typing to get even three lines of the board written. :) -- I hope this helps explain better what I was suggesting. – sarnold Nov 14 '11 at 06:37
  • @sarnold He might be able to copy and paste it actually and just " XXXX".toCharArray(), not sure if that would work, but hey it's worth a try. – Matt Nov 14 '11 at 06:41
  • @Matt: definitely, if it were _my_ problem to solve, I'd come up with some way of writing the levels nearly as I did there -- if that meant coming up with a string parser or tokenizer that could build the _real_ levels, I would :) but that's a _lot_ to dump onto someone who is just beginning. :) – sarnold Nov 14 '11 at 06:43
  • Okay let me put this into a example. I have a 100 by 100 board and it's a 10 by 10 grid. Now I'm saying the very last box in the bottom right corner is a 1 meaning it's a safe spot. How does the array somehow correspond to 98 by 98 down in that corner? Where in the array is it storing some kind of coordinate to say where that 1 is? – ComputerLocus Nov 14 '11 at 06:45
  • I think I understand it now! The array is simply representing the movement and is really only moving one pixel? Correct? But in the GFX side it would be moving maybe more then that? Is that how it's working? – ComputerLocus Nov 14 '11 at 06:48
  • @Fogest honestly don't worry about that right now. Just focus on the ascii version, then you can add graphics. I think an ascii version would be enough if you ask me though. The two have nothing to do with one another. Let me put it like this, if you ever played a game, then that game uses some kind of engine. Every play counterstrike? portal? half-life? Those are all different games, but they use the same engine (the same code). – Matt Nov 14 '11 at 06:49
  • @Fogest yes, if that's the way you want to think of a grid (as 1 pixel), then yes that's fine. A grid really has no resolution, it's just the number of squares you have. – Matt Nov 14 '11 at 06:50
  • @matt so I am starting to understand now what you are talking about. I think I would prefer the int way as it seems easier for me. I don't understand the char way. How do you tell where pacman is on it? It just seems a lot more confusing for a noob like me. – ComputerLocus Nov 14 '11 at 06:53
  • 1
    @Fogest you don't have to put him on the grid. In your case, because you don't understand objects, i would say just create two variables. PacmanX, PacmanY or whatever. Those would be ints corresponding to where hes located. – Matt Nov 14 '11 at 06:59
  • @matt so I'm going to use the first array shown and of course expand on that. I feel like I know what I have to do with it. I have to overhaul my pacman. Remove most of the graphics and redo them after. Looked ugly anyways. – ComputerLocus Nov 14 '11 at 07:01
  • I wish we were in the same room, and could cut out a cute little picture of Pacman and move him around some graph paper with X and blanks in the squares... then get a fancier picture of Pacman and move him around the graph paper. I think it'd be easier. :) – sarnold Nov 14 '11 at 07:03
  • @sarnold I understand the concept of what you are saying but I don't have the needed skills to implement such a element. Though if I do finish the game I will switch to your way for a nice challenge and maybe get some extra marks. – ComputerLocus Nov 14 '11 at 07:05
  • @sarnold yea it sucks having a crappy teacher :( I'm surprised he's letting him even do this. Luckily i had a good teachers at that time. I also wish he had 20 rep, hate that requirement for chat room. – Matt Nov 14 '11 at 07:07
  • @Matt: definitely, that 20 point restriction seems to hurt the people who'd benefit from the chat rooms the most. I'm actually pretty glad his teacher is letting him do this -- it's an _excellent_ challenge and led Fogest here. :) – sarnold Nov 14 '11 at 07:23
  • @sarnold Depends where you are, 2nd semester programming it makes sense. First semester meh, it could be fine. Adding a GUI though, definitely not ok at this level. Thats why i said, the ascii version should be enough. – Matt Nov 14 '11 at 07:30
  • @Matt: actually, with [a lot of effort, even <20 rep users can participate in the chat](http://meta.stackexchange.com/questions/111942/allow-inviting-people-with-rep-20-to-private-chat)! Though it probably would have just been easier to head to oftc.net. :) Hehe. – sarnold Nov 14 '11 at 08:55
  • @sarnold: I've given the OP the last upvote needed to reach 20 rep. So feel free to send this to chat. – Mysticial Nov 14 '11 at 09:10
  • @Mystical, thanks! Now if only we could get that auto-chat link back. hehe. – sarnold Nov 14 '11 at 09:13
  • I've started to understand this and am currently working on the grid. Will report back if I get the part done! – ComputerLocus Nov 14 '11 at 18:08
  • Okay I have another question. When drawing all of the dots is there a faster way to draw them all or should I just have an array and use a for loop and go through drawing them? There must be some trick that is faster then manually entering in each coordinate and such? Or is that the only way? – ComputerLocus Nov 15 '11 at 04:09
  • @Matt The link to the picture of the grid is what I'm now using. I have drawn it and will now make the array. In case anyone is wondering here is what is looks like so far [Pacman Image with Grid and Without](http://i.imgur.com/DgNko.png) – ComputerLocus Nov 15 '11 at 04:53
  • @sarnold I talked to my teacher about your way and now since mentioning it he thinks that would have been a better way then his other ways. Thanks bro! – ComputerLocus Nov 15 '11 at 04:54
  • @Fogest looks good, but remember the GUI should be the last thing you work on. – Matt Nov 17 '11 at 03:19
  • @Fogest, looks good so far! Drawing each dot individually out of the grid won't be too bad; these are small enough amounts of data that repainting the entire board from the data structures every update should be acceptable enough game play. Using [more complicated tricks](http://en.wikipedia.org/wiki/Image_mask#Image_masks) to smooth gameplay performance can come once the simpler tasks are working. – sarnold Nov 17 '11 at 03:33
  • @sarnold that is way beyond the requirements of our game. On top of that I don't really understand the Image Mask either. – ComputerLocus Nov 17 '11 at 23:00