给伟人投票的pygame电子投票程序

"""
   给伟人投票的电子投票程序
"""

import time
import pygame
from pygame.locals import *

def greenen(image):
    """去掉红色和蓝色,保留绿色像素值"""
    image_3Darray = pygame.surfarray.array3d(image) # 转为矩阵
    
    image_3Darray[:,:,:1] = 0
    return pygame.surfarray.make_surface(image_3Darray)
    
class Sprite(pygame.sprite.Sprite):
    """继承自pygame.sprite.Sprite基类的Sprite类"""
    pygame.font.init()
    vfont = pygame.font.Font("msyh.ttf",32)
    
    def __init__(self,image,pos,screen):
        """
           image:图像,pos:坐标,screen:屏幕
        """
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        self.image = image
        self.rect = image.get_rect(center=pos)
        self.votes = 0                # 得票数
        self.image2 = greenen(image)
        self.index = 0
        self.images = [self.image,self.image2]       
        self.interval = 1             # 投票间隔时间
        self.start_time = time.time()
        self.make_votes_image()
        
    def update(self):
        """更新"""
        pass   # 仅为会员查看
            
    def make_votes_image(self):
        """生成新的投票数图层"""
        self.votes_image = Sprite.vfont.render(str(self.votes),True,(255,255,0))
        self.votes_rect = self.votes_image.get_rect()
        self.votes_rect.center = self.rect.center
        self.votes_rect.centery -= 200

    def draw(self):
        """重写draw方法"""
        self.screen.blit(self.image,self.rect)
        self.screen.blit(self.votes_image,self.votes_rect)

def main():

    width,height = 480,360
    background = pygame.image.load("images/背景1.png")
    毛泽东图 = pygame.image.load("images/毛泽东.png")
    孙中山图 = pygame.image.load("images/孙中山.png")
    蒋介石图 = pygame.image.load("images/蒋介石.png")
    
    screen = pygame.display.set_mode((width,height))
    pygame.display.set_caption("给伟人投票交互程序by李兴球")

    group = pygame.sprite.Group()
    mzd = Sprite(毛泽东图,(100,280),screen)
    szs = Sprite(孙中山图,(250,280),screen)
    jjs = Sprite(蒋介石图,(400,280),screen)
    
    group.add(mzd)
    group.add(szs)
    group.add(jjs)    

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

        screen.blit(background,(0,0))
        [s.draw() for s in group]
        pygame.display.update()
        
    pygame.quit()    
    

if __name__== "__main__":

    main()
    

    

pygame电子投票