如何在PyGame中拖动多个图像?

2023-12-07

import pygame
from pygame.locals import *

pygame.display.init()
screen = pygame.display.set_mode((1143,677 ))
img = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")

imgPos = pygame.Rect((0, 0), (0, 0))

LeftButton = 0
while 1:
 for e in pygame.event.get():
    if e.type == QUIT: exit(0)
    if e.type == MOUSEMOTION:
        if e.buttons[LeftButton]:
            rel = e.rel
            imgPos.x += rel[0]
            imgPos.y += rel[1]
screen.fill(0)
screen.blit(img, imgPos)
pygame.display.flip()
pygame.time.delay(30)

我尝试用多个图像来实现此目的,并且图像总是重叠并且只能移动一个图像,所以尝试了以下方法:

 import pygame
 from pygame.locals import *

 pygame.display.init()
 screen = pygame.display.set_mode((1143,677 ))
 img = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
 img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\03.png")
 imgPos = pygame.Rect((0, 0), (0, 0))
 imgPos1 = pygame.Rect((1, 1), (1, 1))
 LeftButton = 0
 while 1:
     for e in pygame.event.get():
    if e.type == QUIT: exit(0)
    if e.type == MOUSEMOTION:
        if e.buttons[LeftButton]:
            rel = e.rel
            imgPos.x += rel[0]
            imgPos.y += rel[1]
screen.fill(0)
screen.blit(img, imgPos)
screen.blit (img1, imgPos1)
pygame.display.flip()
pygame.time.delay(30)

所以图像重叠并且可以移动第二个图像,我想让两个图像用鼠标移动,我希望分别移动图像


PyGame 中的单位是像素。您的对象重叠,因为垂直和水平位置的差异只有一个像素。

imgPos = pygame.Rect((0, 0), (0, 0))
imgPos1 = pygame.Rect((1, 1), (1, 1))

增加对象的距离并根据相应图像的大小设置矩形的大小。例如

imgPos = pygame.Rect((0, 0), img.get_size())
offsetX = img.get_width()
imgPos1 = pygame.Rect((offsetX, 0), img1.get_size())

或者,可以生成一个pygame.Rect对象形成pygame.Surface by get_rect。矩形的位置必须由关键字参数设置:

imgPos = img.get_rect(topleft = (0, 0))
imgPos1 = img1.get_rect(topleft = (img.get_width(), 0))

如果要选择要移动的图像,则需要添加一个变量来指示当前选择的图像:

current_image = None

当点击图像时,变化current_image. Use collidepoint()验证鼠标是否单击了图像:

if e.type == pygame.MOUSEBUTTONDOWN:
    if imgPos.collidepoint(e.pos):
        current_image = 0
    elif imgPos1.collidepoint(e.pos):
        current_image = 1
    else: 
        current_image = None

Move img if current_image == 0并移动img1 if current_image == 1:

if e.type == MOUSEMOTION:
    if e.buttons[LeftButton]:
        rel = e.rel
        if current_image == 0:
            imgPos.x += rel[0]
            imgPos.y += rel[1]
        elif current_image == 1:
            imgPos1.x += rel[0]
            imgPos1.y += rel[1]

最小的例子:

import pygame
from pygame.locals import *

pygame.display.init()
screen = pygame.display.set_mode((1143,677 ))

img = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\03.png")
imgPos = img.get_rect(topleft = (20, 20))
imgPos1 = img1.get_rect(topleft = (60, 20))
current_image = None

LeftButton = 0
while 1:
    for e in pygame.event.get():
        if e.type == QUIT:
            pygame.quit()
            exit(0)

        if e.type == pygame.MOUSEBUTTONDOWN:
            if imgPos.collidepoint(e.pos):
                current_image = 0
            elif imgPos1.collidepoint(e.pos):
                current_image = 1
            else: 
                current_image = None

        if e.type == MOUSEMOTION:
            if e.buttons[LeftButton]:
                rel = e.rel
                if current_image == 0:
                    imgPos.x += rel[0]
                    imgPos.y += rel[1]
                elif current_image == 1:
                    imgPos1.x += rel[0]
                    imgPos1.y += rel[1]
   
    screen.fill(0)
    screen.blit(img, imgPos)
    screen.blit (img1, imgPos1)
    pygame.display.flip()
    pygame.time.delay(30)

See 从 Pygame 中的同一 sprite 类创建具有不同 update() 的多个 sprite用于拖动pygame.sprite.Sprite对象。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在PyGame中拖动多个图像? 的相关文章

随机推荐