Pygame draw multiple/duplicate sprites same class

Viewed 217

The Problem: I want to add the exact same Sprite (it's a 25x25 white box) behind another one

the Sprite starts off at x,y 300,300 I have key functions that allow the Sprite to move in any direction

Let's say, that I move my Sprite to x,y 150,210... how do I grab that NEW position of that original Sprite, and then draw a copy of that Sprite directly "behind" that Sprites new position of x,y 150,210?

The end result is:

  • there are TWO 25x25 squares together.
  • On keypress, the two squares should always stay together. The "TWO" could be 10 or 20 or 40 squares at any point
  • I do not want to increase the height of the sprite as a solution, I explicitly want a copy of the sprite

Please let me know if this question requires better context etc. Thanks in advance

Minimal reproducible example

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)
1 Answers

here is one possible solution i have created a pygame.sprite.group() instead of groupsingle() and then added the squares there and it works fine you can get more squares appear on the back by clicking space bar key and get the new x,y by original_sq.rect.x and original_sq.rect.y moreover commenting line number 68 will show you how new square is created and displayed

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)
Related