0

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?

GGberry
  • 929
  • 5
  • 21

1 Answers1

0

In order to detect when a bubble has been clicked, you need to first detect when something has been clicked, and then check if that click occurred inside a bubble. This requires a few new things in your implementation. First, you need to be able to detect clicks, then a function to delete bubbles, and that will require a way to store bubbles, which you have an array for, but you never use it

You can add this to check_events in order to detect clicks:

def check_events(bubble): # needs to have a reference to the bubble object so it can call a function to remove bubbles
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        
        if event.type == pygame.MOUSEBUTTONDOWN : # check if a mouse button has been pressed on this specific frame
            click_pos = mygame.mouse.get_pos()
            bubble.check_collision(click_pos) # new function in Bubble.py, see below         

Now add this bit of code to Bubble.__init__() :

def __init__(self, screen, bubble_positions): # new addition to __init__() to store bubble positions
    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 = bubble_positions # change this to store the bubble positions
    

And add this to the part where you create the bubble object:

bubble_positions = [(x * 10 + 10, y * 3 + 3) for x in range(10) for y in range(3)] # generate all bubble positions using list comprehension
bubble = Bubble(display, bubble_positions)

Now that you have a list of bubbles that can be changed, you need to draw the bubbles from the list, instead of using hardcoded for loops. Change bubble_action() accordingly:

def bubble_action(bubble):
    for b in bubble.bubble_positions :
        bubble.blitme(b[0], b[1])

Now we needs a new function to detect if there is a bubble at a certain position. This implementation assumes the bubble is a perfect circle with center at (x, y) and radius 5 (you can change this if the radius is different). In Bubble:

def check_collision(pos) :
    r = 5
    for b in self.bubble_positions :
        if (b[0] - pos[0])**2 + (b[1] - pos[1])**2 > r**2) : # checks if pos is inside bubble at position b
            self.bubble.positions.remove(b) # delete the bubble
            break

Now we have all of the necessary functions to detect when a bubble is clicked and delete it.