Book Image

Instant Pygame for Python Game Development How-to

By : Ivan Idris
Book Image

Instant Pygame for Python Game Development How-to

By: Ivan Idris

Overview of this book

<p>Pygame is a library created to make multimedia software documenting easy to design. It adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the Python language. Pygame comes with functions and tools that will help you create a great user experience."Instant Pygame for Python Game Development How-to" is written in a concise and result-oriented format. Leading you through practical recipes, you'll find that this essential reference guide helps you to create visually appealing and thrilling games with a few clicks.This book starts with the basic instructions to install Pygame on different servers. It then goes into creating a sample game and explaining the features of drawing, animating, using fonts and Matplotlib with Pygame. The book then takes you through recipes to get access to some great sound and graphic effects. Giving you the steps to allow you to configure these games on Android and other networks, it ends with a walkthrough of the features of Sprites, OpenGL, and Simulation.</p>
Table of Contents (7 chapters)

Puzzle game with Pygame (Advanced)


We will pick up where we left in the networking example. This time we will create a puzzle game that lets us guess a word. This is just a prototype mind you. It still needs a lot of polishing.

How to do it...

The following steps will help you to create the intended puzzle game:

  1. Server changes: The changes in the server are pretty trivial. We just check whether we guessed the correct word:

    from twisted.internet import reactor, protocol
    
    class Server(protocol.Protocol):
        def dataReceived(self, msg):
             resp = '*' * 20
             print msg
    
             if msg == 'secret':
                resp = msg
    
             self.transport.write(resp)
    
    
    
    def main():
        factory = protocol.ServerFactory()
        factory.protocol = Server
        reactor.listenTCP(8888,factory)
        reactor.run()
    
    if __name__ == '__main__':
        main()
  2. Client changes: The most important changes are the handling of key presses in an input box and handling of the response from the server. The input box lets us type text, edit it with the Backspace key, and submit with the Enter key. A label above the textbox displays the number of attempts and the game status. We use a Twisted looping callback to update the GUI every 30 milliseconds:

    from twisted.internet import reactor, protocol
    from pygame.locals import *
    import pygame
    from twisted.internet.task import LoopingCall
    
    class Client(protocol.Protocol):
        def __init__(self):
           self.STARS = '*' * 20
           self.msg = self.STARS
           self.font = pygame.font.Font(None, 22)
           self.screen = pygame.display.get_surface()
           self.label = 'Guess the word:'
           self.attempts = 0
    
    
        def sendMessage(self, msg):
            self.transport.write(msg)
    
        def dataReceived(self, msg):
           self.msg = msg 
    
           if self.msg != self.STARS:
             self.label = 'YOU WIN!!!!'
    
           self.update_prompt()
    
        def update_prompt(self):
            self.screen.fill((255, 255, 255))
            BG = (0, 255, 0)
            FG = (0, 0, 0)
    
            pygame.draw.rect(self.screen, BG, (100, 200, 200, 20))
    
            self.screen.blit(self.font.render(self.msg, 1, FG), (100, 200))
            self.screen.blit(self.font.render("%d %s" % (self.attempts, self.label), 1, FG), 
                  (140, 180))
            pygame.display.flip()
        
    def handle_events(p):
       while True:
          for event in pygame.event.get():
             if event.type == QUIT:
                reactor.stop()
                return
             elif event.type == KEYDOWN:
                key = event.key
    
                if p.msg == '*' * 20:
                   p.msg = ''
    
                if key == K_BACKSPACE:
                   p.msg = p.msg[0:-1]
                   p.update_prompt()
                elif key == K_RETURN:
                   p.attempts += 1
                   p.sendMessage(p.msg)
                   return
                elif ord('a') <= key <= ord('z'):
                   p.msg += chr(key)
                   p.update_prompt()
    
    def send(p):
       p.update_prompt()
       tick = LoopingCall(handle_events, p)
       tick.start(.03)
    
    
    def main():
        pygame.init()
        screen = pygame.display.set_mode((400, 400))
        pygame.display.set_caption('Puzzle Demo')
    
        c = protocol.ClientCreator(reactor, Client)
        c.connectTCP("localhost", 8888).addCallback(send)
        reactor.run()
    
    if __name__ == '__main__':
        main()

    The following screenshot was taken after guessing the word:

How it works...

Although this seems to be a pretty extensive recipe, only a few lines of the code might require some explanation:

Function

Description

LoopingCall(handle_events, p)

This creates a looping callback. A callback function that is called periodically.

tick.start(.03)

This starts the looping callback with a period of 30 milliseconds.