Add camera.py

main
Filips Kalniņš 2026-02-11 06:47:12 +00:00
parent 677785bf53
commit 51d0964308
1 changed files with 21 additions and 0 deletions

21
camera.py 100644
View File

@ -0,0 +1,21 @@
import pygame
from settings import *
class Camera:
def __init__(self, target):
self.target = target
self.offset = pygame.Vector2(0, 0)
def update(self):
# Smooth camera movement
target_x = self.target.rect.centerx - SCREEN_WIDTH // 2
target_y = self.target.rect.centery - SCREEN_HEIGHT // 2
self.offset.x += (target_x - self.offset.x) * CAMERA_SMOOTHING
self.offset.y += (target_y - self.offset.y) * CAMERA_SMOOTHING
def apply(self, rect):
return rect.move(-self.offset.x, -self.offset.y)
def screen_to_world(self, pos):
return (pos[0] + self.offset.x, pos[1] + self.offset.y)