📅  最后修改于: 2023-12-03 15:18:44.980000             🧑  作者: Mango
Pygame is a set of Python modules used for writing games and multimedia applications. It is built on top of the SDL (Simple DirectMedia Layer) library and provides developers with easy-to-use API for handling graphics, sound, and user input.
Installing Pygame is easy with pip:
pip install pygame
Here's an example of a simple game using Pygame:
import pygame
# Initialize Pygame
pygame.init()
# Set up the game window
window_size = (800, 600)
screen = pygame.display.set_mode(window_size)
pygame.display.set_caption("My Game")
# Run the game loop
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Clear the screen
screen.fill((255, 255, 255))
# Draw objects
pygame.draw.rect(screen, (255, 0, 0), [400, 300, 50, 50])
# Update the screen
pygame.display.update()
This will create a game window and draw a red rectangle in the center. The game loop handles user events and updates the screen.
Pygame provides many useful features for building games: