gpt4 book ai didi

python - 无法调用函数使退出框出现在 pygame 中

转载 作者:行者123 更新时间:2023-12-01 06:21:12 28 4
gpt4 key购买 nike

我试图做到这一点,如果您按ESC,则会出现一个退出框,其中包含文本“您想退出吗?”在上面。如果用户单击y,程序就会结束。我认为第一个问题可能出在 quitBox 函数中,但我根本不知道它出了什么问题。对我来说,函数名称和变量看起来不错。当我调用该函数时,它说

name 'quitBoxPosX' is not defined

我认为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()

关于python - 无法调用函数使退出框出现在 pygame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60344363/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com