From: Skullheadx <704277@pdsb.net> Date: Tue, 12 Jul 2022 04:39:07 +0000 (-0400) Subject: More performance fixes X-Git-Url: http://git.skullheadx.com/projects/suckless.html?a=commitdiff_plain;h=a5538c9d34159950539849dd49854544ff0bcd96;p=Pygame-Jam.git More performance fixes --- diff --git a/Block.py b/Block.py index 15b763a..8f03c48 100644 --- a/Block.py +++ b/Block.py @@ -5,6 +5,8 @@ class Block: textures = {file[:file.index(".")]:pg.transform.scale( pg.image.load(path.join("Assets/world/blocks", file)), (50, 50)) for file in listdir("Assets/world/blocks")} + textures = {name: surf.convert() for name, surf in textures.items()} + width, height = textures["PLACEHOLDER"].get_size() colour = (71, 77, 97) diff --git a/Function/createText.py b/Function/createText.py index 409dfbc..17c7ba0 100644 --- a/Function/createText.py +++ b/Function/createText.py @@ -1,14 +1,21 @@ from pygame import font +from functools import lru_cache -def createText(X, Y, size, textColour, weight, textInfo, alignment = "l"): - fontx = font.Font(f"./Font/Exo2-{weight}.ttf", size) - text = fontx.render(textInfo, True, textColour) - textRect = text.get_rect() - if(alignment == "left" or alignment == "l"): - textRect = (X // 2, Y // 2) # Left aligned text - elif(alignment == "center" or alignment == "c"): - textRect.center = (X//2, Y//2) # Centered text - elif(alignment == "right" or alignment == "r"): - textRect.right = (X//2, Y//2) # Right aligned text - - return text, textRect; \ No newline at end of file +@lru_cache +def get_font(weight, size): + return font.Font(f"./Font/Exo2-{weight}.ttf", size) + + +def createText(X, Y, size, textColour, weight, textInfo, alignment="l"): + fontx = get_font(weight,size) + + text = fontx.render(textInfo, True, textColour) + textRect = text.get_rect() + if (alignment == "left" or alignment == "l"): + textRect = (X // 2, Y // 2) # Left aligned text + elif (alignment == "center" or alignment == "c"): + textRect.center = (X // 2, Y // 2) # Centered text + elif (alignment == "right" or alignment == "r"): + textRect.right = (X // 2, Y // 2) # Right aligned text + + return text, textRect; diff --git a/Game.py b/Game.py index 46878c8..8b811b5 100644 --- a/Game.py +++ b/Game.py @@ -59,6 +59,8 @@ class Game: def update(self, delta): Setup.camera_offset = self.player.update(delta) + Setup.camera_offset.x = max(0, min(Setup.camera_offset.x, MAP_WIDTH - SCREEN_WIDTH)) + Setup.camera_offset.y = max(0, min(Setup.camera_offset.y, MAP_HEIGHT - SCREEN_HEIGHT)) if self.player.dead: self.level = self.scene.level diff --git a/Setup.py b/Setup.py index c6ed120..b7b5dd5 100644 --- a/Setup.py +++ b/Setup.py @@ -36,16 +36,11 @@ MAP_WIDTH, MAP_HEIGHT = 10000,10000 def get_display_rect(collision_rect): - camera_offset.x = max(0, min(camera_offset.x, MAP_WIDTH - SCREEN_WIDTH)) - camera_offset.y = max(0, min(camera_offset.y, MAP_HEIGHT - SCREEN_HEIGHT)) - pos = collision_rect.topleft width, height = collision_rect.w, collision_rect.h return pg.Rect(pos - camera_offset, (width, height)) def get_display_point(vec): - camera_offset.x = max(0, min(camera_offset.x, MAP_WIDTH - SCREEN_WIDTH)) - camera_offset.y = max(0, min(camera_offset.y, MAP_HEIGHT - SCREEN_HEIGHT)) return vec - camera_offset from Area import Area diff --git a/main.py b/main.py index eb0597e..5380486 100644 --- a/main.py +++ b/main.py @@ -51,6 +51,6 @@ while is_running: pg.display.update() delta = clock.tick(fps) - print(clock.get_fps()) + # print(clock.get_fps()) pg.quit()