1

So I'm making a camera class in pygame that will basically allow the user to kind of work the camera in a similar way that it would work in bigger game engines like unity.

The idea essentially works on the basis that there's a big screen or background blitted to the screen and then the camera class would isolate an area and zoom in on that area (if needed) and only that specific area that is isolated would be shown on screen.

So basically I've come up with an idea where the class would use something similar to vector addition where theres a camera position (x, y) which represents where the camera technically is (even though you cannot move the grid in pygame, the camera positions acts as a what if of sorts), then theres a objects list would essentially store any objects coordinates that are blitted to the screen. The code should find the distance between the two vectors and then offsetting it back to the origin (0, 0) in order to make the illusion of camera movement. But obviously I cannot do this. Let me know if you have any Ideas!

heres the code:

import pygame, os
pygame.init() 


run = True
surface = pygame.display.set_mode((500, 500))

path = os.getcwd()
img = pygame.image.load(f"{path}\\Assets\\Animations\\Caped_Hero_Background\\Caped_Hero_Background_0000_Layer-20.png")
resize = pygame.transform.scale(img, (img.get_width(), img.get_height()))

class Camera(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.origin = (x - x, y - y)
        self.z = 1

        self.width = width
        self.height = height
        
        self.differentiate_bool = True

        self.objects = []
        self.objectsRepository = self.objects.copy()

    def append(self, coords):
        self.objects.append(coords)
        self.objectsRepository.append(coords.copy())
    
    def differentiate(self, surface):
        for i in self.objects:
            pygame.draw.line(surface, (255, 0, 0), (self.origin[0], self.origin[1]), (i[0]-self.x, i[1]-self.y), 2)
            i[0] - self.x
        
square_coords = [250, 100]
x = 50
y = 0
cam = Camera(x, y, 500, 500)
cam.append(square_coords)
print(cam.objects)
print(cam.objectsRepository)
while run:
    surface.fill((255, 255, 255))
    pygame.draw.rect(surface, (255, 0, 0), (square_coords[0], square_coords[1], 25, 25))
    cam.differentiate(surface)
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT:
            run = False

    pygame.display.update()

0 Answers0