0

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

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Bumblebee
  • 85
  • 5
  • Can you share your attempt at adding a condition? That's (one of) the right approaches. – ggorlen Sep 22 '22 at 18:16
  • 1
    There is no check in the `explode_balloon`, but simply hide all balloons. . . – Mechanic Pig Sep 22 '22 at 18:19
  • 2
    "What I have going wrong is that when I click one balloon, all of them are disappeared!" I can't understand why this is surprising. When you click the balloon, it calls `explode_balloon`, and the code there very explicitly says: look at every balloon, one at a time, and hide its turtle. – Karl Knechtel Sep 22 '22 at 18:50

2 Answers2

1

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:

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Thank you, I managed to sort it out by adding an inner function in the create_balloon function, however I will also consider your hints as I need to expand the functionality of the game. – Bumblebee Sep 22 '22 at 18:32
  • Good work and thanks for adding your self-answer. The distance approach might be useful when balloons overlap and you want to destroy all nearby balloons. – ggorlen Sep 22 '22 at 18:45
1

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)
Bumblebee
  • 85
  • 5