美文网首页
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作业-球球大作战

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