Jump to content

  • Log In with Google      Sign In   
  • Create Account


Luctus

Member Since 03 Mar 2002
Offline Last Active Dec 11 2013 03:37 AM
-----

#4956705 What *is* game programming?

Posted by Luctus on 07 July 2012 - 12:43 PM

I would say that in a nutshell, game programming consists of doing simulations. If you can program a simulation of a ball bouncing, you can also program games.

A ball-bouncing simulation in it's most basic form consists first of all of a description of the current state of the system: The location and velocity of the ball and the position of any walls. Then at regular intervals, the state of the system is updated by moving the location of the ball a tiny bit according to its velocity and time elapsed since last update, taking any necessary collisions with walls into account by reversing the velocity of the ball along the normal of the wall. After that the walls and the ball at it's current location are drawn to the screen.

The repeated application of updating the state of the simulation and drawing will make the apperance of a ball bouncing around on the screen.

Now add in an additional step of sampling an input device and using that information for moving the walls in the simulation and you've essentially created a very basic pong game.

Any other game will at some abstract level also consist of those same concepts
  • A description of the state of the game
  • Sampling of input
  • Updating the state according to input and rules of the game
  • Rendering the state to screen and other output devices.
The only thing that differ pong from the latest multi-million dollar title is the scale and complexity of how those things are done and the specifics of what is stored in the state and what rules are being applied when updating.


#4431274 Pygame character jumping

Posted by Luctus on 01 April 2009 - 11:54 PM

This is commonly done by doing a simple physics simulation. Beginning by assuming that you have an object that represents your character, with x and y being fields containing it's horizontal respectively vertical position in the world. To implement this you will in addition to it's position also need to keep track it's velocity, which we'll call dx and dy.

Going by common sense, if something has a velocity, it's position will change over time. So, in your function that updates the state of your character, call it update, should contain (in addition to other changes of state, such as animations etc):


def update(self, deltatime):
# Other stuff, animation etc.
# ...

self.x += dx * deltatime
self.y += dy * deltatime




Now, to jump, you simply just need to set it to have an upwards velocity:


def jump(self):
# It should only be able to jump when standing on the ground. How you
# define the onTheGround() function depends on how you handle the
# collision detection.
if onTheGround(self):
self.dy += 20
# Or whatever appropriate value in position units per time unit
# would be. If x and y are measured in pixels and deltatime above
# in seconds (since last update), this would mean a velocity
# of 20 pixels per second upward.




Of course, there's an obvious problem with this since the character will never come down once it begun a jump, just keep jumping higher...we obviously need to also simulate gravity. Newton's law of gravitation says that an object under gravitational force has a constant downwards acceleration. So, we need to account for the downwards acceleration in the update function:


def update(self, deltatime):
# Other stuff, animation etc.
# ...

# It should only simulate falling if it's not standing on the ground
if not onTheGround(self):
# Acceleration is change of velocity over time, so we need to adjust the acceleration every time
# the character is updated.
self.dy -= 5 # ...or whatever value is appropriate for your world.

self.x += dx * deltatime
self.y += dy * deltatime




Finally, to keep everything consistent, your horizontal movement should be treated the same way. So when you want to move the character left and right, just set the horizontal speed accordingly:


def moveLeft(self):
self.dx = -20

def moveRight(self):
self.dx = 20

def stop(self):
self.dx = 0





PARTNERS