Why does my boolean for my loop keep being set back to True?

Liam Jess

so im programming a game and it's my first time using classes. I have a class that has different fields for different aspects of a background and objects for the different backgrounds. I have a while loop for the start screen. This works however when I click on play it's suppose to set start to False and stop the loop but when I press it, it gets set to False once and straight back to True which continues the loop. I use print to see what the boolean is at all times.

I difine start = True at the top of the code

import pygame
import sys

pygame.init()

start = True



screen_width = 900
screen_height = 507
center_x = screen_width/2
center_y = screen_height/2

screen = pygame.display.set_mode((screen_width, screen_height))


clock = pygame.time.Clock()
FPS = 100

WHITE = (255,255,255)

bgx = 0
bgx2 = screen_width




main_bg = pygame.image.load("mainmenu.jpg")
main_bg2 = pygame.image.load("mainmenu.jpg")
first_bg = pygame.image.load("firstlvl.png")
first_bg2 = pygame.image.load("firstlvl.png")
second_bg = pygame.image.load("secondlvl.jpg")
second_bg2 = pygame.image.load("secondlvl.jpg")
third_bg = pygame.image.load("thirdlvl.jpg")
third_bg2 = pygame.image.load("thirdlvl.jpg")

#for i in range(1,7):
    #pygame.mixer.music.load("music"+str(i)+".mp3")


class layout:

#""" This class makes the layout of each screen (including buttons, music and background)"""       
    def __init__(self, background, background2, back_button, pause_button, play_button):
        self.bg = background
        self.bg2 = background2
        self.backbut = back_button
        self.pausebut = pause_button
        self.playbut = play_button

    def create_screen(self):
        screen.fill(WHITE)
        global bgx, bgx2
        bgx -= 3
        bgx2 -= 3
        screen.blit(self.bg, [bgx,0])
        screen.blit(self.bg2, [bgx2,0])

        if bgx + screen_width == 0:
            bgx = screen_width
        elif bgx2 + screen_width == 0:
            bgx2 = screen_width

        if self.playbut == True:
            butimg = pygame.image.load("playbutton.png")
            butrect = pygame.Rect(center_x-(358/2),center_y-(146/2),358,146)
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if butrect.collidepoint(event.pos):
                        start = False
                        print (start)

            screen.blit(butimg,butrect)

        if self.backbut == True:
            butimg2 = pygame.image.load("backbut.png")
            butimg2 = pygame.transform.scale(butimg2, (62, 62))
            butrect2 = pygame.Rect(10,screen_height-72,62,62)
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if butrect2.collidepoint(event.pos):
                        pass
            screen.blit(butimg2,butrect2)
        if self.pausebut == True:
            pass

        pygame.display.flip() 

main_screen = layout(main_bg, main_bg2, False, False, True)
icon_select = layout(main_bg, main_bg2, True, False, False)
lvl_select = layout(main_bg, main_bg2, True, False, True)
lvl_1 = layout(first_bg, first_bg2, False, True, False)
lvl_2 = layout(second_bg, second_bg2, False, True, False)
lvl_3 = layout(third_bg, third_bg2, False, True, False)


while start:
    print (start)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            start = False
            pygame.quit()
            sys.exit()

    main_screen.create_screen()
    clock.tick(FPS)




pygame.quit()
sys.exit


enter code here
PythonUser

In your function create_screen() you need to add a line that says global start to tell the python interpreter that variable was defined in a different scope.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does my while loop keep executing even when the value is true?

Why is my boolean always true?

Why doesn't my boolean variable get set to True?

Why does my 'for' loop break due to a boolean set-membership check, and how do I fix it?

Why does my array of objects keep being added undefined items in it?

PHP & MySQLi OOP - Why is my logged in variable not being set to true?

Why does my new form keep moving to the back?

Why does my function keep coming back as undefined?

Why is my for loop being ignored?

Why is my If statement being ignored in my For loop?

Why is my Boolean not being assigned the correct value?

Why does my while loop ignore the condition and keep going forever?

Why does my file keep closing after the first loop in python

Why does my while loop execute even though condition is not true?

Why is my boolean statement always evaluating to true?

Why does my app crash when firebase rules are set to true?

Why does my variable set in a do loop disappear? (unix shell)

Why does my if condition in my for loop stop validation after one iteration is true?

Why is my for loop being ignored inside of this function?

Why is the code after my for loop being ignored?

In React why does my boolean repeats itself?

Why does my custom type evaluate to boolean?

Why does my boolean not evaluate correctly?

Why are AndroidManifest permissions not being set in my application

Why is my program called "set" not being executed?

why is my state not being set in this component

why is my batch variable being set with a space?

Why deos my "While" loop keep going on?

Why does my FirstFactorial program keep looping back to while condition even after the condition is not met