]> Skullheadx's Git Forge - Pygame-Jam.git/commitdiff
More performance fixes
authorSkullheadx <704277@pdsb.net>
Tue, 12 Jul 2022 04:39:07 +0000 (00:39 -0400)
committerSkullheadx <704277@pdsb.net>
Tue, 12 Jul 2022 04:39:07 +0000 (00:39 -0400)
Block.py
Function/createText.py
Game.py
Setup.py
main.py

index 15b763a991cabcec68300111198b7619d34571f3..8f03c48ee167dbec4fa875935071310df96efa8c 100644 (file)
--- 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)
 
index 409dfbce6e17f38bdeb8c5935b11dd7669e38814..17c7ba0e99a6ef05927a62d6ae2a43ba9372df37 100644 (file)
@@ -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 46878c8c42c719d00406a4a5127d5ada8017806a..8b811b521b4f48ad505fdf62412ad41c9ab21571 100644 (file)
--- 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
 
index c6ed120e158b5f9cc48a8fa9c8b8f9454c0f29ed..b7b5dd5577e04141d1b3ffed29463cc188a8771d 100644 (file)
--- 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 eb0597ebdc6e443947a0f7c62fc5bab2d54fb689..5380486253ae4f652ee9bd2a4f089666a5ba3a18 100644 (file)
--- 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()