如何在pygame中实现两个输入框,而不必重复代码

def InputBox(): 

font = pygame.font.Font(None, 32)

inputBox = pygame.Rect(50, 50, 140, 32)

colourInactive = pygame.Color('lightskyblue3')

colourActive = pygame.Color('dodgerblue2')

colour = colourInactive

text = ''

active = False

isBlue = True

while True:

for event in pg.event.get():

if event.type == pg.QUIT:

pg.quit()

sys.exit()

if event.type == pygame.MOUSEBUTTONDOWN:

if inputBox.collidepoint(event.pos):

active = not active

else:

active = False

colour = colourActive if active else colourInactive

if event.type == pygame.KEYDOWN:

if active:

if event.key == pygame.K_RETURN:

print(text)

text = ''

elif event.key == pygame.K_BACKSPACE:

text = text[:-1]

else:

text += event.unicode

screen.fill(screenGray)

txtSurface = font.render(text, True, colour)

width = max(200, txtSurface.get_width()+10)

inputBox.w = width

screen.blit(txtSurface, (inputBox.x+5, inputBox.y+5))

pygame.draw.rect(screen, colour, inputBox, 2)

if isBlue:

color = (0, 128, 255)

else:

color = (255, 100, 0)

pg.display.flip()

clock.tick(60)

InputBox()

上面是一个工作函数,使屏幕上有一个文本框。现在,你如何在同一屏幕上创建两个文本框,而不是只复制并粘贴相同的代码两次?如何在pygame中实现两个输入框,而不必重复代码

我的想法是,单击或激活的文本框将执行事件部分中的内容,因此您不必重复所有内容。但是,我不知道如何实现它。

在此先感谢

回答:

您可以创建方法类,它在吸引它并处理事件。然后你可以用很多次

全部工作示例

import pygame 

class InputBox():

def __init__(self, x, y):

self.font = pygame.font.Font(None, 32)

self.inputBox = pygame.Rect(x, y, 140, 32)

self.colourInactive = pygame.Color('lightskyblue3')

self.colourActive = pygame.Color('dodgerblue2')

self.colour = self.colourInactive

self.text = ''

self.active = False

self.isBlue = True

def handle_event(self, event):

if event.type == pygame.MOUSEBUTTONDOWN:

self.active = self.inputBox.collidepoint(event.pos)

self.colour = self.colourActive if self.active else self.colourInactive

if event.type == pygame.KEYDOWN:

if self.active:

if event.key == pygame.K_RETURN:

print(self.text)

self.text = ''

elif event.key == pygame.K_BACKSPACE:

self.text = self.text[:-1]

else:

self.text += event.unicode

def draw(self, screen):

txtSurface = self.font.render(self.text, True, self.colour)

width = max(200, txtSurface.get_width()+10)

self.inputBox.w = width

screen.blit(txtSurface, (self.inputBox.x+5, self.inputBox.y+5))

pygame.draw.rect(screen, self.colour, self.inputBox, 2)

if self.isBlue:

self.color = (0, 128, 255)

else:

self.color = (255, 100, 0)

# --- main ---

def mainloop():

# create objects

input1 = InputBox(50, 50)

input2 = InputBox(450, 50)

clock = pygame.time.Clock()

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

# handle every event

input1.handle_event(event)

input2.handle_event(event)

screen.fill((128,128, 128))

# draw it

input1.draw(screen)

input2.draw(screen)

pygame.display.flip()

clock.tick(60)

pygame.init()

screen = pygame.display.set_mode((800,600))

mainloop()


BTW:更多的投入,你可以让他们在列表(如在GUI框架)

def mainloop(): 

widgets = [

InputBox(50, 50),

InputBox(450, 50),

InputBox(50, 150),

InputBox(450, 150),

InputBox(50, 250),

InputBox(450, 250),

InputBox(50, 350),

InputBox(450, 350),

]

clock = pygame.time.Clock()

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

for child in widgets:

child.handle_event(event)

screen.fill((128,128, 128))

for child in widgets:

child.draw(screen)

pygame.display.flip()

clock.tick(60)

回答:

你可以创建一个类,并在需要不同的输入框进行实例化多次。

是这样的:

# pseudocode # 

class InputBox(pygame.Rect):

def __init__(self, position=(50, 50, 140, 32),

font=pygame.font.Font(None, 32),

color_inactive=pygame.Color('lightskyblue3'),

color_active=pygame.Color('dodgerblue2'),

text = '',

active=False)

super().__init__(position) #<- this may need to be adjusted to pygame Rect specs

self.font = font

self.color_inactive = color_inactive

self.color_active = color_active

self.color = color_inactive

self.text = text

self.active = active

以上是 如何在pygame中实现两个输入框,而不必重复代码 的全部内容, 来源链接: utcz.com/qa/257183.html

回到顶部