Как я могу построить этот зум-экран сбоку? - PullRequest
1 голос
/ 17 июня 2020

enter image description here

Итак, я создаю симуляцию физики. И всякий раз, когда нажимается левая кнопка мыши, игрок может управлять высотой шара (перетаскивая). Я хочу построить следующее: всякий раз, когда игрок перетаскивает мяч, чтобы изменить его положение, сбоку будет появляться экран, а внутри него мне нужно масштабировать в реальном времени, где находится мяч (извините, если я не объяснил это слишком хорошо, Я думаю, что изображение лучше объясняет это).

Чтобы уточнить, я хочу, чтобы все это было только в одном окне

Кто-нибудь может мне помочь? :)

1 Ответ

2 голосов
/ 17 июня 2020

Это не так уж сложно. Просто нарисуйте свой материал на отдельной поверхности, затем используйте subsurface и модуль transform. Вот пример:

import pygame
pygame.init()

def main():

    # we'll not draw an the display surface directly
    screen = pygame.display.set_mode((800, 600))

    # but we'll draw everything on this surface
    main = screen.copy()

    # this surface is the zoom window
    zoom = pygame.Surface((400, 300))

    # the ball and its movement vector
    ball = pygame.Rect(550, 100, 40, 40)
    ball_v = pygame.Vector2(0, 0)

    dt = 0
    clock = pygame.time.Clock()

    while True:
        pressed = pygame.mouse.get_pressed()
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                return

        if pressed[0]:
            # dragging the ball if mouse button is pressed
            ball_v = pygame.Vector2(0, 0)
            ball.center = pygame.mouse.get_pos()
        else:
            # if not, we apply the gravity
            ball_v += pygame.Vector2(0, 0.2)

        # move the ball
        ball.move_ip(ball_v)

        # but keep it on the screen
        ball.clamp_ip(main.get_rect())

        # draw the main surface
        main.fill(pygame.Color('white'))
        w = 10
        for y in range(main.get_rect().height):
            pygame.draw.rect(main, pygame.Color('lightgray'), pygame.Rect(500, y, w, 1))
            w = (w - 1) % 20
        pygame.draw.circle(main, pygame.Color('orange'), ball.center, 20)
        screen.blit(main, (0, 0))

        # if the mouse button is pressed, draw the zoom window
        if pressed[0]:
            # the area of the main surface that should be drawn in the zoom window
            rect = pygame.Rect(0, 0, 200, 150)
            # center it on the ball
            rect.center = ball.center
            # ensure it's on the screen
            rect.clamp_ip(main.get_rect())
            # grab the part from the main surface
            sub = main.subsurface(rect)
            # scale it
            zoom.blit(pygame.transform.scale2x(sub), (0, 0))
            pygame.draw.rect(zoom, pygame.Color('black'), zoom.get_rect(), 4)
            screen.blit(zoom, (25, 25))

        pygame.display.flip()
        clock.tick(120)
main()

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...