📜  pygame - Python (1)

📅  最后修改于: 2023-12-03 15:18:44.980000             🧑  作者: Mango

Pygame - Python

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.

Installation

Installing Pygame is easy with pip:

pip install pygame
Creating a Simple Game

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.

Features

Pygame provides many useful features for building games:

  • Graphics rendering and manipulation
  • Sound playback and recording
  • User input handling
  • Collision detection
  • Timers and clocks
  • Networking
Resources