In this Pygame article we want to learn about How to Control Pygame Frame Rate with Time Clock, so first of all let’s talk about Pygame, Pygame is popular Python library for creating 2D games and multimedia applications. when you are building games, than there will be a key challenge on your way, and that is controlling the frame rate, if you have too loo frame rate, than your game maybe sluggish, if you have too high frame rate, than the game may consume too much CPU resources or lead to screen tearing. luckily Pygame provides builtin Time Clock object that can help you control the frame rate of your game
For using Pygame Time Clock, first you need to create a Clock object using Pygame.time.Clock() function. Clock object provides a tick() method that you can use to control the frame rate of your game, in this example we have imported Pygame library, after that we have initialized it and created a Clock object.
1 2 3 4 |
import pygame pygame.init() clock = pygame.time.Clock() |
After that you have created Time Clock object, you can use its tick() method to control the frame rate of your game. tick() method takes an optional argument that specifies maximum frame rate you want to achieve in frames per second or we can say FPS.
1 2 |
FPS = 60 clock.tick(FPS) |
For using Pygame Time Clock in a game loop, you can call tick() method at the end of each iteration of the loop to control the frame rate of your game. for example this is a simple game loop that uses Pygame Time Clock to control the frame rate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
while True: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Update game state # Draw graphics # Control frame rate FPS = 60 clock.tick(FPS) |
This is a complete code for simple Ball Game, that we are controlling the frame rate in the game, so first this example code create the window, after that it generates a list of Ball objects and runs the game loop that updates and draws the ball on the screen.
in here Pygame Time Clock is used to control the frame of the game, and it runs at consistent speed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import pygame import random # Define constants SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 FPS = 60 BALL_SIZE = 20 BALL_SPEED = 5 # Initialize Pygame pygame.init() # Create the game window screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Pygame Frame Rate Control") # Create the Pygame Time Clock clock = pygame.time.Clock() # Define colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # Define BallGame class class BallGame: def __init__(self, x, y): self.x = x self.y = y self.color = random.choice([RED, GREEN, BLUE]) self.speed_x = BALL_SPEED self.speed_y = BALL_SPEED def update(self): # Update position of the ball self.x += self.speed_x self.y += self.speed_y # Check for collisions with the walls if self.x < BALL_SIZE or self.x > SCREEN_WIDTH - BALL_SIZE: self.speed_x *= -1 if self.y < BALL_SIZE or self.y > SCREEN_HEIGHT - BALL_SIZE: self.speed_y *= -1 def draw(self): # Draw ball on the screen pygame.draw.circle(screen, self.color, (self.x, self.y), BALL_SIZE) # Create list of Ball objects balls = [BallGame(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2) for _ in range(10)] # Game loop while True: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() # Update game state for ball in balls: ball.update() # Draw graphics screen.fill(WHITE) for ball in balls: ball.draw() # Control frame rate clock.tick(FPS) # Update the display pygame.display.flip() |
Run the complete code and you will see a bouncing ball in the window the nice frame rate and speed, if you change the frame rate than the speed will also change.