Libraries - pygame Flashcards

1
Q

1) On a pygame surface, where is x,y co-ordinate 0,0?

2) In what direction does the x & y number increase

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

pygame.time.Clock()
clock.tick(60)

The above 2 methods are included in the basic template.

What do they do

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is it important to control your game’s frame rate (i.e., FPS)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

1) Calculate how much time has passed since the last frame & store the value in variable “dt”.

2) What does “dt” stand for

pygame

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

dt = clock.tick(60) / 1000

Should the above be used inside or outside of your game loop, and why

pygame

A

Inside the game loop, because you need it to constantly update so it can modify your speed based on the current FPS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

1) What’s the Pygame method to draw a rectangle
2) Explain the parameters this method takes

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The Pygame method to fill a Surface with a color

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The Pygame method to update the entire screen

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which Pygame method returns a list of recent input events, such as,

pygame.QUIT
pygame.KEYDOWN
pygame.MOUSEBUTTONDOWN

A

pygame.event.get()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

pygame.event.get()

Generally, for what purposes would you use the statement above

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain what “Surface” means in Pygame

A

In Pygame, a “surface” is anything that holds pixel data. So “surface” could be a loaded image (PNG, JPG etc.), another Pygame surface, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

source = pygame.image.load(path)

17
Q

The Pygame method to gets a Rect from a Surface

A

image.get_rect()

18
Q

The Pygame method to load a font

A

pygame.font.SysFont(name, size)

19
Q

The Pygame method to create text Surface

A

font.render(text, antialias, color)

20
Q

The Pygame method to return milliseconds since game started

A

pygame.time.get_ticks()

21
Q

The Pygame method to return (x, y) of mouse pointer

A

pygame.mouse.get_pos()

22
Q

The Pygame method to detect key pressed

A

event.type == pygame.KEYDOWN

23
Q

The Pygame method to detect which key

A

event.key == pygame.K_LEFT

24
Q

The Pygame method to return mouse button state (LMB, RMB, etc.)

A

pygame.mouse.get_pressed()

25
The Pygame method to draws a circle
pygame.draw.circle(surface, color, center, radius)
26
The Pygame method to draw a line
pygame.draw.line(surface, color, start, end)
27
The Pygame method to optimize image for display (no alpha)
image.convert()
28
The Pygame method to optimize image with transparency
image.convert_alpha()
29
The Pygame method to returns a moved Rect (does not modify in-place)
rect.move(x, y)
30
The Pygame method to checks collision with another Rect
rect.colliderect(other)
31
The Pygame method to load a sound effect
pygame.mixer.Sound(path)
32
The Pygame method to play a sound
sound.play()
33
The Pygame method to load background music
pygame.mixer.music.load(path)
34
The Pygame method to plays music (optionally loops)
pygame.mixer.music.play(loops=0)