0

See Having Some Issues With Making Pacman to understand this question fully. Just look at the first part of the accepted answer. And this is in java!

I am using the array as shown in the link above. I have my pacman character moving around fine other then the fact that it is moving 21 pixels every 100ms therefore it's not smooth at all.

Currently I'm just multiplying the x and y of the current array position by 21. I'm wondering how I would go about making this smooth. I have tried some ways of how to move but they have not worked and I'm not very skilled and my ways are not efficient, therefore I have come here looking for a good way to move around smoothly.

Some things that you may need to know: Pacman is first checking spot in direction of key press.(example: right arrow down. Is it clear?) If clear it will proceed to call another class which handles moving pacman until there is a wall or enemy.

Community
  • 1
  • 1
ComputerLocus
  • 3,448
  • 10
  • 47
  • 96

2 Answers2

2

The human eye can see about one frame every 100ms, with some people being able to see faster than that. You may wish to increase the frame rate to at least 20FPS (which is equivalent to one frame every 50ms). If you wish to keep Pac-Man moving at the same speed, you can half the distance and double the speed (make him move 10 or 11 pixels every 50ms).

See The Wikipedia article on framerate for more some basic background information, and to see what others are using successfully.

Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
  • Yes it would be nice to do this, but I don't know how to...while using an array like this. – ComputerLocus Nov 27 '11 at 03:48
  • This should be completely unrelated to the way the maze is stored, or the way that movement is executed. Rather, this pertains to the code that is updating the user interface. – Jon Newmuis Nov 27 '11 at 03:53
  • Even "average" humans will notice significant changes at 10fps (unless this only move a *very small* amount)! I would target ~30fps, and for a game like Pacman it is well within reason! Since timer resolution *and* CPU/JVM execution speeds vary the *time delta* must be taken into account when performing the animation -- or what it okay on one will be too slow or too fast on another. –  Nov 27 '11 at 04:08
1

You need to create two loops. A game loop, which works out the logic of the game, and runs every 100ms, and an animation loop, which runs more frequently, maybe every 20ms and calculates where all the sprites should be.

// in pseudo code
sleep 20 ms
if i++ % 5 == 0
    do game loop
do animation loop with i
// passing i to animation loop will allow you to easily calculate how far sprite is along path to destination
Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • Okay I will try this. But how would I do the movement of the pacman? Because it's multiplying the array right now so that would just speed up the movement. – ComputerLocus Nov 27 '11 at 04:04
  • maybe something like: `(array_from - array_to) / 5 * i` – Billy Moon Nov 27 '11 at 04:13
  • I'm only in first semester of of java in high school so I don't understand that. – ComputerLocus Nov 27 '11 at 04:19
  • ok, basically what I mean is get the total distance of the move, which you can calculate by subtracting the start point from the end point and getting the absolute value (making sure it is a positive value). When you have that distance (the distance you want to move) you can break it into several steps by dividing by however many steps you want - here I used 5. Then in your animation loop, you know the value of i (which step you are on), so you multiply that to get the final value of how far you should be moved at any point in time. This value can be added to the start value to get the co-ord. – Billy Moon Nov 27 '11 at 10:40