Python · 5.2
Making Games with pygame
Name
Class
Date
Mark / 27
Section A
Circle the letter of the correct answer. One mark each.
- 1What are the three stages of a game loop, in order?1
- AHandle events, update, draw
- BHandle events, draw, update
- CDraw, update, handle events
- DUpdate, draw, handle events
- 2Why must
screen.fill()be called each frame?1- AIt reads the keyboard
- BIt makes the game run faster
- COtherwise the previous frame remains and moving objects leave trails
- DIt creates the window
- 3In pygame, moving an object down the screen means:1
- ADecreasing its y coordinate
- BIncreasing its y coordinate
- CIncreasing its x coordinate
- DDecreasing its x coordinate
- 4Which should be used for continuous player movement?1
- Acolliderect
- Bpygame.key.get_pressed()
- CThe KEYUP event
- DThe KEYDOWN event
- 5What does
player.colliderect(enemy)return?1- AThe enemy's position
- BThe overlapping area
- CThe distance between them
- DTrue if the two rectangles overlap
- 6What does
clock.tick(60)do?1- ASets the score to 60
- BRuns the loop 60 times then stops
- CWaits 60 seconds
- DLimits the loop to 60 iterations per second
- 7What does
pygame.display.flip()do?1- AShows everything that has been drawn since the last frame
- BEnds the game
- CClears the screen
- DReverses the image
- 8Why should sprites that move off screen be removed with kill()?1
- AIt is required by pygame
- BIt saves disk space
- COtherwise they accumulate and gradually slow the game down
- DThey would reappear on the other side
- 9What does
all_sprites.update(keys)do?1- ACalls the update method on every sprite in the group
- BCreates new sprites
- CDraws every sprite
- DRemoves every sprite
- 10Which should be used for firing a single shot when space is pressed?1
- Acolliderect
- BThe KEYDOWN event
- Cclock.tick
- Dpygame.key.get_pressed()
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1DescribeDescribe the three stages of a game loop.3
- 2ExplainExplain why the screen must be cleared at the start of each frame in a pygame program.2
- 3ExplainExplain the difference between using pygame.key.get_pressed() and the KEYDOWN event, and state when each should be used.4
- 4WriteWrite pygame code for a sprite class representing a coin that falls down the screen and removes itself when it goes off the bottom.5
- 5ExplainExplain why clock.tick(60) is used in a pygame game loop and what would happen without it.3
Mark scheme
Section A
Section B
1 Describe the three stages of a game loop. 3
One mark for each of the following, to a maximum of 3.
- Handle events such as key presses and closing the window
- Update the game state, moving objects and checking collisions
- Draw everything to the screen and display the frame
Model answer. The first stage handles events, reading everything the player has done since the last frame, such as key presses, mouse clicks and the window being closed. The second stage updates the game state: objects are moved according to input and their own behaviour, collisions are checked, and things such as the score and the number of lives are adjusted. The third stage draws the frame, clearing the screen and then drawing every object in its new position before calling display.flip to make the completed frame visible. These three stages repeat, typically sixty times a second.
2 Explain why the screen must be cleared at the start of each frame in a pygame program. 2
One mark for each of the following, to a maximum of 2.
- Pygame does not clear the screen automatically between frames
- Without clearing, previous frames remain visible so moving objects leave trails
Model answer. Pygame does not clear the display between frames, so whatever was drawn last time is still there when the next frame is drawn on top of it. If the screen is not cleared with screen.fill at the start of the drawing stage, an object that has moved will be drawn in its new position while its old position is still visible, so it leaves a continuous trail across the screen rather than appearing to move.
3 Explain the difference between using pygame.key.get_pressed() and the KEYDOWN event, and state when each should be used. 4
One mark for each of the following, to a maximum of 4.
- get_pressed reports which keys are currently held down
- It is checked every frame so the action repeats while the key is held
- KEYDOWN fires once at the moment a key is pressed
- Use get_pressed for movement and KEYDOWN for one off actions such as firing or jumping
Model answer. The get_pressed function returns the current state of every key, so checking it inside the game loop tells you which keys are being held down at that moment, and any action taken repeats on every frame the key remains down. The KEYDOWN event, by contrast, appears in the event queue once at the instant a key is pressed and does not repeat while it is held. Movement should use get_pressed, so that holding the left arrow moves the player smoothly and continuously. One off actions such as firing a shot, jumping or selecting a menu item should use KEYDOWN, because using get_pressed for firing would produce sixty shots a second while the key was held.
4 Write pygame code for a sprite class representing a coin that falls down the screen and removes itself when it goes off the bottom. 5
One mark for each of the following, to a maximum of 5.
- Defines a class inheriting from pygame.sprite.Sprite
- Calls super().__init__() in the constructor
- Creates an image and a rect for the sprite
- Defines update to increase the y coordinate
- Removes the sprite with kill() when it passes the bottom of the screen
Model answer. class Coin(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((20, 20), pygame.SRCALPHA) pygame.draw.circle(self.image, (202, 171, 213), (10, 10), 10) self.rect = self.image.get_rect(topleft=(x, y)) self.speed = 4 def update(self): self.rect.y += self.speed if self.rect.top > HEIGHT: self.kill() The class inherits from pygame.sprite.Sprite so it can be added to a group, and super().__init__() must be called or the sprite machinery will not be set up. Every sprite needs an image and a rect, because groups use those to draw and position it. The update method increases y each frame, since y increases downwards in pygame, and calls kill once the top of the coin has passed the bottom of the screen, which removes it from every group it belongs to so it is no longer updated or drawn.
5 Explain why clock.tick(60) is used in a pygame game loop and what would happen without it. 3
One mark for each of the following, to a maximum of 3.
- It limits the loop to a maximum of 60 iterations per second
- This makes the game run at the same speed on different computers
- Without it the loop runs as fast as possible, so the game is far too fast on a powerful machine
Model answer. The tick method limits the game loop to a maximum number of iterations per second, in this case sixty, by pausing briefly at the end of each pass if the frame was completed early. This matters because the amount an object moves per frame is fixed in the code, so the speed things appear to travel depends entirely on how many frames happen each second. Without the cap, the loop would run as fast as the hardware allows, which might be several hundred frames a second on a modern machine, and the game would be completely unplayable while running at a totally different speed on a slower computer. Capping the frame rate makes the game behave identically regardless of the hardware it is running on.