Bogdan Andrei
Bogdan Andrei

Reputation: 55

can't make call function to make a quit box appear in pygame

I'm trying to make it if you press ESC a quit box appears with the text "Do you want to quit?" on it. If the user clicks y the program ends. I think the first problem may be in the quitBox function but I don't know at all what is wrong in it. To me the function name and variables look okay. when i call the function it says that

name 'quitBoxPosX' is not defined

I think same goes with quitBoxPosY

Here's the code:

import pygame
import random
pygame.init()

# variables
mainLoop = True
font1 = pygame.font.SysFont('comicsansms', 25)
font2 = pygame.font.SysFont('underline', 35)
white = [255, 255, 255]
green = [0, 150, 0]
gray = [200, 200, 200]
black = [0, 0, 0]
clickNum = 0
clickAmount = 1
FPS = pygame.time.Clock()
pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, 2)
screen = pygame.display.set_mode((1300, 700))

# functions
def switchButton01(events, buttonPlusPos):
    global button02
    button02 = pygame.transform.scale(button02, (100, 100))
    screen.blit(button02, [580, 350])
    for event in events:
        if event.type == pygame.MOUSEBUTTONDOWN:
            global clickNum
            clickNum += 1
            global clickplusx, clickplusy
            clickplusx = mouse
            clickplusy = mouse
            return (clickplusx, clickplusy)
    return buttonPlusPos

def quitBox():
    global quitBoxImage
    global quitBoxPosX, quitBoxPosY
    quitBoxImage = pygame.transform.scale(quitBoxImage, (300, 200))
    quitBoxPosX = 430
    quitBoxPosY = 200
    return (quitBox, quitBoxPosX, quitBoxPosY)

# load images
button01 = pygame.image.load('button_100.png')
button02 = pygame.image.load('button_100hovered.png')
icon = pygame.image.load('icon_128.png')
buttonPlusImage = pygame.image.load('buttonPlus_32.png')
upgradeIcon = pygame.image.load('upgradesicon_64.png')
quitBoxImage = pygame.image.load('emptyGUI.png')

# title, icon
pygame.display.set_caption("incremental button")
pygame.display.set_icon(icon)

buttonPlusPos = None
while mainLoop:
    pygame.display.flip()
    FPS.tick(144)
    screen.fill(white)

    # actual content in the game
    events = pygame.event.get()
    mouse = pygame.mouse.get_pos()

    # define objects
    button01 = pygame.transform.scale(button01, (100, 100))
    click_counter_text = font1.render("Click counter: ", True, black, white)
    button01rect = button01.get_rect(center=(630, 400))
    button01collidepoint = button01rect.collidepoint(pygame.mouse.get_pos())
    click_counter = font1.render((str(clickNum)), True, black, white)
    upgradeIcon = pygame.transform.smoothscale(upgradeIcon, (30, 30))
    upgrades = font2.render('UPGRADES:', True, black)
    FPScounter = font1.render('Frames per second:' + str(int(FPS.get_fps())), True, black, white)


    # show objects
    screen.blit(FPScounter, [20, 10])
    screen.blit(button01, [580, 350])
    screen.blit(click_counter_text, [525, 50])
    upgradesBG = pygame.draw.rect(screen, gray, (1060, 44, 193, 600))
    screen.blit(upgradeIcon, [1064, 46])
    screen.blit(upgrades, [1100, 50])
    screen.blit(click_counter, [700, 50])

    if button01collidepoint:
        buttonPlusPos = switchButton01(events, buttonPlusPos)
    if buttonPlusPos:
        screen.blit(buttonPlusImage, buttonPlusPos)

    # quits
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            quitBoxPos = quitBox(quitBoxPosX, quitBoxPosY)
            if quitBoxPos:
                screen.blit(quitBoxImage(quitBoxPosX, quitBoxPosY))
                font1.render(quitBox, 'Do you want to quit?', True, black, white)
                if event.type == pygame.KEYDOWN and event.key == pygame.K_y:
                    pygame.quit()
                    quit()





Upvotes: 1

Views: 51

Answers (1)

Rabbid76
Rabbid76

Reputation: 210948

quitBox has to return a the quit box data tuple, which consist of the image and the position of the box:

def quitBox():
    img = pygame.transform.scale(quitBoxImage, (300, 200))
    text = font1.render('Do you want to quit?', True, black, white)
    img.blit(text, text.get_rect(center = img.get_rect().center))
    return img, (430, 200)

You have to initialize the variable quitBoxData before the main application loop and to draw the box in the loop if quitBoxData is set:

quitBoxData = None
buttonPlusPos = None
while mainLoop:
    # [...]

    if quitBoxData:
        screen.blit(*quitBoxData)

    # [...]

When a KEYDOWN event occurs, then you have to do different things, dependent if quitBoxData is set or not. If it is not set and ESC is pressed, then you have to invoke quitBox and to get the data. If quitBoxData is not set an y is pressed, then leave the application (mainLoop = False) else continue (quitBoxData = None):

while mainLoop:
    # [...]

    # quits
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN: 
            if quitBoxData:
                if event.key == pygame.K_y:
                    mainLoop = False
                else:
                    quitBoxData = None
            else:
                if event.key == pygame.K_ESCAPE:
                    quitBoxData = quitBox()

Full application code:

import pygame
import random
pygame.init()

# variables
mainLoop = True
font1 = pygame.font.SysFont('comicsansms', 25)
font2 = pygame.font.SysFont('underline', 35)
white = [255, 255, 255]
green = [0, 150, 0]
gray = [200, 200, 200]
black = [0, 0, 0]
clickNum = 0
clickAmount = 1
FPS = pygame.time.Clock()
pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, 2)
screen = pygame.display.set_mode((1300, 700))

# functions
def switchButton01(events, buttonPlusPos):
    global button02
    button02 = pygame.transform.scale(button02, (100, 100))
    screen.blit(button02, [580, 350])
    for event in events:
        if event.type == pygame.MOUSEBUTTONDOWN:
            global clickNum
            clickNum += 1
            global clickplusx, clickplusy
            clickplusx = mouse
            clickplusy = mouse
            return (clickplusx, clickplusy)
    return buttonPlusPos

def quitBox():
    img = pygame.transform.scale(quitBoxImage, (300, 200))
    text = font1.render('Do you want to quit?', True, black, white)
    img.blit(text, text.get_rect(center = img.get_rect().center))
    return img, (430, 200)

# load images
button01 = pygame.image.load('button_100.png')
button02 = pygame.image.load('button_100hovered.png')
icon = pygame.image.load('icon_128.png')
buttonPlusImage = pygame.image.load('buttonPlus_32.png')
upgradeIcon = pygame.image.load('upgradesicon_64.png')
quitBoxImage = pygame.image.load('emptyGUI.png')

# title, icon
pygame.display.set_caption("incremental button")
pygame.display.set_icon(icon)

quitBoxData = None
buttonPlusPos = None
while mainLoop:
    pygame.display.flip()
    FPS.tick(144)
    screen.fill(white)

    # actual content in the game
    events = pygame.event.get()
    mouse = pygame.mouse.get_pos()

    # define objects
    button01 = pygame.transform.scale(button01, (100, 100))
    click_counter_text = font1.render("Click counter: ", True, black, white)
    button01rect = button01.get_rect(center=(630, 400))
    button01collidepoint = button01rect.collidepoint(pygame.mouse.get_pos())
    click_counter = font1.render((str(clickNum)), True, black, white)
    upgradeIcon = pygame.transform.smoothscale(upgradeIcon, (30, 30))
    upgrades = font2.render('UPGRADES:', True, black)
    FPScounter = font1.render('Frames per second:' + str(int(FPS.get_fps())), True, black, white)


    # show objects
    screen.blit(FPScounter, [20, 10])
    screen.blit(button01, [580, 350])
    screen.blit(click_counter_text, [525, 50])
    upgradesBG = pygame.draw.rect(screen, gray, (1060, 44, 193, 600))
    screen.blit(upgradeIcon, [1064, 46])
    screen.blit(upgrades, [1100, 50])
    screen.blit(click_counter, [700, 50])

    if button01collidepoint:
        buttonPlusPos = switchButton01(events, buttonPlusPos)
    if buttonPlusPos:
        screen.blit(buttonPlusImage, buttonPlusPos)
    if quitBoxData:
        screen.blit(*quitBoxData)

    # quits
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN: 
            if quitBoxData:
                if event.key == pygame.K_y:
                    mainLoop = False
                else:
                    quitBoxData = None
            else:
                if event.key == pygame.K_ESCAPE:
                    quitBoxData = quitBox()

Upvotes: 1

Related Questions