美文网首页
Day-19 pygame相关操作

Day-19 pygame相关操作

作者: 水果坚果燕麦片 | 来源:发表于2019-01-17 14:06 被阅读0次

    鼠标相关事件

    import pygame,random
    from color import Color
    '''
    1.鼠标相关的事件
    鼠标事件要关注事件发生的位置:event.pos
    '''
    
    def base_game():
        pygame.init()
        window = pygame.display.set_mode((400,600))
        pygame.display.set_caption('lost castle')
        window.fill(Color.white)
    
        pygame.display.flip()
        # flag = False
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    print('鼠标按下', event.pos)
                    pygame.draw.circle(window,Color.rand_color(),event.pos,random.randint(10,20))
                    pygame.display.update()
                    # flag = True
                elif event.type == pygame.MOUSEBUTTONUP:
                    print('鼠标弹起')
                    # flag = False
                elif event.type == pygame.MOUSEMOTION:
                    # if flag:
                    pass
    
    
    def main():
        base_game()
    
    
    if __name__ == '__main__':
        main()
    

    期间封装一个颜色类

    from random import randint
    
    
    class Color(object):
        white = (255,255,255)
        black = (0,0,0)
        red = (255,0,0)
        green = (0,255,0)
        blue = (0,0,255)
        yellow = (255,255,0)
        gray = (155,155,125)
    
        @staticmethod
        def rand_color():
            return randint(0,255),randint(0,255),randint(0,255)
    
    
    def main():
        pass
    
    
    if __name__ == '__main__':
        main()
    

    按钮

    import pygame
    from color import Color
    from random import randint
    
    
    class Button:
        def __init__(self, x, y, width, height, text='', background_color=Color.red, text_color=Color.white):
            self.x = x
            self.y = y
            self.width = width
            self.height = height
            self.text = text
            self.background_color = background_color
            self.text_color = text_color
            self.font_size = 30
    
        def show(self, window):
            pygame.draw.rect(window, self.background_color, (self.x, self.y, self.width, self.height))
            font = pygame.font.SysFont('Times', self.font_size)
            text = font.render(self.text, True, self.text_color)
            w, h = text.get_size()
            x = self.width / 2 - w / 2 + self.x
            y = self.height / 2 - h / 2 + self.y
            window.blit(text, (x, y))
    
        def is_cliecked(self, pos):
            x, y = pos
            return  (self.x <= x <= self.x + self.width) and (self.y <= y <= self.y + self.height)
    
    
    def main():
        pygame.init()
        window = pygame.display.set_mode((400, 600))
        pygame.display.set_caption('事件')
        window.fill(Color.white)
    
        # add_btn(window)
        add_btn = Button(100, 100, 100, 50, 'del')
        add_btn.show(window)
    
        btn2 = Button(100, 250, 100, 60, 'Score', background_color=Color.yellow, text_color=Color.black)
        btn2.show(window)
    
        pygame.display.flip()
        is_move = False
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    mx, my = event.pos
                    if add_btn.is_cliecked(event.pos):
                        print('删除!')
                        continue
                    if btn2.is_cliecked(event.pos):
                        # print('hello')
                        btn2.text = str(randint(0, 100))
                        btn2.show(window)
                        pygame.display.update()
                        continue
    
    
    
    if __name__ == '__main__':
        main()
    

    相关文章

      网友评论

          本文标题:Day-19 pygame相关操作

          本文链接:https://www.haomeiwen.com/subject/rvgbdqtx.html