Home arrow Python arrow Page 5 - A PyGame Working Example, continued

The Game in Action - Python

In this fourth article in our series covering the creation of a video game using Python and PyGame, we will code the internals of our game.

TABLE OF CONTENTS:
  1. A PyGame Working Example, continued
  2. Setting Things Up
  3. Cleaning Up
  4. Constructing the Main Loop
  5. The Game in Action
By: Peyton McCullough
Rating: starstarstarstarstar / 9
February 14, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

All that's left now is creating a Python script that will make use of our level and game module. To run our level, simply create a file named playAsteroid.py:

import pydodge

pydodge.loadLevel('asteroid')
pydodge.setup()
pydodge.loadBackground()
pydodge.loadSprites()
pydodge.run()

Of course, we can always customize our game a bit more. Let's say that we want to use a title screen rather than forcing the user to jump right into the level. Also, let's display either “Level Complete” or “Game Over”:

import pydodge
import pygame

pydodge.loadLevel('asteroid')
pydodge.setup()
pydodge.loadBackground()

# Add a title
font1 = pygame.font.Font(None, 25)
text1 = font1.render('PyDodge Asteroid', True, (255, 255, 255))
textRect1 = text1.get_rect()
textRect1.centerx = pydodge.screen.get_rect().centerx
textRect1.y = 100
pydodge.screen.blit(text1, textRect1)

# Add "Press <Enter> To Play"
font2 = pygame.font.Font(None, 17)
text2 = font2.render('Press <Enter> To Play', True, (255, 255,
255))
textRect2 = text2.get_rect()
textRect2.centerx = pydodge.screen.get_rect().centerx
textRect2.y = 150
pydodge.screen.blit(text2, textRect2)

# Update the screen
pygame.display.update()

# Wait for enter to be pressed
# The user can also quit
waiting = True
while waiting:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         sys.exit()
      elif event.type == pygame.KEYDOWN:
         if event.key == pygame.K_RETURN:
            waiting = False
            break

pydodge.loadBackground()
pydodge.loadSprites()

# The user has won the game
if pydodge.run(100, 300):
   text3 = font1.render('Level Complete', True, (255, 255, 255))
   textRect3 = text3.get_rect()
   textRect3.centerx = pydodge.screen.get_rect().centerx
   textRect3.y = 150
   pydodge.screen.blit(text3, textRect3)

# The user has lost the game
else:
   text3 = font1.render('Game Over', True, (255, 255, 255))
   textRect3 = text3.get_rect()
   textRect3.centerx = pydodge.screen.get_rect().centerx
   textRect3.y = 150
   pydodge.screen.blit(text3, textRect3)

pygame.display.update()

# Wait for the user to quit
while True:
   for event in pygame.event.get():
      if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN):
         sys.exit()

Conclusion

As you can see, creating a functioning game with PyGame is rather easy. Our game module weighs in at around five kilobytes. Using the module, you can also customize games, loading whatever levels you would like and displaying extra messages and what-not.

From here, try customizing your game even further. You can try adding a menu where the user can select a difficulty level. You can also link multiple levels together and randomize the layout lists in levels. It's up to your imagination.

Of course, there's a lot more to PyGame than a simple space game like this, so feel free to explore the library and examine one of the many example games available on the PyGame website. Good luck!



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: