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()
上記は、テキストボックスを持つ画面を作るための作業関数です。今、同じコードを2回コピーして貼り付けることなく、同じ画面に2つのテキストボックスを作成する方法はありますか?コードを繰り返しなくても、pygameで2つの入力ボックスを実装する方法
私の考えでは、クリックされた、またはアクティブ化されたテキストボックスは、イベント部分の内容を処理するので、すべてを2回繰り返す必要はありません。しかし、私はそれをどのように実装するか考えていません。事前