如果您按ESC键,则会尝试显示一个退出框,并显示文本“您要退出吗?”。在上面。如果用户单击y,程序结束。我认为第一个问题可能在quitBox函数中,但我根本不知道它有什么问题。对我来说,函数名称和变量看起来还可以。当我调用该功能时,它说


  名称'quitBoxPosX'未定义


我认为quitBoxPosY也是如此

这是代码:

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





最佳答案

quitBox必须返回一个退出框数据元组,该元组由图像和框的位置组成:

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)


您必须在主应用程序循环之前初始化变量quitBoxData,并在设置了quitBoxData的情况下在循环中绘制框:

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

    if quitBoxData:
        screen.blit(*quitBoxData)

    # [...]


发生KEYDOWN事件时,取决于是否设置quitBoxData,您必须做不同的事情。如果未设置并且按ESC,则必须调用quitBox并获取数据。如果未设置quitBoxData,则按y,然后离开应用程序(mainLoop = False),否则继续(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()


完整的应用程序代码:

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

10-06 05:19