26 lines
657 B
Python
26 lines
657 B
Python
import pygame as pg
|
|
|
|
class Camera():
|
|
def __init__(self):
|
|
self.zoom = 1
|
|
self.offset = pg.math.Vector2(0, 0)
|
|
|
|
def world_to_screen(self, pos):
|
|
return (
|
|
int((pos[0] - self.offset.x) * self.zoom),
|
|
int((pos[1] - self.offset.y) * self.zoom)
|
|
)
|
|
|
|
def screen_to_world(self, pos):
|
|
return (
|
|
pos[0] / self.zoom + self.offset.x,
|
|
pos[1] / self.zoom + self.offset.y
|
|
)
|
|
|
|
def move(self, dx, dy):
|
|
self.offset.x += dx
|
|
self.offset.y += dy
|
|
|
|
def change_zoom(self, amount):
|
|
self.zoom += amount
|
|
self.zoom = max(0.5, min(self.zoom, 4.0)) |