从 Pygame 中的同一个 Sprite class 拖动多个具有不同 "update ()" 方法的 Sprite

Drag multiple sprites with different "update ()" methods from the same Sprite class in Pygame

我正在尝试制作多个精灵,它们都来自同一个 pygame 精灵 class。

class animatedSprites(pygame.sprite.Sprite):
    def __init__(self, spriteName):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((width,height))
        self.images = []
        self.images.append(spriteName[0])
        self.rect = self.image.get_rect() #sets sprites size and pos
        self.rect.center = (POSITIONx1[4], POSITIONy1[4])
        self.animation_frames = 12
        self.current_frame = 0
        self.previous_positiony = self.rect.y
        self.previous_positionx = self.rect.x

    def update(self):
        global MOUSEPRESSED
        if (MOUSEPRESSED == 1):
            self.rect = self.image.get_rect()
            self.rect.y = POSITIONy1[get_square_under_mousey()] - 25
            self.rect.x = POSITIONx1[get_square_under_mousex()] - 25
        if (MOUSEPRESSED == 2):
            if collide(plant, bought2):
                self.rect.y = self.previous_positiony
                self.rect.x = self.previous_positionx
            else:
                self.rect = self.image.get_rect()
                self.rect.y = POSITIONy1[get_square_under_mousey()] + 35#pulled x and y pos from func
                self.rect.x = POSITIONx1[get_square_under_mousex()] - 25
                self.previous_positiony = self.rect.y
                self.previous_positionx = self.rect.x
            MOUSEPRESSED = 0

我使用这个 class 在屏幕上创建一个精灵,然后使用 def update() 来控制精灵。精灵的控制方式是,当用户点击精灵时,他们可以用鼠标拖动它,并将它移动到屏幕上任何他们喜欢的地方。问题是,如果我使用此 class 创建第二个精灵并在屏幕上同时显示两个精灵,当用户“拿起”一个精灵时,它们都会移动到鼠标位置。我只想根据点击的是哪一个移动。

我假设他们都在移动到相同的位置,因为他们都使用相同的 def update() 来决定他们的移动,所以我的问题是,pygame 或 python 让被点击的人移动而不是同时移动,而不创建第二个 class animatedSprites(pygame.sprite.Sprite): 成功。我想同时在屏幕上显示多个精灵,但不想制作数十个单独的 classes 和 update() defs 来分别控制每个精灵。

抱歉,如果这没有意义,我是 python 和 pygame 的初学者。

对于精灵管理,一个Sprite Group会帮助你。

所以在设置过程中,执行如下操作:

all_sprites = pygame.sprite.Group()
# create five initial sprites
for number in range(5):
    new_sprite = animatedSprites(f"Sprite {number}")
    all_sprites.add(new_sprite)

然后在您的事件处理过程中:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        # set termination condition here
        …
    elif event.type == pygame.MOUSEBUTTONDOWN:
        # use the click position in the name
        new_sprite = animatedSprites(f"Sprite X: {event.pos[0]} Y: {event.pos[1]}")
        all_sprites.add(new_sprite)

处理事件后:

# update game state
all_sprites.update()
# draw background
…
# draw sprites
all_sprites.draw()
# update display
pygame.display.update()


            

我建议创建一个可以拖动 pygame.Rect 对象的 class DragOperator:

class DragOperator:
    def __init__(self, sprite):
        self.sprite = sprite
        self.dragging = False
        self.rel_pos = (0, 0)
    def update(self, event_list):
        for event in event_list:
            if event.type == pygame.MOUSEBUTTONDOWN:
                self.dragging = self.sprite.rect.collidepoint(event.pos)
                self.rel_pos = event.pos[0] - self.sprite.rect.x, event.pos[1] - self.sprite.rect.y
            if event.type == pygame.MOUSEBUTTONUP:
                self.dragging = False
            if event.type == pygame.MOUSEMOTION and self.dragging:
                self.sprite.rect.topleft = event.pos[0] - self.rel_pos[0], event.pos[1] - self.rel_pos[1]

矩形的拖动是在update方法中实现的。在 pygame.sprite.Sprite 对象中使用此 class。将事件列表传递给 Spriteupdate 方法并将其委托给拖动运算符:

class animatedSprites(pygame.sprite.Sprite):
    def __init__(self, spriteName):
        # [...]

        self.drag = DragOperator(self)

    def update(self, event_list):
        self.drag.update(event_list) 

将事件列表从主应用程序循环传递到 pygame.sprite.Group:

all_sprites = pygame.sprite.Group()
all_sprites.add(animatedSprites("my_sprite"))

run = True
while run:
    event_list = pygame.event.get()
    for event in event_list:
        if event.type == pygame.QUIT:
            run = False

    all_sprites.update(event_list)

    # [...]

最小示例: repl.it/@Rabbid76/PyGame-MouseDrag

import pygame

class DragOperator:
    def __init__(self, sprite):
        self.sprite = sprite
        self.dragging = False
        self.rel_pos = (0, 0)
    def update(self, event_list):
        for event in event_list:
            if event.type == pygame.MOUSEBUTTONDOWN:
                self.dragging = self.sprite.rect.collidepoint(event.pos)
                self.rel_pos = event.pos[0] - self.sprite.rect.x, event.pos[1] - self.sprite.rect.y
            if event.type == pygame.MOUSEBUTTONUP:
                self.dragging = False
            if event.type == pygame.MOUSEMOTION and self.dragging:
                self.sprite.rect.topleft = event.pos[0] - self.rel_pos[0], event.pos[1] - self.rel_pos[1]

class SpriteObject(pygame.sprite.Sprite):
    def __init__(self, x, y, color):
        super().__init__() 
        self.original_image = pygame.Surface((50, 50), pygame.SRCALPHA)
        pygame.draw.circle(self.original_image, color, (25, 25), 25)
        self.drag_image = pygame.Surface((50, 50), pygame.SRCALPHA)
        pygame.draw.circle(self.drag_image, color, (25, 25), 25)
        pygame.draw.circle(self.drag_image, (255, 255, 255), (25, 25), 25, 4)
        self.image = self.original_image 
        self.rect = self.image.get_rect(center = (x, y))
        self.drag = DragOperator(self)
    def update(self, event_list):
        self.drag.update(event_list) 
        self.image = self.drag_image if self.drag.dragging else self.original_image

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

sprite_object = SpriteObject(*window.get_rect().center, (255, 255, 0))
group = pygame.sprite.Group([
    SpriteObject(window.get_width() // 3, window.get_height() // 3, (255, 0, 0)),
    SpriteObject(window.get_width() * 2 // 3, window.get_height() // 3, (0, 255, 0)),
    SpriteObject(window.get_width() // 3, window.get_height() * 2 // 3, (0, 0, 255)),
    SpriteObject(window.get_width() * 2// 3, window.get_height() * 2 // 3, (255, 255, 0)),
])

run = True
while run:
    clock.tick(60)
    event_list = pygame.event.get()
    for event in event_list:
        if event.type == pygame.QUIT:
            run = False

    group.update(event_list)

    window.fill(0)
    group.draw(window)
    pygame.display.flip()

pygame.quit()
exit()