{"id":9705,"date":"2022-04-08T10:27:15","date_gmt":"2022-04-08T04:57:15","guid":{"rendered":"https:\/\/copyassignment.com\/?p=9705"},"modified":"2022-11-28T18:24:42","modified_gmt":"2022-11-28T12:54:42","slug":"balloon-shooter-game-using-python-pygame","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/balloon-shooter-game-using-python-pygame\/","title":{"rendered":"Balloon Shooter Game using Python PyGame"},"content":{"rendered":"\n<p>We all know the importance of the pygame library in terms of game development. We know that Pygame is a collection of Python modules for making video games. It is a collection of computer graphics and sound libraries for the Python programming language. Pygame is well suited to the development of client-side applications that may be packaged as a standalone executable.<\/p>\n\n\n\n<p>In our last article, we have covered the basics of the pygame library and did two projects. Therefore, in this article Balloon Shooter Game using Python PyGame, we are going to develop a fully working and full-fledged Ballon Shooter game. So, therefore, let&#8217;s get started and develop another project that will surely help you to brush up on your concepts.<\/p>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">What the game is all about?<\/h2>\n\n\n\n<p>The simple plot in this game is that there will be balloons moving all over the screen. The player will have a target like an arrow and with the help of a mouse, the player has to bust those moving ballons. There will be a counter for busted balloons and on busting each moving balloon successfully, the busting score will increase.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What we will use?<\/h2>\n\n\n\n<p>For the development of the Balloon Shooter Game using Python PyGame, we will use various pygame modules to add different functionalities to the game. We have to code for continuous movement of balloons, a shooting functionality, and updating the score every time the balloon is busted. All these functions can be done using various modules like draw, mouse, render, etc.<\/p>\n\n\n\n<p>For the balloon&#8217;s color and shape, we will make use of the draw module that provides various functions for shapes like ellipses, circles, etc. Therefore, in order to interact with the system and above all, to add functionality like quit game and resume the game, we will use the sys module and add functionality that is based on random events we will use the random library. Apart from these libraries, we will also use the math module.<\/p>\n\n\n\n<p>Now that we know the basic libraries and modules that we will use for the development of this game, let&#8217;s get started with some rules of the games and the flow of the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Game Rules to keep in mind:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It should be a single-player game.<\/li>\n\n\n\n<li>The game should be able to keep track of the total number of ballons busted.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Code flow:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#library\">Importing pygame, random, sys, and math<\/a><\/li>\n\n\n\n<li><a href=\"#init\">Pygame library initialization<\/a><\/li>\n\n\n\n<li><a href=\"#variables\">Declaring the required variables<\/a><\/li>\n\n\n\n<li><a href=\"#colours\">Declaring variables that store color codes<\/a><\/li>\n\n\n\n<li><a href=\"#class\">Defining balloon class<\/a><\/li>\n\n\n\n<li><a href=\"#location\">Show balloon location using pointer() function<\/a><\/li>\n\n\n\n<li><a href=\"#score\">Display the score<\/a><\/li>\n\n\n\n<li><a href=\"#close\" target=\"_blank\" rel=\"noreferrer noopener\">To close the game<\/a><\/li>\n\n\n\n<li><a href=\"#events\">Function to keep track of the events<\/a><\/li>\n\n\n\n<li><a href=\"#game\">Call to game() function<\/a><\/li>\n\n\n\n<li><a href=\"#completecode\">Complete code for Balloon Shooter Game using Python<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Getting started:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"library\">Importing pygame, random, sys, and math :<\/h3>\n\n\n\n<p>The very first task we will do in developing the Balloon Shooter Game using Python PyGame is importing all the required libraries and modules for this project. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pygame\nimport sys\nimport random\nfrom math import *<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"init\">Pygame library initialization:<\/h3>\n\n\n\n<p>Now after importing, we will initialize the pygame library. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pygame.init()<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>All imported pygame modules are initialized via pygame.init(). If a module fails, no exceptions will be thrown, but the overall number of successful and unsuccessful inits will be returned as a tuple. Individual modules can be individually initialized, however, <strong>pygame.init()<\/strong> initializes all imported pygame modules, which provides a convenient method to get things started. Individual module init() routines will throw exceptions if they fail.<\/p>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"variables\">Declaring the required variables<\/h3>\n\n\n\n<p>Now after importing, we will declare all the variables that we will use in the development of this project.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>width = 700\nheight = 600\n\ndisplay = pygame.display.set_mode((width, height))\npygame.display.set_caption(\"CopyAssignment - Balloon Shooter Game\")\nclock = pygame.time.Clock()\n\nmargin = 100\nlowerBound = 100\n\nscore = 0<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br><strong><em>Line 1 &amp; 2:<\/em><\/strong> The width and height variable will be used to set the size of the display screen<br><strong><em><span>On-L<\/span>ine 3<\/em><\/strong>: display basically stores the pygame screen that is shown using the set_mode() function.<br><strong><em>Line 4:<\/em><\/strong> This line sets the name of the screen to &#8220;CopyAssignment &#8211; Balloon Shooter Game&#8221;. This is done using the set_display function that is used to give the screen a name.<br><strong><em>Line 5:<\/em><\/strong> Keeps track of time.<br><strong><em>On-Line 6 &amp; 7:<\/em><\/strong> Stores the margin value<br><strong><em>Line 8:<\/em><\/strong> Keeps track of the score. This value is updated as the player bust the balloons.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"colours\">Declaring variables that store color codes:<\/h3>\n\n\n\n<p>These variables store the color code in RBG form<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>white = (230, 230, 230)\nlightBlue = (4, 27, 96)\nred = (231, 76, 60)\nlightGreen = (25, 111, 61)\ndarkGray = (40, 55, 71)\ndarkBlue = (64, 178, 239)\ngreen = (35, 155, 86)\nyellow = (244, 208, 63)\nblue = (46, 134, 193)\npurple = (155, 89, 182)\norange = (243, 156, 18)\n\nfont = pygame.font.SysFont(\"Arial\", 25)<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br><strong><em>Line 1 &amp; 11:<\/em><\/strong> Variable declaration with values.<br><strong><em>Line 12:<\/em><\/strong> This font variable holds the font that is used to display the number of balloons busted. This is done using the SysFont() function from the font module that takes in the name of the font and the size of the font.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"class\">Defining balloon class:<\/h3>\n\n\n\n<p>Now comes the actual coding for the game. First of all, we will define a class and in that class, we will write the logic for functions that can do the following functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A function that moves the balloon<\/li>\n\n\n\n<li>A function to show the balloon on screen<\/li>\n\n\n\n<li>To check if the balloon is busted or not:<\/li>\n\n\n\n<li>Function to reset balloons<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><em><strong>Class definition:<\/strong><\/em><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Balloon:\n    def __init__(self, speed):\n        self.a = random.randint(30, 40)\n        self.b = self.a + random.randint(0, 10)\n        self.x = random.randrange(margin, width - self.a - margin)\n        self.y = height - lowerBound\n        self.angle = 90\n        self.speed = -speed\n        self.proPool= &#091;-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]\n        self.length = random.randint(50, 100)\n        self.color = random.choice(&#091;red, green, purple, orange, yellow, blue])<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>In this class named Balloon, we have used the __init()__ function to assign the value for speed. We have used the self parameter as a reference to the current class to access variables. We have used to access variables that help to set the angle of the balloon, speed, etc.<\/p>\n\n\n\n<p>The last line, self.color holds the list of different colors that will be selected on a random basis and will set the color of the ballon to that selected color.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><em><strong>A function that moves the balloon<\/strong><\/em>:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def move(self):\n        direct = random.choice(self.proPool)\n\n        if direct == -1:\n            self.angle += -10\n        elif direct == 0:\n            self.angle += 0\n        else:\n            self.angle += 10\n\n        self.y += self.speed*sin(radians(self.angle))\n        self.x += self.speed*cos(radians(self.angle))\n\n        if (self.x + self.a &gt; width) or (self.x &lt; 0):\n            if self.y &gt; height\/5:\n                self.x -= self.speed*cos(radians(self.angle)) \n            else:\n                self.reset()\n        if self.y + self.b &lt; 0 or self.y &gt; height + 30:\n            self.reset()<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>This function holds the logic for the movement of the balloons. We have used the <strong>proPool <\/strong>which is a list of 0,1 and -1. On a random basis, any element is selected, and then using the conditionals the angle is set.<\/p>\n\n\n\n<p>The angle of the movement of the balloon is set using the <strong>sin <\/strong>function from the math module. <strong>angle <\/strong>is passed as a parameter that is set to 90.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><em>Logic to show the balloon on screen<\/em><\/strong>:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def show(self):\n        pygame.draw.line(display, darkBlue, (self.x + self.a\/2, self.y + self.b), (self.x + self.a\/2, self.y + self.b + self.length))\n        pygame.draw.ellipse(display, self.color, (self.x, self.y, self.a, self.b))\n        pygame.draw.ellipse(display, self.color, (self.x + self.a\/2 - 5, self.y + self.b - 3, 10, 10))<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>This function is responsible for displaying the balloons on the screen. The balloon tails are drawn using the line function. And the balloon is drawn using the ellipse function with display, color, and the position passed as parameters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><em>To check if the balloon is busted or not<\/em><\/strong>:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>    def burst(self):\n        global score\n        pos = pygame.mouse.get_pos()\n\n        if isonBalloon(self.x, self.y, self.a, self.b, pos):\n            score += 1\n            self.reset()<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>The above code check if the balloon is busted or not. <strong>global <\/strong>is a keyword used to set the variable as global.<\/p>\n\n\n\n<p>the pos variable takes the cursor position and then using conditionals this position is compared with the position of the balloon using the isonBalloon() function. The basic logic is that if the position of the cursor and balloon is matched and a MOUSEBUTTONDOWN event occurs then there should be an increase in score. And so If the position matches then the value in the score variable is increased by 1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><em>Function to reset balloons:<\/em><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>    def reset(self):\n        self.a = random.randint(30, 40)\n        self.b = self.a + random.randint(0, 10)\n        self.x = random.randrange(margin, width - self.a - margin)\n        self.y = height - lowerBound \n        self.angle = 90\n        self.speed -= 0.002\n        self.proPool = &#091;-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]\n        self.length = random.randint(50, 100)\n        self.color = random.choice(&#091;red, green, purple, orange, yellow, blue])<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>This actually resets the balloon&#8217;s position. When the balloons are busted and when the score is increased, the reset() function is called<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>balloons = &#091;]\nnoBalloon = 10\nfor i in range(noBalloon):\n    obj = Balloon(random.choice(&#091;1, 1, 2, 2, 2, 2, 3, 3, 3, 4]))\n    balloons.append(obj)\n\ndef isonBalloon(x, y, a, b, pos):\n    if (x &lt; pos&#091;0] &lt; x + a) and (y &lt; pos&#091;1] &lt; y + b):\n        return True\n    else:\n        return False<\/code><\/pre>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"location\">Show balloon location using pointer() function<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def pointer():\n    pos = pygame.mouse.get_pos()\n    r = 25\n    l = 20\n    color = lightGreen\n    for i in range(noBalloon):\n        if isonBalloon(balloons&#091;i].x, balloons&#091;i].y, balloons&#091;i].a, balloons&#091;i].b, pos):\n            color = red\n    pygame.draw.ellipse(display, color, (pos&#091;0] - r\/2, pos&#091;1] - r\/2, r, r), 4)\n    pygame.draw.line(display, color, (pos&#091;0], pos&#091;1] - l\/2), (pos&#091;0], pos&#091;1] - l), 4)\n    pygame.draw.line(display, color, (pos&#091;0] + l\/2, pos&#091;1]), (pos&#091;0] + l, pos&#091;1]), 4)\n    pygame.draw.line(display, color, (pos&#091;0], pos&#091;1] + l\/2), (pos&#091;0], pos&#091;1] + l), 4)\n    pygame.draw.line(display, color, (pos&#091;0] - l\/2, pos&#091;1]), (pos&#091;0] - l, pos&#091;1]), 4)\n\ndef lowerPlatform():\n    pygame.draw.rect(display, darkGray, (0, height - lowerBound, width, lowerBound))<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>This function is responsible for showing the location of balloons on the display screen. At first, we initialized variable pos() that gets the position of the mouse cursor and declared variables l, r, and color.<\/p>\n\n\n\n<p>We then used a for loop that runs in the range of 10. And as the loop runs the balloons that are made of a line and two ellipses are displayed continuously. Their position is decided using the pos, l, and r variables.<\/p>\n\n\n\n<p>In the last line, we defined a function named lowerPlatform() that will display a dark grey colored rectangle at the bottom of the screen on white the score will be displayed.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"score\">Display the score:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def showScore():\n    scoreText = font.render(\"Balloons Bursted : \" + str(score), True, white)\n    display.blit(scoreText, (150, height - lowerBound + 50))<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>This function&#8217;s main logic is to keep the track of the score. The text &#8220;Balloons Bursted&#8221; is displayed using the render() function and is displayed in white color.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"close\">To close the game:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def close():\n    pygame.quit()\n    sys.exit()<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>Whenever the user wants to quit the game, the function close() is called and this function contains:<\/p>\n\n\n\n<p><strong>pygame.quit:<\/strong> This actually runs that code whose main purpose is to deactivate the pygame library<br><strong>sys.exit():<\/strong> This is used in order to simply close the program without creating any dialogue box.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"events\">To keep track of the events:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def game():\n    global score\n    loop = True\n\n    while loop:\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                close()\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_q:\n                    close()\n                if event.key == pygame.K_r:\n                    score = 0\n                    game()\n\n            if event.type == pygame.MOUSEBUTTONDOWN:\n                for i in range(noBalloon):\n                    balloons&#091;i].burst()\n\n        display.fill(lightBlue)\n        \n        for i in range(noBalloon):\n            balloons&#091;i].show()\n\n        pointer()\n        \n        for i in range(noBalloon):\n            balloons&#091;i].move()\n\n        \n        lowerPlatform()\n        showScore()\n        pygame.display.update()\n        clock.tick(60)\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><br>This is the main function of this code and here we have used a while loop in order to compare the events that are happening during the game and take steps accordingly.<\/p>\n\n\n\n<p>We used <strong>event.type()<\/strong> to compare the events. If the player wants to quit then the QUIT event takes place. If the player tries to bust the balloons then the MOUSEBUTTONDOWN event takes place.<\/p>\n\n\n\n<p>We used a fill function of the display module to fill the colour of the screen. In the end, we called two functions lowerPlatform() which will display a rectangle and showScore() which will show the scores on that rectangle.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"game\">Call to game() function:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>game()<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>This is simply a call to the game() function because in order to start the game the call to this function is important<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"completecode\">Complete code for Balloon Shooter Game using Python<\/h2>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">import pygame\nimport sys\nimport random\nfrom math import *\n\npygame.init()\n\nwidth = 700\nheight = 600\n\ndisplay = pygame.display.set_mode((width, height))\npygame.display.set_caption(\"CopyAssignment - Balloon Shooter Game\")\nclock = pygame.time.Clock()\n\nmargin = 100\nlowerBound = 100\n\nscore = 0\n\nwhite = (230, 230, 230)\nlightBlue = (4, 27, 96)\nred = (231, 76, 60)\nlightGreen = (25, 111, 61)\ndarkGray = (40, 55, 71)\ndarkBlue = (64, 178, 239)\ngreen = (35, 155, 86)\nyellow = (244, 208, 63)\nblue = (46, 134, 193)\npurple = (155, 89, 182)\norange = (243, 156, 18)\n\nfont = pygame.font.SysFont(\"Arial\", 25)\n\nclass Balloon:\n    def __init__(self, speed):\n        self.a = random.randint(30, 40)\n        self.b = self.a + random.randint(0, 10)\n        self.x = random.randrange(margin, width - self.a - margin)\n        self.y = height - lowerBound\n        self.angle = 90\n        self.speed = -speed\n        self.proPool= [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]\n        self.length = random.randint(50, 100)\n        self.color = random.choice([red, green, purple, orange, yellow, blue])\n        \n    def move(self):\n        direct = random.choice(self.proPool)\n\n        if direct == -1:\n            self.angle += -10\n        elif direct == 0:\n            self.angle += 0\n        else:\n            self.angle += 10\n\n        self.y += self.speed*sin(radians(self.angle))\n        self.x += self.speed*cos(radians(self.angle))\n\n        if (self.x + self.a > width) or (self.x &lt; 0):\n            if self.y > height\/5:\n                self.x -= self.speed*cos(radians(self.angle)) \n            else:\n                self.reset()\n        if self.y + self.b &lt; 0 or self.y > height + 30:\n            self.reset()\n            \n    def show(self):\n        pygame.draw.line(display, darkBlue, (self.x + self.a\/2, self.y + self.b), (self.x + self.a\/2, self.y + self.b + self.length))\n        pygame.draw.ellipse(display, self.color, (self.x, self.y, self.a, self.b))\n        pygame.draw.ellipse(display, self.color, (self.x + self.a\/2 - 5, self.y + self.b - 3, 10, 10))\n            \n    def burst(self):\n        global score\n        pos = pygame.mouse.get_pos()\n\n        if isonBalloon(self.x, self.y, self.a, self.b, pos):\n            score += 1\n            self.reset()\n                \n    def reset(self):\n        self.a = random.randint(30, 40)\n        self.b = self.a + random.randint(0, 10)\n        self.x = random.randrange(margin, width - self.a - margin)\n        self.y = height - lowerBound \n        self.angle = 90\n        self.speed -= 0.002\n        self.proPool = [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]\n        self.length = random.randint(50, 100)\n        self.color = random.choice([red, green, purple, orange, yellow, blue])\n       \nballoons = []\nnoBalloon = 10\nfor i in range(noBalloon):\n    obj = Balloon(random.choice([1, 1, 2, 2, 2, 2, 3, 3, 3, 4]))\n    balloons.append(obj)\n\ndef isonBalloon(x, y, a, b, pos):\n    if (x &lt; pos[0] &lt; x + a) and (y &lt; pos[1] &lt; y + b):\n        return True\n    else:\n        return False\n    \ndef pointer():\n    pos = pygame.mouse.get_pos()\n    r = 25\n    l = 20\n    color = lightGreen\n    for i in range(noBalloon):\n        if isonBalloon(balloons[i].x, balloons[i].y, balloons[i].a, balloons[i].b, pos):\n            color = red\n    pygame.draw.ellipse(display, color, (pos[0] - r\/2, pos[1] - r\/2, r, r), 4)\n    pygame.draw.line(display, color, (pos[0], pos[1] - l\/2), (pos[0], pos[1] - l), 4)\n    pygame.draw.line(display, color, (pos[0] + l\/2, pos[1]), (pos[0] + l, pos[1]), 4)\n    pygame.draw.line(display, color, (pos[0], pos[1] + l\/2), (pos[0], pos[1] + l), 4)\n    pygame.draw.line(display, color, (pos[0] - l\/2, pos[1]), (pos[0] - l, pos[1]), 4)\n\ndef lowerPlatform():\n    pygame.draw.rect(display, darkGray, (0, height - lowerBound, width, lowerBound))\n    \ndef showScore():\n    scoreText = font.render(\"Balloons Bursted : \" + str(score), True, white)\n    display.blit(scoreText, (150, height - lowerBound + 50))\n    \ndef close():\n    pygame.quit()\n    sys.exit()\n    \ndef game():\n    global score\n    loop = True\n\n    while loop:\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                close()\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_q:\n                    close()\n                if event.key == pygame.K_r:\n                    score = 0\n                    game()\n\n            if event.type == pygame.MOUSEBUTTONDOWN:\n                for i in range(noBalloon):\n                    balloons[i].burst()\n\n        display.fill(lightBlue)\n        \n        for i in range(noBalloon):\n            balloons[i].show()\n\n        pointer()\n        \n        for i in range(noBalloon):\n            balloons[i].move()\n\n        \n        lowerPlatform()\n        showScore()\n        pygame.display.update()\n        clock.tick(60)\n        \ngame()\n<\/pre><\/div>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Output:<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/04\/Screenshot-2022-04-07-143811-1024x926.png\" alt=\"Balloon Shooter Game output \" class=\"wp-image-9723 lazyload\" width=\"686\" height=\"620\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/04\/Screenshot-2022-04-07-143811-1024x926.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/04\/Screenshot-2022-04-07-143811-300x271.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/04\/Screenshot-2022-04-07-143811-768x695.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/04\/Screenshot-2022-04-07-143811-1026x928.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/04\/Screenshot-2022-04-07-143811-675x611.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/04\/Screenshot-2022-04-07-143811.png 1048w\" data-sizes=\"(max-width: 686px) 100vw, 686px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 686px; --smush-placeholder-aspect-ratio: 686\/620;\" \/><\/figure>\n\n\n\n<p style=\"font-size:25px\"><strong>Video output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\n<div style=\"width: 640px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-9705-1\" width=\"640\" height=\"360\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/04\/Screen-Recording-08-Apr-22-9-57-51-AM.mp4?_=1\" \/><a href=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/04\/Screen-Recording-08-Apr-22-9-57-51-AM.mp4\">https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/04\/Screen-Recording-08-Apr-22-9-57-51-AM.mp4<\/a><\/video><\/div>\n<\/div><\/figure>\n\n\n\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-9886351916045880\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-9886351916045880\"\n     data-ad-slot=\"7933252109\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">EndNote:<\/h2>\n\n\n\n<p>We hope this article on Balloon Shooter Game using Python PyGame was helpful and was full of learnings for our readers. So finally, we are done with the coding for this game. We added many functionalities to this game from our end but now is your time to add some more and make this game extra amazing. Use different modules of the pygame library to add other features like sound, image, etc. And also try making this game for two players, if possible. But all in all, try something new. <\/p>\n\n\n\n<p>Pygame community also accepts and showcases the projects developed by the programmer on their official website. Check out here: <a href=\"https:\/\/www.pygame.org\/tags\/all\" target=\"_blank\" rel=\"noreferrer noopener\">Pygame official projects showcase<\/a><\/p>\n\n\n\n<p>Thank you for visiting our website.<\/p>\n\n\n\n<p><strong>Also Read:<\/strong><\/p>\n\n\n<ul class=\"wp-block-latest-posts__list is-grid columns-3 wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/create-your-own-chatgpt-with-python\/\">Create your own ChatGPT with\u00a0Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/sqlite-crud-operations-in-python\/\">SQLite | CRUD Operations in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/event-management-system-project-in-python\/\">Event Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/ticket-booking-and-management-in-python\/\">Ticket Booking and Management in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hostel-management-system-project-in-python\/\">Hostel Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/sales-management-system-project-in-python\/\">Sales Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/bank-management-system-project-in-cpp\/\">Bank Management System Project in C++<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/python-download-file-from-url-4-methods\/\">Python Download File from URL | 4 Methods<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/python-programming-examples-fundamental-programs-in-python\/\">Python Programming Examples | Fundamental Programs in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/spell-checker-in-python\/\">Spell Checker in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/portfolio-management-system-in-python\/\">Portfolio Management System in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/stickman-game-in-python\/\">Stickman Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/contact-book-project-in-python\/\">Contact Book project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/loan-management-system-project-in-python\/\">Loan Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/cab-booking-system-in-python\/\">Cab Booking System in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/brick-breaker-game-in-python\/\">Brick Breaker Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/tank-game-in-python\/\">Tank game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/gui-piano-in-python\/\">GUI Piano in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/ludo-game-in-python\/\">Ludo Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/rock-paper-scissors-game-in-python\/\">Rock Paper Scissors Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/snake-and-ladder-game-in-python\/\">Snake and Ladder Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/puzzle-game-in-python\/\">Puzzle Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/medical-store-management-system-project-in-python\/\">Medical Store Management System Project in\u00a0Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/creating-dino-game-in-python\/\">Creating Dino Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/tic-tac-toe-game-in-python\/\">Tic Tac Toe Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/test-typing-speed-using-python-app\/\">Test Typing Speed using Python App<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/scientific-calculator-in-python-2\/\">Scientific Calculator in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/gui-to-do-list-app-in-python-tkinter\/\">GUI To-Do List App in Python Tkinter<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/scientific-calculator-in-python\/\">Scientific Calculator in Python using Tkinter<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/gui-chat-application-in-python-tkinter\/\">GUI Chat Application in Python Tkinter<\/a><\/li>\n<\/ul>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>Tags<\/strong>-&gt; <a href=\"https:\/\/copyassignment.com\/?s=12th+class\" target=\"_blank\" rel=\"noreferrer noopener\">class 12th python project<\/a>, class 12th python programming, <a href=\"https:\/\/copyassignment.com\/?s=gui\" target=\"_blank\" rel=\"noreferrer noopener\">GUI python project<\/a>, <a href=\"https:\/\/copyassignment.com\/?s=game\" target=\"_blank\" rel=\"noreferrer noopener\">game in python<\/a>, ballon shooter game in python pygame, ballon shooter game using pygame python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all know the importance of the pygame library in terms of game development. We know that Pygame is a collection of Python modules for&#8230;<\/p>\n","protected":false},"author":62,"featured_media":9755,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,1060,1061,1147,1403],"tags":[1410,1142,1411,1412],"class_list":["post-9705","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-allcategorites","category-game-in-python","category-gui-python-projects","category-pygame-tutorial","category-python-projects","tag-ballon","tag-pygame","tag-shooter","tag-shooting","wpcat-22-id","wpcat-1060-id","wpcat-1061-id","wpcat-1147-id","wpcat-1403-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/9705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=9705"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/9705\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media\/9755"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=9705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=9705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=9705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}