本文介绍了在 Pygame 中缩放图像/矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 pygame.js 中缩放"一个矩形.有点像当您在桌面上缩放窗口时,我想知道您如何在 pygame 中这样做.您的光标将控制矩形的大小.

Is it possible to "scale" a rectangle inside of pygame. Kinda like when you scale your windows on your desktop, I'm wondering how you could do so in pygame. Your cursor will control the size of the rectangle.

我使用了 skrx 方法,并尝试对 2 个矩形执行此操作:

I used skrx method and I tried to do it for 2 rectangles:

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)

但是对于第二个矩形,它会正确移动,winCrime"似乎有问题.winVirus"工作得很好.

but for the second rectangle, it would move properly there seems to be a problem with "winCrime". winVirus" works perfectly fine.

推荐答案

你可以简单地改变矩形的宽度和高度(wh 属性).当鼠标移动时(MOUSEMOTION 事件被添加到队列中)并且如果矩形被选中,则将相对移动 event.rel 添加到宽度和高度.

You can simply change the width and the height of the rect (the w and h attributes). When the mouse gets moved (a MOUSEMOTION event is added to the queue) and if the rect is selected, add the relative movement event.rel to the width and the height.

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)

如果您想对多个矩形执行此操作,只需将单击的矩形分配给一个变量(在本例中为 selected_rect),然后对其进行缩放.


If you want to do this with several rects, you can just assign the clicked rect to a variable (selected_rect in this case) and then scale it.

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
rect1 = pg.Rect(100, 100, 161, 100)
rect2 = pg.Rect(300, 200, 161, 100)
selected_rect = None

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.MOUSEBUTTONDOWN:
            # Set selected_rect to the colliding rect.
            for rect in (rect1, rect2):
                if rect.collidepoint(event.pos):
                    selected_rect = rect
        elif event.type == pg.MOUSEBUTTONUP:
            selected_rect = None  # De-select.
        elif event.type == pg.MOUSEMOTION:
            if selected_rect is not None:  # Scale if a rect is selected.
                selected_rect.w += event.rel[0]
                selected_rect.h += event.rel[1]
                selected_rect.w = max(selected_rect.w, 10)
                selected_rect.h = max(selected_rect.h, 10)

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

这篇关于在 Pygame 中缩放图像/矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:13