Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Creating Sprites

In this lesson, you’ll create the player as a sprite. Here’s how you use Sprite objects with the current game to define the player. Insert this code after line 18:

16 # Define constants for the screen width and height
17 SCREEN_WIDTH = 800
18 SCREEN_HEIGHT = 600
19 
20 # Define a Player object by extending pygame.sprite.Sprite
21 # The surface drawn on the screen is now an attribute of 'player'
22 class Player(pygame.sprite.Sprite):
23     def __init__(self):
24         super(Player, self).__init__()
25         self.surf = pygame.Surface((75, 25))
26         self.surf.fill((255, 255, 255))
27         self.rect = self.surf.get_rect()
28 
29 # Initalize pygame
30 pygame.init()

You need to remove lines 59 to 72, which included creating the previous surf object and displaying it. Those lines will be replaced with displaying the new player sprite. You also will be changing the background screen to be black. Here are the all the changes you will make during this lesson:

 1 # Import the pygame module
 2 import pygame
 3 
 4 # Import pygame.locals for easier access to key coordinates
 5 # Updated to conform to flake8 and black standards
 6 from pygame.locals import (
 7     K_UP,
 8     K_DOWN,
 9     K_LEFT,
10     K_RIGHT,
11     K_ESCAPE,
12     KEYDOWN,
13     QUIT,
14 )
15 
16 # Define constants for the screen width and height
17 SCREEN_WIDTH = 800
18 SCREEN_HEIGHT = 600
19 
20 # Define a Player object by extending pygame.sprite.Sprite
21 # The surface drawn on the screen is now an attribute of 'player'
22 class Player(pygame.sprite.Sprite):
23     def __init__(self):
24         super(Player, self).__init__()
25         self.surf = pygame.Surface((75, 25))
26         self.surf.fill((255, 255, 255))
27         self.rect = self.surf.get_rect()
28 
29 # Initialize pygame
30 pygame.init()
31 
32 # Create the screen object
33 # The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35 
36 # Instantiate player. Right now, this is just a rectangle.
37 player = Player()
38 
39 # Variable to keep the main loop running
40 running = True
41 
42 # Main loop
43 while running:
44     # Look at every event in the queue
45     for event in pygame.event.get():
46         # Did the user hit a key?
47         if event.type == KEYDOWN:
48             # Was it the Escape key? If so, stop the loop.
49             if event.key == K_ESCAPE:
50                 running = False       
51         # Did the user click the window close button? If so, stop the loop.
52         elif event.type == QUIT:
53             running = False
54 
55     # Fill the screen with black
56     screen.fill((0, 0, 0))
57 
58     # Draw the player on the screen
59     screen.blit(player.surf, player.rect)
60 
61     # Update the display
62     pygame.display.flip()

For more information about the Sprite class from the pygame documentation, and additional information on sprites and using inheritance with super(), check out the following resources:

For more information about the RGB color model, check out the following resources:

Comments & Discussion

Become a Member to join the conversation.