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:

    How to do it...

    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()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset