帮我把下面代码添加注释class Button: def __init__(self, text='按钮', left=0, top=0, width=90, height=30, color=(200, 200, 200)): self.text = text self.color = color self.rect = pygame.Rect(left, top, width, height) self.font = pygame.font.Font(r"C:\Windows\Fonts\simsun.ttc", 16) self.text_surface = self.font.render(self.text, True, (0, 0, 0)) self.text_rect = self.text_surface.get_rect() # self.text_rect.center = self.rect.center self.active = False self.function = None button_group.append(self) def set_text(self, new_text): self.text = new_text self.text_surface = self.font.render(self.text, True, (0, 0, 0)) self.text_rect = self.text_surface.get_rect() def draw(self, screen): if self.active: pygame.draw.rect(screen, (180, 180, 200), self.rect, 0) else: pygame.draw.rect(screen, self.color, self.rect, 0) self.text_rect.center = self.rect.center screen.blit(self.text_surface, self.text_rect) def get_click(self, event): if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: mx, my = event.pos if self.rect.left <= mx <= self.rect.right and self.rect.top <= my <= self.rect.bottom: self.active = True elif event.type == pygame.MOUSEBUTTONUP and self.active: if event.button == 1: mx, my = event.pos if self.rect.left <= mx <= self.rect.right and self.rect.top <= my <= self.rect.bottom: # print(f'{self.text}按钮被点击') if callable(self.function): self.function() self.active = False def click_connect(self, function): self.function = function
时间: 2024-01-14 08:02:51 浏览: 139
以下是代码的注释:
```
class Button:
def __init__(self, text='按钮', left=0, top=0, width=90, height=30, color=(200, 200, 200)):
# 初始化函数,设置按钮的文本、位置、大小和颜色
self.text = text
self.color = color
self.rect = pygame.Rect(left, top, width, height) # 按钮的矩形区域
self.font = pygame.font.Font(r"C:\Windows\Fonts\simsun.ttc", 16) # 按钮文本的字体
self.text_surface = self.font.render(self.text, True, (0, 0, 0)) # 渲染按钮文本
self.text_rect = self.text_surface.get_rect() # 获取按钮文本的矩形区域
# self.text_rect.center = self.rect.center
self.active = False # 按钮是否被点击
self.function = None # 按钮被点击时调用的函数
button_group.append(self) # 将按钮对象添加到按钮组中
def set_text(self, new_text):
# 设置按钮的文本
self.text = new_text
self.text_surface = self.font.render(self.text, True, (0, 0, 0))
self.text_rect = self.text_surface.get_rect()
def draw(self, screen):
# 绘制按钮
if self.active:
pygame.draw.rect(screen, (180, 180, 200), self.rect, 0)
else:
pygame.draw.rect(screen, self.color, self.rect, 0)
self.text_rect.center = self.rect.center
screen.blit(self.text_surface, self.text_rect)
def get_click(self, event):
# 获取鼠标点击事件
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mx, my = event.pos
if self.rect.left <= mx <= self.rect.right and self.rect.top <= my <= self.rect.bottom:
self.active = True
elif event.type == pygame.MOUSEBUTTONUP and self.active:
if event.button == 1:
mx, my = event.pos
if self.rect.left <= mx <= self.rect.right and self.rect.top <= my <= self.rect.bottom:
# print(f'{self.text}按钮被点击')
if callable(self.function):
self.function()
self.active = False
def click_connect(self, function):
# 将按钮与函数连接,当按钮被点击时调用函数
self.function = function
```
阅读全文