缩放Pygam中的图像/矩形

2024-09-29 19:28:36 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以在pygame内部“缩放”一个矩形。有点像当你在桌面上缩放你的窗口时,我想知道你怎么能在pygame中这样做。光标将控制矩形的大小。在

我用skrx方法,试着用两个矩形来做:

if evnt.type == MOUSEBUTTONDOWN:
        if winVirus.collidepoint(evnt.pos):
            rectVSelect = True
        elif winCrime.collidepoint(evnt.pos):
            rectCSelect = True
    elif evnt.type == MOUSEBUTTONUP:
        rectVSelect = False
        rectCSelect = False
    if evnt.type == MOUSEMOTION:
        if rectVSelect:
            winVirus.w += evnt.rel[0]
            winVirus.h += evnt.rel[1]
            winVirus.w = max(winVirus.w, 50)
            winVirus.h = max(winVirus.h, 50)  
        elif rectCSelect:
            winCrime.w += evnt.rel[0]
            winCrime.h += evnt.rel[1]
            winCrime.w = max(winVirus.w, 50)
            winCrime.h = max(winVirus.h, 50)

但对于第二个矩形,它会正常移动,“wincreme”似乎有问题。“winVirus”非常好用。在


Tags: postrueiftypepygamemaxrel矩形
2条回答

这是一个有效的例子。你得适应你的情况。在

import pygame

rect = pygame.Rect(50, 50, 100, 100)
screen = pygame.display.set_mode((200, 200))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEMOTION and event.buttons[0]:
            p = event.pos[0] - event.rel[0], event.pos[1] - event.rel[1]
            if pygame.Rect(rect.left, rect.top - 2, rect.width, 4).collidepoint(*p): # top
                rect = pygame.Rect(rect.left, rect.top + event.rel[1], rect.width, rect.height - event.rel[1])
            elif pygame.Rect(rect.left, rect.bottom - 2, rect.width, 4).collidepoint(*p): # bottom
                rect = pygame.Rect(rect.left, rect.top, rect.width, rect.height + event.rel[1])
            if pygame.Rect(rect.left - 2, rect.top, 4, rect.height).collidepoint(*p): # left
                rect = pygame.Rect(rect.left + event.rel[0], rect.top, rect.width - event.rel[0], rect.height)
            elif pygame.Rect(rect.right - 2, rect.top, 4, rect.height).collidepoint(*p): # right
                rect = pygame.Rect(rect.left, rect.top, rect.width + event.rel[0], rect.height)

    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (255, 255, 255), rect)
    pygame.display.update()

您只需更改rect的宽度和高度(wh属性)。当鼠标移动时(队列中添加了一个MOUSEMOTION事件),如果选择了rect,则将相对移动event.rel添加到宽度和高度。在

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
rect = pg.Rect(100, 100, 161, 100)
rect_selected = False

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.MOUSEBUTTONDOWN:
            # Set rect_selected to True when the user clicks on the rect.
            if rect.collidepoint(event.pos):
                rect_selected = True
        elif event.type == pg.MOUSEBUTTONUP:
            rect_selected = False
        elif event.type == pg.MOUSEMOTION:
            if rect_selected:
                # Simply adjust the width and the height of the screen
                # by subtracting the relative mouse movement.
                rect.w += event.rel[0]
                rect.h += event.rel[1]
                # 10*10 px is the minimal size, so that the rect can
                # still be clicked.
                rect.w = max(rect.w, 10)
                rect.h = max(rect.h, 10)

    screen.fill((30, 30, 30))
    pg.draw.rect(screen, (0, 100, 250), rect)
    pg.display.flip()
    clock.tick(30)

如果您想用多个rect来完成这个任务,您只需将单击的rect分配给一个变量(在本例中是selected_rect),然后缩放它。在

^{pr2}$

相关问题 更多 >

    热门问题