Libraries - pygame Flashcards
1) On a pygame surface, where is x,y co-ordinate 0,0?
2) In what direction does the x & y number increase
1) x,y co-ordinate 0,0 is at the top-left of the surface
2) The “x” numbers increase to the right, & the “y” numbers increase downwards
pygame.time.Clock()
clock.tick(60)
The above 2 methods are included in the basic template.
What do they do
pygame.time.Clock() = Creates the clock object that lets you use clock.tick() to control your game’s frame rate
clock.tick(60) = caps the frame rate to 60 FPS by telling the computer to wait just enough so that the game only runs 60 frames per second
Why is it important to control your game’s frame rate (i.e., FPS)
By default, your game will run as fast as your computer can go (e.g., 1,000+ FPS). This can cause the following;
1) Character speed will vary with FPS, hence could cause super fast and inconsistent movement speed
2) High CPU usage
1) Calculate how much time has passed since the last frame & store the value in variable “dt”.
2) What does “dt” stand for
pygame
1) dt = clock.tick(60) / 1000
2) “dt” = delta time (change in time)
clock.tick(60) delays the loop (if necessary) so that your game doesn’t run faster than 60FPS, & it returns the number of milliseconds that have passed since the last tick. The MINimum number will be around 16.7ms at 60FPS, but it could be higher if PFS drops below 60FPS (e.g., if your game slows to 50FPS, the time between frames will be 20ms)
Normally, if you move something like this:
player.x += 5 # Moves 5 pixels per frame
Why is it better to use this:
speed = 300 # pixels per second
player.x += speed * dt
pygame
player.x += 5
This causes movement speed (pixels per frame) to drop if FPS drops
speed = 300
player.x += speed * dt
This ensure movement speed (pixels per frame) remains the same regardless of FPS
“dt” is essentially a modifier applied to “speed” to control for varying FPS
dt = clock.tick(60) / 1000
Should the above be used inside or outside of your game loop, and why
pygame
Inside the game loop, because you need it to constantly update so it can modify your speed based on the current FPS
1) What’s the Pygame method to draw a rectangle
2) Explain the parameters this method takes
pygame.draw.rect(surface, color, rect, width=0)
surface = the variable name that the main game window was assigned to
colour = colour - e.g., red is (255, 0, 0)
rect = rectangle possition & size, e.g., (x, y, width, height) e.g., (100, 200, 50, 50)
width = thickness of shape’s edges (takes a single integer) - it defaults to to fill the shape if “0” or omitted
Note that “color” & “rect” usually takes a tuple
pygame.draw.rect(surface, color, rect, width=0)
Is it better to use the above draw method before or after modifying the player position (or modifying any other object position)
It’s better to use the draw method after modifying an object’s possition.
If you draw before updating the position, you’ll always show the previous frame’s position, which creates a visual lag or stutter.
1) Type the Pygame method that returns the current state of every key on the keyboard and store the returned value in the variable “keys”
2) In what form is the data returned
1) keys = pygame.key.get_pressed()
2) It returns a tuple with a fixed length of 323 index places i.e., (0, 0, 0 … 0). Each space represents a different key on the keyboard. If the numbe in any particular space is “1”, that key is being pressed - if “0”, it’s not. To access a particular index place, you use pygame’s “key constants”, e.g.,
keys[pygame.K_LEFT] # left arrow
or
keys[pygame.K_a] # the “a” key
or
keys[pygame.K_SPACE] # the spacebar
There is a long list of “key constants” you can look up
The Pygame method to fill a Surface with a color
surface.fill(color, default, default)
(or screen.fill(color, default, default) etc.)
E.g., surface.fill((30, 30, 30))
Note the double brackets “(30,30,30)” is all part of the 1st arguement
The Pygame method to update the entire screen
pygame.display.flip()
Explaination: you’re drawing stuff onto a canvas in memory (called the display surface) - but the player doesn’t see it yet - .flip() then updates the entire screen to display everything you’ve just drawn since the last frame.
Hence why .flip() goes somewhere near the end of your game loop.
Which Pygame method returns a list of recent input events, such as,
pygame.QUIT
pygame.KEYDOWN
pygame.MOUSEBUTTONDOWN
pygame.event.get()
pygame.event.get()
Generally, for what purposes would you use the statement above
Rather than detecting, for example, continuous movement, this is used to detect one-time events such as,
1) pygame.QUIT
2) pygame.KEYDOWN
3) pygame.KEYUP
4) pygame.MOUSEMOTION
5) pygame.MOUSEBUTTONDOWN
6) pygame.MOUSEBUTTONUP
You can also set custom user events
Pygame automatically keeps an event queue in the background. pygame.event.get() checks this event queue, returns a List of events that occured since last time you checked the event que, then clears the queue. As it returns a List, you will need to use a for loop to loop through the List to detect the particular event you are looking for, then type “if event.type == pygame.xxxxx:” to react to that event.
Explain what “Surface” means in Pygame
In Pygame, a “surface” is anything that holds pixel data. So “surface” could be a loaded image (PNG, JPG etc.), another Pygame surface, etc.
image = pygame.image.load(path)
The above loads an image & stores it in “image”
Use a Pygame method to draw “image” onto your screen’s surface
screen.blit(source, position)
Including “image”, it could look like,
screen.blit(image, (100, 100))
“Blit” is short for “block transfer”, a term from computer graphics
screen.blit(source, position)
surface.blit(source, position)
The above two methods can take an image as the “source” parameter
Use a Pygame method to load an image and store it in variable “source” so that it can be used in the above methods
source = pygame.image.load(path)
The Pygame method to gets a Rect from a Surface
image.get_rect()
The Pygame method to load a font
pygame.font.SysFont(name, size)
The Pygame method to create text Surface
font.render(text, antialias, color)
The Pygame method to return milliseconds since game started
pygame.time.get_ticks()
The Pygame method to return (x, y) of mouse pointer
pygame.mouse.get_pos()
The Pygame method to detect key pressed
event.type == pygame.KEYDOWN
The Pygame method to detect which key
event.key == pygame.K_LEFT
The Pygame method to return mouse button state (LMB, RMB, etc.)
pygame.mouse.get_pressed()