1

I created a small game with Pygame Zero and the MU IDE. The player has to click (collect) as many bananas as possible in a certain time. Each time a banana is collected, a new banana appears at random position.

After the time has elapsed, a new screen will appear with information on how many bananas have been collected. For this I created an If Else statement, but the Else branch is not executed by the program (Except for reducing the time by 1 second and close of game window). I don't know why. Does anyone have an idea why the Else branch is not executed?

from random import randint 
import time
import pygame

HEIGHT = 800
WIDTH = 800
score = 0
time_left = 10

banana = Actor("banana")
monkey = Actor("monkey")

def draw():
    screen.clear()
    screen.fill("white")
    banana.draw()
    monkey.draw()
    screen.draw.text("Number of bananas collected: " + str(score),      color = "black", topleft=(10,10))
    screen.draw.text("Time: " + str(time_left), color = "black", topleft=(10,50))

def place_banana():
    banana.x = randint(125, 790)
    banana.y = randint(186, 790)
    monkey.x = 50
    monkey.y = 740

def on_mouse_down(pos):
    global score
    if banana.collidepoint(pos): 
        score = score + 1 
        place_banana()

def update_time_left():
    global time_left
    if time_left:  # wenn Zeit > 0 Sekunden ist
        time_left = time_left - 1
    else:  
        screen.fill("pink")  # code is not executed
        game_over() 

place_banana() 
clock.schedule_interval(update_time_left, 1.0)

def game_over():
    screen.fill("pink") # code is not executed
    global time_left
    message = ("Ende. Number of bananas collected")   # code is not  executed
    time_left = 0
    time.sleep(5.5)
    quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
siclaro
  • 137
  • 13
  • 2
    It might help if you rephrased your question. Looks like your else statement works, but screen.fill("pink") does not work as intended. Rephrasing will get you more direct and relevant help. – Harsha Oct 04 '19 at 15:24
  • Of course the code is executed, but between `screen.fill("pink")` and `quit()` the display is never updated. – Rabbid76 Oct 04 '19 at 15:36
  • I don't know exactly how `pygame` works, but `if time_left` is truthy. That means if the clock doesn't perfectly hit `0` on the loop, `else` will never execute. Ie, if it goes from time 1 to time -0.00001, then you will never exit the game. It might be better to do if `time_left > 0: ... else: ...` to explicitly exclude negative values. – James Oct 04 '19 at 15:51

1 Answers1

1

Of course the code is executed, but you cant "see" the result on the display, because after screen.fill("pink") (and before time.sleep(5.5)) the display is not updated.
You've to update the display by pygame.display.update() and to handle the events by (e.g) pygame.event.pump() after the display is filled in pink.

def game_over():

    screen.fill("pink")
    # [...] draw something else (e.g. some text)    

    pygame.display.update()
    pygame.event.pump()

    time.sleep(5.5)
    quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    Thankyou! Yes i forgot to update display. As a beginner I don't think about all things. I changed it and now it is working! – siclaro Oct 04 '19 at 16:05