Skip to content
Snippets Groups Projects
Commit 004066c6 authored by @dion-tsang's avatar @dion-tsang
Browse files

update first glance at pygame

parent 644a358a
No related branches found
No related tags found
No related merge requests found
Pipeline #493680 passed
# First Glance at Pygame
## Create a Pygame window
```python
import pygame
# Initialize pygame
pygame.init()
# Constants
WIDTH,HEIGHT=800,600
SAND = (255,255,255)
# Game setup
# window size
screen = pygame.display.set_mode((WIDTH,HEIGHT))
# window caption
pygame.display.set_caption("Chrome Dino Game")
# Create a Frame Rate object
clock = pygame.time.Clock()
# Game loop
running = True
while running:
screen.fill(SAND)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
clock.tick(30)
pygame.quit()
```
## Annotation:
Here's what each part does:
`clock = pygame.time.Clock()`: This creates a clock object to help control the game's frame rate.
`pygame.display.flip()`: This updates the full display surface to the screen, rendering all the changes made since the last flip.
`clock.tick(30)`: This limits the game to run at a maximum of 30 frames per second, ensuring consistent gameplay speed.
### what is a proper frame rate for a smooth animation ?
A proper frame rate for smooth animation typically ranges from 30 to 60 **frames per second (FPS)**.
**30 FPS** is generally acceptable for many games and provides a decent experience.
**60 FPS** offers smoother motion and is preferred for fast-paced games, making animations look more fluid.
Some high-performance games even target higher frame rates, like 120 FPS or more, but this often requires more powerful hardware. For most projects, aiming for 30 or 60 FPS is a good standard!
### Others
**Frame Rate**: The frame rate is the number of frames (images) displayed per second in a game. It affects how smooth the animation appears. A higher frame rate results in smoother motion, while a lower frame rate can make the game feel choppy.
**Setting the Frame Rate**: We set the frame rate to ensure consistent performance across different computers. Limiting it helps prevent the game from running too fast or too slow, creating a better user experience.
**Using flip()**: The pygame.display.flip() function updates the display, making all the drawn elements visible at once. Without it, changes to the graphics might not appear on the screen until the next update, which can lead to flickering or incomplete rendering.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment