Python pygame之大球吃小球

2018-07-30  本文已影响234人  Fizz翊

本次采用了pygame模块,random模块,math模块。random模块用于随机颜色
math模块用了sqrt函数计算两个坐标之间的距离。

# @Author  : Fizzyi
import  pygame
import random
import math
def random_color():
    return random.randint(0,255),random.randint(0,255),random.randint(0,255)



if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,400))
    screen.fill((255,255,255))
    pygame.display.flip()
    #all_balls中保存多个球
    #每个球要保存:半径,圆心坐标,颜色,X的速度 Y的速度
    all_balls = [
        {
            'r':random.randint(10,20),
             'pos':(100,100),
             'color':(random_color()),
            'x_speed': random.randint(-3, 3),
            'y_speed': random.randint(-3, 3),
            'live':True
        },
        {
            'r': random.randint(10, 20),
            'pos': (400, 200),
            'color': (random_color()),
            'x_speed': random.randint(-3, 3),
            'y_speed': random.randint(-3, 3),
            'live': True

        },
        {
            'r': random.randint(10, 20),
            'pos': (300, 100),
            'color': (random_color()),
            'x_speed': random.randint(-3, 3),
            'y_speed': random.randint(-3, 3),
            'live': True

        }
    ]

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

            if event.type == pygame.MOUSEBUTTONDOWN:
                    #点一下鼠标创建一个球
                ball = {
                    'r': random.randint(10, 25),
                    'pos': event.pos,
                    'color': (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
                    'x_speed': random.randint(-3, 3),
                    'y_speed': random.randint(-3, 3),
                    'live': True
                }

                #保存球
                all_balls.append(ball)
        #刷新界面
        screen.fill((255,255,255))
        for ball in all_balls:
            #取出球原来的x,y以及速度
            x,y = ball['pos']
            x_speed = ball['x_speed']
            y_speed = ball['y_speed']

            if x + ball['r'] >= 600:
                # x = 600 - ball['r']
                x_speed *= -1
            if x-ball['r'] <= 0:
                # x = ball['r']
                x_speed *= -1
            if y+ball['r'] >= 400:
                # y = 400 - ball['r']
                y_speed *= -1
            if y-ball['r'] <= 0:
                #y =ball['r']
                y_speed *= -1
            x += x_speed
            y += y_speed
            pygame.draw.circle(screen,ball['color'],(x,y),ball['r'])
            #更新球的坐标
            ball['pos'] = x,y

            ball['x_speed'] = x_speed

            ball['y_speed'] = y_speed
            for ball1 in all_balls:
                if all_balls.index(ball) == all_balls.index(ball1):
                    pass
                else:
                    other_x,other_y = ball1['pos']
                    dx = x-other_x
                    dy = y-other_y
                    distance =math.sqrt(dx ** 2+dy ** 2)
                    if distance < ball['r'] + ball1['r']:
                        if ball['r'] >ball1['r']:
                            ball['r'] = int(ball['r'] + ball1['r']/5)
                            all_balls.remove(ball1)
                    # elif ball['']:
                    #     all_balls.remove(ball)

        pygame.display.update()
pygame.gif
上一篇下一篇

猜你喜欢

热点阅读