我不确定我是否正确使用类。我正在尝试使用 pygame 构建一个简单的菜单。这是我第一次涉足 gui 的东西。我不知道如何构建我的代码。
我发现我可以创建一个通用的 Button 类来处理所有鼠标悬停/鼠标单击的内容,然后我可以为每个按钮子类化它,并重写该do_action
方法以给每个按钮一个特定的操作。
class Button(pygame.sprite.Sprite):
global screen_flags
def __init__(self, images, pos):
pygame.sprite.Sprite.__init__(self)
self.images = images
self.image = images[0]
self.rect = self.image.get_rect()
self.rect.move_ip(pos)
def update(self, events, surface):
# if not screen_flags['config']:
for event in events:
if event.type == MOUSEMOTION:
if self.rect.collidepoint(event.pos):
self.image = self.images[1]
else:
self.image = self.images[0]
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1 and self.rect.collidepoint(event.pos):
self.image = self.images[-1]
screen_flags['config'] = 1
self.do_action()
self.set_flag()
elif event.type == MOUSEBUTTONUP:
self.image = self.images[0]
screen.blit(self.image, self.rect)
def do_action(self):
pass
def set_flag(self):
pass
class CheckBox(Button):
def __init__(self, images, pos):
Button.__init__(self, images, pos)
self.is_clicked = False
def update(self, events, surface):
for event in events:
if event.type == MOUSEMOTION:
if not self.is_clicked:
if self.rect.collidepoint(event.pos):
self.image = self.images[1]
else:
self.image = self.images[0]
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1 and self.rect.collidepoint(event.pos):
if not self.is_clicked:
self.image = self.images[-1]
self.is_clicked = True
else:
self.image = self.images[0]
self.is_clicked = False
screen.blit(self.image, self.rect)
class Cancel(Button):
def do_action(self):
screen_flags['config'] = 0
所以,正如你所看到的,他们还没有真正做任何事情。我可以打开和关闭复选框,打开和关闭一个“配置”窗口,但这就是我所得到的。其余代码:
global count
global sTime
config_button_img = load_sliced_images(25, 25, c_buttons)
config_button = Button(config_button_img, (608,4))
input_bar = load_sliced_images(351,33, inpt_bar)
text_box = Textbox(input_bar, (144,155))
s_button = load_sliced_images(110,32, sbmt_bttn)
submit = Button(s_button, (241,301))
c_button = load_sliced_images(110,32, cncl_bttn)
cancel = Cancel(c_button, (385, 301))
c_boxes = load_sliced_images(20,19, chk_box)
check_box = CheckBox(c_boxes, (200,200))
try:
while True:
# ****************************
# Pygame section
events = pygame.event.get()
for event in events:
if event.type == QUIT:
screen_flags['alert'] = 1
ding.play(0,1000)
elif event.type == MOUSEBUTTONDOWN:
text_box.set_focus(event.button, event.pos)
screen.blit(background, (0,0))
screen.blit(client_logo, (0,30))
screen.blit(tag, (174,462))
if screen_flags['config']:
screen.blit(config_window_img, (0,0))
submit.update(events, screen)
cancel.update(events, screen)
check_box.update(events, screen)
if screen_flags['alert']:
screen.blit(alert_dialog, (0,0))
config_button.update(events, screen)
pygame.display.update()
except KeyboardInterrupt:
try:
pygame.quit()
except:
pass
所以这有点像预期的那样。我不确定从这里去哪里。我是否继续在类中封装逻辑?例如,我尝试做的下一件事是,当单击“取消”按钮时,它会取消选中复选框。
我试图改变Cancel
班级来做到这一点。
class Cancel(Button):
def do_action(self):
screen_flags['config'] = 0
check_box.is_clicked=False
但是,这给了我一个 GlobalName 错误。如何从不同的类中定位实例方法?这甚至是正确的方法吗?或者只有一些逻辑,比如update()
照顾鼠标的东西,然后通过将变量传入和传出不同的类来处理main()
类的作用?我应该让所有的类都使用全局变量吗?
有没有关于gui实践的好文章。诸如如何构造代码等之类的事情?
希望以上是有道理的。