gpt4 book ai didi

python - 如何在 pygame 的一个表面上有多个输入框并将这些输入存储在变量中?

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

import pygame
pygame.init()

display_width = 1024
display_height = 600
display = pygame.display.set_mode((display_width,display_height))

blue = (0,0,200)
light_blue = (0,0,255)
black = (0,0,0)
red = (200,0,0,255)
light_red = (255,0,0)
white = (255,255,255)
light_purple = (139,0,139,255)
purple = (128,0,128,255)

smallfont = pygame.font.SysFont("comicsansms", 50)
medfont = pygame.font.SysFont("Algerian", 50)
largefont = pygame.font.SysFont("Algerian", 85)


altSmall = pygame.font.SysFont("Algerian",20)
altMed = pygame.font.SysFont("Algerian",30)
altLarge = pygame.font.SysFont("Algerian",75)
extrasmall = pygame.font.SysFont("comicsansms",15)


clock = pygame.time.Clock()

def text_objects(text, color,size = "small"):

textSurface = smallfont.render(text, True, black)

if size == "small":
textSurface = smallfont.render(text, True, color)
if size == "medium":
textSurface = medfont.render(text, True, color)
if size == "large":
textSurface = largefont.render(text, True, color)

if size == "altMed":
textSurface = altMed.render(text,True,color)

if size == "altSmall":
textSurface = altSmall.render(text,True,color)

if size == "medFont":
textSurface = medfont.render(text,True,color)

if size == "smallFont":
textSurface = smallfont.render(text,True,color)

if size == "extraSmall":
textSurface = extrasmall.render(text,True,color)

return textSurface, textSurface.get_rect()

def alt_med_pos_message_to_screen(msg,color,x,y,size = "smallfont"):
screen_text = smallfont.render(msg,True,color)
display.blit(screen_text,[x,y])

class Button():

text = str()
x = int()
y = int()
width = int()
height = int()
inactive_color = None
active_color = None
action = str()
active = bool()

def __init__(self,text,x,y,width,height,inactive_color,active_color,action,text_color,text_size,active):
self.text = text
self.x = x
self.y = y
self.width = width
self.height = height
self.inactive_color = inactive_color
self.active_color = active_color
self.action = action
self.text_color = text_color
self.text_size = text_size
self.active = active

def nothing():
pass

def handle_event(self,event):

if self.is_hovered() and event.type == pygame.MOUSEBUTTONDOWN:
self.action()

def text_to_button(self,surface):
textSurf, textRect = text_objects(self.text,self.text_color,self.text_size)
textRect.center = ((self.x+(self.width/2)), self.y+(self.height/2))
surface.blit(textSurf, textRect)


def is_hovered(self):
cur = pygame.mouse.get_pos()
return self.x + self.width > cur[0] > self.x and self.y + self.height > cur[1] > self.y

def drawButton(self, surface):

if self.active:
pygame.draw.rect(surface,self.inactive_color,[self.x,self.y,self.width,self.height])

if self.is_hovered() and self.active:
pygame.draw.rect(surface, self.active_color,[self.x,self.y,self.width,self.height])

self.text_to_button(surface)

def home_screen():

backgroundImg = 'C:/Users/student/Desktop/Geometry Calc/Images/programbackground.png'
statisticsImg = 'C:/Users/student/Desktop/Geometry Calc/Images/statistics.png'
algebraImg = 'C:/Users/student/Desktop/Geometry Calc/Images/algebra-math.png'
geometryImg = 'C:/Users/student/Desktop/Geometry Calc/Images/geometry.png'

background = pygame.image.load(backgroundImg)
statistics = pygame.image.load(statisticsImg)
algebra = pygame.image.load(algebraImg)
geometry = pygame.image.load(geometryImg)

homeScreen = True
StatisticsButton = Button("Statistics",100,250,250,85,purple,light_purple,statistics_formulas,black,"smallfont",True)
GeometryButton = Button("Geometry",100,315,250,85,purple,light_purple,geometry_formulas,black,"smallfont",True)
AlgebraButton = Button("Algebra",100,390,250,75,purple,light_purple,algebra_formulas,black,"smallfont",True)
while homeScreen:
for evt in pygame.event.get():
StatisticsButton.handle_event(evt)
GeometryButton.handle_event(evt)
AlgebraButton.handle_event(evt)
if evt.type == pygame.QUIT:
homeScreen = False
pygame.quit()
quit()

display.blit(background,[0,0])
alt_med_pos_message_to_screen("Formula Hack",red,display_width/2-150,100,size = "smallfont")
StatisticsButton.drawButton(display)
GeometryButton.drawButton(display)
AlgebraButton.drawButton(display)

display.blit(statistics,[350,250])
display.blit(geometry,[350,325])
display.blit(algebra,[350,400])

pygame.display.update()

def statistics_formulas():

inStatistics = True
backgroundImg ='C:/Users/student/Desktop/Geometry Calc/Images/StatisticsBackground.png'

Zscore = Button("1. Z-score",900,25,75,35,purple,light_purple,zscore,black,"extraSmall",True)


statsBackground = pygame.image.load(backgroundImg)
while inStatistics:
for evt in pygame.event.get():
Zscore.handle_event(evt)
if evt.type == pygame.QUIT:
inStatistics = False
pygame.quit()
quit()

display.blit(statsBackground,[0,0])

Zscore.drawButton(display)
pygame.display.update()

def geometry_formulas():
print("geometry")

def algebra_formulas():
print("algebra")

def zscore():
screen = pygame.display.set_mode((display_width, display_height))
font = pygame.font.Font(None, 32)
clock = pygame.time.Clock()
input_box = pygame.Rect(100, 100, 140, 32)
color_inactive = pygame.Color('lightskyblue3')
color_active = pygame.Color('dodgerblue2')
color = color_inactive
active = False
text = "0"
done = False
input1 = int(text)

while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if input_box.collidepoint(event.pos):
# Toggle the active variable.
active = not active
else:
active = False
# Change the current color of the input box.
color = color_active if active else color_inactive
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
text = ""
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode

screen.fill((30, 30, 30))
# Render the current text.
txt_surface = font.render(text, True, color)
# Resize the box if the text is too long.
width = max(200, txt_surface.get_width()+10)
input_box.w = width
# Blit the text.
screen.blit(txt_surface, (input_box.x+5, input_box.y+5))

# Blit the input_box rect.
pygame.draw.rect(screen, color, input_box, 2)

pygame.display.flip()
clock.tick(30)

home_screen()

如果知道有帮助的话,我只需要存储整数、值,谢谢!问题所在的代码的主要部分位于函数“zScore”下的底部附近,但它确实使用了在整个代码中创建的一些类。如有任何帮助,我们将不胜感激。

我首先尝试做的是将变量文本存储在另一个变量中,并使用此输入变量执行基本操作,但当然输入框只接受字符串。然后我尝试使用 int() 函数尝试将输入框中输入的字符串转换为整数,但这也不起作用。我也无法在同一屏幕上使用多个输入框。

最佳答案

您只需在用户按 Enter 键时将 text 变量转换为 int 即可。这应该有效:

if event.key == pygame.K_RETURN:
try:
input1 = int(text)
except ValueError:
print('Invalid input')
text = ""

关于python - 如何在 pygame 的一个表面上有多个输入框并将这些输入存储在变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53089636/

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