<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ pygame - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ pygame - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 24 May 2026 22:25:07 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/pygame/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ PyGame Tutorial – How to Build a Bouncing Ball Game ]]>
                </title>
                <description>
                    <![CDATA[ In this tutorial, you'll learn how to create a simple yet funny bouncing ball game using the PyGame library. Whether you're a beginner seeking to grasp the fundamentals of game development or an enthusiast eager to explore PyGame's capabilities, this... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/pygame-tutorial-build-a-bouncing-game/</link>
                <guid isPermaLink="false">66d45f67a326133d124409f9</guid>
                
                    <category>
                        <![CDATA[ Game Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pygame ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Juan P. Romano ]]>
                </dc:creator>
                <pubDate>Tue, 23 Jan 2024 22:46:54 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/01/bouncing_ball.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this tutorial, you'll learn how to create a simple yet funny bouncing ball game using the PyGame library.</p>
<p>Whether you're a beginner seeking to grasp the fundamentals of game development or an enthusiast eager to explore PyGame's capabilities, this article is your guide to crafting a simple yet engaging game.</p>
<p>You just need to break down the steps, read the code, and enjoy the ride!</p>
<p>At the end of this tutorial, your game should look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/bouncing_game.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-how-to-set-up-the-development-environment">How to set up the development environment</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-defining-the-game-concept">Defining the game concept</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-app-initialization">App initialization</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-create-a-pygame-instance">How to create a PyGame instance</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-create-the-game-screens">How to create the game screens</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-main-game-loop">Main game loop</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-how-to-set-up-the-development-environment">How to Set Up the Development Environment</h2>
<h3 id="heading-tools-and-software">Tools and Software</h3>
<p>Before you get into the coding adventure, let's talk about the tools that will fuel your creativity.</p>
<h4 id="heading-python">Python</h4>
<p>You'll be using Python, a versatile and beginner-friendly programming language. Its clean syntax and extensive library support make it an excellent choice for game development.</p>
<p>If you don't have Python installed, head over to <a target="_blank" href="https://www.python.org/">python.org</a> to download and install the latest version.</p>
<h4 id="heading-pygame">PyGame</h4>
<p>Your secret weapon for this project. PyGame, a set of Python modules, simplifies game development by handling multimedia elements like images and sounds. It's the engine that will power your game.</p>
<p>To install PyGame, you can use the following command in your terminal or command prompt:</p>
<pre><code class="lang-bash">pip install pygame
</code></pre>
<h4 id="heading-text-editor">Text Editor</h4>
<p>Whether it's VSCode, PyCharm, or Thonny, these environments offer features like syntax highlighting and debugging tools. Choose the one you're most comfortable with, and let's get ready to write some code!</p>
<p>I will be using VSCode but feel free to use whichever you like. If you feel adventurous, you can try Thonny!</p>
<p>Now that you're armed with the right tools, let's jump into the code and bring your game to life.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/screenshot-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>This is Thonny</em></p>
<h2 id="heading-defining-the-game-concept">Defining the Game Concept</h2>
<h3 id="heading-game-concept-and-mechanics">Game Concept and Mechanics</h3>
<p>Let's break down the core concepts and mechanics you'll be implementing.</p>
<p><strong>Platform Control:</strong></p>
<ul>
<li><p>Objective: Use arrow keys to move the platform left or right.</p>
</li>
<li><p>Implementation: Your platform moves horizontally based on key input.</p>
</li>
<li><p>Limitations: Ensure the platform stays within the screen boundaries.</p>
</li>
</ul>
<p><strong>Bouncing Ball Dynamics:</strong></p>
<ul>
<li><p>Objective: Guide the bouncing ball across the screen.</p>
</li>
<li><p>Implementation: The ball moves independently, bouncing off walls and the platform.</p>
</li>
<li><p>Interaction: Score points each time the ball successfully bounces off the platform.</p>
</li>
</ul>
<p><strong>Scoring System:</strong></p>
<ul>
<li><p>Objective: Accumulate points based on successful bounces.</p>
</li>
<li><p>Implementation: Your coding determines the scoring system.</p>
</li>
<li><p>Challenge: Optimize the scoring mechanism for higher scores.</p>
</li>
</ul>
<p><strong>Level Progression:</strong></p>
<ul>
<li><p>Objective: Advance through levels as your score reaches milestones.</p>
</li>
<li><p>Implementation: With every 10 points, you progress to a new level.</p>
</li>
<li><p>Challenge: Expect increased difficulty and complexity with each level.</p>
</li>
</ul>
<p><strong>Dynamic Platform Color:</strong></p>
<ul>
<li><p>Objective: Platform color changes with each level, adding a visual dynamic.</p>
</li>
<li><p>Implementation: Colors are randomly generated upon reaching a new level.</p>
</li>
<li><p>Aesthetic Touch: Adds variety and excitement to the gaming experience.</p>
</li>
</ul>
<p><strong>Lives and Game Over:</strong></p>
<ul>
<li><p>Objective: Avoid letting the ball fall off the screen to maintain lives.</p>
</li>
<li><p>Implementation: Lives decrease with missed bounces. Game over occurs when lives run out (you have only 3 lives).</p>
</li>
<li><p>Restart: After game over, restart with three lives and a fresh score.</p>
</li>
</ul>
<h2 id="heading-how-to-code-the-game">How to Code the Game</h2>
<h3 id="heading-app-initialization">App initialization</h3>
<p>In this section, you'll define constants such as screen dimensions, ball and platform properties, and colors.</p>
<p>You'll also initialize PyGame, set up the display screen, and create a clock object to control the frame rate.</p>
<p>Then you'll initialize variables for the game, including ball position, speed, platform position, speed, score, lives, and current level.</p>
<p>Here's the code to do all that:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> pygame
<span class="hljs-keyword">import</span> sys
<span class="hljs-keyword">import</span> random

<span class="hljs-comment"># Constants</span>
WIDTH, HEIGHT = <span class="hljs-number">800</span>, <span class="hljs-number">600</span>
BALL_RADIUS = <span class="hljs-number">20</span>
PLATFORM_WIDTH, PLATFORM_HEIGHT = <span class="hljs-number">100</span>, <span class="hljs-number">10</span>
FPS = <span class="hljs-number">60</span>
BLACK = (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
WHITE = (<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>)
RED = (<span class="hljs-number">255</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
YELLOW = (<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">0</span>)
ORANGE = (<span class="hljs-number">255</span>, <span class="hljs-number">165</span>, <span class="hljs-number">0</span>)
LIGHT_BLUE = (<span class="hljs-number">173</span>, <span class="hljs-number">216</span>, <span class="hljs-number">230</span>)  <span class="hljs-comment"># Light blue color for the level indicator</span>
</code></pre>
<p>Let's break down the code step by step:</p>
<p><strong>Importing Libraries:</strong></p>
<ul>
<li><p><code>import pygame</code>: Imports the Pygame library, which is used for developing games in Python.</p>
</li>
<li><p><code>import sys</code>: Imports the sys module, providing access to some variables used or maintained by the interpreter and functions that interact with the interpreter.</p>
</li>
</ul>
<p><strong>Constants:</strong></p>
<ul>
<li><p><code>WIDTH, HEIGHT = 800, 600</code>: Defines the dimensions of the game window.</p>
</li>
<li><p><code>BALL_RADIUS = 20</code>: Specifies the radius of the bouncing ball.</p>
</li>
<li><p><code>PLATFORM_WIDTH, PLATFORM_HEIGHT = 100, 10</code>: Sets the dimensions of the platform.</p>
</li>
<li><p><code>FPS = 60</code>: Defines the frames per second, controlling the speed of the game.</p>
</li>
<li><p>Various color constants like <code>BLACK</code>, <code>WHITE</code>, <code>RED</code>, <code>YELLOW</code>, <code>ORANGE</code>, and <code>LIGHT_BLUE</code> represent RGB values for colors used in the game.</p>
</li>
</ul>
<h2 id="heading-how-to-create-a-pygame-instance">How to Create a PyGame Instance</h2>
<p>Now you're going to create the PyGame window and define some global variables that you'll use for every level of the game:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Initialize Pygame</span>
pygame.init()

<span class="hljs-comment"># Create the screen</span>
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(<span class="hljs-string">"Bouncing Ball Game"</span>)
font = pygame.font.Font(<span class="hljs-literal">None</span>, <span class="hljs-number">36</span>)

<span class="hljs-comment"># Clock to control the frame rate</span>
clock = pygame.time.Clock()

<span class="hljs-comment"># Initialize variables for the game</span>
ball_pos = [WIDTH // <span class="hljs-number">2</span>, HEIGHT // <span class="hljs-number">2</span>]
ball_speed = [random.uniform(<span class="hljs-number">2</span>, <span class="hljs-number">4</span>), random.uniform(<span class="hljs-number">2</span>, <span class="hljs-number">4</span>)]  <span class="hljs-comment"># Faster starting speed</span>
platform_pos = [WIDTH // <span class="hljs-number">2</span> - PLATFORM_WIDTH // <span class="hljs-number">2</span>, HEIGHT - PLATFORM_HEIGHT - <span class="hljs-number">10</span>]
platform_speed = <span class="hljs-number">10</span>
score = <span class="hljs-number">0</span>
lives = <span class="hljs-number">3</span>
current_level = <span class="hljs-number">1</span>
platform_color = ORANGE  <span class="hljs-comment"># Initialize platform color</span>
</code></pre>
<p><strong>Pygame Initialization:</strong></p>
<ul>
<li><code>pygame.init()</code>: Initializes the Pygame library.</li>
</ul>
<p><strong>Create the Game Window:</strong></p>
<ul>
<li><p><code>screen = pygame.display.set_mode((WIDTH, HEIGHT))</code>: Creates the game window with the specified dimensions.</p>
</li>
<li><p><code>pygame.display.set_caption("Bouncing Ball Game")</code>: Sets the title of the game window.</p>
</li>
<li><p><code>font = pygame.font.Font(None, 36)</code>: Initializes a font object for rendering text.</p>
</li>
</ul>
<p><strong>Clock for Frame Rate Control:</strong></p>
<ul>
<li><code>clock = pygame.time.Clock()</code>: Creates a clock object to control the frame rate.</li>
</ul>
<p><strong>Game Variables Initialization:</strong></p>
<ul>
<li><p><code>ball_pos = [WIDTH // 2, HEIGHT // 2]</code>: Initializes the starting position of the ball at the center of the screen.</p>
</li>
<li><p><code>ball_speed = [random.uniform(2, 4), random.uniform(2, 4)]</code>: Initializes the starting speed of the ball with random values.</p>
</li>
<li><p><code>platform_pos = [WIDTH // 2 - PLATFORM_WIDTH // 2, HEIGHT - PLATFORM_HEIGHT - 10]</code>: Initializes the starting position of the platform.</p>
</li>
<li><p><code>platform_speed = 10</code>: Sets the speed at which the platform moves.</p>
</li>
<li><p><code>score = 0</code>: Initializes the player's score.</p>
</li>
<li><p><code>lives = 3</code>: Initializes the number of lives the player has.</p>
</li>
<li><p><code>current_level = 1</code>: Initializes the current level of the game.</p>
</li>
<li><p><code>platform_color = ORANGE</code>: Initializes the color of the platform.</p>
</li>
</ul>
<h2 id="heading-how-to-create-the-game-screens">How to Create the Game Screens</h2>
<p>You will create at least three screens, one for start the game, one for winning the game, and one for losing the game. You can use the following code to do all that:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Functions for screens</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start_screen</span>():</span>
    screen.fill(BLACK)
    show_text_on_screen(<span class="hljs-string">"Bouncing Ball Game"</span>, <span class="hljs-number">50</span>, HEIGHT // <span class="hljs-number">4</span>)
    show_text_on_screen(<span class="hljs-string">"Press any key to start..."</span>, <span class="hljs-number">30</span>, HEIGHT // <span class="hljs-number">3</span>)
    show_text_on_screen(<span class="hljs-string">"Move the platform with arrow keys..."</span>, <span class="hljs-number">30</span>, HEIGHT // <span class="hljs-number">2</span>)
    pygame.display.flip()
    wait_for_key()

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">game_over_screen</span>():</span>
    screen.fill(BLACK)
    show_text_on_screen(<span class="hljs-string">"Game Over"</span>, <span class="hljs-number">50</span>, HEIGHT // <span class="hljs-number">3</span>)
    show_text_on_screen(<span class="hljs-string">f"Your final score: <span class="hljs-subst">{score}</span>"</span>, <span class="hljs-number">30</span>, HEIGHT // <span class="hljs-number">2</span>)
    show_text_on_screen(<span class="hljs-string">"Press any key to restart..."</span>, <span class="hljs-number">20</span>, HEIGHT * <span class="hljs-number">2</span> // <span class="hljs-number">3</span>)
    pygame.display.flip()
    wait_for_key()

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">victory_screen</span>():</span>
    screen.fill(BLACK)
    show_text_on_screen(<span class="hljs-string">"Congratulations!"</span>, <span class="hljs-number">50</span>, HEIGHT // <span class="hljs-number">3</span>)
    show_text_on_screen(<span class="hljs-string">f"You've won with a score of <span class="hljs-subst">{score}</span>"</span>, <span class="hljs-number">30</span>, HEIGHT // <span class="hljs-number">2</span>)
    show_text_on_screen(<span class="hljs-string">"Press any key to exit..."</span>, <span class="hljs-number">20</span>, HEIGHT * <span class="hljs-number">2</span> // <span class="hljs-number">3</span>)
    pygame.display.flip()
    wait_for_key()

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wait_for_key</span>():</span>
    waiting = <span class="hljs-literal">True</span>
    <span class="hljs-keyword">while</span> waiting:
        <span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pygame.event.get():
            <span class="hljs-keyword">if</span> event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            <span class="hljs-keyword">elif</span> event.type == pygame.KEYDOWN:
                waiting = <span class="hljs-literal">False</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">show_text_on_screen</span>(<span class="hljs-params">text, font_size, y_position</span>):</span>
    font = pygame.font.Font(<span class="hljs-literal">None</span>, font_size)
    text_render = font.render(text, <span class="hljs-literal">True</span>, WHITE)
    text_rect = text_render.get_rect(center=(WIDTH // <span class="hljs-number">2</span>, y_position))
    screen.blit(text_render, text_rect)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">change_platform_color</span>():</span>
    <span class="hljs-keyword">return</span> (random.randint(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>), random.randint(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>), random.randint(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>))
</code></pre>
<p>Let's go through the functions:</p>
<p><code>start_screen()</code> Function:</p>
<ul>
<li><p>Clears the screen with a black background using <code>screen.fill(BLACK)</code>.</p>
</li>
<li><p>Displays the game title and instructions using <code>show_text_on_screen</code>.</p>
</li>
<li><p>Flips the display to make the changes visible with <code>pygame.display.flip()</code>.</p>
</li>
<li><p>Calls <code>wait_for_key()</code> to wait for a key press before proceeding.</p>
</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/image-105.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><code>game_over_screen()</code> Function:</p>
<ul>
<li><p>Clears the screen with a black background.</p>
</li>
<li><p>Displays the game over message, the final score, and instructions for restarting.</p>
</li>
<li><p>Flips the display.</p>
</li>
<li><p>Calls <code>wait_for_key()</code> to wait for a key press.</p>
</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/image-106.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><code>victory_screen()</code> Function:</p>
<ul>
<li><p>Clears the screen with a black background.</p>
</li>
<li><p>Displays a congratulatory message, the final score, and instructions for exiting.</p>
</li>
<li><p>Flips the display.</p>
</li>
<li><p>Calls <code>wait_for_key()</code> to wait for a key press.</p>
</li>
</ul>
<p><code>wait_for_key()</code> Function:</p>
<ul>
<li><p>Waits for either a quit event (closing the game window) or a key press event.</p>
</li>
<li><p>If the event is quitting, it exits the game using <code>pygame.quit()</code> and <code>sys.exit()</code>.</p>
</li>
<li><p>If the event is a key press, it breaks out of the waiting loop.</p>
</li>
</ul>
<p><code>show_text_on_screen(text, font_size, y_position)</code> Function:</p>
<ul>
<li><p>Renders text on the screen with the specified font size and Y position.</p>
</li>
<li><p>Uses the <code>pygame.font.Font</code> class to create a font object.</p>
</li>
<li><p>Renders the text onto a surface with the specified color (white in this case).</p>
</li>
<li><p>Positions the text in the center of the screen at the specified Y position.</p>
</li>
<li><p>Blits (draws) the text onto the game screen.</p>
</li>
</ul>
<p><code>change_platform_color()</code> Function:</p>
<ul>
<li>Returns a random RGB color for changing the platform color.</li>
</ul>
<p>These functions handle different aspects of the game screens, user interactions, and display of text. They contribute to the overall structure and user experience of the game.</p>
<h2 id="heading-main-game-loop">Main Game Loop</h2>
<p>And now, you'll code the main loop of the game with it's applied mechanics. Let's check out the code first and then the explanation.</p>
<p>This is the longest block of code in this tutorial, so be patient – it's worth it.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Main game loop</span>
start_screen()
game_running = <span class="hljs-literal">True</span>

<span class="hljs-keyword">while</span> game_running:
    <span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pygame.event.get():
        <span class="hljs-keyword">if</span> event.type == pygame.QUIT:
            game_running = <span class="hljs-literal">False</span>

    keys = pygame.key.get_pressed()

    <span class="hljs-comment"># Move the platform</span>
    platform_pos[<span class="hljs-number">0</span>] += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * platform_speed
    platform_pos[<span class="hljs-number">1</span>] += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * platform_speed

    <span class="hljs-comment"># Ensure the platform stays within the screen boundaries</span>
    platform_pos[<span class="hljs-number">0</span>] = max(<span class="hljs-number">0</span>, min(platform_pos[<span class="hljs-number">0</span>], WIDTH - PLATFORM_WIDTH))
    platform_pos[<span class="hljs-number">1</span>] = max(<span class="hljs-number">0</span>, min(platform_pos[<span class="hljs-number">1</span>], HEIGHT - PLATFORM_HEIGHT))

    <span class="hljs-comment"># Move the ball</span>
    ball_pos[<span class="hljs-number">0</span>] += ball_speed[<span class="hljs-number">0</span>]
    ball_pos[<span class="hljs-number">1</span>] += ball_speed[<span class="hljs-number">1</span>]

    <span class="hljs-comment"># Bounce off the walls</span>
    <span class="hljs-keyword">if</span> ball_pos[<span class="hljs-number">0</span>] &lt;= <span class="hljs-number">0</span> <span class="hljs-keyword">or</span> ball_pos[<span class="hljs-number">0</span>] &gt;= WIDTH:
        ball_speed[<span class="hljs-number">0</span>] = -ball_speed[<span class="hljs-number">0</span>]

    <span class="hljs-keyword">if</span> ball_pos[<span class="hljs-number">1</span>] &lt;= <span class="hljs-number">0</span>:
        ball_speed[<span class="hljs-number">1</span>] = -ball_speed[<span class="hljs-number">1</span>]

    <span class="hljs-comment"># Check if the ball hits the platform</span>
    <span class="hljs-keyword">if</span> (
        platform_pos[<span class="hljs-number">0</span>] &lt;= ball_pos[<span class="hljs-number">0</span>] &lt;= platform_pos[<span class="hljs-number">0</span>] + PLATFORM_WIDTH
        <span class="hljs-keyword">and</span> platform_pos[<span class="hljs-number">1</span>] &lt;= ball_pos[<span class="hljs-number">1</span>] &lt;= platform_pos[<span class="hljs-number">1</span>] + PLATFORM_HEIGHT
    ):
        ball_speed[<span class="hljs-number">1</span>] = -ball_speed[<span class="hljs-number">1</span>]
        score += <span class="hljs-number">1</span>

    <span class="hljs-comment"># Check if the player advances to the next level</span>
    <span class="hljs-keyword">if</span> score &gt;= current_level * <span class="hljs-number">10</span>:
        current_level += <span class="hljs-number">1</span>
        ball_pos = [WIDTH // <span class="hljs-number">2</span>, HEIGHT // <span class="hljs-number">2</span>]
        ball_speed = [random.uniform(<span class="hljs-number">2</span>, <span class="hljs-number">4</span>), random.uniform(<span class="hljs-number">2</span>, <span class="hljs-number">4</span>)]  <span class="hljs-comment"># Randomize the ball speed</span>
        platform_color = change_platform_color()

    <span class="hljs-comment"># Check if the ball falls off the screen</span>
    <span class="hljs-keyword">if</span> ball_pos[<span class="hljs-number">1</span>] &gt;= HEIGHT:
        <span class="hljs-comment"># Decrease lives</span>
        lives -= <span class="hljs-number">1</span>
        <span class="hljs-keyword">if</span> lives == <span class="hljs-number">0</span>:
            game_over_screen()
            start_screen()  <span class="hljs-comment"># Restart the game after game over</span>
            score = <span class="hljs-number">0</span>
            lives = <span class="hljs-number">3</span>
            current_level = <span class="hljs-number">1</span>
        <span class="hljs-keyword">else</span>:
            <span class="hljs-comment"># Reset the ball position</span>
            ball_pos = [WIDTH // <span class="hljs-number">2</span>, HEIGHT // <span class="hljs-number">2</span>]
            <span class="hljs-comment"># Randomize the ball speed</span>
            ball_speed = [random.uniform(<span class="hljs-number">2</span>, <span class="hljs-number">4</span>), random.uniform(<span class="hljs-number">2</span>, <span class="hljs-number">4</span>)]

    <span class="hljs-comment"># Clear the screen</span>
    screen.fill(BLACK)

    <span class="hljs-comment"># Draw the ball</span>
    pygame.draw.circle(screen, WHITE, (int(ball_pos[<span class="hljs-number">0</span>]), int(ball_pos[<span class="hljs-number">1</span>])), BALL_RADIUS)

    <span class="hljs-comment"># Draw the platform</span>
    pygame.draw.rect(screen, platform_color, (int(platform_pos[<span class="hljs-number">0</span>]), int(platform_pos[<span class="hljs-number">1</span>]), PLATFORM_WIDTH, PLATFORM_HEIGHT))

    <span class="hljs-comment"># Display information</span>
    info_line_y = <span class="hljs-number">10</span>  <span class="hljs-comment"># Adjust the vertical position as needed</span>
    info_spacing = <span class="hljs-number">75</span>  <span class="hljs-comment"># Adjust the spacing as needed</span>

    <span class="hljs-comment"># Draw the score in an orange rectangle at the top left</span>
    score_text = font.render(<span class="hljs-string">f"Score: <span class="hljs-subst">{score}</span>"</span>, <span class="hljs-literal">True</span>, WHITE)
    score_rect = score_text.get_rect(topleft=(<span class="hljs-number">10</span>, info_line_y))
    pygame.draw.rect(screen, ORANGE, score_rect.inflate(<span class="hljs-number">10</span>, <span class="hljs-number">5</span>))
    screen.blit(score_text, score_rect)

    <span class="hljs-comment"># Draw the level indicator in a light-blue rectangle at the top left (next to the score)</span>
    level_text = font.render(<span class="hljs-string">f"Level: <span class="hljs-subst">{current_level}</span>"</span>, <span class="hljs-literal">True</span>, WHITE)
    level_rect = level_text.get_rect(topleft=(score_rect.topright[<span class="hljs-number">0</span>] + info_spacing, info_line_y))
    pygame.draw.rect(screen, LIGHT_BLUE, level_rect.inflate(<span class="hljs-number">10</span>, <span class="hljs-number">5</span>))
    screen.blit(level_text, level_rect)

    <span class="hljs-comment"># Draw the lives in a red rectangle at the top left (next to the level)</span>
    lives_text = font.render(<span class="hljs-string">f"Lives: <span class="hljs-subst">{lives}</span>"</span>, <span class="hljs-literal">True</span>, WHITE)
    lives_rect = lives_text.get_rect(topleft=(level_rect.topright[<span class="hljs-number">0</span>] + info_spacing, info_line_y))
    pygame.draw.rect(screen, RED, lives_rect.inflate(<span class="hljs-number">10</span>, <span class="hljs-number">5</span>))
    screen.blit(lives_text, lives_rect)

    <span class="hljs-comment"># Update the display</span>
    pygame.display.flip()

    <span class="hljs-comment"># Control the frame rate</span>
    clock.tick(FPS)

<span class="hljs-comment"># Quit Pygame</span>
pygame.quit()
</code></pre>
<p>Alright, that was a lot – but here's what we did in the above code:</p>
<p><strong>Initialization and Start Screen:</strong></p>
<ul>
<li><p>Calls <code>start_screen()</code> to display the initial screen with instructions.</p>
</li>
<li><p>Sets <code>game_running</code> to <code>True</code> to initiate the main game loop.</p>
</li>
</ul>
<p><strong>Main Game Loop:</strong></p>
<ul>
<li>Continues until <code>game_running</code> becomes <code>False</code> (for example, when the user closes the game window).</li>
</ul>
<p><strong>Event Handling:</strong></p>
<ul>
<li><p>Checks for events using <code>pygame.event.get()</code>.</p>
</li>
<li><p>If a quit event (closing the game window) is detected, sets <code>game_running</code> to <code>False</code> to exit the loop.</p>
</li>
</ul>
<p><strong>Platform Movement:</strong></p>
<ul>
<li><p>Reads the state of the arrow keys using <code>pygame.key.get_pressed()</code>.</p>
</li>
<li><p>Adjusts the platform position based on arrow key inputs.</p>
</li>
<li><p>Ensures the platform stays within the screen boundaries.</p>
</li>
</ul>
<p><strong>Ball Movement and Bouncing:</strong></p>
<ul>
<li><p>Updates the ball's position based on its speed.</p>
</li>
<li><p>Implements bouncing off the walls by reversing the speed when reaching the screen edges.</p>
</li>
</ul>
<p><strong>Collision Detection:</strong></p>
<ul>
<li><p>Checks if the ball hits the platform by comparing positions.</p>
</li>
<li><p>If a collision occurs, the ball's vertical speed is reversed, and the player scores a point.</p>
</li>
</ul>
<p><strong>Level Progression:</strong></p>
<ul>
<li><p>Checks if the player has scored enough points to advance to the next level.</p>
</li>
<li><p>If so, increments the level, resets the ball's position, randomizes its speed, and changes the platform color.</p>
</li>
</ul>
<p><strong>Checking for Game Over:</strong></p>
<ul>
<li><p>Monitors if the ball falls off the screen.</p>
</li>
<li><p>Decreases the number of lives if the ball is below the screen.</p>
</li>
<li><p>If lives run out, displays the game over screen, restarts the game, and resets score, lives, and level.</p>
</li>
</ul>
<p><strong>Screen Rendering:</strong></p>
<ul>
<li><p>Clears the screen with a black background.</p>
</li>
<li><p>Draws the ball and platform on the screen.</p>
</li>
<li><p>Displays score, level, and lives information in rectangles with specific colors.</p>
</li>
</ul>
<p><strong>Display Update and Frame Rate Control:</strong></p>
<ul>
<li><p>Updates the display to show the changes.</p>
</li>
<li><p>Controls the frame rate with <code>clock.tick(FPS)</code>.</p>
</li>
</ul>
<p><strong>Game Termination:</strong></p>
<ul>
<li>Exits PyGame and terminates the program when the main loop is exited.</li>
</ul>
<p>This structure ensures continuous gameplay, handling user input, updating game state, and providing visual feedback to the player.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you've navigated through the intricacies of several key functions that form the backbone of your bouncing ball game.</p>
<p>Let's recap their roles:</p>
<p>The <code>start_screen()</code> function clears the screen with a black background, displays the game title and instructions, flips the display for visibility, and waits for a key press using <code>wait_for_key()</code>.</p>
<p>The <code>game_over_screen()</code> function clears the screen with a black background, displays the game over message, final score, and restart instructions, flips the display, and waits for a key press with <code>wait_for_key()</code>.</p>
<p>The <code>victory_screen()</code> function clears the screen with a black background, displays a congratulatory message, final score, and exit instructions, flips the display, and waits for a key press with <code>wait_for_key()</code>.</p>
<p>The <code>wait_for_key()</code> function waits for either a quit event (closing the game window) or a key press event. It exits the game if quitting, and breaks out of the waiting loop if a key is pressed.</p>
<p>The <code>show_text_on_screen(text, font_size, y_position)</code> function renders text on the screen with specified attributes, utilizes the <code>pygame.font.Font</code> class for creating a font object, and positions and draws the text in the center of the screen.</p>
<p>And the <code>change_platform_color()</code> function returns a random RGB color for changing the platform color.</p>
<p>Following these functions, you delved into the main game loop, the heart of our game. It orchestrates the game's mechanics, including event handling, platform and ball movement, collision detection, level progression, and game termination.</p>
<p>You witnessed how this loop ensures continuous gameplay, responsive user interactions, and dynamic visuals.</p>
<p>Now that you're equipped with the understanding of these functions and the main loop, you have the knowledge to experiment, tweak, and expand your game. Feel empowered to embark on your journey of game development, explore new features, and create unique gaming experiences. Happy coding!</p>
<p>If you want to download the code or play the game, you can find it here:</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://github.com/jpromanonet/bouncing_ball_game">https://github.com/jpromanonet/bouncing_ball_game</a></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ PyGame Tutorial – How to Build an Alien Abduction game ]]>
                </title>
                <description>
                    <![CDATA[ Ever since I was a kid, I've been hooked on video games. My first experience with coding involved kind of a wild project – a Tron races simulator in Quick Basic on an ancient IBM computer (I was like 6 years old, so I didn't really understand what I ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/pygame-tutorial-alien-abduction-game/</link>
                <guid isPermaLink="false">66d45f6538f2dc3808b790b5</guid>
                
                    <category>
                        <![CDATA[ Game Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pygame ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Juan P. Romano ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jan 2024 23:55:29 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/12/alien_abduction_game.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Ever since I was a kid, I've been hooked on video games. My first experience with coding involved kind of a wild project – a Tron races simulator in Quick Basic on an ancient IBM computer (I was like 6 years old, so I didn't really understand what I was doing).</p>
<p>Fast forward through years of coding and here I am, still writing software and going into the world of game development.</p>
<p>With my sights set on becoming an indie game developer, I decided to explore PyGame, given my "fluency" in Python with teaching it and all.</p>
<p>After some brainstorming, I landed on a quirky concept: a game where you play as an alien cruising around in a UFO. Your mission? Hit a specific abduction quota each week to dodge sanctions from the Intergalactic Federation.</p>
<p>I won't lie, I was heavily inspired by the old "Luna lander" that I had in the same old IBM computer.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/12/image-65.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Black and White Luna Lander screen shot</em></p>
<h3 id="heading-why-i-think-making-games-is-cooler-than-your-average-coding-project">Why I think making games is cooler than your average coding project</h3>
<p>So, imagine you're coding, and suddenly it hits you – games! Yep, we're talking about the stuff that turns a screen into an interactive universe where you're not just a coder – you're a wizard creating spells (or lines of code) that make things move, shoot, and explode without legal repercussions.</p>
<p>Here's why diving into game development is like finding the secret level into software development:</p>
<ol>
<li><p><strong>It's coding with a purpose:</strong> Forget boring programs – you're creating a universe where aliens abduct cows because...why not?</p>
</li>
<li><p><strong>No more lone wolf coding:</strong> Games need more than code. They also need stories, characters, and cool graphics. Suddenly, you're not just a coder – you're a one-person army conquering design, storytelling, and maybe a bit of sound engineering.</p>
</li>
<li><p><strong>It's like LEGO for coders:</strong> You get to build stuff, break it, and then build it again! Game development is all about trial and error, making it the LEGO of coding projects. Who said debugging can't be fun?</p>
</li>
<li><p><strong>Creativity party:</strong> Ever wanted to create a world where gravity works sideways, and cows wear spacesuits? In game development, your creativity runs wild.</p>
</li>
<li><p><strong>Solving mysteries, coding edition:</strong> Games come with puzzles. Not just for players, but for you, the mastermind behind the scenes. Think of it as creating your own coding mystery novel where every bug is a plot twist waiting to be solved.</p>
</li>
<li><p><strong>Learn by playing:</strong> Remember those boring textbooks? Well, forget them. With game development, you're not just reading – you're playing.</p>
</li>
<li><p><strong>Show off your skills:</strong> Building games isn't just about fun. It's about flexing your coding muscles. Your game is your trophy, your badge of honor. Imagine showing up to a job interview with a game you built. Who's cool now?</p>
</li>
</ol>
<p>So, why settle for coding when you can create a universe? Game development is where coding becomes an epic adventure, and you, my friend, are the hero. <strong>Ready to level up?</strong></p>
<p>Alright, let's craft this game together, step by step, and make it a blast! Wondering what it'll look like when it's all done? Check out this snazzy preview:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/12/image-66.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Preview of what the game will look like, showing an alien spaceship descending to pick up some cows.</em></p>
<h2 id="heading-table-of-contents">Table of Contents:</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-how-to-set-up-the-environment">How to Set Up the Environment</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-design-the-game-concept">How to Design the Game Concept</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-code-the-game">How to Code the Game</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-how-to-set-up-the-environment">How to Set Up the Environment</h2>
<h3 id="heading-install-pygame">Install PyGame</h3>
<p>Let's kick off this journey by setting up the playground. First, you'll need to install PyGame. Here's a simple guide to get you started.</p>
<h3 id="heading-1-create-a-new-directory">1. Create a new directory</h3>
<p>Open your favorite terminal and navigate to the location where you want to keep your game. Create a new directory for your project. Let's call it "<em>AlienAbductionGame.</em>"</p>
<pre><code class="lang-bash">mkdir AlienAbductionGame
</code></pre>
<h3 id="heading-2-navigate-to-the-project-directory">2. Navigate to the project directory</h3>
<p>Move into your newly created directory.</p>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> AlienAbductionGame
</code></pre>
<h3 id="heading-3-install-pygame">3. Install PyGame</h3>
<pre><code class="lang-bash">pip install pygame
</code></pre>
<p>Great! Now you're armed with PyGame, and you're ready to get into the coding.</p>
<h3 id="heading-what-else-do-you-need-to-start">What else do you need to start?</h3>
<p>You already have Python with PyGame installed on your computer. So, the last thing you need is your favorite code editor. I will use and recommend Visual Studio Code, but you can use any other editor that you like or feel comfortable with.</p>
<p>Once you have everything installed and your editor ready, let's start by understanding the game concept and its mechanics.</p>
<h2 id="heading-how-to-design-the-game-concept">How to Design the Game Concept</h2>
<h3 id="heading-game-idea-alien-abduction-adventure">Game idea: Alien abduction adventure</h3>
<p>Picture this: You're an alien pilot cruising through the cosmos in your trusty UFO. Unfortunately, you're falling behind on your weekly quota of abductions, and the Intergalactic Federation isn't too pleased. To avoid a cosmic penalty, you must abduct various targets on Earth.</p>
<h3 id="heading-mechanics">Mechanics</h3>
<h4 id="heading-abduction-quota-and-levels">Abduction quota and levels:</h4>
<p>At the start of each level, you're given a specific abduction quota. This represents the number of cows you must abduct to progress.</p>
<p>The game consists of ten levels, each with an increasing quota. Completing one level unlocks the next and restart the quota counter.</p>
<h4 id="heading-ufo-controls">UFO Controls:</h4>
<p>Use the arrow keys or your preferred controls to navigate the UFO across the game screen.</p>
<p>Precise movements are crucial for successful abductions. Mastering the controls ensures efficient target acquisition.</p>
<h4 id="heading-targets-adorable-cows">Targets – adorable cows:</h4>
<p>Targets are charming, pixelated cows wandering on the Earth's surface. You'll approach a cow, activate your tractor beam (space bar), and watch the cow dissappear from your screen.</p>
<h4 id="heading-tractor-beam-mechanics">Tractor beam mechanics:</h4>
<p>You'll press the space bar to activate the tractor beam, which extends from your UFO to the ground.</p>
<p>When the beam touches a cow, it triggers the abduction process, and the cow dissappears, increasing your score and abduction quota.</p>
<h4 id="heading-timer-and-urgency">Timer and urgency:</h4>
<p>Each level comes with a countdown timer, adding a sense of urgency to your abductions.</p>
<p>Successfully reaching the abduction quota before the timer hits zero ensures advancement to the next level.</p>
<h3 id="heading-how-to-decide-on-game-elements-spaceship-targets-and-so-on">How to decide on game elements (spaceship, targets, and so on)</h3>
<p>When it comes to selecting your game elements, simplicity is key. The simpler, the better. So, let's take a look at what you'll need to make the game mechanics work.</p>
<ol>
<li><strong>Spaceship (UFO):</strong></li>
</ol>
<ul>
<li><p>Instead of intricate designs, focus on a distinctive UFO sprite, a cute one</p>
</li>
<li><p>Prioritize smooth movements and responsiveness to player controls.</p>
</li>
</ul>
<ol start="2">
<li><strong>Targets (cows):</strong></li>
</ol>
<ul>
<li><p>Opt for a cute cow sprite that aligns with the game's humor.</p>
</li>
<li><p>Consider changing the cow's color when it's abducted – you will code a red bubble signifying abduction.</p>
</li>
</ul>
<ol start="3">
<li><strong>Background and environment:</strong></li>
</ol>
<ul>
<li><p>Maintain a clean and straightforward background, adjusting the color scheme to match the game's progression.</p>
</li>
<li><p>Gradually modify the star color with each level to signify advancement.</p>
</li>
</ul>
<ol start="4">
<li><strong>Tractor beam:</strong></li>
</ol>
<ul>
<li>Ensure the tractor beam is visually distinguishable. You can experiment with a basic, yet effective, plain yellow color.</li>
</ul>
<ol start="5">
<li><strong>Level indicators:</strong></li>
</ol>
<ul>
<li>Implement a simple level indicator displaying the current level. This can be a small section at the corner of the screen.</li>
</ul>
<ol start="6">
<li><strong>Game Over and victory screens:</strong></li>
</ol>
<ul>
<li>Design straightforward screens indicating success or failure. A simple "Game Over" and "You Win" with accompanying text will suffice, like the following one</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/12/image-64.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Winning screen</em></p>
<h2 id="heading-how-to-code-the-game">How to Code the Game</h2>
<h3 id="heading-walkthrough-of-key-sections-of-the-code">Walkthrough of key sections of the code</h3>
<p>Now that you have a clear concept and understanding of our game mechanics and it's elements, it's time to get into the code. We'll walk through the key sections of the code.</p>
<p>First, let's start by importing the stuff you need to code our game:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> pygame
<span class="hljs-keyword">import</span> random
<span class="hljs-keyword">import</span> sys
</code></pre>
<p>In this section, you are importing three modules: <code>pygame</code>, <code>random</code>, and <code>sys</code>.</p>
<ul>
<li><p><strong>pygame:</strong> This is the main library you're using to create the game. It provides functions and tools for handling graphics, user input, and more.</p>
</li>
<li><p><strong>random:</strong> you'll use this module to generate random numbers. This comes in handy when you want things to happen unpredictably, like the initial position of targets.</p>
</li>
<li><p><strong>sys:</strong> This module provides access to some variables used or maintained by the Python interpreter and to functions that interact strongly with the interpreter. You'll use it for handling system events, such as quitting the game when the player closes the window.</p>
</li>
</ul>
<p>So, you're basically gearing up with the tools you need to make the game visually appealing, dynamic, and responsive to player input.</p>
<h3 id="heading-how-to-code-the-start-screen">How to code the start screen</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start_screen</span>(<span class="hljs-params">screen</span>):</span>
    <span class="hljs-comment"># Constants</span>
    WIDTH, HEIGHT = <span class="hljs-number">800</span>, <span class="hljs-number">600</span>
    BLACK = (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
    WHITE = (<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>)
    FONT_SIZE = <span class="hljs-number">30</span>

    <span class="hljs-comment"># Font for displaying the text</span>
    font = pygame.font.Font(<span class="hljs-literal">None</span>, FONT_SIZE)

    <span class="hljs-comment"># Fill the screen with the background color</span>
    screen.fill(BLACK)

    <span class="hljs-comment"># Introduction text</span>
    intro_text = [
        <span class="hljs-string">"Welcome, Alien Abductor!"</span>,
        <span class="hljs-string">"You're behind on your weekly quota of abductions."</span>,
        <span class="hljs-string">"Help the alien catch up by abducting targets on Earth!"</span>,
        <span class="hljs-string">""</span>,
        <span class="hljs-string">"----------------------------------------------------------------------------------------------"</span>,
        <span class="hljs-string">"Move the UFO with ARROWS and"</span>, 
        <span class="hljs-string">"press SPACE to abduct cows with the track bean."</span>,
        <span class="hljs-string">"----------------------------------------------------------------------------------------------"</span>,
        <span class="hljs-string">""</span>,
        <span class="hljs-string">"Press any key to start the game..."</span>,
        <span class="hljs-string">""</span>,
    ]

    <span class="hljs-comment"># Render and display the introduction text</span>
    y_position = HEIGHT // <span class="hljs-number">4</span>
    <span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> intro_text:
        text = font.render(line, <span class="hljs-literal">True</span>, WHITE)
        text_rect = text.get_rect(center=(WIDTH // <span class="hljs-number">2</span>, y_position))
        screen.blit(text, text_rect)
        y_position += FONT_SIZE

    pygame.display.flip()

    <span class="hljs-comment"># Wait for a key press to start the game</span>
    wait_for_key()

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wait_for_key</span>():</span>
    waiting = <span class="hljs-literal">True</span>
    <span class="hljs-keyword">while</span> waiting:
        <span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pygame.event.get():
            <span class="hljs-keyword">if</span> event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            <span class="hljs-keyword">elif</span> event.type == pygame.KEYDOWN:
                waiting = <span class="hljs-literal">False</span>
</code></pre>
<p><strong>Code explanation:</strong></p>
<ul>
<li><p>The <code>start_screen</code> function takes a <code>screen</code> parameter, which is the Pygame window where you'll display our introduction. It sets up some constants for screen dimensions, colors, and font size.</p>
</li>
<li><p>The screen is filled with a black background.</p>
</li>
<li><p>An introduction text is defined in <code>intro_text</code>, providing a welcome message, instructions, and a prompt to start the game.</p>
</li>
<li><p>Using Pygame's font functionality, the text is rendered and displayed on the screen.</p>
</li>
<li><p>The function then waits for a key press to start the game by calling the <code>wait_for_key</code> function.</p>
</li>
<li><p>The <code>wait_for_key</code> function loops until a key is pressed or the user closes the window. If a key is pressed, the loop exits, and the introduction screen disappears.</p>
</li>
</ul>
<p>So, by now, you should have something like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/12/image-68.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Start screen</em></p>
<h3 id="heading-how-to-build-text-on-the-screen-function">How to build text on the screen function</h3>
<p>Now, let's look at the following block of code:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">show_text_on_screen</span>(<span class="hljs-params">screen, text, font_size, y_position</span>):</span>
    font = pygame.font.Font(<span class="hljs-literal">None</span>, font_size)
    text_render = font.render(text, <span class="hljs-literal">True</span>, (<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>))
    text_rect = text_render.get_rect(center=(WIDTH // <span class="hljs-number">2</span>, y_position))
    screen.blit(text_render, text_rect)
</code></pre>
<p>This code defines a function <code>show_text_on_screen</code> that displays a text message on the Pygame window. Let's break it down:</p>
<ul>
<li>The function takes four parameters:</li>
</ul>
<ol>
<li><p><code>screen</code>: The Pygame window where the text will be displayed</p>
</li>
<li><p><code>text</code>: The message or text string to be displayed.</p>
</li>
<li><p><code>font_size</code>: The size of the font for the text.</p>
</li>
<li><p><code>y_position</code>: The vertical position on the screen where the text will be centered.</p>
</li>
</ol>
<ul>
<li><p>It creates a Pygame font object with the specified size.</p>
</li>
<li><p>Using the font object, it renders the text with white color (<code>(255, 255, 255)</code> represents RGB values for white).</p>
</li>
<li><p>The <code>get_rect</code> method is used to get the rectangular area that encloses the rendered text. The center of this rectangle is set to be at the horizontal center and the specified vertical position.</p>
</li>
<li><p>Finally, the rendered text is blitted (drawn) onto the screen at the specified position.</p>
</li>
<li><p>This function provides a convenient way to display text messages on the screen with a specified font size and vertical position.</p>
</li>
</ul>
<h3 id="heading-how-to-code-the-game-over-screen">How to code the "Game Over" screen</h3>
<p>Now, you will code a game over screen, so keep going and check the next block of code:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">game_over_screen</span>(<span class="hljs-params">screen</span>):</span>
    screen.fill((<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>))  <span class="hljs-comment"># Fill the screen with black</span>
    show_text_on_screen(screen, <span class="hljs-string">"Game Over"</span>, <span class="hljs-number">50</span>, HEIGHT // <span class="hljs-number">3</span>)
    show_text_on_screen(screen, <span class="hljs-string">f"Your final score: <span class="hljs-subst">{score}</span>"</span>, <span class="hljs-number">30</span>, HEIGHT // <span class="hljs-number">2</span>)
    show_text_on_screen(screen, <span class="hljs-string">"Press any key to exit..."</span>, <span class="hljs-number">20</span>, HEIGHT * <span class="hljs-number">2</span> // <span class="hljs-number">3</span>)
    pygame.display.flip()
    wait_for_key()
</code></pre>
<p>Picking apart the game over screen:</p>
<ul>
<li><p><code>screen.fill((0, 0, 0))</code>: Fills the entire Pygame window with a black background.</p>
</li>
<li><p><code>show_text_on_screen(screen, "Game Over", 50, HEIGHT // 3)</code>: Displays the text "Game Over" with a font size of 50, centered vertically at one-third of the screen height.</p>
</li>
<li><p><code>show_text_on_screen(screen, f"Your final score: {score}", 30, HEIGHT // 2)</code>: Displays the player's final score with a font size of 30, centered vertically at half of the screen height.</p>
</li>
<li><p><code>show_text_on_screen(screen, "Press any key to exit...", 20, HEIGHT * 2 // 3)</code>: Displays the instruction to press any key to exit with a font size of 20, centered vertically at two-thirds of the screen height.</p>
</li>
<li><p><code>pygame.display.flip()</code>: Updates the display to show the changes.</p>
</li>
<li><p><code>wait_for_key()</code>: Waits for a key press before proceeding, effectively making the screen stay until the player interacts.</p>
</li>
</ul>
<p>This function is used to show a game over screen with relevant information such as the final score and an exit instruction, something that by now should look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/12/image-67.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Game Over screen</em></p>
<h3 id="heading-you-win-how-to-code-the-victory-screen">You win! How to code the victory screen :)</h3>
<p>And last but not least, you have the victory screen, so let's take a look at the screen code:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">victory_screen</span>(<span class="hljs-params">screen</span>):</span>
    screen.fill((<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>))  <span class="hljs-comment"># Fill the screen with black</span>
    show_text_on_screen(screen, <span class="hljs-string">"Congratulations!"</span>, <span class="hljs-number">50</span>, HEIGHT // <span class="hljs-number">3</span>)
    show_text_on_screen(screen, <span class="hljs-string">f"You've completed all levels with a score of <span class="hljs-subst">{score}</span>"</span>, <span class="hljs-number">30</span>, HEIGHT // <span class="hljs-number">2</span>)
    show_text_on_screen(screen, <span class="hljs-string">"Press any key to exit..."</span>, <span class="hljs-number">20</span>, HEIGHT * <span class="hljs-number">2</span> // <span class="hljs-number">3</span>)
    pygame.display.flip()
    wait_for_key()
</code></pre>
<p>Code explanation:</p>
<ul>
<li><p><code>screen.fill((0, 0, 0))</code>: Fills the entire Pygame window with a black background.</p>
</li>
<li><p><code>show_text_on_screen(screen, "Congratulations!", 50, HEIGHT // 3)</code>: Displays the text "Congratulations!" with a font size of 50, centered vertically at one-third of the screen height.</p>
</li>
<li><p><code>show_text_on_screen(screen, f"You've completed all levels with a score of {score}", 30, HEIGHT // 2)</code>: Displays a congratulatory message with the player's final score and a font size of 30, centered vertically at half of the screen height.</p>
</li>
<li><p><code>show_text_on_screen(screen, "Press any key to exit...", 20, HEIGHT * 2 // 3)</code>: Displays the instruction to press any key to exit with a font size of 20, centered vertically at two-thirds of the screen height.</p>
</li>
<li><p><code>pygame.display.flip()</code>: Updates the display to show the changes.</p>
</li>
<li><p><code>wait_for_key()</code>: Waits for a key press before proceeding, effectively making the screen stay until the player interacts.</p>
</li>
</ul>
<p>This function is used to show a victory screen with a congratulatory message and the player's final score.</p>
<p>So now you have the start of the game, the screens you will use to communicate with the player.</p>
<h3 id="heading-lets-build-some-sprites">Let's build some sprites</h3>
<p>Next up, you will code the base parameters such as the window size, sprites (the UFO and cow), colors, stars, and so on.</p>
<p>Take a look to the following block of code:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Load spaceship and cow images</span>
ovni = pygame.image.load(<span class="hljs-string">"ovni.png"</span>)
cow = pygame.image.load(<span class="hljs-string">"cow.png"</span>)

<span class="hljs-comment"># Resize images if needed</span>
ovni = pygame.transform.scale(ovni, (<span class="hljs-number">50</span>, <span class="hljs-number">50</span>))
cow = pygame.transform.scale(cow, (<span class="hljs-number">40</span>, <span class="hljs-number">40</span>))

<span class="hljs-comment"># Initialize Pygame</span>
pygame.init()

<span class="hljs-comment"># Constants</span>
WIDTH, HEIGHT = <span class="hljs-number">800</span>, <span class="hljs-number">600</span>
FPS = <span class="hljs-number">60</span>
BLACK = (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
WHITE = (<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>)
RED = (<span class="hljs-number">255</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
YELLOW = (<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">0</span>)
GRAY = (<span class="hljs-number">169</span>, <span class="hljs-number">169</span>, <span class="hljs-number">169</span>)
ORANGE = (<span class="hljs-number">255</span>, <span class="hljs-number">165</span>, <span class="hljs-number">0</span>)
LIGHT_BLUE = (<span class="hljs-number">173</span>, <span class="hljs-number">216</span>, <span class="hljs-number">230</span>)  <span class="hljs-comment"># Light blue color for the level indicator</span>
SHIP_GREEN = (<span class="hljs-number">0</span>, <span class="hljs-number">255</span>, <span class="hljs-number">0</span>)  <span class="hljs-comment"># Green color for the ship</span>
GRASS_GREEN = (<span class="hljs-number">0</span>, <span class="hljs-number">100</span>, <span class="hljs-number">0</span>)  <span class="hljs-comment"># Darker green color for the grass</span>
STAR_COUNT = int(WIDTH * HEIGHT * <span class="hljs-number">0.001</span>)
</code></pre>
<p>Code explanation:</p>
<ul>
<li><p><code>ovni = pygame.image.load("ovni.png")</code> and <code>cow = pygame.image.load("cow.png")</code>: Load the spaceship (ovni) and cow images from files.</p>
</li>
<li><p><code>ovni = pygame.transform.scale(ovni, (50, 50))</code> and <code>cow = pygame.transform.scale(cow, (40, 40))</code>: Resize the images if needed. In this case, the spaceship is resized to a width and height of 50 pixels, and the cow is resized to a width and height of 40 pixels.</p>
</li>
<li><p><code>pygame.init()</code>: Initializes Pygame.</p>
</li>
</ul>
<p>Constants:</p>
<ul>
<li><p><code>WIDTH, HEIGHT = 800, 600</code>: Sets the width and height of the game window.</p>
</li>
<li><p><code>FPS = 60</code>: Sets the frames per second for the game.</p>
</li>
<li><p>Colors: <code>BLACK, WHITE, RED, YELLOW, GRAY, ORANGE</code>: Define color constants using RGB values. <code>LIGHT_BLUE, SHIP_GREEN, GRASS_GREEN</code>: Additional color constants for specific elements.</p>
</li>
<li><p><code>STAR_COUNT</code>: Calculates the number of stars based on the window size.</p>
</li>
</ul>
<p>This section handles the setup of images for the spaceship and cow, adjusting their sizes as needed. It also establishes constants for various aspects of the game.</p>
<p>The subsequent steps involve creating the game window and implementing player controls, collision detection, and other essential elements of the gameplay.</p>
<h3 id="heading-how-to-code-the-game-window-and-set-up-the-fps">How to code the game window and set up the FPS</h3>
<pre><code class="lang-python"><span class="hljs-comment"># Create the game window</span>
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(<span class="hljs-string">"Alien Abduction Game"</span>)

<span class="hljs-comment"># Clock to control the frame rate</span>
clock = pygame.time.Clock()

<span class="hljs-comment"># Player (alien spaceship)</span>
player_rect = pygame.Rect(WIDTH // <span class="hljs-number">2</span> - <span class="hljs-number">25</span>, <span class="hljs-number">10</span>, <span class="hljs-number">50</span>, <span class="hljs-number">50</span>)
player_speed = <span class="hljs-number">5</span>

<span class="hljs-comment"># List to store targets (animals)</span>
targets = []

<span class="hljs-comment"># Set initial score</span>
score = <span class="hljs-number">0</span>

<span class="hljs-comment"># Font for displaying the score, level, and timer</span>
font = pygame.font.Font(<span class="hljs-literal">None</span>, <span class="hljs-number">36</span>)

<span class="hljs-comment"># Flag to track if spacebar is pressed</span>
space_pressed = <span class="hljs-literal">False</span>

<span class="hljs-comment"># List to store stars</span>
stars = [{<span class="hljs-string">'x'</span>: random.randint(<span class="hljs-number">0</span>, WIDTH), <span class="hljs-string">'y'</span>: random.randint(<span class="hljs-number">0</span>, HEIGHT), <span class="hljs-string">'size'</span>: random.randint(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>),
          <span class="hljs-string">'color'</span>: LIGHT_BLUE} <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> range(STAR_COUNT)]

<span class="hljs-comment"># Grassy area at the bottom</span>
grass_rect = pygame.Rect(<span class="hljs-number">0</span>, HEIGHT - <span class="hljs-number">40</span>, WIDTH, <span class="hljs-number">40</span>)

<span class="hljs-comment"># Level and Countdown Variables</span>
current_level = <span class="hljs-number">1</span>
abduction_target = <span class="hljs-number">10</span>  <span class="hljs-comment"># Initial target</span>
countdown_timer = <span class="hljs-number">60</span>  <span class="hljs-comment"># Initial countdown timer in seconds</span>
current_score = <span class="hljs-number">0</span>  <span class="hljs-comment"># New counter to track the score for the current level</span>

<span class="hljs-comment"># Counter to control the pace of target appearances</span>
target_spawn_counter = <span class="hljs-number">0</span>
TARGET_SPAWN_RATE = max(<span class="hljs-number">30</span>, <span class="hljs-number">120</span> - (current_level * <span class="hljs-number">90</span>))  <span class="hljs-comment"># Adjust the rate based on the current level</span>

<span class="hljs-comment"># List of colors for each level</span>
level_colors = [
    LIGHT_BLUE,
    ORANGE,
    RED,
    YELLOW,
    GRAY,
    (<span class="hljs-number">0</span>, <span class="hljs-number">255</span>, <span class="hljs-number">0</span>),  <span class="hljs-comment"># Green</span>
    (<span class="hljs-number">255</span>, <span class="hljs-number">0</span>, <span class="hljs-number">255</span>),  <span class="hljs-comment"># Purple</span>
    (<span class="hljs-number">0</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>),  <span class="hljs-comment"># Cyan</span>
    (<span class="hljs-number">255</span>, <span class="hljs-number">165</span>, <span class="hljs-number">0</span>),  <span class="hljs-comment"># Orange</span>
    (<span class="hljs-number">128</span>, <span class="hljs-number">0</span>, <span class="hljs-number">128</span>),  <span class="hljs-comment"># Indigo</span>
]
</code></pre>
<p>Code explanation:</p>
<p><strong>Creating the Game Window:</strong></p>
<ul>
<li><p><code>screen = pygame.display.set_mode((WIDTH, HEIGHT))</code>: This line creates the game window with the specified width and height.</p>
</li>
<li><p><code>pygame.display.set_caption("Alien Abduction Game")</code>: Sets the title or caption of the game window.</p>
</li>
</ul>
<p><strong>Clock for Frame Rate:</strong></p>
<ul>
<li><code>clock = pygame.time.Clock()</code>: Initializes a clock object to control the frame rate of the game.</li>
</ul>
<p><strong>Player (Alien Spaceship):</strong></p>
<ul>
<li><p><code>player_rect = pygame.Rect(WIDTH // 2 - 25, 10, 50, 50)</code>: Defines a rectangular area for the player (alien spaceship) at the top center of the window.</p>
</li>
<li><p><code>player_speed = 5</code>: Sets the speed at which the player can move.</p>
</li>
</ul>
<p><strong>Targets (Animals):</strong></p>
<ul>
<li><code>targets = []</code>: Initializes an empty list to store targets (animals).</li>
</ul>
<p><strong>Score:</strong></p>
<ul>
<li><code>score = 0</code>: Initializes the score to zero.</li>
</ul>
<p><strong>Font for Displaying Text:</strong></p>
<ul>
<li><code>font = pygame.font.Font(None, 36)</code>: Creates a font object for displaying score, level, and timer.</li>
</ul>
<p><strong>Spacebar Pressed Flag:</strong></p>
<ul>
<li><code>space_pressed = False</code>: Initializes a flag to track whether the spacebar is pressed.</li>
</ul>
<p><strong>Stars:</strong></p>
<ul>
<li><code>stars = [...]</code>: Creates a list of stars with random positions, sizes, and colors. This is used to create a starry background.</li>
</ul>
<p><strong>Grassy Area at the Bottom:</strong></p>
<ul>
<li><code>grass_rect = pygame.Rect(0, HEIGHT - 40, WIDTH, 40)</code>: Defines a rectangular area at the bottom for a grassy background.</li>
</ul>
<p><strong>Level and Countdown Variables:</strong></p>
<ul>
<li><p><code>current_level = 1</code>: Initializes the game at level 1.</p>
</li>
<li><p><code>abduction_target = 10</code>: Sets the initial abduction target.</p>
</li>
<li><p><code>countdown_timer = 60</code>: Sets the initial countdown timer in seconds.</p>
</li>
<li><p><code>current_score = 0</code>: Initializes a counter to track the score for the current level.</p>
</li>
</ul>
<p><strong>Target Spawn Counter:</strong></p>
<ul>
<li><code>target_spawn_counter = 0</code>: Initializes a counter to control the pace of target appearances.</li>
</ul>
<p><strong>Target Spawn Rate:</strong></p>
<ul>
<li><code>TARGET_SPAWN_RATE = max(30, 120 - (current_level * 90))</code>: Adjusts the rate of target appearance based on the current level.</li>
</ul>
<p><strong>Colors for Each Level:</strong></p>
<ul>
<li><code>level_colors = [...]</code>: Defines a list of colors for each level, which will be used in the game.</li>
</ul>
<p>These variables and settings establish the initial state of the game, defining the player, targets, scoring system, and visual elements. Your game will evolve and respond to player actions based on these initial conditions.</p>
<h3 id="heading-how-to-code-the-game-mechanisms">How to code the game mechanisms</h3>
<p>Take a look at these functions that you will use to run the game. Try to code it yourself before you copy and paste the code. This will help you understand the thinking process between the mechanics and the actual code.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Function to display the start screen</span>
start_screen(screen)

<span class="hljs-comment"># Main game loop</span>
running = <span class="hljs-literal">True</span>
game_started = <span class="hljs-literal">False</span>  <span class="hljs-comment"># Flag to track whether the game has started</span>

<span class="hljs-keyword">while</span> running:
    <span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pygame.event.get():
        <span class="hljs-keyword">if</span> event.type == pygame.QUIT:
            running = <span class="hljs-literal">False</span>
        <span class="hljs-keyword">elif</span> event.type == pygame.KEYDOWN:
            <span class="hljs-keyword">if</span> game_started:
                game_started = <span class="hljs-literal">True</span>  <span class="hljs-comment"># Set the flag to True to avoid calling start_screen repeatedly</span>
                <span class="hljs-keyword">continue</span>  <span class="hljs-comment"># Skip the rest of the loop until the game has started</span>
            <span class="hljs-keyword">elif</span> event.key == pygame.K_SPACE:
                space_pressed = <span class="hljs-literal">True</span>
        <span class="hljs-keyword">elif</span> event.type == pygame.KEYUP <span class="hljs-keyword">and</span> event.key == pygame.K_SPACE:
            space_pressed = <span class="hljs-literal">False</span>

    keys = pygame.key.get_pressed()

    <span class="hljs-comment"># Move the player</span>
    player_rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * player_speed
    player_rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * player_speed

    <span class="hljs-comment"># Ensure the player stays within the screen boundaries</span>
    player_rect.x = max(<span class="hljs-number">0</span>, min(player_rect.x, WIDTH - player_rect.width))
    player_rect.y = max(<span class="hljs-number">0</span>, min(player_rect.y, HEIGHT - player_rect.height))

    <span class="hljs-comment"># Spawn a new target based on the counter</span>
    target_spawn_counter += <span class="hljs-number">1</span>
    <span class="hljs-keyword">if</span> target_spawn_counter &gt;= TARGET_SPAWN_RATE:
        target_rect = pygame.Rect(random.randint(<span class="hljs-number">0</span>, WIDTH - <span class="hljs-number">20</span>), HEIGHT - <span class="hljs-number">50</span>, <span class="hljs-number">50</span>, <span class="hljs-number">50</span>)
        targets.append(target_rect)
        target_spawn_counter = <span class="hljs-number">0</span>

    <span class="hljs-comment"># Update star glow animation and color for the current level</span>
    <span class="hljs-keyword">for</span> star <span class="hljs-keyword">in</span> stars:
        star[<span class="hljs-string">'size'</span>] += <span class="hljs-number">0.05</span>
        <span class="hljs-keyword">if</span> star[<span class="hljs-string">'size'</span>] &gt; <span class="hljs-number">3</span>:
            star[<span class="hljs-string">'size'</span>] = <span class="hljs-number">1</span>
        star[<span class="hljs-string">'color'</span>] = level_colors[current_level - <span class="hljs-number">1</span>]
</code></pre>
<p>Code explanation:</p>
<p><strong>Display Start Screen:</strong></p>
<ul>
<li><code>start_screen(screen)</code>: Calls the <code>start_screen</code> function to display the initial start screen.</li>
</ul>
<p><strong>Main Game Loop:</strong></p>
<ul>
<li><p><code>running = True</code>: Initializes a flag to control the main game loop.</p>
</li>
<li><p><code>game_started = False</code>: Initializes a flag to track whether the game has started.</p>
</li>
</ul>
<p><strong>Event Handling:</strong></p>
<ul>
<li><p>The loop iterates through pygame events to check for user input and window events.</p>
</li>
<li><p><code>pygame.QUIT</code>: If the user closes the game window, the <code>running</code> flag is set to <code>False</code>, exiting the game.</p>
</li>
<li><p><code>pygame.KEYDOWN</code>: If a key is pressed:</p>
</li>
<li><p>If the game has started (<code>game_started</code> is <code>True</code>), the loop is skipped, preventing repeated calls to the start screen.</p>
</li>
<li><p>If the spacebar (<code>pygame.K_SPACE</code>) is pressed and the game hasn't started, <code>space_pressed</code> is set to <code>True</code>.</p>
</li>
<li><p><code>pygame.KEYUP</code>: If a key is released:</p>
</li>
<li><p>If the released key is the spacebar, <code>space_pressed</code> is set to <code>False</code>.</p>
</li>
</ul>
<p><strong>Player Movement:</strong></p>
<ul>
<li><p>The player's position (<code>player_rect</code>) is updated based on arrow key input.</p>
</li>
<li><p>The player's x-coordinate is adjusted by the difference between the right and left arrow keys multiplied by the player's speed.</p>
</li>
<li><p>The player's y-coordinate is adjusted by the difference between the down and up arrow keys multiplied by the player's speed.</p>
</li>
<li><p>The player's position is constrained to stay within the screen boundaries.</p>
</li>
</ul>
<p><strong>Spawn Targets:</strong></p>
<ul>
<li><p>A counter (<code>target_spawn_counter</code>) is incremented in each iteration.</p>
</li>
<li><p>If the counter exceeds the target spawn rate (<code>TARGET_SPAWN_RATE</code>), a new target is created at a random x-coordinate within the screen width and a fixed y-coordinate above the screen's bottom edge.</p>
</li>
<li><p>The target is represented by a rectangle (<code>target_rect</code>) added to the <code>targets</code> list.</p>
</li>
<li><p>The counter is reset.</p>
</li>
</ul>
<p><strong>Update Star Animation:</strong></p>
<ul>
<li><p>The animation of stars is updated by increasing their size. If the size exceeds 3, it is reset to 1.</p>
</li>
<li><p>The color of each star is set based on the current level.</p>
</li>
</ul>
<p>This part of the code handles user input, player movement, target spawning, and updates the star animation. It's a crucial component of the game loop, ensuring player interaction and progression in the game.</p>
<h3 id="heading-how-to-render-the-elements-of-the-game-player-assets-and-so-on">How to render the elements of the game (player, assets, and so on)</h3>
<p>The next part of the code handles the rendering of game elements on the screen, including stars, the grassy area, the player's spaceship, and targets. It also manages the drawing of the tractor beam and handles collisions between the tractor beam and targets.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Clear the screen</span>
    screen.fill(BLACK)

    <span class="hljs-comment"># Draw stars with level-based color</span>
    <span class="hljs-keyword">for</span> star <span class="hljs-keyword">in</span> stars:
        pygame.draw.circle(screen, star[<span class="hljs-string">'color'</span>], (star[<span class="hljs-string">'x'</span>], star[<span class="hljs-string">'y'</span>]), int(star[<span class="hljs-string">'size'</span>]))

    <span class="hljs-comment"># Draw the grassy area</span>
    pygame.draw.rect(screen, GRASS_GREEN, grass_rect)

    <span class="hljs-comment"># Draw the player and targets</span>
    screen.blit(ovni, player_rect)

    <span class="hljs-keyword">for</span> target <span class="hljs-keyword">in</span> targets:
        screen.blit(cow, target)

    <span class="hljs-comment"># Draw the tractor beam when spacebar is pressed</span>
    <span class="hljs-keyword">if</span> space_pressed:
        tractor_beam_rect = pygame.Rect(player_rect.centerx - <span class="hljs-number">2</span>, player_rect.centery, <span class="hljs-number">4</span>, HEIGHT - player_rect.centery)
        pygame.draw.line(screen, YELLOW, (player_rect.centerx, player_rect.centery),
                         (player_rect.centerx, HEIGHT), <span class="hljs-number">2</span>)

        <span class="hljs-comment"># Check for collisions with targets</span>
        <span class="hljs-keyword">for</span> target <span class="hljs-keyword">in</span> targets[:]:
            <span class="hljs-keyword">if</span> tractor_beam_rect.colliderect(target):
                <span class="hljs-comment"># Change the color of the tractor beam to yellow</span>
                pygame.draw.line(screen, YELLOW, (player_rect.centerx, player_rect.centery),
                                 (player_rect.centerx, target.bottom), <span class="hljs-number">2</span>)
                <span class="hljs-comment"># Change the color of the target to red</span>
                pygame.draw.rect(screen, RED, target)
                targets.remove(target)
                current_score += <span class="hljs-number">1</span>
                score += <span class="hljs-number">1</span>
</code></pre>
<p>Code explanation:</p>
<p><strong>Clear the Screen:</strong></p>
<ul>
<li><code>screen.fill(BLACK)</code>: Fills the entire screen with a black color, effectively clearing the previous frame.</li>
</ul>
<p><strong>Draw Stars:</strong></p>
<ul>
<li><p>Iterates through the list of stars (<code>stars</code>) and draws each star as a circle on the screen.</p>
</li>
<li><p>The circle's color is determined by the star's color attribute, and its position and size are based on the star's x and y coordinates and size.</p>
</li>
</ul>
<p><strong>Draw Grassy Area:</strong></p>
<ul>
<li><code>pygame.draw.rect(screen, GRASS_GREEN, grass_rect)</code>: Draws a rectangle representing the grassy area at the bottom of the screen. The color is set to <code>GRASS_GREEN</code>.</li>
</ul>
<p><strong>Draw Player and Targets:</strong></p>
<ul>
<li><p><code>screen.blit(ovni, player_rect)</code>: Draws the player's spaceship (<code>ovni</code>) on the screen at the position specified by <code>player_rect</code>.</p>
</li>
<li><p><code>for target in targets: screen.blit(cow, target)</code>: Draws each target (cow) from the <code>targets</code> list on the screen at their respective positions.</p>
</li>
</ul>
<p><strong>Draw Tractor Beam (When Spacebar is Pressed):</strong></p>
<ul>
<li><p>Checks if the spacebar is pressed (<code>space_pressed</code> is <code>True</code>).</p>
</li>
<li><p>If true, <code>tractor_beam_rect</code>: Creates a rectangle representing the tractor beam based on the player's position.</p>
</li>
<li><p><code>pygame.draw.line</code>: Draws a yellow line representing the tractor beam from the player's center to the bottom of the screen.</p>
</li>
<li><p>Checks for collisions between the tractor beam and targets.</p>
</li>
<li><p>If a collision is detected, the target is removed, the tractor beam color changes to yellow, and the target's color changes to red.</p>
</li>
<li><p>The score is updated.</p>
</li>
</ul>
<h3 id="heading-game-logic">Game logic</h3>
<p>Now you are almost done – but first you need to add some logic in the game like the countdown timer which gives you one minute per level. You also need to draw like a navbar, a simple one with four elements containing the general score, level indicator, timer and abductions.</p>
<p>Here's how we'll do all that:</p>
<pre><code class="lang-python">info_line_y = <span class="hljs-number">10</span>  <span class="hljs-comment"># Adjust the vertical position as needed</span>
    info_spacing = <span class="hljs-number">75</span>  <span class="hljs-comment"># Adjust the spacing as needed</span>

    <span class="hljs-comment"># Draw the score in an orange rectangle at the top left</span>
    score_text = font.render(<span class="hljs-string">f"Score: <span class="hljs-subst">{score}</span>"</span>, <span class="hljs-literal">True</span>, WHITE)
    score_rect = score_text.get_rect(topleft=(<span class="hljs-number">10</span>, info_line_y))
    pygame.draw.rect(screen, ORANGE, score_rect.inflate(<span class="hljs-number">10</span>, <span class="hljs-number">5</span>))
    screen.blit(score_text, score_rect)

    <span class="hljs-comment"># Draw the level indicator in a light-blue rectangle at the top left (next to the score)</span>
    level_text = font.render(<span class="hljs-string">f"Level: <span class="hljs-subst">{current_level}</span>"</span>, <span class="hljs-literal">True</span>, WHITE)
    level_rect = level_text.get_rect(topleft=(score_rect.topright[<span class="hljs-number">0</span>] + info_spacing, info_line_y))
    pygame.draw.rect(screen, LIGHT_BLUE, level_rect.inflate(<span class="hljs-number">10</span>, <span class="hljs-number">5</span>))
    screen.blit(level_text, level_rect)

    <span class="hljs-comment"># Draw the countdown timer in a red rectangle at the top left (next to the level)</span>
    timer_text = font.render(<span class="hljs-string">f"Time: <span class="hljs-subst">{int(countdown_timer)}</span>"</span>, <span class="hljs-literal">True</span>, WHITE)
    timer_rect = timer_text.get_rect(topleft=(level_rect.topright[<span class="hljs-number">0</span>] + info_spacing, info_line_y))
    pygame.draw.rect(screen, RED, timer_rect.inflate(<span class="hljs-number">10</span>, <span class="hljs-number">5</span>))
    screen.blit(timer_text, timer_rect)

    <span class="hljs-comment"># Draw the targets to acquire for the current level in a gray rectangle at the top left (next to the timer)</span>
    targets_text = font.render(<span class="hljs-string">f"Abductions: <span class="hljs-subst">{current_score}</span>/<span class="hljs-subst">{abduction_target}</span>"</span>, <span class="hljs-literal">True</span>, WHITE)
    targets_rect = targets_text.get_rect(topleft=(timer_rect.topright[<span class="hljs-number">0</span>] + info_spacing, info_line_y))
    pygame.draw.rect(screen, GRAY, targets_rect.inflate(<span class="hljs-number">10</span>, <span class="hljs-number">5</span>))
    screen.blit(targets_text, targets_rect)

    <span class="hljs-comment"># Update the display</span>
    pygame.display.flip()

    <span class="hljs-comment"># Cap the frame rate</span>
    clock.tick(FPS)
</code></pre>
<p>Code explanation:</p>
<p><strong>Info Line Positioning:</strong></p>
<ul>
<li><p><code>info_line_y = 10</code>: Specifies the vertical position for the information line at the top of the screen.</p>
</li>
<li><p><code>info_spacing = 75</code>: Sets the spacing between different pieces of information on the line.</p>
</li>
</ul>
<p><strong>Draw Score:</strong></p>
<ul>
<li><p><code>score_text</code>: Renders the score text using the game font.</p>
</li>
<li><p><code>score_rect</code>: Retrieves the rectangle that encloses the rendered score text.</p>
</li>
<li><p><code>pygame.draw.rect</code>: Draws an orange rectangle around the score text, creating a background.</p>
</li>
<li><p><code>screen.blit(score_text, score_rect)</code>: Blits (renders) the score text onto the screen.</p>
</li>
</ul>
<p><strong>Draw Level Indicator:</strong></p>
<ul>
<li><p><code>level_text</code>: Renders the level indicator text.</p>
</li>
<li><p><code>level_rect</code>: Retrieves the rectangle for the level indicator text.</p>
</li>
<li><p><code>pygame.draw.rect</code>: Draws a light-blue rectangle around the level indicator text.</p>
</li>
<li><p><code>screen.blit(level_text, level_rect)</code>: Blits the level indicator text onto the screen.</p>
</li>
</ul>
<p><strong>Draw Countdown Timer:</strong></p>
<ul>
<li><p><code>timer_text</code>: Renders the countdown timer text.</p>
</li>
<li><p><code>timer_rect</code>: Retrieves the rectangle for the countdown timer text.</p>
</li>
<li><p><code>pygame.draw.rect</code>: Draws a red rectangle around the countdown timer text.</p>
</li>
<li><p><code>screen.blit(timer_text, timer_rect)</code>: Blits the countdown timer text onto the screen.</p>
</li>
</ul>
<p><strong>Draw Abduction Targets:</strong></p>
<ul>
<li><p><code>targets_text</code>: Renders the text indicating the number of abductions required for the current level.</p>
</li>
<li><p><code>targets_rect</code>: Retrieves the rectangle for the abduction targets text.</p>
</li>
<li><p><code>pygame.draw.rect</code>: Draws a gray rectangle around the abduction targets text.</p>
</li>
<li><p><code>screen.blit(targets_text, targets_rect)</code>: Blits the abduction targets text onto the screen.</p>
</li>
</ul>
<p><strong>Update Display:</strong></p>
<ul>
<li><code>pygame.display.flip()</code>: Updates the display to reflect the changes made in this iteration.</li>
</ul>
<p><strong>Frame Rate Cap:</strong></p>
<ul>
<li><code>clock.tick(FPS)</code>: Caps the frame rate to the specified frames per second (<code>FPS</code>). This ensures that the game runs at a consistent speed across different systems.</li>
</ul>
<p>And now we're ready for the last code block. It contains the countdown timer logic that's related to the level progression logic and the abductions reset. This means that you need to restart the abductions made by the player to zero every time you level up, for example:</p>
<ul>
<li><p>Level 1: 0/10 abductions</p>
</li>
<li><p>Level 2: 0/20 abductions</p>
</li>
</ul>
<p>And so on – the maximum abductions quota is 100 abductions in level 10, adding 10 abductions to the required quota for every level.</p>
<h3 id="heading-timer-logic-and-quitting-function">Timer logic and quitting function</h3>
<p>Check out the last block of code:</p>
<pre><code class="lang-python"> <span class="hljs-comment"># Countdown Timer Logic</span>
    countdown_timer -= <span class="hljs-number">1</span> / FPS  <span class="hljs-comment"># Decrease the timer based on the frame rate</span>
    <span class="hljs-keyword">if</span> countdown_timer &lt;= <span class="hljs-number">0</span>:
        <span class="hljs-comment"># Check if the player reached the abduction target for the current level</span>
        <span class="hljs-keyword">if</span> current_score &lt; abduction_target:
            <span class="hljs-comment"># Player didn't reach the abduction target, end the game</span>
            game_over_screen(screen)
            running = <span class="hljs-literal">False</span>
        <span class="hljs-keyword">else</span>:
            <span class="hljs-comment"># Move to the next level</span>
            current_level += <span class="hljs-number">1</span>
            <span class="hljs-keyword">if</span> current_level &lt;= <span class="hljs-number">10</span>:
                current_score = <span class="hljs-number">0</span>
                abduction_target = <span class="hljs-number">10</span> * current_level
                countdown_timer = <span class="hljs-number">60</span>  <span class="hljs-comment"># Reset the countdown timer for the next level</span>
                <span class="hljs-comment"># Reset the targets text for the next level</span>
                targets_text = font.render(<span class="hljs-string">f"Abductions: <span class="hljs-subst">{current_score}</span>/<span class="hljs-subst">{abduction_target}</span>"</span>, <span class="hljs-literal">True</span>, WHITE)
                targets_rect = targets_text.get_rect(topleft=(timer_rect.topright[<span class="hljs-number">0</span>] + info_spacing, info_line_y))

    <span class="hljs-comment"># Check if the player reached the abduction target for the current level</span>
    <span class="hljs-keyword">if</span> current_score &gt;= abduction_target:
        <span class="hljs-comment"># Move to the next level</span>
        current_level += <span class="hljs-number">1</span>
        <span class="hljs-keyword">if</span> current_level &lt;= <span class="hljs-number">10</span>:
            current_score = <span class="hljs-number">0</span>
            abduction_target = <span class="hljs-number">10</span> * current_level
            countdown_timer = <span class="hljs-number">60</span>  <span class="hljs-comment"># Reset the countdown timer for the next level</span>
            <span class="hljs-comment"># Reset the targets text for the next level</span>
            targets_text = font.render(<span class="hljs-string">f"Abductions: <span class="hljs-subst">{current_score}</span>/<span class="hljs-subst">{abduction_target}</span>"</span>, <span class="hljs-literal">True</span>, WHITE)
            targets_rect = targets_text.get_rect(topleft=(timer_rect.topright[<span class="hljs-number">0</span>] + info_spacing, info_line_y))
        <span class="hljs-keyword">else</span>:
            victory_screen(screen)
            running = <span class="hljs-literal">False</span>

<span class="hljs-comment"># Quit Pygame</span>
pygame.quit()
</code></pre>
<p><strong>Countdown Timer Logic:</strong></p>
<ul>
<li><code>countdown_timer -= 1 / FPS</code>: Decreases the countdown timer by the fraction <code>1 / FPS</code>. This adjustment ensures that the timer decreases uniformly based on the frame rate.</li>
</ul>
<p><strong>Check Timer Expiration:</strong></p>
<ul>
<li><code>if countdown_timer &lt;= 0:</code>: Checks if the countdown timer has reached or fallen below zero.</li>
</ul>
<p><strong>Player Didn't Reach Abduction Target:</strong></p>
<ul>
<li><p><code>if current_score &lt; abduction_target:</code>: Checks if the player's current score is less than the abduction target for the current level.</p>
</li>
<li><p><code>game_over_screen(screen)</code>: Calls the function to display the game over screen.</p>
</li>
<li><p><code>running = False</code>: Sets the <code>running</code> flag to <code>False</code>, terminating the game loop.</p>
</li>
</ul>
<p><strong>Move to Next Level:</strong></p>
<ul>
<li><p><code>else:</code>: Executes when the player has reached the abduction target for the current level.</p>
</li>
<li><p><code>current_level += 1</code>: Increments the current level.</p>
</li>
<li><p><code>if current_level &lt;= 10:</code>: Checks if there are more levels to proceed to.</p>
</li>
<li><p><code>current_score = 0</code>: Resets the current score for the next level.</p>
</li>
<li><p><code>abduction_target = 10 * current_level</code>: Sets the abduction target for the next level.</p>
</li>
<li><p><code>countdown_timer = 60</code>: Resets the countdown timer for the next level.</p>
</li>
<li><p>Resets the targets text for the next level.</p>
</li>
<li><p><code>targets_text = font.render(f"Abductions: {current_score}/{abduction_target}", True, WHITE)</code></p>
</li>
<li><p><code>targets_rect = targets_text.get_rect(topleft=(timer_rect.topright[0] + info_spacing, info_line_y))</code></p>
</li>
</ul>
<p><strong>Player Reached Abduction Target for All Levels:</strong></p>
<ul>
<li><p><code>else:</code>: Executes when the player has successfully completed all levels (reached level 10).</p>
</li>
<li><p><code>victory_screen(screen)</code>: Calls the function to display the victory screen.</p>
</li>
<li><p><code>running = False</code>: Sets the <code>running</code> flag to <code>False</code>, terminating the game loop.</p>
</li>
</ul>
<p><strong>Quit Pygame:</strong></p>
<ul>
<li><code>pygame.quit()</code>: Cleans up and quits Pygame after the game loop has ended.</li>
</ul>
<p>Now, you're done and you can run the game by typing this in your shell:</p>
<pre><code class="lang-bash">python aliend_aductions_game.py
</code></pre>
<p>If everything it's ok, you should see the start screen that will let you play after you press any key.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>We've covered a lot in this tutorial. You started out by setting up the development environment, installing PyGame, and designing the game concept with simple mechanics.</p>
<p>Then you followed the step-by-step process of coding the game, from initializing the environment to implementing player controls, collisions, and dynamic elements. You learned how to integrate images for the spaceship and cows, creating an engaging visual experience.</p>
<p>The game features various levels, each with its own challenge and a countdown timer, adding an element of urgency. You implemented a scoring system, level progression, and engaging graphics to enhance the player experience.</p>
<h3 id="heading-encouragement-for-further-learning">Encouragement for further learning:</h3>
<p>Congratulations on reaching this point! Game development is a dynamic and rewarding learning path, and there's always more to learn. As you continue, consider delving into more advanced topics such as:</p>
<ol>
<li><p><strong>Advanced graphics:</strong> enhance your game with more detailed graphics, animations, and visual effects.</p>
</li>
<li><p><strong>Sound and music:</strong> integrate sound effects and background music to elevate the gaming experience.</p>
</li>
<li><p><strong>Game physics:</strong> explore realistic movements and interactions within the game world.</p>
</li>
<li><p><strong>Multiplayer functionality:</strong> learn how to implement multiplayer features for a more interactive experience.</p>
</li>
<li><p><strong>Optimization techniques:</strong> dive into optimizing your code and graphics for better performance.</p>
</li>
</ol>
<p>If you reach this point, thanks for reading and I hope you enjoy this post as much as I enjoyed doing it, also you can check the original code of this game and download the assets from here:</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://github.com/jpromanonet/alien_abduction_game">https://github.com/jpromanonet/alien_abduction_game</a></div>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
