美文网首页
pygame事件

pygame事件

作者: pubalabala | 来源:发表于2018-07-30 20:45 被阅读0次

    pygame事件

    """__author__ == Jefferson"""
    import pygame
    from random import randint
    sizex = 1200
    sizey = 900
    
    def rand_color():
        return randint(0,255),randint(0,255),randint(0,255)
    #all_balls中保存多个球
    #每个球要保存: 半径, 圆心坐标, 颜色, x速度, y速度
    all_balls = [
        {
            'r': randint(10,20),
            'pos':(randint(0,sizex),randint(0,sizey)),
            'color': rand_color(),
            'x_speed': randint(-2,2),
            'y_speed': randint(-2,2)
        },
    {
            'r': randint(10,20),
            'pos':(randint(0,sizex),randint(0,sizey)),
            'color': rand_color(),
            'x_speed': randint(-2,2),
            'y_speed': randint(-2,2)
        },
    ]
    
    
    if __name__ == '__main__':
        pygame.init()
        screen = pygame.display.set_mode((sizex,sizey))
        screen.fill((255,255,255))
        pygame.display.flip()
    
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
    
                if event.type == pygame.MOUSEBUTTONDOWN:
                    ball = {
                        'r': randint(10,20),
                        'pos':event.pos,
                        'color': rand_color(),
                        'x_speed': randint(-2,2),
                        'y_speed': randint(-2,2)
                    }
                    all_balls.append(ball)
    
            #刷新界面
            screen.fill((255,255,255))
            for ball_dict in all_balls:
                x,y = ball_dict['pos']
                x_speed = ball_dict['x_speed']
                y_speed = ball_dict['y_speed']
                x += x_speed
                y += y_speed
                pygame.draw.circle(screen,ball_dict['color'],(x,y),ball_dict['r'])
                #更新球对应的坐标
                ball_dict['pos'] = x,y
    
                if x + ball_dict['r'] >= sizex:
                    x = sizex - ball_dict['r']
                    ball_dict['x_speed'] *= -1
                if x - ball_dict['r'] <= 0:
                    x = ball_dict['r']
                    ball_dict['x_speed'] *= -1
                if y + ball_dict['r'] >= sizey:
                    y = sizey - ball_dict['r']
                    ball_dict['y_speed'] *= -1
                if y - ball_dict['r'] <= 0:
                    y = ball_dict['r']
                    ball_dict['y_speed'] *= -1
    
                for ball_dict1 in all_balls:
                    if (ball_dict['pos'][0] - ball_dict1['pos'][0])**2 \
                        + (ball_dict['pos'][1] - ball_dict1['pos'][1])**2\
                        < (ball_dict['r'] + ball_dict1['r'])**2:
                        if ball_dict['r'] - ball_dict1['r'] > 0:
                            ball_dict['r'] += ball_dict1['r']//2
                            ball_dict1['r'] = 0
    
                        elif ball_dict['r'] - ball_dict1['r'] < 0:
                            ball_dict1['r'] += ball_dict['r'] // 2
                            ball_dict['r'] = 0
    
    
                if ball_dict['r'] > sizey//2:
                    screen.fill((255, 255, 255))
                    font = pygame.font.SysFont('Times', 100)
                    title = font.render('Game Over', True, (randint(0, 255), randint(0, 255), randint(0, 255)))
                    screen.blit(title, (sizex//2-230, sizey//2-80))
                    break
    
                i = 0
                while i< len(all_balls):
                    if all_balls[i]['r'] == 0:
                        del all_balls[i]
                        i -= 1
                    i += 1
    
            pygame.display.update()
    

    鼠标事件的应用

    """__author__ == Jefferson"""
    import pygame
    import random
    sizex = 600
    sizey = 600
    def color():
        return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
    
    def draw_ball(screen,point):#在点击的位置产生一个球
        pygame.draw.circle(screen, color(), point, random.randint(10, 20))
        pygame.display.flip()
        # pygame.displat.update(),更新屏幕同flip()
    
    #画一个按钮
    def draw_button(color,text):
        pygame.draw.rect(screen,color[0],(sizex//2-50,sizey//2-30,100,60))#矩形框
        font = pygame.font.SysFont('Times',30)
        title = font.render(text,True,color[1])
        screen.blit(title,(sizex//2-40,sizey//2-20))
        return color,text
    
    def is_point_in(point, rect):
        x,y = point
        rx, ry, rw, rh = rect
        if (rx <= x <= rx+rw) and (ry<=y<=ry+rh):
            return True
        return False
    
    if __name__ == '__main__':
        pygame.init()
        screen = pygame.display.set_mode((sizex,sizey))
        screen.fill((255,255,255))
        pygame.display.set_caption('鼠标事件')
    
        text = 'click!'
        color1 = color()
        color2 = color()
        button = draw_button((color1, color2), text)
        pygame.display.flip()
        while True:
            i = 0
            for event in pygame.event.get():
                #退出
                i += 1
                if event.type == pygame.QUIT:
                    exit()
    
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if is_point_in(event.pos,(sizex//2-50,sizey//2-30,100,60)):
                        text = 'click!'
                        color1 = color()
                        color2 = color()
                        button = draw_button((color1,color2),text)
                        draw_ball(screen,(random.randint(0,sizex),random.randint(0,sizey)))
                        pygame.display.flip()
    
                if event.type == pygame.MOUSEMOTION:
                    screen.fill((255,255,255))
                    draw_button(button[0],button[1])
                    draw_ball(screen,event.pos)
    
    
    """__author__ == Jefferson"""
    import pygame
    image = pygame.image.load('./1.jpg')
    image = pygame.transform.rotozoom(image, 0, 0.3)
    sizex = 600
    sizey = 600
    rect = [0,0,image.get_size()[0],image.get_size()[1]]
    
    def image_(screen,point = (135,135)):
        screen.blit(image, (point[0]-135,point[1]-135))
        return point[0]-135,point[1]-135,image.get_size()[0],image.get_size()[1]
    
    def is_point_in(point, rect):
        x,y = point
        rx, ry, rw, rh = rect
        if (rx <= x <= rx+rw) and (ry<=y<=ry+rh):
            return True
        return False
    
    if __name__ == '__main__':
        pygame.init()
        screen = pygame.display.set_mode((sizex, sizey))
        screen.fill((255, 255, 255))
        pygame.display.set_caption('鼠标事件2')
        image_(screen)
        pygame.display.flip()
        flag = False
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
    
                if event.type == pygame.MOUSEBUTTONDOWN:
                    flag = True
    
                if event.type == pygame.MOUSEBUTTONUP:
                    flag = False
    
    
    
                if event.type == pygame.MOUSEMOTION:
                    if flag == True and is_point_in(event.pos,rect):
                        screen.fill((255, 255, 255))
                        rect = image_(screen, event.pos)
                        pygame.display.flip()
    
    
    
    

    pygame动画

    """__author__ == Jefferson"""
    import pygame
    from random import randint
    sizex = 600
    sizey = 400
    
    def static_page(screen):
        font = pygame.font.SysFont('Times',40)
        title = font.render('welcome',True,(0,0,0))
        screen.blit(title,(200,200))
    
    def animation_title(screen):
        font = pygame.font.SysFont('Times',40)
        title = font.render('Python',True,(randint(0,255),randint(0,255),randint(0,255)))
        screen.blit(title,(300,300))
    
    if __name__ == '__main__':
        pygame.init()
        screen = pygame.display.set_mode((sizex,sizey))
        screen.fill((255,255,255))
    
        static_page(screen)
    
        pygame.display.flip()
    
        while True:
            #for中的代码只有在事件发生后才会执行
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
    
            pygame.time.delay(1)#在此处线程阻塞指定的时间
    
            #动画前要将原来的内容全部清空
            screen.fill((255,255,255))
            static_page(screen)
            animation_title(screen)
    
            #内容展示完成后要更新到屏幕上
            pygame.display.update()
    
    

    ballgame

    """__author__ == Jefferson"""
    import pygame
    from random import randint
    
    sizex = 600
    sizey = 400
    x_speed = 0
    y_speed = 0
    speed = 1
    Up = 273
    Down = 274
    Left = 276
    Right = 275
    
    def draw_ball(place,color,pos):
        pygame.draw.circle(place,color,pos,20,0)
        return pos
    
    def color():
        return randint(0, 255), randint(0, 255), randint(0, 255)
    
    if __name__ == '__main__':
        pygame.init()
        screen = pygame.display.set_mode((sizex,sizey))
        screen.fill((255,255,255))
    
        pygame.display.flip()
    
        pos = draw_ball(screen,color(),(randint(0,sizex),randint(0,sizey)))
    
        #初始坐标
        ball_x = 100
        ball_y = 100
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
    
                if event.type == pygame.KEYDOWN:
                    if event.key == Up:
                        y_speed = 0-speed
                        x_speed = 0
                    elif event.key == Down:
                        y_speed = speed
                        x_speed = 0
                    elif event.key == Right:
                        x_speed = speed
                        y_speed = 0
                    elif event.key == Left:
                        x_speed = 0-speed
                        y_speed = 0
    
            #刷新屏幕
            screen.fill((255,255,255))
            ball_x += x_speed
            ball_y += y_speed
            if ball_x + 20 >=sizex:
                ball_x = sizex-20
                x_speed *= -1
            if ball_x -20 <= 0:
                ball_x = 20
                x_speed *= -1
            if ball_y +20 >= sizey:
                ball_y = sizey-20
                y_speed *= -1
            if ball_y-20 <= 0:
                ball_y = 20
                y_speed *= -1
    
            draw_ball(screen,(255,0,0),(ball_x,ball_y))
            pygame.display.update()
    

    小球游戏

    """__author__ == Jefferson"""
    import pygame
    from random import randint
    sizex = 1200
    sizey = 900
    
    def rand_color():
        return randint(0,255),randint(0,255),randint(0,255)
    #all_balls中保存多个球
    #每个球要保存: 半径, 圆心坐标, 颜色, x速度, y速度
    all_balls = [
        {
            'r': randint(10,20),
            'pos':(randint(0,sizex),randint(0,sizey)),
            'color': rand_color(),
            'x_speed': randint(-2,2),
            'y_speed': randint(-2,2)
        },
    {
            'r': randint(10,20),
            'pos':(randint(0,sizex),randint(0,sizey)),
            'color': rand_color(),
            'x_speed': randint(-2,2),
            'y_speed': randint(-2,2)
        },
    ]
    
    
    if __name__ == '__main__':
        pygame.init()
        screen = pygame.display.set_mode((sizex,sizey))
        screen.fill((255,255,255))
        pygame.display.flip()
    
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
    
                if event.type == pygame.MOUSEBUTTONDOWN:
                    ball = {
                        'r': randint(10,20),
                        'pos':event.pos,
                        'color': rand_color(),
                        'x_speed': randint(-3,3),
                        'y_speed': randint(-3,3)
                    }
                    all_balls.append(ball)
    
            #刷新界面
            screen.fill((255,255,255))
            for ball_dict in all_balls:
                x,y = ball_dict['pos']
                x_speed = ball_dict['x_speed']
                y_speed = ball_dict['y_speed']
                x += x_speed
                y += y_speed
                pygame.draw.circle(screen,ball_dict['color'],(x,y),ball_dict['r'])
                #更新球对应的坐标
                ball_dict['pos'] = x,y
    
                if x + ball_dict['r'] >= sizex:
                    x = sizex - ball_dict['r']//2
                    ball_dict['x_speed'] *= -1
                if x - ball_dict['r'] <= 0:
                    x = ball_dict['r']
                    ball_dict['x_speed'] *= -1
                if y + ball_dict['r'] >= sizey:
                    y = sizey - ball_dict['r']//2
                    ball_dict['y_speed'] *= -1
                if y - ball_dict['r'] <= 0:
                    y = ball_dict['r']
                    ball_dict['y_speed'] *= -1
    
                for ball_dict1 in all_balls:
                    if (ball_dict['pos'][0] - ball_dict1['pos'][0])**2 \
                        + (ball_dict['pos'][1] - ball_dict1['pos'][1])**2\
                        < (ball_dict['r'] + ball_dict1['r'])**2:
                        if ball_dict['r'] - ball_dict1['r'] > 0:
                            ball_dict['r'] += ball_dict1['r']//2
                            ball_dict1['r'] = 0
    
                        elif ball_dict['r'] - ball_dict1['r'] < 0:
                            ball_dict1['r'] += ball_dict['r'] // 2
                            ball_dict['r'] = 0
    
    
                if ball_dict['r'] > sizey//2:
                    screen.fill((255, 255, 255))
                    font = pygame.font.SysFont('Times', 100)
                    title = font.render('Game Over', True, (randint(0, 255), randint(0, 255), randint(0, 255)))
                    screen.blit(title, (sizex//2-230, sizey//2-80))
                    break
    
                i = 0
                while i< len(all_balls):
                    if all_balls[i]['r'] == 0:
                        del all_balls[i]
                        i -= 1
                    i += 1
    
            pygame.display.update()
    

    相关文章

      网友评论

          本文标题:pygame事件

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