{"id":224,"date":"2015-01-11T20:50:00","date_gmt":"2015-01-11T20:50:00","guid":{"rendered":"https:\/\/santiagomarquezsolis.com\/index.php\/2026\/04\/20\/game-programming-in-python-part-1\/"},"modified":"2026-04-20T16:12:37","modified_gmt":"2026-04-20T16:12:37","slug":"game-programming-in-python-part-1","status":"publish","type":"post","link":"https:\/\/santiagomarquezsolis.com\/index.php\/en\/2015\/01\/11\/game-programming-in-python-part-1\/","title":{"rendered":"Game Programming in Python. Part 1."},"content":{"rendered":"<p>Welcome to this introduction to game programming in Python! In this series, we&#8217;ll use the <strong>pygame<\/strong> library to create interactive games step by step. By the end of Part 1, you&#8217;ll have a working window with basic graphics and event handling.<\/p>\n<h2>Why Python for Games?<\/h2>\n<p>Python might not be the first language that comes to mind for game development (C++ and Unity\/C# are more common for professional games), but it&#8217;s an excellent choice for:<\/p>\n<ul>\n<li>Learning game development concepts<\/li>\n<li>Rapid prototyping<\/li>\n<li>Creating small, fun games<\/li>\n<li>Teaching programming through game creation<\/li>\n<\/ul>\n<h2>What is pygame?<\/h2>\n<p>pygame is a Python library built on top of SDL (Simple DirectMedia Layer) that provides functionality for creating games and multimedia applications. It handles:<\/p>\n<ul>\n<li>Window creation and display<\/li>\n<li>Drawing shapes, images, and text<\/li>\n<li>Handling keyboard and mouse input<\/li>\n<li>Playing sounds and music<\/li>\n<li>Basic collision detection<\/li>\n<\/ul>\n<h2>Installation<\/h2>\n<pre><code>pip install pygame<\/code><\/pre>\n<h2>Your First pygame Window<\/h2>\n<pre><code>import pygame\r\nimport sys\r\n\r\n# Initialize pygame\r\npygame.init()\r\n\r\n# Set up the display\r\nWIDTH, HEIGHT = 800, 600\r\nscreen = pygame.display.set_mode((WIDTH, HEIGHT))\r\npygame.display.set_caption(\"My First Game\")\r\n\r\n# Define colors\r\nBLACK = (0, 0, 0)\r\nWHITE = (255, 255, 255)\r\nRED = (255, 0, 0)\r\nGREEN = (0, 255, 0)\r\nBLUE = (0, 0, 255)\r\n\r\n# Game loop\r\nclock = pygame.time.Clock()\r\n\r\nwhile True:\r\n    # Handle events\r\n    for event in pygame.event.get():\r\n        if event.type == pygame.QUIT:\r\n            pygame.quit()\r\n            sys.exit()\r\n        \r\n        if event.type == pygame.KEYDOWN:\r\n            if event.key == pygame.K_ESCAPE:\r\n                pygame.quit()\r\n                sys.exit()\r\n    \r\n    # Fill the screen with a color\r\n    screen.fill(BLACK)\r\n    \r\n    # Draw some shapes\r\n    pygame.draw.circle(screen, RED, (400, 300), 50)\r\n    pygame.draw.rect(screen, GREEN, (100, 100, 150, 100))\r\n    pygame.draw.line(screen, BLUE, (0, 0), (WIDTH, HEIGHT), 3)\r\n    \r\n    # Update the display\r\n    pygame.display.flip()\r\n    \r\n    # Cap the frame rate\r\n    clock.tick(60)<\/code><\/pre>\n<h2>Understanding the Game Loop<\/h2>\n<p>The game loop is the heart of any game. It runs continuously until the game ends, and each iteration is called a \u00abframe.\u00bb Our loop:<\/p>\n<ol>\n<li><strong>Handles events:<\/strong> Keyboard input, mouse clicks, window close button<\/li>\n<li><strong>Updates game state:<\/strong> Move objects, check collisions, update scores<\/li>\n<li><strong>Draws everything:<\/strong> Clear screen, draw all objects, display result<\/li>\n<\/ol>\n<h2>Adding a Moving Object<\/h2>\n<pre><code>import pygame\r\nimport sys\r\n\r\npygame.init()\r\nWIDTH, HEIGHT = 800, 600\r\nscreen = pygame.display.set_mode((WIDTH, HEIGHT))\r\npygame.display.set_caption(\"Moving Ball\")\r\nclock = pygame.time.Clock()\r\n\r\n# Ball properties\r\nball_x, ball_y = WIDTH \/\/ 2, HEIGHT \/\/ 2\r\nball_radius = 20\r\nball_speed_x = 5\r\nball_speed_y = 5\r\n\r\nwhile True:\r\n    for event in pygame.event.get():\r\n        if event.type == pygame.QUIT:\r\n            pygame.quit()\r\n            sys.exit()\r\n    \r\n    # Move the ball\r\n    ball_x += ball_speed_x\r\n    ball_y += ball_speed_y\r\n    \r\n    # Bounce off walls\r\n    if ball_x + ball_radius > WIDTH or ball_x - ball_radius < 0:\r\n        ball_speed_x = -ball_speed_x\r\n    if ball_y + ball_radius > HEIGHT or ball_y - ball_radius < 0:\r\n        ball_speed_y = -ball_speed_y\r\n    \r\n    # Draw\r\n    screen.fill((0, 0, 0))\r\n    pygame.draw.circle(screen, (255, 100, 0), (int(ball_x), int(ball_y)), ball_radius)\r\n    \r\n    pygame.display.flip()\r\n    clock.tick(60)<\/code><\/pre>\n<h2>Handling Keyboard Input<\/h2>\n<p>There are two ways to handle keyboard input in pygame:<\/p>\n<ul>\n<li><strong>Events:<\/strong> For one-time actions (jumping, shooting)<\/li>\n<li><strong>Key state:<\/strong> For continuous movement (walking)<\/li>\n<\/ul>\n<pre><code># Continuous movement with key state\r\nkeys = pygame.key.get_pressed()\r\nif keys[pygame.K_LEFT]:\r\n    player_x -= player_speed\r\nif keys[pygame.K_RIGHT]:\r\n    player_x += player_speed\r\nif keys[pygame.K_UP]:\r\n    player_y -= player_speed\r\nif keys[pygame.K_DOWN]:\r\n    player_y += player_speed<\/code><\/pre>\n<h2>Summary<\/h2>\n<p>In Part 1, we learned:<\/p>\n<ul>\n<li>How to set up a pygame window<\/li>\n<li>The game loop structure (events \u2192 update \u2192 draw)<\/li>\n<li>Drawing basic shapes (circles, rectangles, lines)<\/li>\n<li>Creating moving objects with collision detection<\/li>\n<li>Handling keyboard input<\/li>\n<\/ul>\n<p>In Part 2, we'll build on these fundamentals to create a complete Tic-Tac-Toe game with a proper game board, win detection, and a restart mechanism. See you there!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to this introduction to game programming in Python! In this series, we&#8217;ll use the pygame library to create interactive games step by step. By the end of Part 1, you&#8217;ll have a working window with basic graphics and event handling. Why Python for Games? Python might not be the first language that comes to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[162,282,284],"tags":[],"class_list":["post-224","post","type-post","status-publish","format-standard","hentry","category-blog-en","category-games-en","category-python-en"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/santiagomarquezsolis.com\/index.php\/wp-json\/wp\/v2\/posts\/224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/santiagomarquezsolis.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/santiagomarquezsolis.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/santiagomarquezsolis.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/santiagomarquezsolis.com\/index.php\/wp-json\/wp\/v2\/comments?post=224"}],"version-history":[{"count":1,"href":"https:\/\/santiagomarquezsolis.com\/index.php\/wp-json\/wp\/v2\/posts\/224\/revisions"}],"predecessor-version":[{"id":250,"href":"https:\/\/santiagomarquezsolis.com\/index.php\/wp-json\/wp\/v2\/posts\/224\/revisions\/250"}],"wp:attachment":[{"href":"https:\/\/santiagomarquezsolis.com\/index.php\/wp-json\/wp\/v2\/media?parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/santiagomarquezsolis.com\/index.php\/wp-json\/wp\/v2\/categories?post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/santiagomarquezsolis.com\/index.php\/wp-json\/wp\/v2\/tags?post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}