{"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":"python","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,"search_vec":"'2.6.1':46 '2d':67 '2d-graphics':66 'access':26 'bugfix':51 'bundl':54 'comput':29 'cross':24 'cross-platform':23 'current':42 'design':9 'develop':63 'directmedia':18 'frequent':48 'game':13,62 'game-develop':61 'graphic':68 'gui':64 'hardwar':32 'joystick':40 'keyboard':38 'layer':19 'leverag':15 'librari':21,60 'minor':49 'modul':8 'mous':37 'multimedia':31,65 'often':53 'platform':25 'provid':22 'pygam':1,2 'python':7 'releas':52 'sdl':20,59 'set':5 'simpl':17 'sound':35 'stabl':43 'under':58 'updat':55 'version':44 'video':12,36 'write':11","created_at":"2026-04-11T01:38:37.440778+00:00","updated_at":"2026-04-16T18:27:37.930136+00:00","problems":[{"fix":"Install Pygame using pip in your terminal: `pip install pygame`. If using an IDE like VS Code or PyCharm, ensure the correct Python interpreter (the one with Pygame installed) is selected for your project.","cause":"Pygame is not installed in the Python environment being used, or the Python interpreter in your IDE is not correctly configured to use the environment where Pygame is installed.","error":"ModuleNotFoundError: No module named 'pygame'"},{"fix":"Ensure `pygame.init()` (which initializes all Pygame modules) or specifically `pygame.display.init()` is called at the very beginning of your script, before any operations that interact with the display.","cause":"A display-related function (e.g., `pygame.display.set_mode()`, `pygame.display.set_caption()`, `pygame.display.update()`) is called before `pygame.init()` or `pygame.display.init()` has been successfully executed.","error":"pygame.error: video system not initialized"},{"fix":"Call `pygame.mixer.init()` after `pygame.init()` and before loading or playing any sounds or music. While `pygame.init()` initializes most modules, explicitly calling `pygame.mixer.init()` ensures the sound system is ready.","cause":"Sound and music functions (e.g., `pygame.mixer.Sound()`, `pygame.mixer.music.load()`, `pygame.mixer.music.play()`) are called without explicitly initializing the Pygame mixer module.","error":"pygame.error: mixer not initialized"},{"fix":"Call `pygame.font.init()` after `pygame.init()` and before attempting to create or render any fonts. `pygame.init()` usually covers this, but explicit initialization might be needed if `pygame.init()` failed or was skipped for specific modules.","cause":"Text rendering functions (e.g., `pygame.font.SysFont()`, `pygame.font.Font()`) are used before the `pygame.font` module has been initialized.","error":"pygame.error: font not initialized"},{"fix":"Ensure `for event in pygame.event.get():` is called exactly once per iteration within your main game loop to process all pending events. Avoid calling `pygame.event.get()` more than once in a single frame to prevent event loss.","cause":"The Pygame event queue is not being processed correctly within the main game loop, often due to `pygame.event.get()` being called multiple times in a single frame, or not at all, leading to events being missed or the window becoming unresponsive.","error":"pygame.event.get() is not returning any event / event.key not working"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.6.1","cli_name":"","cli_version":null,"type":"library","homepage":"https://www.pygame.org","github":"https://github.com/pygame/pygame","docs":"https://pygame.org/docs","changelog":null,"pypi":"https://pypi.org/project/pygame/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops","testing","observability"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-28","next_check":"2026-07-28","install_tag":null}}