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)

Drawing with Pygame (Simple)


Before we start creating cool games, we need an introduction to the drawing functionality of Pygame. As we noticed in the previous recipe, in Pygame we draw on the Surface objects. There is a myriad of drawing options—different colors, rectangles, polygons, lines, circles, ellipses, animation, and different fonts.

How to do it...

The following steps will help you diverge into the different drawing options you can use with Pygame:

  1. Imports: We will need the NumPy library to randomly generate RGB values for the colors, so we will add an extra import for that:

    import numpy 
  2. Initializing colors: Generate four tuples containing three RGB values each with NumPy:

    colors = numpy.random.randint(0, 255, size=(4, 3))

    Then define the white color as a variable:

    WHITE = (255, 255, 255)
  3. Set the background color: We can make the whole screen white with the following code:

    screen.fill(WHITE)
  4. Drawing a circle: Draw a circle in the center with the window using the first color we generated:

    pygame.draw.circle(screen, colors[0], (200, 200), 25, 0)
  5. Drawing a line: To draw a line we need a start point and an end point. We will use the second random color and give the line a thickness of 3:

    pygame.draw.line(screen, colors[1], (0, 0), (200, 200), 3)
  6. Drawing a rectangle: When drawing a rectangle, we are required to specify a color, the coordinates of the upper-left corner of the rectangle, and its dimensions:

    pygame.draw.rect(screen, colors[2], (200, 0, 100, 100))
  7. Drawing an ellipse: You might be surprised to discover that drawing an ellipse requires similar parameters as for rectangles. The parameters actually describe an imaginary rectangle that can be drawn around the ellipse:

    pygame.draw.ellipse(screen, colors[3], (100, 300, 100, 50), 2)

    The resulting window with a circle, line, rectangle, and ellipse using random colors:

    The code for the drawing demo is as follows:

    import pygame, sys
    from pygame.locals import *
    import numpy 
    
    pygame.init()
    screen = pygame.display.set_mode((400, 400))
    
    pygame.display.set_caption('Drawing with Pygame')
    colors = numpy.random.randint(0, 255, size=(4, 3))
    
    WHITE = (255, 255, 255)
    
    #Make screen white
    screen.fill(WHITE)
    
    #Circle in the center of the window
    pygame.draw.circle(screen, colors[0], (200, 200), 25, 0)
    
    # Half diagonal from the upper-left corner to the center
    pygame.draw.line(screen, colors[1], (0, 0), (200, 200), 3)
    
    pygame.draw.rect(screen, colors[2], (200, 0, 100, 100))
    
    pygame.draw.ellipse(screen, colors[3], (100, 300, 100, 50), 2)
    
    while True: 
       for event in pygame.event.get():
          if event.type == QUIT:
             pygame.quit()
             sys.exit()
    
       pygame.display.update()