Python Programming 3

on November 19 | in Technology | by | with No Comments

A Simple “Pong” Game

Python is a very powerful programming languauge, made even more powerful in terms of graphics by the Pygame library. If you want to learn the basics of pygame, click here. This also includes a link to download Pygame, and a link to Python basics.

Now in Python Programming 2, you where shown how to make a program to print the Id numbers of the keys pressed. Now that you know how to make windows pop up, close, and check keypresses, and print stuff, maybe you can make a simple game (It will require Python 2, not Python 3. It is useful to have them both).

To start with, create this program and save it as (note, this starts off as a large jump above the last example in Python Programming 2):

import pygame, sys; from pygame.locals import * # a “;” mark means: use the rest of the line as if it was a seperate line.


WINDOWWIDTH = pygame.display.Info().current_w
WINDOWHEIGHT = pygame.display.Info().current_h
print “loading screen…”
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)))#make a pygame window as big as the screen.
print “loading graphics…”

normalspeed = 512

ballrect = Rect(width/2, height/2,  16, 16)#all graphics need rects to draw themselves on.
playerrect = Rect(width-20, 0,20,120)

computerrect = Rect(0,0,20,120)
speed = [normalspeed,normalspeed]
clock = pygame.time.Clock()

mainloop = True
~~~~seconds = clock.tick(30) / 1000.0 #indents are shown as ~ marks in the tutorial

~~~~for event in pygame.event.get():
~~~~~~~~if event.type == pygame.QUIT:
~~~~~~~~elif event.type == pygame.KEYDOWN:
~~~~~~~~~~~~if event.key == K_ESCAPE:
~~~~~~~~~~~~elif event.key == K_UP:
~~~~~~~~~~~~~~~~playerspeed = -normalspeed
~~~~~~~~~~~~elif event.key == K_DOWN:
~~~~~~~~~~~~~~~~playerspeed = normalspeed

This is a lot of code, and for some it might seem a little bit daunting. But it is worth it. All the event handling and all the variables are set up. Now it is time to draw to the screen with these bits of code. When the text is light grey it means to type after that place in your code.

 ~~~~~~~~~~~~elif event.key == K_UP:
~~~~~~~~~~~~~~~~playerspeed = -normalspeed
~~~~~~~~~~~~elif event.key == K_DOWN:
~~~~~~~~~~~~~~~~playerspeed = normalspeed
~~~~ballrect.x  += speed[0] * seconds
~~~~ballrect.y += speed[1] * seconds
~~~~if ballrect.left < 0 or ballrect.right > width:
~~~~~~~~ballrect.x = width/2
~~~~if < 0 or ballrect.bottom > height:
~~~~~~~~speed[1] = -speed[1]
~~~~computerrect.y = round(ballrect.y)
~~~~playerrect.y += playerspeed * seconds
~~~~if < 0: = 0
~~~~if playerrect.bottom > height:
~~~~~~~~playerrect.bottom = height
~~~~if computerrect.colliderect(ballrect):
~~~~~~~~speed[0] = -normalspeed
~~~~if playerrect.colliderect(ballrect):
~~~~~~~~speed[0] = normalspeed, (255,255,255), int(round(ballrect.x + 8)), int(round(ballrect.y + 8)),10)
~~~~pygame.draw.rect(screen, (255, 255,255), computerrect)

~~~~pygame.draw.rect(screen, (255, 255,255), playerrect)


Now remember: Don’t type the ~ marks and if you get errors check carefuly against this code.

Pin It

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

« »

Scroll to top