我正在开始屏幕上进行按钮,但它们不会显示。我制作了按钮类,其中包含按钮所需的一切。我敢打赌,我班上的draw函数出了点问题。有谁知道如何解决这一问题?如果还有其他错误,请通知我。谢谢!
class Button(object):
def __init__(self, text, text_size, text_color, x, y, width, height, ic, ac):
self.text = text
self.text_size = text_size
self.text_color = text_color
self.x = x
self.y = y
self.width = width
self.height = height
self.ic = ic
self.ac = ac
self.inactive_button = pygame.Surface((width, height))
self.inactive_button.fill(ic)
self.active_button = pygame.Surface((width, height))
self.active_button.fill(ac)
self.button_rect = self.inactive_button
self.rect = self.button_rect.get_rect()
font_name = pygame.font.match_font('arial')
font = pygame.font.Font(font_name, text_size)
font_text = font.render(text, True, text_color)
text_rect = font_text.get_rect(center=self.rect.center)
self.inactive_button.blit(font_text, text_rect)
self.active_button.blit(font_text, text_rect)
self.rect.topleft = (x, y)
self.hovered = False
# self.clicked = False
def update(self):
if self.hovered:
self.button_rect = self.active_button
else:
self.button_rect = self.inactive_button
def draw(self, screen):
screen.blit(self.button_rect, self.rect)
def handle_event(self, event, command=None):
mouse = pygame.mouse.get_pos()
if event.type == pygame.MOUSEMOTION:
self.hovered = self.rect.collide_rect(mouse, self.rect)
if event.type == pygame.MOUSEBUTTONDOWN:
if command == 'play' and command is not None:
game_loop()
elif command == 'quit' and command is not None:
pygame.quit()
quit()
elif command == 'instructions' and command is not None:
instructions()
elif command == 'credits' and command is not None:
credits()
elif command == 'back' and command is not None:
start_screen()
def start_screen():
start = True
while start:
clock.tick(FPS)
for events in pygame.event.get():
if events.type == pygame.QUIT:
pygame.quit()
quit()
Button("Play", 55, WHITE, 180, 285, 120, 60, BLUE, LIGHT_BLUE)
Button("Quit", 30, BLUE, 202, 450, 80, 40, WHITE, GRAY)
Button("Credits", 35, BLACK, 310, 45, 140, 30, WHITE, GRAY)
Button("Instructions", 30, BLACK, 20, 45, 140, 30, WHITE, GRAY)
start_screen()
答案 0 :(得分:1)
它的工作原理与您先前的问题不同。
您必须在while True
之前和for event
内部创建实例,使用b1.handle_event(events)
检查是否单击了按钮,在for event
之后使用b1.draw()
绘制它(在清除屏幕之后以及在display.flip()之前)
def start_screen():
start = True
b1 = Button("Play", 55, WHITE, 180, 285, 120, 60, BLUE, LIGHT_BLUE)
b2 = Button("Quit", 30, BLUE, 202, 450, 80, 40, WHITE, GRAY)
b3 = Button("Credits", 35, BLACK, 310, 45, 140, 30, WHITE, GRAY)
b4 = Button("Instructions", 30, BLACK, 20, 45, 140, 30, WHITE, GRAY)
while start:
clock.tick(FPS)
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
b1.handle_event(event)
b2.handle_event(event)
b3.handle_event(event)
b4.handle_event(event)
# - updates (without draws) -
b1.update()
b2.update()
b3.update()
b4.update()
# - draws (without updates) -
screen.fill((0,0,0))
b1.draw(screen)
b2.draw(screen)
b3.draw(screen)
b4.draw(screen)
pygame.display.flip()
编辑:
您有错误的缩进,并且不检查是否单击了悬停按钮,因此一键单击可能一次按下所有按钮。
def handle_event(self, event, command=None):
mouse = pygame.mouse.get_pos()
if event.type == pygame.MOUSEMOTION:
self.hovered = self.rect.collide_rect(mouse, self.rect)
if event.type == pygame.MOUSEBUTTONDOWN:
if self.hovered:
if command == 'play' and command is not None:
game_loop()
elif command == 'quit' and command is not None:
pygame.quit()
quit()
elif command == 'instructions' and command is not None:
instructions()
elif command == 'credits' and command is not None:
credits()
elif command == 'back' and command is not None:
start_screen()
但是为了使按钮更通用-您可以分配函数的名称而不是字符串
def handle_event(self, event, command=None):
mouse = pygame.mouse.get_pos()
if event.type == pygame.MOUSEMOTION:
self.hovered = self.rect.collide_rect(mouse, self.rect)
if event.type == pygame.MOUSEBUTTONDOWN:
if self.hovered and command is not noen:
command()
甚至将函数名称放在__init__