Modifica il metodo load_image per gestire il colore trasparente e ridimensionare l'immagine in base alla dimensione della cella; aggiorna il metodo draw_image per utilizzare la posizione dello sprite
This commit is contained in:
+15
-3
@@ -28,9 +28,21 @@ class GameWindow:
|
||||
|
||||
def load_image(self, path, transparent_color=None):
|
||||
image_path = os.path.join(os.path.dirname(__file__), "..", "assets", path)
|
||||
image = sdl2.ext.load_image(image_path)
|
||||
image = Image.open(image_path)
|
||||
if transparent_color:
|
||||
image = image.convert("RGBA")
|
||||
datas = image.getdata()
|
||||
new_data = []
|
||||
for item in datas:
|
||||
if item[:3] == transparent_color:
|
||||
new_data.append((255, 255, 255, 0))
|
||||
else:
|
||||
new_data.append(item)
|
||||
image.putdata(new_data)
|
||||
# Ridimensiona l'immagine in base a cell_size
|
||||
return self.factory.from_surface(image)
|
||||
scale = self.cell_size // 20
|
||||
image = image.resize((image.width * scale, image.height * scale), Image.NEAREST)
|
||||
return self.factory.from_surface(sdl2.ext.pillow_to_surface(image))
|
||||
|
||||
def draw_text(self, text, font, position, color):
|
||||
sprite = self.factory.from_text(text, color=color, fontmanager=font)
|
||||
@@ -42,7 +54,7 @@ class GameWindow:
|
||||
|
||||
def draw_image(self, x, y, sprite, tag, anchor="nw"):
|
||||
sprite.position = (x, y)
|
||||
self.renderer.copy(sprite, dstrect=(x, y, sprite.size[0], sprite.size[1]))
|
||||
self.renderer.copy(sprite, dstrect=sprite.position)
|
||||
|
||||
def draw_rectangle(self, x, y, width, height, tag, outline="red"):
|
||||
self.renderer.draw_rect((x, y, width, height), color=sdl2.ext.Color(255, 0, 0))
|
||||
|
||||
Reference in New Issue
Block a user