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

Now, for Pong, we need to move our paddles left and right. We'll do this by changing the left property of the rectangles we are using for them. For now, only make the increase or decrease of the left property by 1. Another thing to keep in mind is that we don't want the paddle to leave the left side of the screen, so we only move the paddle left if the left property is already larger than 0. The same applies to the right side of the screen, but we will need to check to make sure the left property is smaller that 300. This is because our sprite is 100 pixels long, so we can only move its left edge a maximum of 100 pixels away from the right side of the window.

The following is an example of how to do the checks and make the paddle move, you would just need to repeat it for each key that you wish to use.

	if keys[pygame.K_LEFT]:
		if (rect_paddle1.left > 0):
			rect_paddle1.left -= 1

If you cannot decide what keys to use for each paddle, make the one paddle use the left and right arrows, and the other use "a" and "d" (K_a and K_d). Finish the paddle movement code so that both paddles can move left and right independently.

Pong

You should now have a game where you have two paddles and a ball drawn on the screen. You should be able to move each paddle using a separate set of keys, and exit the game with the quit button with no problems.

Now we are going to make our ball move. To do this, we will be making two variables to control our balls movement, "hspeed" or horizontal speed, and "vspeed" or vertical. This way, all we have to do is add "hspeed" to the left property of the ball to move it from side to side, and when the ball hits the side of the window, all we have to do is invert the sign of "hspeed". The same is done for the "vspeed" with the ball's top property, but instead of changing the direction of the ball, we place it back in the centre of the window as a reset of sorts if it reaches the top or bottom of the screen.

First, let's declare hspeed and vspeed at Place 1. We give them the starting value of 0, and we keep looping till it is randomly assigned a non-zero integer between -1 and 1, making sure that we have a direction to move. Just below that we'll make a variable to slow down the updating of the ball's movement

	hspeed = 0
	vspeed = 0
	while hspeed == 0:
	    hspeed = random.randint(-1,1)
	    hspeed = hspeed
	while vspeed == 0:
	    vspeed = random.randint(-1,1)
	    vpseed = vspeed
slower = 15

We then move to Place 2 – which is where we will update the balls movement – and add in the following code:

	    slower -= 1
	    if slower == 0:
	        slower = 15
	        #ball movement updating code will go here

I've used 15 as the number of repeats we want between each time the ball moves, if you find that this is too slow for you feel free to decrease it.



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