Added resizable screen

This commit is contained in:
theo@manjaro 2021-09-19 20:12:35 +02:00
parent 706b7e0d92
commit f3ccc88196
2 changed files with 31 additions and 2 deletions

View File

@ -9,7 +9,11 @@ class Game():
self.DISPLAY_WIDTH, self.DISPLAY_HEIGHT = 1280, 720
self.window = pygame.Surface((self.DISPLAY_WIDTH,self.DISPLAY_HEIGHT))
self.realwindow = pygame.display.set_mode((self.DISPLAY_WIDTH,self.DISPLAY_HEIGHT))
self.realwindow = pygame.Surface((self.DISPLAY_WIDTH,self.DISPLAY_HEIGHT))
self.screen = pygame.display.set_mode((self.DISPLAY_WIDTH,self.DISPLAY_HEIGHT),pygame.RESIZABLE)
self.screenw,self.screenh = self.DISPLAY_WIDTH,self.DISPLAY_HEIGHT
self.resizescreenw,self.resizescreenh = self.DISPLAY_WIDTH,self.DISPLAY_HEIGHT
self.screenoffx,self.screenoffy = 0,0
pygame.display.set_caption("Pinmik Panik !")
pygame.init()
pygame.mixer.init()
@ -133,6 +137,9 @@ class Game():
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.VIDEORESIZE:
self.screenw,self.screenh = int(event.w),int(event.h)
self.screen = pygame.display.set_mode((self.screenw,self.screenh),pygame.RESIZABLE)
if event.type == pygame.KEYDOWN:
for i in self.inputs["keys"].keys():
if event.key == self.inputs["keys"][i]["keycode"]: # Vérifie si une des touches du dico est préssée
@ -145,7 +152,13 @@ class Game():
if event.key == self.inputs["keys"][i]["keycode"]: # Vérifie si une des touches du dico est préssée
self.inputs["keys"][i]["pressed"] = False
self.inputs["mouse"]["pos"] = pygame.mouse.get_pos() # Position
x,y = pygame.mouse.get_pos()
x-=self.screenoffx
y-=self.screenoffy
x/=self.resizescreenw/self.DISPLAY_WIDTH
y/=self.resizescreenh/self.DISPLAY_HEIGHT
self.inputs["mouse"]["pos"] = x,y
self.inputs["mouse"]["rel"] = pygame.mouse.get_rel() # Déplacement par rapport à la frame précédente
# Getting mouse position on camera
mx = self.inputs["mouse"]["pos"][0]*self.globals["cameraw"]/self.DISPLAY_WIDTH+self.globals["camerax"]

View File

@ -69,3 +69,19 @@ class GameLoop():
game.realwindow.blit(temp,[0,0])
for i in values:
i.afterdraw() # Draw on the "real screen"
# Draw the game window on screen
game.screen.fill([0,0,0])
w,h = game.screenw,game.screenh
height_on_width = round(game.screenw/16*9)
if height_on_width > h:
w = round(game.screenh/9*16)
else:
h = height_on_width
game.screenoffx = (game.screenw-w)/2
game.screenoffy = (game.screenh-h)/2
game.resizescreenw = w
game.resizescreenh = h
game.pygame.draw.circle(game.realwindow,[255,255,0],game.inputs["mouse"]["pos"],5)
game.screen.blit(game.pygame.transform.scale(game.realwindow,[w,h]),[game.screenoffx,game.screenoffy])