{"id":2688,"library":"pygame","title":"Pygame","description":"Pygame is a set of Python modules designed for writing video games. It leverages the Simple DirectMedia Layer (SDL) library, providing cross-platform access to your computer's multimedia hardware, such as sound, video, mouse, keyboard, and joystick. The current stable version is 2.6.1, with frequent minor and bugfix releases, often bundling updates to the underlying SDL library.","status":"active","version":"2.6.1","language":"en","source_language":"en","source_url":"https://github.com/pygame/pygame","tags":["game-development","gui","multimedia","2d-graphics"],"install":[{"cmd":"pip install pygame","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"pygame","correct":"import pygame"},{"symbol":"init","correct":"pygame.init()"},{"symbol":"display","correct":"pygame.display.set_mode((width, height))"},{"symbol":"event","correct":"for event in pygame.event.get():"},{"symbol":"image","correct":"pygame.image.load('path/to/image.png')"},{"symbol":"time","correct":"pygame.time.Clock()"}],"quickstart":{"code":"import pygame\n\npygame.init()\n\nWIDTH, HEIGHT = 800, 600\nSCREEN = pygame.display.set_mode((WIDTH, HEIGHT))\npygame.display.set_caption(\"Pygame Quickstart\")\n\nWHITE = (255, 255, 255)\nRED = (255, 0, 0)\n\nrunning = True\nclock = pygame.time.Clock()\nFPS = 60\n\nplayer_rect = pygame.Rect(WIDTH // 2 - 25, HEIGHT // 2 - 25, 50, 50)\n\nwhile running:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            running = False\n\n    # Drawing\n    SCREEN.fill(WHITE) # Fill the background\n    pygame.draw.rect(SCREEN, RED, player_rect) # Draw a red square\n\n    pygame.display.flip() # Update the full display Surface to the screen\n\n    clock.tick(FPS) # Control frame rate\n\npygame.quit()","lang":"python","description":"This quickstart code initializes Pygame, creates a window, draws a red square on a white background, and handles the basic event loop for closing the window. It demonstrates essential components like initialization, display setup, event handling, drawing, and display updates."},"warnings":[{"fix":"Always call `pygame.init()` before using any Pygame functions and `pygame.quit()` when your application is done to properly free resources.","message":"Forgetting to call `pygame.init()` at the beginning of your script can lead to `pygame` modules being uninitialized, causing errors or unexpected behavior. Similarly, omitting `pygame.quit()` at the end can leave system resources tied up.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `pygame.event.get()` is called at least once per frame to process user inputs and system events.","message":"Not regularly processing the event queue with `pygame.event.get()` or `pygame.event.pump()` inside your game loop will cause the window to become unresponsive and can lead to a \"not responding\" state, especially on Windows.","severity":"gotcha","affected_versions":"All versions"},{"fix":"End each frame of your game loop with `pygame.display.flip()` (for full screen update) or `pygame.display.update(rect_list)` (for partial updates).","message":"After drawing to the screen, you must call either `pygame.display.update()` or `pygame.display.flip()` to make the changes visible to the user. Forgetting this will result in a blank or static screen.","severity":"gotcha","affected_versions":"All versions"},{"fix":"After `image = pygame.image.load(\"my_image.png\")`, call `image = image.convert()` or `image = image.convert_alpha()` depending on whether the image has transparency.","message":"For optimal performance, especially with frequently blitted images, loaded `pygame.Surface` objects should be converted to the display surface's pixel format using `.convert()` or `.convert_alpha()` (for images with transparency) immediately after loading.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}