Exnrt Logo
  • Home
  • Technology
    • Artificial Intelligence
    • WordPress
  • Programming
    ProgrammingShow More
    Mistral AI Model
    Mistral-7B Instruct Fine-Tuning using Transformers LoRa
    19 1
    Hugging Face Website
    Hugging Face Transformers Pipeline, what can they do?
    16 1
    AI generated images using SDXL-Lightning huggingface
    SDXL-Lightning model using hugging face Transformers
    14 1
    Gemma AI Model
    Finetune Gemma Models with Transformers
    12 1
    HTML Quiz App
    Quiz App Using HTML, CSS, and JavaScript
    9 1
  • Business
    • Ads
    • SEO
  • My Feed
    • My Interests
    • My Saves
    • History
  • Web Tools
    • Markdown Editor
    • JSON Studio
    • Table File Viewer
    • TextDiff Lite
    • QR Code Generator
Notification
Sign In
ExnrtExnrtExnrt
Font ResizerAa
  • Artificial Intelligence
  • Technology
  • Business
  • Ads
  • SEO
Search
  • Blog
  • Ads
  • Programming
  • Technology
  • Artificial Intelligence
  • WordPress
  • SEO
  • Business
  • Education

Top Stories

Explore the latest updated news!
Fine Tuning Siglip2 a ViT on Image Classification Task.

Fine Tuning Siglip2 on Image Classification Task

13
AI-Generated-Image-using-Flux-1

How to Fine-Tune Flux.1 Using AI Toolkit

14
microsoft/Phi-3-mini-128k-instruct

How to fine-tune Microsoft/Phi-3-mini-128k-instruct

14

Stay Connected

Find us on socials
248.1k Followers Like
61.1k Followers Follow
165k Subscribers Subscribe
ProgrammingBlog

Balloon Blast Game Project Using Python

Ateeq Azam
Last updated: March 17, 2024 10:36 am
By Ateeq Azam Add a Comment 8
Share
Balloon Blast Game
Balloon Blast Game
SHARE

The “Balloon Blast Game” using Python is a simple arcade-style game project where the player’s objective is to shoot and pop balloons that appear on the screen. The game is implemented using the Pygame library, which is a popular library for creating 2D games in Python. Here’s a general description of the game:

Table of Content
GameplayFeaturesInstall pygame LibraryPython CodeInitialization:Balloon Class:Game Initialization:Game Loop:Closing the Game:Final Words

Objective: Pop as many balloons as possible to score points.

Gameplay

  1. Balloons of different colors and sizes appear randomly at the top of the game screen.
  2. The player controls a cursor (usually a crosshair) using the mouse.
  3. By clicking on a balloon with the cursor, the player can pop it.
  4. Each popped balloon awards the player with points.
  5. The game continues until the player decides to quit or until a certain time limit is reached.
  6. The player’s score is displayed on the screen.

Features

  • Balloons move in random directions or follow predefined paths.
  • Balloons may have different point values based on their size or color.
  • Sound effects and background music can be added to enhance the gaming experience.
  • The game can have multiple levels with increasing difficulty.

It’s a straightforward and fun game that serves as a good starting point for learning game development in Python, especially with the Pygame library. Developers can customize and expand upon this basic concept to create more complex and engaging games with various challenges and features.

Install pygame Library

The “Balloon Blast Game” using Python, as described in your previous text, can be implemented using the Pygame library. Pygame is a popular library for creating 2D games in Python, and it provides the necessary functions and features for game development, including handling graphics, input, sound, and more.

To create the game, you’ll need to install the Pygame library. You can install it using pip, the Python package manager, by running the following command in your terminal or command prompt:

pip install pygame

Once Pygame is installed, you can use it to handle game graphics, input from the mouse and keyboard, and other game-related functionalities. It simplifies the process of creating 2D games in Python and is a suitable choice for developing the “Balloon Blast Game” you mentioned.

Python Code

Python
import pygame
import sys
import random
from math import *

pygame.init()
width = 700
height = 750

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

margin = 100
lowerBound = 100

score = 0

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

font = pygame.font.SysFont("Arial", 25)

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.proPool= [-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])

    def move(self):
        direct = random.choice(self.proPool)

        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()

    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))

    def burst(self):
        global score
        pos = pygame.mouse.get_pos()

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

    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.proPool = [-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])

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 isonBalloon(x, y, a, b, pos):
    if (x < pos[0] < x + a) and (y < pos[1] < y + b):
        return True
    else:
        return False

def pointer():
    pos = pygame.mouse.get_pos()
    r = 25
    l = 20
    color = lightGreen
    for i in range(noBalloon):
        if isonBalloon(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)

game()

Download Full Project:

Download Full Project

Resource ready for free download! Sign up with your email to get instant access.

The code i provided is for a simple game called “Balloon Blast” implemented using the Pygame library in Python. Here’s a brief overview of what the code is doing:

Initialization:

  • It initializes the Pygame library and sets up the game window with a specific width and height.
  • Various colors and fonts are defined for later use.
  • A class called Balloon is defined to represent balloons in the game.

Balloon Class:

  • The Balloon class represents individual balloons that appear in the game.
  • Balloons have properties such as size (a and b), position (x and y), angle, speed, color, and more.
  • Balloons can move in different directions based on their angle and speed.
  • When a balloon is clicked by the player’s mouse cursor, it bursts, and the player’s score increases.

Game Initialization:

  • It initializes a list of Balloon objects to create a set of balloons.
  • The game loop is set up, which continuously updates the game’s state and renders it on the screen.

Game Loop:

  • The game loop is the heart of the game. It runs continuously until the player decides to quit.
  • Inside the loop, the following happens:
    • Event handling: The code listens for various events such as mouse clicks and key presses.
    • Rendering: It draws the balloons on the game window, updates their positions, and displays the player’s score.
    • Collision detection: It checks if the player clicked on a balloon and increases the score accordingly.
    • Lower platform: It draws a lower platform at the bottom of the screen.

Closing the Game:

  • The game can be closed either by clicking the window’s close button or by pressing ‘q’ on the keyboard.
  • Pressing ‘r’ on the keyboard resets the player’s score and starts a new game.

Final Words

The code provides the basic mechanics for a simple balloon-shooting game where the player clicks on balloons to burst them and tries to achieve a higher score. It demonstrates the usage of Pygame for game development in Python. Players can try to improve upon this base code by adding more features, levels, and challenges to make it a complete game.

TAGGED:Programming
Share This Article
Facebook Twitter Copy Link Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Leave a comment
Subscribe
Login
Notify of
guest

guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

You Might Also Like

Fine Tuning Siglip2 a ViT on Image Classification Task.
Fine Tuning Siglip2 on Image Classification Task
AI-Generated-Image-using-Flux-1
How to Fine-Tune Flux.1 Using AI Toolkit
microsoft/Phi-3-mini-128k-instruct
How to fine-tune Microsoft/Phi-3-mini-128k-instruct
AI Generated: A professional real llama looking like a hacker in a dark lab with light yellow lights
How to Fine-tune Meta Llama-3 8B

Other Posts

best ecommerce WordPress theme
10 Best eCommerce WordPress Themes Free & Paid
WordPress Blog
Ways to make money from ChatGPT
How to Earn $100 Daily Using ChatGPT
Business Artificial Intelligence Blog Technology
Introduction to C Programming Language
History of C Language: Introduction, Timeline & Benefits
Programming Blog
Business Revenue
Tiers 1, 2 & 3 Countries: For Publishers and Advertisers
Ads Blog SEO

Latest Posts

Uncover the Latest stories that related to your interest!

At Exnrt.com, we believe in empowering computer science students with the knowledge and skills they need to succeed in their careers. Our goal is to provide accessible and engaging tutorials that help students and professionals develop their skills and advance their careers.

  • Categories:
  • Business
  • Technology
  • Ads
  • SEO

Quick Links

  • Blog
  • Technology
  • Artificial Intelligence
  • Business

About US

  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2024 All Rights Reserved – Exnrt by ateeq.pk

wpDiscuz
Welcome Back!

Sign in to your account

Register Lost your password?