VOOZH about

URL: https://www.geeksforgeeks.org/python/balloon-archer-game-in-python-using-the-pygame-module/

⇱ Balloon Archer game in Python using the Pygame module - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Balloon Archer game in Python using the Pygame module

Last Updated : 23 Jul, 2025

Balloon Archer is a 2D game where the player uses a bow and arrow to pop balloons that float across the screen. The main theme of this game is to shoot down the balloons using arrows and on each successful attempt, the player earns a point and on each unsuccessful attempt, the player loses a life. If the player loses all 5 lives, then it's GAME OVER!

The player stands on the left half of the screen and must shoot the balloons that go upwards from bottom to top on the right half of the screen. If the arrow goes off the screen without hitting any balloons, then life is deducted. The player must navigate the archer to aim perfectly. This article will cover how to create such a game in Object Oriented Programming style (OOP) in Python.

Balloon Archer Game in Python

The Balloon Archer game has the following rules:

  1. The player controls the archer's movements using the arrow keys. The archer can move left, right, up, and down within the specified constrained region.
  2. The player can shoot the arrows using the spacebar. The arrows move horizontally from left to right.
  3. The player's score increases by one for each successful balloon hit.
  4. If an arrow goes off the screen without hitting any single balloon, the player loses a life.
  5. The goal is to achieve the maximum score possible before running out of lives.

Balloon Archer Game Controls

The Balloon Archer game can be played in Python using keyboard controls. These controls are as follows:

  1. Use left/right and up/down arrows to move the archer
  2. Use the space bar to shoot the arrow
  3. Use 'r' or 'q' to restart or quit the game

Functionalities of Balloon Archer Game in Python

The implementation of the balloon archer game using the Pygame module is divided into 6 parts. We will see the stepwise implementation of the game for a better understanding.

Initial Setup

Here, we initialize the module and define all the necessary global variables that are required all over the game. We will install the Pygame module for defining the colors, FPS, font, dimensions of the screen, etc, and the random module to randomly display the balloons on the screen within a specified region.

The pygame.init() method is used to initialize the necessary modules. Then we used the pygame.font.Font() method to create a font object and pass the font file and the size of the font as the parameter. The pygame.display.set_caption() method is used to set the title of the Pygame window. It set the title as whatever string is provided in the parameters. Then pygame.time.Clock() method is used to create a clock object to keep track of time.

The Helper Functions

The helper functions are user-defined functions that are used by the game manager. The populateBalloons() function is used for generating balloons at random positions on the right half of the screen. It takes in the balloons' width, height, and speed, counts them, and spaces them accordingly. The 'listOfBalloons' Python List stores the list of randomly generated balloons.

The gameOver() function is called when the player runs out of all the lives. This function asks for the player's input to restart or terminate the game. If the player presses 'r', then the game restarts, and if the player presses 'q', then the game is terminated. The font.render() method is used to render the text on a surface object. The screen.blit() method is used to draw the surface on this surface. It takes the rendered text and its size as parameters.

The next is event handling in Pygame. In this game, we will be using keyboard events only to play, quit, shoot arrows, and move up or down and right or left. The pygame.event.get() returns a list of current events. We are using the following event in this section:

  • pygame.QUIT: used to quit the Pygame window
  • pygame.KEYDOWN: detects if a keyboard key is pressed or not
  • pygame.K_r: checks if "r" key was pressed or not
  • pygame.K_q: checks if "q" key was pressed or not

The pygame.display.update() method as the name suggests, is used to update the content of the Pygame window.

The Archer Class

This class consists of all the methods needed to control the player. We first initialize the data members of the class, i.e., height, width, and speed. The pygame.transform.scale() method is used to change the size of the image. We will be using the "archer.png" image. The pygame.get_rect() method returns the rectangular object of the image. The position of the archer is controlled using the "archerRect".

👁 Image
archer.png

The display() function displays the transformed image on the screen whereas the update() function updates the archer position on the screen based on the xFac and yFac variables. These variables are obtained based on the key pressed by the user. This method also prevents the player from moving beyond the left edge and the center of the screen and contains the player's position to the left half of the screen. If the player presses the up/down arrow keys, then the value of yFac will be -1/1, and only the Y component of the player is changed. If the player presses the left/right arrow keys, then the value of xFac will be -1/1, and only the X component of the player will be changed.

The Balloon Class

This class consists of all the methods needed to control the balloons. The __init__() method takes the parameters such as the initial position, width, height, and speed of the balloon and loads an image to represent it. The position and the behavior of the balloon are controlled using the "balloonRect" after transforming the "balloon.png" image.

👁 balloon.png
balloon.png

The display() function displays the transformed image on the screen whereas the update() function is used to update the Y coordinate of the balloon. If the balloon goes beyond Y=0, then its position is set back to Y=HEIGHT.

The Arrow Class

This class consists of all the methods needed to control the arrows. The constructor takes in the parameters such as the arrow's initial position, width, height, and speed. It then loads an image to represent it. The position and behavior of the arrow are controlled using the "arrowRect" after transforming the "arrow.png" image. The image is loaded using the method.

👁 arrow.png
arrow.png

The display() function displays the transformed image on the screen whereas the update() function is used to update the arrow's position. It just increments the arrow's X coordinate by its speed. The updateHit() function is used to check if the arrow has hit any balloons on its way. If it hits a balloon, then the "hit" variable is set to 1 else it is set to 0. The getHit() function is used to return the value of the "hit" variable maintained by the arrow object. Based on this return value, the decision to reduce a life will be made.

The Game Manager

This is used to implement the game logic and control the game objects. The initial score is set to 0 and lives to 5. Then using the pygame.fill() method the background color of the screen is set to "green". We will use event handling for moving the archer up/down and left/right and also use the space bar for shooting arrows:

  • pygame.K_UP: checks if the "up" key was pressed or not
  • pygame.K_DOWN: checks if the "down" key was pressed or not
  • pygame.K_LEFT: checks if the "left" key was pressed or not
  • pygame.K_RIGHT: checks if the "right" key was pressed or not
  • pygame.K_SPACE: checks if "space-bar" was pressed or not

By using the following steps, you will be able to create the Balloon Archer game in Python using the Pygame module.

Complete Implementation of The Balloon Archer Game

Output:

👁 Python Balloon Archer Game
Python Balloon Archer Game
👁 Working of Python Balloon Archer Game
Working of Python Balloon Archer Game
Comment
Article Tags: