球球球

作者: zzzsssr | 来源:发表于2018-07-31 20:10 被阅读43次
import pygame
from random import randint
from math import sqrt

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,400))
    screen.fill((255, 255, 255))
    pygame.display.set_caption('球球球')
    pygame.display.flip()

    # all_balls中保存多个球
    # 每个球要保存:半径、圆心坐标、颜色、x速度、y速度
    all_balls = [
        # {
        #     'r': randint(10, 20),
        #     'pos': (100, 100),
        #     'color':random_color,
        #     'x_speed': randint(-3, 3),
        #     'y_speed': randint(-3, 3),
        # }
    ]

    while True:
        random_color = randint(0, 255), randint(0, 255), randint(0, 255)
        pygame.time.delay(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                # 点一下鼠标创建一个球
                ball = {
                        'r': randint(10, 25),
                        'pos': event.pos,
                        'color': random_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坐标以及他们的速度
            x, y = ball_dict['pos']
            x_speed = ball_dict['x_speed']
            y_speed = ball_dict['y_speed']
            # 球碰到边界弹回
            if x + ball_dict['r'] >= 600:
                x = 600 - ball_dict['r']
                x_speed *= -1
            if x <= ball_dict['r']:
                x = ball_dict['r']
                x_speed *= -1
            if y + ball_dict['r'] >= 400:
                y = 400 - ball_dict['r']
                y_speed *= -1
            if y <= ball_dict['r']:
                y = ball_dict['r']
                y_speed *= -1
            x += x_speed
            y += y_speed
            pygame.draw.circle(screen, ball_dict['color'], (x, y), ball_dict['r'])

            # 更新球对应的坐标
            ball_dict['pos'] = x, y

            ball_dict['x_speed'] = x_speed
            ball_dict['y_speed'] = y_speed

            # 遍历第二个球
            for two_ball in all_balls:
                if all_balls.index(ball_dict) == all_balls.index(two_ball):
                    pass
                else:
                    one_ball_x, one_ball_y = ball_dict['pos']
                    two_ball_x, two_ball_y = two_ball['pos']
                    new_R = ball_dict['r'] + two_ball['r']
                    new_X = (one_ball_x - two_ball_x) ** 2
                    new_Y = (one_ball_y - two_ball_y) ** 2
                    # 判断两个球之间的距离
                    if sqrt(new_X + new_Y) <= new_R:
                        if ball_dict['r'] >= two_ball['r']:
                            ball_dict['r'] = ball_dict['r'] + two_ball['r']

                            all_balls.remove(two_ball)



        pygame.display.update()

相关文章

  • 球球球

  • 球球

    球球刚到我家时,只有一个月大,刚刚断奶。它是一只土黄色的小土狗,两只小耳朵像两片多肉,绒绒的,厚厚的。离开...

  • 球球

    闲来无事,带着外孙去邻居家串门。外孙三周多,特别淘气,整天登台摸罐,没有时闲。除了至亲好友处,一般都不敢带着他串门...

  • 球球

    球三个月了。较初来家时,确是长高了,也长胖了不少。它是一点儿也不娇气的,十分好养,随便什么都可以吃,也随便什么都可...

  • 球球

    【http://www.qqdzzw.cn/】

  • 球球

    球球是一只流浪狗,后来被宠物收容所收入,没人知道这毛茸茸的晃着脑袋的狗狗为什么会被人遗弃,直到有一天,有一个收养...

  • 球球

    我养了一只猫,她叫球球。非名门之后,却异常乖巧。之前一次发她照片给朋友看,朋友都夸她小仙女。 我之前并不喜欢猫,甚...

  • 球球

    球球不是吃素的 虽然看见许多狗吃西瓜吃桃子吃苹果啥的,但这个球々压根不屑,肉最好,骨其次,要不虾头,鱼骨头也行,吃...

  • 球球

    今天是养球球的第九天,也是军训得最后一日,正在大学学姐撩学弟,学长撩学妹的迎新晚会中,我躺在宿舍的床上,守着自己家...

  • 球球

    今天老泪纵横了一下,为了我的球球~ 球球应该是我难得用情的事物了。即使和男友分手,都没有如此不舍。 我总觉得和球球...

网友评论

      本文标题:球球球

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