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()
上面是一個作業函式,使螢屏上有一個文本框。現在,你如何在同一螢屏上創建兩個文本框,而不是只復制并粘貼相同的代碼兩次?
我的想法是,單擊或激活的文本框將執行事件部分中的內容,因此您不必重復所有內容。但是,我不知道如何實作它。
在此先感謝
uj5u.com熱心網友回復:
您可以創建方法類,它在吸引它并處理事件。然后你可以用很多次
全部作業示例
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)
uj5u.com熱心網友回復:
你可以創建一個類,并在需要不同的輸入框進行實體化多次。
是這樣的:
# 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
uj5u.com熱心網友回復:
感謝分享 學習到了~轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/253191.html
標籤:其他技術討論專區
下一篇:自動駕駛何時能夠實作
