]> Skullheadx's Git Forge - PygameGUIEngine.git/commitdiff
Image rendering
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Fri, 14 Apr 2023 17:01:42 +0000 (13:01 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Fri, 14 Apr 2023 17:01:42 +0000 (13:01 -0400)
image.PNG [new file with mode: 0644]
image.py [new file with mode: 0644]
main.py

diff --git a/image.PNG b/image.PNG
new file mode 100644 (file)
index 0000000..5be4233
Binary files /dev/null and b/image.PNG differ
diff --git a/image.py b/image.py
new file mode 100644 (file)
index 0000000..55a4cb8
--- /dev/null
+++ b/image.py
@@ -0,0 +1,48 @@
+import pygame
+from color import Color
+
+
+class Image:
+
+    def __init__(self, x, y, image_path, border_color=Color.BLACK, border_width=0):
+        self.x = x
+        self.y = y
+        self.image_path = image_path
+        self.image = pygame.image.load(self.image_path)
+        self.width = self.image.get_width()
+        self.height = self.image.get_height()
+
+        self.border_color = border_color
+        self.border_width = border_width
+
+    def get_width(self):
+        return self.width
+
+    def get_height(self):
+        return self.height
+
+    def set_position(self, x, y):
+        self.x = x
+        self.y = y
+
+    def move(self, dx, dy):
+        self.x += dx
+        self.y += dy
+
+    def set_image(self, image_path):
+        self.image_path = image_path
+        self.image = pygame.image.load(self.image_path)
+        self.width = self.image.get_width()
+        self.height = self.image.get_height()
+
+    def set_border_color(self, border_color):
+        self.border_color = border_color
+
+    def resize(self, width, height):
+        self.image = pygame.transform.scale(self.image, (width, height))
+        self.width = width
+        self.height = height
+
+    def draw(self, screen):
+        screen.blit(self.image, (self.x, self.y))
+        pygame.draw.rect(screen, self.border_color, (self.x, self.y, self.width, self.height), self.border_width)
diff --git a/main.py b/main.py
index 28a6225d031e26ce14054ff77d5f7db7627b4ffa..3ae413d8cea4a6736bab350dbc1ffcf1eb44a034 100644 (file)
--- a/main.py
+++ b/main.py
@@ -4,6 +4,8 @@ from rectangle import Rectangle
 from text import Text, Label
 from button import Button
 from container import Container
+from image import Image
+
 
 pygame.init()
 
@@ -22,6 +24,10 @@ e = Button(100, 300, "Quit", "Imprint Shadow", 40, Color.BLUE, Color.BLACK, Colo
            lambda: print("Goodbye World"))
 f = Container(400, 100, Color.WHITE, Color.BLACK, 1, 15, 20, 50, True, [d, e])
 f.add_child(a)
+
+g = Image(0, -400, "image.PNG", Color.BLACK, 1)
+g.resize(600, 400)
+
 is_running = True
 while is_running:
     for event in pygame.event.get():
@@ -46,6 +52,9 @@ while is_running:
     f.move(delta * -10, 0)
     f.draw(screen)
 
+    g.move(0, delta * 50)
+    g.draw(screen)
+
     pygame.display.update()
     delta = clock.tick(60) / 1000  # Seconds since last frame