DEV Community

loading...
Cover image for Balloon Shooter Game Using Python

Balloon Shooter Game Using Python

Arnab Dey
Certified Professional in the field of Computer Science. Worked at NSC, IIT Kharagpur. Member of ASTM International.
・4 min read

Balloon Shooter

Balloon shooting is a simple archery game or Balloon smasher game. Balloon bursting game has simple rules but require great skill of shooting. Balloon popping or balloon shooting is best game to play in your leisure time. This game is similar to classic Bubble breaker or bubble pop game. You will get limited arrows to shoot the balloons and you have to utilize it in best possible way to score high by bursting the balloons. The smaller the balloon you shoot, more the score you get. The objective of this balloon shooting game is to pop as many balloons as possible using as few arrows as possible. You can shoot multiple balloons with one arrow as well, make best use of your arrows to shoot multiple balloons coming in line. In this game a lot of options are available to score more with special balloons coming in the way like balloon with bomb, 3 balloons at a time, wheels and small balloon. It all depends on your shooting skill and logical thinking how you can score more.

image

This fun learning game is all time favourite among kids and adults. Rule of Balloon shooting game is simple and fun is never less, game still remains challenging. Show your mastery in this amazing game by pulling the rope and adjusting the angle of arrows to shoot maximum balloons in one shot, aim carefully. Multiply your scores by aiming at multiple balloons with one shot. It is like sniper games where the bullets are replaced with bundles of arrows, and you get a predictable, physics-based ricochet effect. Balloons of all colors, shapes and sizes are coming down your way. Prepare your strategy and become master in balloon shooting.

Be warned this addictive game is hard to stop. It is very intuitive and easy to learn but you need to master it to score high. Amazing graphics, interesting sound effects and smooth animation spices up the entertainment in the game. Compile this interesting game and let the fun begin.

Let us learn how to develop balloon shooting game and its technique with Pygame

------------------------------------

Balloon Shooter
Language - Python
Modules - pygame, sys, random, math
Controls - Mouse
By - Arnab Dey

------------------------------------

import pygame
import sys
import random
from math import *

pygame.init()

width = 500
height = 500

display = pygame.display.set_mode((width, height))
pygame.display.set_caption("Balloon Shooter")
clock = pygame.time.Clock()

margin = 100
lowerBound = 100

score = 0

Colors

white = (230, 230, 230)
lightBlue = (174, 214, 241)
red = (231, 76, 60)
lightGreen = (25, 111, 61)
darkGray = (40, 55, 71)
darkBlue = (21, 67, 96)
green = (35, 155, 86)
yellow = (244, 208, 63)
blue = (46, 134, 193)
purple = (155, 89, 182)
orange = (243, 156, 18)

font = pygame.font.SysFont("Snap ITC", 25)

Balloon Class

class Balloon:
def init(self, speed):
self.a = random.randint(30, 40)
self.b = self.a + random.randint(0, 10)
self.x = random.randrange(margin, width - self.a - margin)
self.y = height - lowerBound
self.angle = 90
self.speed = -speed
self.probPool = [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]
self.length = random.randint(50, 100)
self.color = random.choice([red, green, purple, orange, yellow, blue])

# Move balloon around the Screen
def move(self):
    direct = random.choice(self.probPool)

    if direct == -1:
        self.angle += -10
    elif direct == 0:
        self.angle += 0
    else:
        self.angle += 10

    self.y += self.speed*sin(radians(self.angle))
    self.x += self.speed*cos(radians(self.angle))

    if (self.x + self.a > width) or (self.x < 0):
        if self.y > height/5:
            self.x -= self.speed*cos(radians(self.angle)) 
        else:
            self.reset()
    if self.y + self.b < 0 or self.y > height + 30:
        self.reset()

# Show/Draw the balloon  
def show(self):
    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))
    pygame.draw.ellipse(display, self.color, (self.x, self.y, self.a, self.b))
    pygame.draw.ellipse(display, self.color, (self.x + self.a/2 - 5, self.y + self.b - 3, 10, 10))

# Check if Balloon is bursted
def burst(self):
    global score
    pos = pygame.mouse.get_pos()

    if onBalloon(self.x, self.y, self.a, self.b, pos):
        score += 1
        self.reset()

# Reset the Balloon
def reset(self):
    self.a = random.randint(30, 40)
    self.b = self.a + random.randint(0, 10)
    self.x = random.randrange(margin, width - self.a - margin)
    self.y = height - lowerBound 
    self.angle = 90
    self.speed -= 0.002
    self.probPool = [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]
    self.length = random.randint(50, 100)
    self.color = random.choice([red, green, purple, orange, yellow, blue])
Enter fullscreen mode Exit fullscreen mode

balloons = []
noBalloon = 10
for i in range(noBalloon):
obj = Balloon(random.choice([1, 1, 2, 2, 2, 2, 3, 3, 3, 4]))
balloons.append(obj)

def onBalloon(x, y, a, b, pos):
if (x < pos[0] < x + a) and (y < pos[1] < y + b):
return True
else:
return False

show the location of Mouse

def pointer():
pos = pygame.mouse.get_pos()
r = 25
l = 20
color = lightGreen
for i in range(noBalloon):
if onBalloon(balloons[i].x, balloons[i].y, balloons[i].a, balloons[i].b, pos):
color = red
pygame.draw.ellipse(display, color, (pos[0] - r/2, pos[1] - r/2, r, r), 4)
pygame.draw.line(display, color, (pos[0], pos[1] - l/2), (pos[0], pos[1] - l), 4)
pygame.draw.line(display, color, (pos[0] + l/2, pos[1]), (pos[0] + l, pos[1]), 4)
pygame.draw.line(display, color, (pos[0], pos[1] + l/2), (pos[0], pos[1] + l), 4)
pygame.draw.line(display, color, (pos[0] - l/2, pos[1]), (pos[0] - l, pos[1]), 4)

def lowerPlatform():
pygame.draw.rect(display, darkGray, (0, height - lowerBound, width, lowerBound))

def showScore():
scoreText = font.render("Balloons Bursted : " + str(score), True, white)
display.blit(scoreText, (150, height - lowerBound + 50))

def close():
pygame.quit()
sys.exit()

def game():
global score
loop = True

while loop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            close()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                close()
            if event.key == pygame.K_r:
                score = 0
                game()

        if event.type == pygame.MOUSEBUTTONDOWN:
            for i in range(noBalloon):
                balloons[i].burst()

    display.fill(lightBlue)

    for i in range(noBalloon):
        balloons[i].show()

    pointer()

    for i in range(noBalloon):
        balloons[i].move()


    lowerPlatform()
    showScore()
    pygame.display.update()
    clock.tick(60)
Enter fullscreen mode Exit fullscreen mode

game()

Github Source Code : https://github.com/arnab132/Balloon-Shooter-Game-Python

Discussion (1)