Starting Small - Python and Pygame
Page 4 of 4
<1 | 2 | 3 | 4>
Single-page view
Starting Small - Python and Pygame

The movement for the ball is slightly more complicated then it is for a paddle because you have more things to do. If we are at either horizontal edge of the window we will have to reverse the hspeed. With vertical edges we won't reverse the direction, but rather move the ball to the middle again.

        if hspeed < 0:
            if (rect_ball.left + hspeed > 0):
                rect_ball.left += hspeed
            else:
                hspeed = -hspeed
        else:
            if (rect_ball.left + hspeed < 375):
                rect_ball.left += hspeed
            else:
                hspeed = -hspeed
        #vertical movement for ball
        if vspeed < 0:
            if (rect_ball.top + vspeed > 0):
                rect_ball.top += vspeed
            else:
                rect_ball.left = 200 - 12.5
                rect_ball.top = 200 - 12.5
        else:
            if (rect_ball.top + vspeed < 375):
                rect_ball.top += vspeed
            else:
                rect_ball.left = 200 - 12.5
                rect_ball.top = 200 - 12.5

The biggest drawback you will have with using Pygame when you start out will be its lack of built in collision detection. I will explain very briefly how the collision detection works and give you the code for this game. Be sure to read our series on collision detection for games for an expansion of this concept.

When we check if the ball collides with a paddle we check two things: is the top left corner of the paddle inside the ball, or is the top left corner of the ball inside the paddle. If you are feeling brave you can write yourself a function that takes your 2 rectangles and returns whether or not they are colliding or not. After we have found a collision, we'll check if the ball is heading towards the paddle. If the "vspeed" is negative, the ball is moving up, so if the ball's top property is above the halfway line and it has collided, I need to make the value of "vspeed" its opposite. This ensures that when the ball hits the paddle it will change direction and not get stuck on the paddle, which will happen if its "vspeed" changes constantly every frame.

The following code handles the collision with paddles and doesn't get put inside the slower if-statement, but goes into the main game loop

    #ball collision with paddle1
    coll = False
    if ((rect_ball.left >= rect_paddle1.left) and \
(rect_ball.left <= rect_paddle1.left + rect_paddle1.width) and \
(rect_ball.top >= rect_paddle1.top) and \
(rect_ball.top <= rect_paddle1.top + rect_paddle1.height)):
        coll = True
    elif ((rect_paddle1.left >= rect_ball.left) and \
(rect_paddle1.left <= rect_ball.left + rect_ball.width) and \
(rect_paddle1.top >= rect_ball.top) and \
(rect_paddle1.top <= rect_ball.top + rect_ball.height)):
        coll = True
    #ball collision with paddle2
    if ((rect_ball.left >= rect_paddle2.left) and \
(rect_ball.left <= rect_paddle2.left + rect_paddle2.width) and \
(rect_ball.top + rect_ball.height >= rect_paddle2.top) and \
(rect_ball.top + rect_ball.height <= rect_paddle2.top + rect_paddle2.height)):
        coll = True
    elif ((rect_paddle2.left >= rect_ball.left) and \
(rect_paddle2.left <= rect_ball.left + rect_ball.width) and \
(rect_paddle2.top >= rect_ball.top) and \
(rect_paddle2.top <= rect_ball.top + rect_ball.height)):
        coll = True
    
    if coll:
        if ((rect_ball.top < 200) and (vspeed < 0)):
            vspeed = -vspeed
        elif ((rect_ball.top > 200) and (vspeed > 0)):
            vspeed = -vspeed

If all has gone well and you haven't run into any problems, you have now completed Pong in Python using Pygame.

Pygame Pong

Things to try yourself:

  • Try loading a sound or two and playing them (for instance when the ball hits a paddle or wall)
    	sound_[name] = pygame.mixer.sound("[file name]")
    	sound_[name].play()
    	
  • Try changing the caption of the window
    	pygame.display.set_caption("Game.Dev")
    
  • Try make the one paddle be controlled by the computer rather than another human player

    Hint:

    To do this you need to use if statements that check if the balls left property is to the left or right of the middle of the one paddle, and then move towards it if the balls vspeed makes it head towards that paddle. This should make it seem like you're playing against a human player.

You can get the full source for the game in this article from here if you have problems.



Words from the readers
No comments posted for this article yet. Have something to say? Make yourself heard below.
Have your say: