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()