How to create a window?

To create a window in Pygame, you need to follow these steps after initializing Pygame. Here's a concise example:

Steps to Create a Window:

  1. Initialize Pygame: Use pygame.init().
  2. Set the Window Size: Define the width and height.
  3. Create the Window: Use pygame.display.set_mode() to create the window.
  4. Set the Window Title: Optionally, use pygame.display.set_caption().

Example Code:

import pygame

# Initialize Pygame
pygame.init()

# Set the window size
width, height = 800, 600

# Create the window
screen = pygame.display.set_mode((width, height))

# Set the window title
pygame.display.set_caption("My Pygame Window")

# Main loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

# Quit Pygame
pygame.quit()

Explanation:

  • pygame.display.set_mode((width, height)): Creates a window of the specified size.
  • pygame.display.set_caption("Title"): Sets the title of the window.
  • Main Loop: Keeps the window open until you close it.

Feel free to ask if you have more questions or need further assistance!

0 Comments

no data
Be the first to share your comment!