SoFunction
Updated on 2024-10-30

Specific uses of the pygame timing module time

synopsis

We used it before when updating the graphics to regulate the response time of the dead loop. In fact, we don't need to import other packages, pygame provides the time module to regulate the frame rate of the game.

The main types of time are as follows

Methods and Classes
get_ticks Get the number of milliseconds since pygame was initialized
wait(milliseconds)
delay(milliseconds)
Time delay, the former is lighter, the latter is more accurate
set_timer Repeated creation of events in the event queue
Clock clock object

clock object

Clock is the clock class in the time module and encapsulates the following methods

methodologies
tick, tick_busy_loop Updated clocks, the former lighter, the latter more accurate
get_time, get_rawtime Number of milliseconds since the last tick
get_fps Calculating the clock frame rate

Here is a simple test of the clock class

import pygame as pg

c = ()
()    # 5
c.tick_busy_loop()  # 1
c.get_time()    # 1

parabolic motion

Next, use the time module to redo the parabolic motion with the following code

import pygame as pg

()

size = width, height = 640, 320
speed = [10, 0]

screen = .set_mode(size)

ball = ("intro_ball.gif")
rect = ball.get_rect()

th = 0
while True:
    if  in [ for e in ()]:
        ()
        break
    (20)
    rect = (speed)
    if >width:
        speed = [10, 0]
        rect = ball.get_rect()
    if >height:
        speed[1] = -speed[1]
    speed[1] += 1
    th += 5
    ("black")
    ((ball, th), rect)
    ()

The effect is as follows

to this article on the specific use of pygame timing module time article is introduced to this, more related pygame time content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!