美文网首页
day11-小球碰撞

day11-小球碰撞

作者: 旧时初_2e8d | 来源:发表于2018-07-30 22:41 被阅读0次
"""__author__zhangdong__"""
import pygame
from random import randint
from math import sqrt

if __name__ == '__main__':
    pygame.init()
    screen  = pygame.display.set_mode((1000,800))
    screen.fill((255,255,255))
    pygame.display.set_caption('多个球')

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


    ]


    pygame.display.flip()
    ball_x = 100
    ball_y = 100
    x_speed =0
    y_speed =0

    while True:
        # pygame.time.delay(30)
        pygame.time.Clock().tick(30)
        randint_color = randint(0, 255), randint(0, 255), randint(0, 255)
        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':randint_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
                # y_speed *= -1
            if x <= ball_dict['r']:
                x = ball_dict['r']
                x_speed *= -1
                # y_speed *= -1
            if y + ball_dict['r'] >= 400:
                y = 400 - ball_dict['r']
                # x_speed *= -1
                y_speed *= -1
            if y <= ball_dict['r']:
                y = ball_dict['r']
                # x_speed *= -1
                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
            # ball_dict['r'] = r

            #遍历第二个球
            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.flip()

效果


11.gif

相关文章

  • day11-小球碰撞

    效果

  • Java 小球碰撞

    小球碰撞的关键是 判断小球是否碰撞过:1.当两小球间距小于小球直径时发生碰撞。2.当小球碰到边缘时发生碰撞。

  • day-11-pygame的应用

    小球碰撞,大球碰击小球,小球消失

  • 小球碰撞

  • 2018-09-04 Day12-pygame小游戏

    1、小球碰撞 2、接球游戏 3、大球吃小球

  • day12-作业

    实现鼠标点击屏幕产生小球,小球自动移动,与屏幕碰撞会反弹。小球之间碰撞,会随机吃掉。 定义部分颜色的模块

  • 构造函数&&类--canvas小球碰撞

    1. 构造函数 canvas小球碰撞 2. 类 canvas小球碰撞 有上可以看出,用类写面向对象的方法,...

  • pygame小球碰撞

    import pygameimport ymj_colorfrom random import randint""...

  • 算法(小球碰撞)

    两小球碰撞时的速度方向和碰撞时两圆心的连线不在同一条线上(图【1】)。 需要做速度分解(图【2】)。 分解后在碰撞...

  • 射线拓展

    小球撞击砖块,小球和砖块都必须有碰撞体,当小球撞击到砖块之后,小球消失,在小球原本位置再生成一个小球,点击鼠标继续...

网友评论

      本文标题:day11-小球碰撞

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