Why all balloons are hidden and not only the one I click?

Viewed 34

I'm creating a simple game with Python using turtle package.

My aim is to have some balloons on the screen running from right to left and then when one of them is clicked to make it disappear.

What I have going wrong is that when I click one balloon, all of them are disappeared!

Here is my code

1- Main

from turtle import Screen
from balloon import Balloon
import time

screen = Screen()
screen.title('Balloons Nightmare')
screen.setup(width=600, height=600)
screen.bgpic(picname='sky-clouds.gif')
screen.tracer(0)

balloons_manager = Balloon()
current_x = 0
current_y = 0

screen.listen()
screen.onclick(fun=balloons_manager.explode_balloon, btn=1)

game_is_on = True
while game_is_on:
    time.sleep(0.1)
    screen.update()

    balloons_manager.create_balloon()
    balloons_manager.move_balloon()

screen.exitonclick()

2- balloon module

import random
from turtle import Turtle

COLORS = ["red", "yellow", "green", "blue", "black"]
MOVEMENT_SPEED = 2


class Balloon:
    def __init__(self):
        self.all_balloons = []
        self.balloon_speed = MOVEMENT_SPEED
        self.x = 0
        self.y = 0
        self.hidden = None

    def create_balloon(self):
        random_choice = random.randint(1, 9)
        if random_choice == 1:
            new_balloon = Turtle("circle")
            new_balloon.penup()
            new_balloon.turtlesize(stretch_wid=2, stretch_len=0.75)
            new_balloon.color(random.choice(COLORS))
            random_y_cor = random.randint(-50, 280)
            new_balloon.goto(320, random_y_cor)
            self.hidden = new_balloon.isvisible()
            self.all_balloons.append(new_balloon)

    def move_balloon(self):
        for balloon in self.all_balloons:
            balloon.backward(self.balloon_speed)

    def explode_balloon(self, x, y):
        for balloon in range(len(self.all_balloons)):
            print(self.all_balloons[balloon].position())
            self.all_balloons[balloon].hideturtle()
        # for balloon in self.all_balloons:
        #     balloon.hideturtle()

I tried so many changes but nothing helped me, example of what I tried so far getting the current x,y coordinates of the balloon so that on click to hide only the one with these coordinates but didn't work for me or I did something wrong with it

Any hints will be appreciated.

Thank you

2 Answers

Here's your code triggered by the click handler:

def explode_balloon(self, x, y):
    for balloon in range(len(self.all_balloons)):
        print(self.all_balloons[balloon].position())
        self.all_balloons[balloon].hideturtle()

This loops over all balloons and hides them unconditionally.

You probably want to use an if in there to only conditionally trigger the hiding behavior. Compare the x and y coordinates of the click against the current balloon in the loop. Only hide the balloon if the distance is less than a certain amount (say, the radius of the balloon).

Another approach is to use turtle.onclick to add a handler function that will be trigged when the turtle is clicked.

Related:

I fixed the issue by adding an inner function in create_balloon function

def create_balloon(self):
    random_choice = random.randint(1, 9)
    if random_choice == 1:
        new_balloon = Turtle("circle")
        new_balloon.penup()
        new_balloon.turtlesize(stretch_wid=2, stretch_len=0.75)
        new_balloon.color(random.choice(COLORS))
        random_y_cor = random.randint(-50, 280)
        new_balloon.goto(320, random_y_cor)

        def hide_the_balloon(x, y):
            return new_balloon.hideturtle()

        new_balloon.onclick(hide_the_balloon)
        self.all_balloons.append(new_balloon)
Related