I'm making a bubble clicker game using python. The main goal of the game is to click some bubbles on the screen. For now, there are 3 rows of 10 bubbles appearing on the screen.
The game currently has no interactivity and I would like to add some. But the problem is that I don't know how to make each individual bubbles detect mouse press.
Here is the code for the main program of the game, Clicking_Game.py
:
import pygame
from pyfiles.bubble import Bubble
from pyfiles.settings import Settings
import pyfiles.game_functions as gf
def run_game():
""" Main Function """
pygame.init()
# Initialize settings
clock = pygame.time.Clock()
settings = Settings()
FPS = settings.FPS
# Importing classes and displaying
display = pygame.display.set_mode((settings.screen_width, settings.screen_height)) # flags=pygame.NOFRAME
bubble = Bubble(display)
# Displaying caption, icon and background image
version = 'v1.0.0'
icon = pygame.image.load('pyfiles/images/bubble.png')
background_image = pygame.image.load('pyfiles/images/background_image.jpg')
pygame.display.set_caption(f'Clicking Game {version}') # Displaying caption
pygame.display.set_icon(icon) # Displaying icon
display.blit(background_image, (0, 0)) # Displaying background image
while True:
# Checking for events
gf.check_events()
clock.tick(FPS)
# Displaying Items
gf.bubble_action(bubble)
# Updating
gf.update_screen()
run_game()
Here is the code for game_functions.py
:
import pygame, sys
def check_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
def bubble_action(bubble):
global all_sprites
global row
columns = 3
rows = 10
for column in range(1, 50 * columns, 50):
for row in range(1, 60 * rows, 60):
bubble.blitme(row, column)
def update_screen():
pygame.display.flip()
And finally, here is code for bubbles.py
:
import pygame
class Bubble(pygame.sprite.Sprite):
def __init__(self, screen):
pygame.sprite.Sprite.__init__(self)
self.screen = screen
self.image = pygame.image.load('./pyfiles/images/bubble.png')
# Locate all of the bubbles on screen
self.bubble_positions = []
def blitme(self, x, y):
self.image_x = x
self.image_y = y
self.screen.blit(self.image, (self.image_x, self.image_y))
With what I currently have, what can I add ( or remove ) to make bubbles disappear individually when they are clicked by the player?