Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8
Pygame
Nishanth 126162009 M.TECH AI & DS Introduction to Pygame What is Pygame?
Pygame is a Python library used for creating 2D games.It helps with
handling graphics, sound, and user input for game development.Built on top of SDL (Simple DirectMedia Layer). Why Use Pygame?
• Easy to use for beginners.
• Cross-platform, supports Windows, macOS, and Linux.
• Great for learning game logic and multimedia programming
Basic Pygame Structure Structure of a Simple Pygame ProgramContent:
• Initialize Pygame: pygame.init()
• Set up the game window: screen = pygame.display.set_mode((width, height)) • Game loop:Keep the game running until the player quits. • Handle events: Use pygame.event. • get() to capture input. • Update the display: pygame.display.update() to refresh the screen. • Quit the game:pygame.quit() pygame.event.get() Function: pygame. • event.get()Purpose: Gets all the events (e.g., mouse clicks, key presses) from the event queue. • Input: No input required.Output: A list of all the events that occurred. • Application: Used to process user inputs like quitting the game or moving characters. pygame.time.Clock() Function: pygame. • time.Clock()Purpose: Controls how fast the game runs (frames per second). • Input: No input required. • Output: A clock object that can be used to control the game speed. • Application: Used to limit the speed of the game. pygame.time.set_timer() Function: pygame. • time.set_timer(event_id, milliseconds)Purpose: Repeatedly generates a custom event every specified number of milliseconds. • Input:event_id: An event type to be generated. • milliseconds: Time between events in milliseconds. • Output: After the specified time, a new event is added to the event queue. • Application: Used to trigger events at regular intervals, such as spawning enemies or updating the game state pygame.Surface.scroll() Function: surface.scroll(dx=0, dy=0) • Purpose: Shifts the contents of a surface by the given offsets dx and dy. • Input:dx: Horizontal offset (positive to move right, negative to move left). • dy: Vertical offset (positive to move down, negative to move up). • Output: The surface is moved, and the area outside the surface will be left as undefined pixels. • Application: Used for creating scrolling backgrounds or moving objects in the game, such as when the player walks across a long map.