美文网首页
Day10作业-球球大作战

Day10作业-球球大作战

作者: 周zau1 | 来源:发表于2018-07-30 20:27 被阅读71次

原本想大球吃小球,但是好像出了点bug

球球大乱斗
# Jay
import pygame
import random
random_color = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)


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

    # all_balls 中保存多个球
    # 每个球要保存:半径、圆心坐标、颜色、x 速度、y 速度
    all_balls = [
        {'r':random.randint(10, 20), 'pos':(random.randint(30, 1000), random.randint(30, 500)), '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)},
        {'r':random.randint(10, 20), 'pos':(random.randint(30, 1000), random.randint(30, 500)), '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)}

    ]


    while True:
        n = 0
        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)
                }
                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 + 20 >= 1024:
                x_speed *= -1
            if x - 20 <= 0:
                x_speed *= -1
            if y - 20 >= 0:
                y_speed *= -1
            if y + 20 <= 600:
                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 ball in all_balls:     # 遍历每个球
            n += 1
            m = n - 1
            ball_num = len(all_balls)      # 球的数量
            ball_pos = ball['pos']      # 球的坐标
            ball_r = ball['r']          # 球的半径
            x, y = ball_pos
            for ball1 in all_balls[n:ball_num]:     # 遍历ball 后面的每个球
                m += 1
                ball1_pos = ball1['pos']        # ball 后面的球的坐标
                ball1_r = ball['r']             # 球的半径
                x1, y1 = ball1_pos
                distens = pow((pow(x-x1, 2) + pow(y-y1, 2)), 1/2)       # 两球间的距离
                if distens <= ball_r + ball1_r:         # 距离小于半径和 说明已碰撞
                    """判断哪个球较小,较小的球消失"""
                    if ball_r >= ball1_r:
                        del all_balls[m]
                        ball['r'] = ball_r + 1
                    else:
                        del all_balls[n-1]
                        ball['r'] = ball1_r + 1


        pygame.display.update()

相关文章

  • Day10作业-球球大作战

    原本想大球吃小球,但是好像出了点bug

  • day13_python_pygame(老师例题)

    球球大作战

  • 球球大作战

  • 球球大作战

    欢迎来到球球大作战,也是一个不错的选择,球球大作战是一个大球吃小球的一个游戏

  • 球球大作战

    球球大作战,大家应该都玩过,可是我今天玩的球球大作战可不是游戏哦! 这个星期五的晚上,妈妈说要带我...

  • 2017-07-20

    我最喜欢的电子游戏是球球大作战,跟贪吃蛇大作战相似。它的规则是求底下还没有标志的时候不能吐球、喷球。如果球在你...

  • 2017-08-25

    今天我们就来聊一款网上热门游戏球球大作战! 今天装逼松鼠带你挑战球球大作战生存模式! 首先让我看到生存模式,你有可...

  • 中秋佳节!球球大作战中秋皮肤花好月圆获取方法

    球球大作战中秋节花好月圆新皮肤上线啦,本次更新了2款光环皮肤,下面小编为大家带来的是球球大作战中秋光环皮肤花好月圆...

  • 球球大作战

  • 球球大作战

    前不久玩了一款游戏,球球大作战,知道它还是因为朋友们在各种群里分享的,刚开始也没当回事,就当一个广告一样置之不理,...

网友评论

      本文标题:Day10作业-球球大作战

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