示例#1
0
def draw_planet(p: Planet):
    if p.get_pos()[0] - p.get_radius() > screen_width:
        p.set_pos((0 - p.get_radius(), p.get_pos()[1]))
    elif p.get_pos()[0] + p.get_radius() < 0:
        p.set_pos((screen_width + p.get_radius(), p.get_pos()[1]))

    if p.get_pos()[1] - p.get_radius() > screen_height:
        p.set_pos((p.get_pos()[0], 0 - p.get_radius()))
    elif p.get_pos()[1] + p.get_radius() < 0:
        p.set_pos((p.get_pos()[0], screen_height + p.get_radius()))

    pygame.draw.circle(screen, p.get_color(), p.get_pos(), p.get_radius())

    velocity = Vector2(p.get_vel())
    if velocity.magnitude() > 0:
        try:
            velocity.scale_to_length(p.get_radius())
        except ValueError:
            print("faulty scale_to_length")
示例#2
0
            run = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if curr_planet is None:
                pos = pygame.mouse.get_pos()
                curr_planet = Planet(Vector2(pos), Vector2(0, 0), 0.1)
                if pygame.mouse.get_pressed()[2]:
                    curr_planet.UNMOVABLE = True

        elif event.type == pygame.MOUSEBUTTONUP:
            if curr_planet is not None:
                planets.append(curr_planet)
                curr_planet = None
        elif event.type == pygame.MOUSEMOTION:
            if curr_planet is not None:
                m_pos = pygame.mouse.get_pos()
                p_pos = curr_planet.get_pos()
                curr_planet.set_vel(scale(subtract(m_pos, p_pos),
                                          ARROW_FACTOR))
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                GRAVITY_COEFFICIENT = GRAVITY_COEFFICIENT + 0.1 if GRAVITY_COEFFICIENT + 0.1 < 3 else 3
            elif event.key == pygame.K_DOWN:
                GRAVITY_COEFFICIENT = GRAVITY_COEFFICIENT - 0.1 if GRAVITY_COEFFICIENT - 0.1 >= 0 else 0
            elif event.key == pygame.K_BACKSPACE:
                if len(planets) > 0:
                    planets.pop(len(planets) - 1)
            elif event.key == pygame.K_ESCAPE:
                planets.clear()

    draw_background()
    draw_stars()