Ajout d'un zoom de camera

This commit is contained in:
theo@manjaro 2021-07-02 21:06:34 +02:00
parent facdf19e6e
commit 69c591aaae
5 changed files with 27 additions and 6 deletions

View File

@ -9,7 +9,8 @@ class Game():
self.datadir = lib.get_save_dir("slime") self.datadir = lib.get_save_dir("slime")
self.DISPLAY_WIDTH, self.DISPLAY_HEIGHT = 1280, 720 self.DISPLAY_WIDTH, self.DISPLAY_HEIGHT = 1280, 720
self.window = pygame.display.set_mode(((self.DISPLAY_WIDTH,self.DISPLAY_HEIGHT))) self.window = pygame.Surface((self.DISPLAY_WIDTH,self.DISPLAY_HEIGHT))
self.realwindow = pygame.display.set_mode((self.DISPLAY_WIDTH,self.DISPLAY_HEIGHT))
pygame.display.set_caption("kfr 2 ???") pygame.display.set_caption("kfr 2 ???")
pygame.init() pygame.init()
pygame.mixer.init() pygame.mixer.init()
@ -75,6 +76,7 @@ class Game():
self.globals["cameray"] = 0 self.globals["cameray"] = 0
self.globals["scamerax"] = 3 self.globals["scamerax"] = 3
self.globals["scameray"] = 0 self.globals["scameray"] = 0
self.scaleCamera()
settings = {"sfx":1,"bgm":1} settings = {"sfx":1,"bgm":1}
self.globals["bgmvolume"] = settings["bgm"] self.globals["bgmvolume"] = settings["bgm"]
@ -246,8 +248,12 @@ class Game():
p = Particle(self,sprites,posx,posy,velx,vely,modvelx,modvely,flipx,flipy,fps) p = Particle(self,sprites,posx,posy,velx,vely,modvelx,modvely,flipx,flipy,fps)
self.gameloop.summon(p) self.gameloop.summon(p)
def perror(self,msg): def scaleCamera(self,neww=None,newh=None):
print("ERROR: "+str(msg)) if not neww:
neww=self.DISPLAY_WIDTH
if not newh:
newh=self.DISPLAY_HEIGHT
self.globals["cameraw"] = neww
self.globals["camerah"] = newh
def pinfo(self,msg): self.globals["tempsubsurface"] = pygame.Surface((neww,newh))
print("INFO : "+str(msg))

View File

@ -53,6 +53,9 @@ class Player(BaseObject):
else: else:
self.move(hor*self.speed*self.game.dt,self.vertical*self.game.dt) self.move(hor*self.speed*self.game.dt,self.vertical*self.game.dt)
self.game.globals["camerax"] = self.rect.center[0]-self.game.globals["cameraw"]/2
self.game.globals["cameray"] = self.rect.center[1]-self.game.globals["camerah"]/2
def move(self,movex,movey): def move(self,movex,movey):
hstoped = False hstoped = False
hor = int(movex+self.hrest) hor = int(movex+self.hrest)
@ -108,3 +111,6 @@ class Player(BaseObject):
result = rect result = rect
break break
return result return result
def draw(self):
self.game.window.blit(self.sprite,[self.rect[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]])

View File

@ -14,7 +14,7 @@ class TilesetRenderer(BaseObject):
def draw(self): def draw(self):
for layer in self.layers: for layer in self.layers:
self.game.window.blit(layer["surface"],(layer["offsets"])) self.game.window.blit(layer["surface"],(layer["offsets"][0]-self.game.globals["camerax"],layer["offsets"][1]-self.game.globals["cameray"]))
def reinit(self,level): def reinit(self,level):
json = level["data"] json = level["data"]

View File

@ -59,3 +59,10 @@ class GameLoop():
# Je stabilise la caméra # Je stabilise la caméra
game.globals["camerax"]-=rx game.globals["camerax"]-=rx
game.globals["cameray"]-=ry game.globals["cameray"]-=ry
# Get the right ratio
widthratio = game.DISPLAY_WIDTH/game.globals["cameraw"]
heightratio = game.DISPLAY_HEIGHT/game.globals["camerah"]
game.globals["tempsubsurface"].blit(game.window,[0,0])
temp = game.pygame.transform.scale(game.globals["tempsubsurface"],(game.DISPLAY_WIDTH,game.DISPLAY_HEIGHT))
game.realwindow.blit(temp,[0,0])

View File

@ -8,6 +8,7 @@ from gamedata.objects.combat.player import Player
from gamedata.objects.combat.tileset import TilesetRenderer from gamedata.objects.combat.tileset import TilesetRenderer
def main(game): def main(game):
game.scaleCamera()
game.gameloop.reinit() # Je réinitialis la boucle game.gameloop.reinit() # Je réinitialis la boucle
bg = MenuBackground(game) bg = MenuBackground(game)
menu = MainMenu(game.DISPLAY_WIDTH/4,round(game.DISPLAY_HEIGHT*3/8),game,game.DISPLAY_WIDTH//2,game.DISPLAY_HEIGHT//2) menu = MainMenu(game.DISPLAY_WIDTH/4,round(game.DISPLAY_HEIGHT*3/8),game,game.DISPLAY_WIDTH//2,game.DISPLAY_HEIGHT//2)
@ -15,6 +16,7 @@ def main(game):
game.gameloop.summon(menu) game.gameloop.summon(menu)
def fight(game): def fight(game):
game.scaleCamera(416,234)
game.gameloop.reinit() game.gameloop.reinit()
mapname = game.lib.choice(list(game.levels_lib.keys())) mapname = game.lib.choice(list(game.levels_lib.keys()))
tileset = TilesetRenderer(0,0,game,mapname) tileset = TilesetRenderer(0,0,game,mapname)