Welcome to my Programming Blog...

I am currently working on a couple of projects: An original game called "Implosion" which I am porting from Flash to the iPad, and a remake of Q*bert in Python (pygame), as part of the class I am teaching. Please feel free to use the "Labels" (at right) to follow a specific project or theme. If you are one of my Python students, I recommend that you check out the Python thread.

Thursday, April 22, 2010

hoppin' around

I've got Q*bert moving around now, which was a fun little challenge. For a while, I was expecting to have to do some complicated collision detection- search through all the cubes, detect which one(s) Q*bert is touching on screen, and narrowing them down to see which ones might be involved in a collision, and which ones Q*bert is passing "in front of."

Then I realized a few things.

  1. I only need to worry about Q*bert when he is moving downwards on the screen - when his yVelocity is positive.
  2. If Q*bert "knows" which cube he is jumping onto, then I only need to manage collisions with that particular cube - in fact, all other cubes are problematic distractions.
  3. As far as the collision with that cube, all I need to do is figure out when Q*bert's feet have arrived at a certain height on the top face of the cube.
  4. ... and all this will work on most of the other critters on the screen.
To make Q*bert jump, then, I had to give him an initial x- and y- velocity (depending on which way he was moving); set a variable, IAmMoving, to True; and let him know which Cube he was trying to land on. It took a little bit of tinkering with the starting x- and y-velocities and the downwards acceleration to find values that looked right and got him to land on the right spot.

To make him stop, I checked periodically (while he was moving) to see whether he was moving down, and whether his feet were below the middle of the top face of the cube. If so, I set IAmMoving to False. (I also corrected his position to the center of that face, so there isn't any drift over time.)

So now Q*bert (or his rectangle, at least) is hopping around.

No comments:

Post a Comment