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
17SCREEN_WIDTH = 800
18SCREEN_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'
22class 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
30pygame.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
2import pygame
3
4# Import pygame.locals for easier access to key coordinates
5# Updated to conform to flake8 and black standards
6from 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
17SCREEN_WIDTH = 800
18SCREEN_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'
22class 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
30pygame.init()
31
32# Create the screen object
33# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
34screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
35
36# Instantiate player. Right now, this is just a rectangle.
37player = Player()
38
39# Variable to keep the main loop running
40running = True
41
42# Main loop
43while 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:
Sprite
class frompygame
documentation- Sprites article on Wikipedia
- Supercharge Your Classes With Python super()
For more information about the RGB color model, check out the following resources:
the1howie on Oct. 31, 2024
Hi Chris, why do we need to send parameters to super()? Would it not suffice to use the parameterless super()? Thanks.