Countdown and image moveDown() and random()

Hi there, I would like the countdown timer to disappear when it reaches 0, and at that point, only then the dog starts moving down, but right now the dog moves down at the beginning already. Also, there is only one dog instead of many. Any ideas as how I could solve this? Thanks :smiley:

add_library('sound')
add_library("sound")
ellX = 350 #initial dog position 
ellY = 0
countdown = 5 #beginning countdown

def setup():
    global sf, x, bgImage, y, speed, acceleration, countdown, dog 
    countdown = 5
    size(800, 600)
    frameRate(1)
    sf = SoundFile(this,"backgroundmusic.mp3")
    sf.play()
    bgImage = loadImage("backgroundimage.jpeg")
    dog = loadImage("dog.png")

def draw():
    global bgImage, x, y, dog, moveDown, ellX, ellY, ball, countdown
    image(bgImage, 0, 0, width, height)
    stroke(0, 255, 0)
    textSize(50)
    text(countdown, width/2, height/2)
    image(dog, ellX, ellY, 130, 50)
    
    if countdown > 0:
        countdown = countdown - 1
    if countdown == 0:
        moveDown = True
    else:
        noLoop()

    if moveDown == True:
        ellY = ellY + 4
    if ellY <= 650:
        moveDown = True 
    if ellY> 650:
        ellY = 0
        ellX = random (30, 570) #random placement 
        moveDown = True

You already have a if-statement to show or not to show your countdown, namely if countdown > 0 : If you put text(countdown, width/2, height/2) in there it disappears when countdown reaches zero.

Dog moves because you change ellY straight away not after countdown reaches zero.

To have more dog you need data structure to store those dogs and easily refer to them. Array is most commonly used, but in python dictionary might work as well

2 Likes

To add to what @SomeOne has said –

You might employ a list of dictionaries:

dogs = []  # an empty list to hold the dogs

def setup():
    global dogs
    size(500, 500)
    # create a dictionary for the first dog
    first_dog = {'x':350, 'y':0, 'size':50}
    # add the first dog to the dogs list
    dogs.append(first_dog)

def draw():
    # render and increment the y-value of every dog in the list
    for dog in dogs:
        circle(dog['x'], dog['y'], dog['size'])
        dog['y'] += 1

def mouseClicked():
    # each mouse-click, add a dog with a randomized x-value to the list
    global dogs
    dogs.append({'x':random(width), 'y':0, 'size':50})

Note that the list will grow endlessly, full of β€˜dogs’ that have long since passed the bottom of the display window. Check the different list methods in the reference to remove the dogs that have expired.

2 Likes

Thank you, if I want to replace the ball, with a dog picture instead, do I use dogs = loadImage("dog.png"), but then it’s not a list anymore. How do I do this?

Thank you, but for the countdown, the countdown text (numbers) is still there. How do I make it disappear when it reaches 0? (meaning out of the frame)

As I tried to say earlier if you put the text in if statement it will be shown only when countdown as larger than zero

    if countdown > 0:
        countdown = countdown - 1
        text(countdown, width/2, height/2)

I’m assuming the dog image is the same for every dog? So, something like –

dogs = []

def setup():
    global dog_img, dogs
    size(500, 500)
    dog_img = loadImage('dog.png')
    first_dog = {'x':350, 'y':0}
    dogs.append(first_dog)

def draw():
    for dog in dogs:
        image(dog_img, dog['x'], dog['y'])
        dog['y'] += 1
1 Like

Got it! Thank you so much

Got it! Thank you very much!