Pygame 绘制 multiple/duplicate 个相同的精灵 class

Pygame draw multiple/duplicate sprites same class

问题: 我想在另一个 Sprite 后面添加完全相同的 Sprite(它是一个 25x25 的白框)

精灵从 x,y 300,300 开始 我有让精灵向任何方向移动的关键功能

比方说,我将我的 Sprite 移动到 x,y 150,210...我如何抓住原始 Sprite 的新位置,然后直接在 Sprite 的新位置“后面”绘制该 Sprite 的副本x,y 150,210?

最后的结果是:

如果这个问题需要更好的上下文等,请告诉我 提前致谢

最小可重现示例

import pygame
from random import randint
from sys import exit

pygame.init()
game_active = True
clock = pygame.time.Clock() #an object to track time

def display_surface():
    disp_surface = pygame.display.set_mode(size = (610, 700))
    return disp_surface
disp_surface = display_surface()

class square(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        image1 = pygame.image.load("square1.png").convert_alpha()
        x_pos = 300
        y_pos = 300

        self.image = image1
        self.rect = self.image.get_rect(x = x_pos,y = y_pos)

squaregroup = pygame.sprite.GroupSingle()
squaregroup.add(square())

while True:
    for eachevent in pygame.event.get():
        if eachevent.type == pygame.QUIT:
            pygame.quit()
            exit()
        
        if eachevent.type == pygame.KEYDOWN and eachevent.key == pygame.K_SPACE:
            game_active = True

        keys = pygame.key.get_pressed()
        events = pygame.event.get()
        
        left = keys[pygame.K_LEFT]
        right = keys[pygame.K_RIGHT]
        up = keys[pygame.K_UP]
        down = keys[pygame.K_DOWN]
     
        if left:
            squaregroup.sprite.rect.x -= 1
        if right:
            squaregroup.sprite.rect.x += 1
        if up:
            squaregroup.sprite.rect.y -= 1
        if down:
            squaregroup.sprite.rect.y += 1
    
    if game_active:
        disp_surface = display_surface()
        squaregroup.draw(disp_surface)
    
    else:
        disp_surface.fill((64,64,64))

    pygame.display.update()
    clock.tick(60)

这是一种可能的解决方案 我创建了一个 pygame.sprite.group() 而不是 groupsingle() 然后在那里添加方块,它工作正常 您可以通过单击 space 栏键在背面出现更多方块,并通过 original_sq.rect.x 和 original_sq.rect.y 获得新的 x,y 此外,注释行号 68 将向您展示如何创建和显示新方块

import pygame
from random import randint
from sys import exit

pygame.init()
game_active = True
clock = pygame.time.Clock() #an object to track time

def display_surface():
    disp_surface = pygame.display.set_mode(size = (610, 700))
    return disp_surface
disp_surface = display_surface()

class square(pygame.sprite.Sprite):
    def __init__(self,x_pos=300,y_pos=300,color=(255,0,0)):
        super().__init__()
        image1 = pygame.Surface([25,25])
        image1.fill((color))
        self.x_pos = x_pos
        self.y_pos = y_pos

        self.image = image1
        self.rect = self.image.get_rect(x = x_pos,y = y_pos)
    #this is just the same movements nothing new 
    def update(self,left,right,up,down):
        if left:
            self.rect.left-=1
        if right:
            self.rect.right+=1
        if up:
            self.rect.top-=1
        if down:
            self.rect.bottom+=1

squaregroup = pygame.sprite.Group()
original_sq=square()
squaregroup.add(original_sq)
while True:
    for eachevent in pygame.event.get():
        if eachevent.type == pygame.QUIT:
            pygame.quit()
            exit()
        
        if eachevent.type == pygame.KEYDOWN and eachevent.key == pygame.K_SPACE:
            game_active = True
            
            # this line add a rect above all the rects but the original is still on top of that because of draw
            squaregroup.add(square(original_sq.rect.x,original_sq.rect.y,(original_sq.rect.x%255,original_sq.rect.x%255,original_sq.rect.y%255)))
            #squaregroup.add(square(original_sq.rect.x,original_sq.rect.y))
            
            #this shows that there are actually some squares dwarn behind the original_sq
            print(len(squaregroup))
        keys = pygame.key.get_pressed()
        events = pygame.event.get()
        
        left = keys[pygame.K_LEFT]
        right = keys[pygame.K_RIGHT]
        up = keys[pygame.K_UP]
        down = keys[pygame.K_DOWN]
     
        squaregroup.update(left,right,up,down)

    if game_active:
        disp_surface = display_surface()
        squaregroup.draw(disp_surface)

        #comment the bellow line to remove the original square and then you will be able to see different color square when you move and press space because the new rect created by pressing spacebar is on top
        pygame.draw.rect(disp_surface,(255,255,255),original_sq)
    else:
        disp_surface.fill((64,64,64))

    pygame.display.update()
    clock.tick(60)