First Glance at Pygame¶
Class Overview¶
Lesson 3: Basic Game Mechanics
Objective: Learn about game loops and event handling.
Activities:
Introduce concepts of game loops and user input.
Code a simple game loop in Pygame or Scratch.
Interdisciplinary Links: Mathematics (logic), Computer Science (programming basics).
第 3 课:基本游戏机制
目标:了解游 戏循环和事件处理。
活动:介绍游戏循环和用户输入的概念。
在 Pygame 或 Scratch 中编写一个简单的游戏循环。
跨学科链接:数学(逻辑)、计算机科学(编程基础)。
Create a Pygame window¶
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.
A basic running cycle¶
To achieve a nearly smooth running animation for a character, you typically want to use at least 8 to 12 frames for a basic running cycle. Here’s a breakdown:
8 frames can provide a simple running cycle, showing the character in different positions as they run.
12 frames or more will give a more fluid and detailed animation, capturing more nuances in the running motion.
For even smoother animations, you can use 15 to 24 frames, but keep in mind that this requires more artwork and can increase file size. The key is to balance the number of frames with performance and storage considerations in your game.