1

I’m trying to make a 2D game where that background or screen can be dragged. When this happens I want all the sprites to move with it. How can I implement this?

  • What is your module to make the game? – BrainFl Mar 18 '22 at 12:47
  • @BrainFlooder pygame –  Mar 19 '22 at 00:48
  • Stack Overflow is not a code writing service. You have to be more specific. Where is your code? What have you tried so far? Where do you struggle? Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Rabbid76 Mar 19 '22 at 08:12
  • @Rabbid76 I'm not asking for someone to write my code. I'm asking how to implement this or examples of it. I have never made such a feature before. –  Mar 19 '22 at 15:58
  • So you are searching for a tutorial. This is off topic. Please read [What topics can I ask about here?](https://stackoverflow.com/help/on-topic). – Rabbid76 Mar 19 '22 at 16:25

1 Answers1

0

You would need to detect the MOUSEMOTION event and then check if the left button is clikced using the event.buttons. From there, you can get the number of pixels the screen has been dragged (both horizontally and vertically) to find the new position that the screen should be set to, and hence, the new position for the sprites. Working example below:

import pygame
from pygame.locals import *
from random import randrange

# init global variables
left_btn = 0
cur_x = 0  # current X position
cur_y = 0  # current Y position
go = True
img_pos = pygame.Rect((0, 0), (0, 0))

# Sprite object
class Sprite(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
        self.width = width
        self.height = height
        self.image = pygame.Surface([width, height])
        pygame.draw.rect(self.image, color, pygame.Rect(0, 0, width, height))
        self.rect = self.image.get_rect()
  
    def update(self, x, y):
        self.rect = Rect(self.rect.x + x, self.rect.y + y, self.width, self.height)

# init display
pygame.display.init()
screen = pygame.display.set_mode((800, 600))
img = pygame.image.load('my_image.png')

# Create random Sprites
all_sprites_list = pygame.sprite.Group()
for _ in range(10):
    c = (randrange(255), randrange(255), randrange(255))
    s = Sprite(c, 20, 20)
    s.rect.x = randrange(500)
    s.rect.y = randrange(500)
    all_sprites_list.add(s)

while go:    
    for e in pygame.event.get():
        if e.type == pygame.QUIT: go = False
        if e.type == MOUSEMOTION:
            if e.buttons[left_btn]:
                rel = e.rel
                img_pos.x += rel[0]
                img_pos.y += rel[1]

    # calculate diff in X and Y positions
    x = img_pos.x - cur_x
    y = img_pos.y - cur_y
    
    # update Sprites' position
    all_sprites_list.update(x, y)
    
    # update current X and Y positions
    cur_x = img_pos.x
    cur_y = img_pos.y
    
    screen.fill(0)
    screen.blit(img, img_pos) # update screen's position
    all_sprites_list.draw(screen)
    pygame.display.flip()
    pygame.time.delay(30)
  
pygame.quit()
Chris
  • 18,724
  • 6
  • 46
  • 80