yellowcake
yellowcake

Reputation: 137

Game with Python - Add option "Restart game"

I created a small game with Pygame Zero and the MU IDE. After the game is finished, the user should be asked if he wants to play again. If he chooses yes, the game should start from the beginning. I know that I can do this with a While loop, but I don't know how.

I tried to insert a while loop. In the while loop the game functions are called, but it is not working. I tried this:

play_again = raw_input("If you'd like to play again, please type 'yes'")
while playagain == "yes"
      draw()
      place_banana()
      on_mouse_down(pos)
      update_time_left()

....

I know that this is not correct, but i have no idea how to do it right

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

Upvotes: 1

Views: 345

Answers (2)

ThePythonator
ThePythonator

Reputation: 154

One issue which will definitely stop your code from running is the colons at the end of all four functions in your suggested while loop. The colon is used to define functions or for if/else statements etc, not for executing a function.

I'm not sure if there are other issues stopping it from running because you haven't given all the source code, but your while loop should look like this:

play_again = "yes"
while play_again == "yes":
    draw()
    place_banana()
    on_mouse_down(pos)
    update_time_left()
    play_again = raw_input("If you'd like to play again, please type 'yes'")

Another thing is that using shell input for pygame programs isn't the best because normally the user wouldn't know to look at the terminal, so look into options for building the input into the actual UI of the game.

Upvotes: 2

Jeff R
Jeff R

Reputation: 90

You need to wrap your code in a while loop and have an input at the beginning that will ask for play_again. Set play_again to 'yes' outside the while loop but call the input for play_again inside the while loop.

Upvotes: 1

Related Questions