Pygame
:material-circle-edit-outline: 约 462 个字 :fontawesome-solid-code: 39 行代码 :material-image-multiple-outline: 2 张图片 :material-clock-time-two-outline: 预计阅读时间 5 分钟
Fonts and text renders¶
- Pygame renders text to a surface with the font module
- Font module can locate and load a font as a Font object
How to render text:the font.render() method returns a surface with the text rendered on itPython
a_font.size()
: method returns the size of the text , a turple of width and heighta_font.get_height()
: method returns the height of the fonta_font.get_linesize()
: method returns the height of a line of texta_font.metrics
:returns a list with one turple per character in ttext
Sprites(Object-Oriented Models)¶
Python | |
---|---|
Python | |
---|---|
sprite_group.draw(screen)
: method draws all the sprites in the group to the screen
sprite operations with group:
Group.clear(surface, background)
: clears the sprites from the screenGroup.draw(surface)
: draws the sprites to the screen
LayerUpdates class:
Python | |
---|---|
DirtySprite class:
Python | |
---|---|
DirtySprite.dirty
: attribute to indicate whether the sprite needs to be updatedDirtySprite.dirty = 1
: set the sprite to be updated
DirtySprite.dirty = 0
: set the sprite not to be updated
LayeredDirty class:Python | |
---|---|
Animation¶
Animation: a series of images displayed in rapid succession to create the illusion of movement
how to make shure that the animation is smooth?
- slow the game loop down to a consistent number
- measure the time between frames and adjust the speed of the animation accordingly
Approach 1: Slowerloop
a tick() method to control the speed of the game loop
delta=clock.tick()
it will return number of milliseconds since last time
Randomness¶
Randomness: a key element in games
random.randint(a, b)
: returns a random integer between a and b
random.choice(sequence)
: returns a random element from a sequence
random.shuffle(sequence)
: shuffles the sequence in place
random.seed()
: initializes the random number generator
LCG: Linear Congruential Generator: a simple algorithm to generate random numbers
\(X_{n+1}=(aX_n+c)mod m\)
\(ex: X_{n+1}=(1664525X_n+1013904223)mod 2^{32}\)
LCG has a specific period, and the period is the maximum number of random numbers that can be generated before the sequence repeats
PRNG: Pseudo-Random Number Generator¶
PRNG: a deterministic algorithm that generates a sequence of numbers that approximates the properties of random numbers
a good PRNG should have a long period, be unpredictable, and have a uniform distribution
Random distributions¶
Uniform distribution: each number has an equal chance of being selected
Normal distribution: a bell-shaped curve with the mean at the center
Asymmetric distribution: a distribution that is not symmetrical: Dropping the lowest roll, reroll the lowest
Critical Hit: a game mechanic that allows a player to deal extra damage
How to construct a critical hit?
Python | |
---|---|
use die-size to control the scale