Pygame电子菜单制作_按钮类

"""
   电子菜单制作
"""
import pygame
from pygame.locals import *

class Button(pygame.sprite.Sprite):
    def __init__(self,name,size,pos,title_surface,bgcolors):
        """
           name:名字,size:宽高二元组,pos:渲染坐标,
           title_surface:字体图,bgcolor:背景颜色
        """
        self.name = name
        pygame.sprite.Sprite.__init__(self)
        self.bgcolors= bgcolors           # 两种颜色轮换
        self.index = 0
        self.image0 = pygame.Surface(size)# 新建第一个造型
        self.image0.fill(bgcolors[0])
        self.image1 = pygame.Surface(size)# 新建第二个造型
        self.image1.fill(bgcolors[1])
        # 由于两个造型大小相同,这里让它们共用一个中心点
        self.rect = self.image0.get_rect(center=pos)
        x = self.rect.width//2           # 矩形对象中心点x坐标
        y = self.rect.height//2          # 矩形对象中心点y坐标
        
        r = title_surface.get_rect()      # 标题的矩形对象
        r.center = (x,y)                  # 把(x,y)设为按钮中心点
        
        self.image0.blit(title_surface,r) # 把标题渲染到image0
        self.image1.blit(title_surface,r) # 把标题渲染到image1
        self.images = self.image0,self.image1
        self.image = self.images[self.index]
        self.collide_cursor = False
        
    def update(self):
        """
           更新造型
        """        
        mpos = pygame.mouse.get_pos()
        if self.rect.collidepoint(mpos):   # 碰到鼠标指针
            self.index = 1
            self.image = self.images[self.index]
            self.collide_cursor = True
            pygame.display.set_caption(self.name)            
        else:
            self.index = 0
            self.image = self.images[self.index]
            self.collide_cursor = False
            
            

size = width,height = 480,360
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame电子菜单制作by lixingqiu")

start_name = '开始游戏'
help_name = '帮助help'
exit_name = '关闭窗口'

myfont = pygame.font.Font("msyh.ttf",16)
start_string = myfont.render(start_name,True,(255,0,0))
help_string  = myfont.render(help_name,True,(25,0,255))
exit_string = myfont.render(exit_name,True,(0,0,255))

pos = width//2,100
bgcolors = [(128,132,251),(80,232,51)]
start_button = Button(start_name,(120,40),pos,start_string,bgcolors)

pos = width//2,150
help_button = Button(help_name,(120,40),pos,help_string,bgcolors)

pos = width//2,200
exit_button = Button(exit_name,(120,40),pos,exit_string,bgcolors)

group = pygame.sprite.Group()
group.add(start_button)
group.add(help_button)
group.add(exit_button)

running = True
while running:
    for event in pygame.event.get():
        if event.type == QUIT:running = False

    group.update()
    ms = pygame.mouse.get_pressed()
    for button in group:
        if button.collide_cursor and ms[0]:
           if button.name== exit_name:
              print('关闭窗口')               
           if button.name== start_name:
              print('进入游戏程序')               
           if button.name == help_name:
              print("显示相关帮助信息")
           running = False
    
    group.draw(screen)
    pygame.display.update()

pygame.quit()

Pygame电子菜单制作button类