2

I am working on a program and i need to switch through different loops. this works thought when i try to switch back to the previous loop i crashes.

Any suggestions?

P.S. the bellow are examples

e.g. Function = Home

(change loop)

Function = txtbox

(change loop)

Function = Home (Crashes here)

import pygame, sys, time, random
from pygame.locals import *
import math
import sys
import os
# set up pygame
pygame.init()




# set up the window
WINDOWWIDTH = 1200
WINDOWHEIGHT = 650
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 1, 32)
pygame.display.set_caption("Mango")


Function = "Home"

font = pygame.font.SysFont("Fonts", 30)

#colors
TEXTCOLOR = (255, 255, 255)
TEXTCOLORS = (255, 0, 0)


# run the game loop
while Function == "Home":
    # check for the QUIT event
    events = pygame.event.get()
    for event in events:
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONUP:
            Function = "txtbox"
            break


    pygame.display.flip()



while Function == "txtbox":

    events = pygame.event.get()
    # process other events
    for event in events:

          if event.type == pygame.MOUSEBUTTONUP:
            Function = "Home"
            break

    pygame.display.flip()

2 Answers 2

1

It doesn't crash. It simply finishes execution when Function is set to "Home" in the last loop. That loop simply ends.

Try enclosing those two while loops inside another while loop that runs forever.

while True:
    while Function == "Home":
        # check for the QUIT event
        events = pygame.event.get()
        for event in events:
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONUP:
                Function = "txtbox"
                break


        pygame.display.flip()



    while Function == "txtbox":

        events = pygame.event.get()
        # process other events
        for event in events:

              if event.type == pygame.MOUSEBUTTONUP:
                Function = "Home"
                break

        pygame.display.flip()
Sign up to request clarification or add additional context in comments.

1 Comment

I confirm it doesn't crash and just terminates as expected.
0

You're on a good track.

Try this:

  • extract every state of your game to a function,
  • have a variable that knows which state is currently "active".

Example code:

def home():
    events = pygame.event.get()
    for event in events:
        ...

    if something_happened:
        switch_state(txtbox)


def txtbox():
    events = pygame.event.get()
    for event in events:
        ...

    if something:
        switch_state(home)

Function = home  # assign the function itself to a variable

def switch_state(new_state):
    global Function
    Function = new_state

...

while True:
    Function()  # call the function which is currently active

Next steps:

  • Write the states as objects, instead of functions (so that they can keep some data about themselves - for example you'd have a state "Level" and all data about the particular level in it)
  • Instead of one global Function(), keep a list of states, so that you can push a new state on top, and then pop it and go back to whatever state you've been on previously. This will let you manage multiple game screens easily.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.