Skip to content

Commit 6c2d936

Browse files
authored
Room Icons (#38)
* Add icons for the rooms * Add pillow to the requirements * Add helper function for getting the icon for a room type * Draw the icons * Add start room icon * Add version for pillow * Change pillow version to 11.0.0
1 parent 1142e08 commit 6c2d936

File tree

8 files changed

+37
-3
lines changed

8 files changed

+37
-3
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ starlette~=0.41.2
44
black~=24.10.0
55
pylint~=3.3.1
66
flake8~=7.1.1
7-
mypy~=1.8.0
7+
mypy~=1.8.0
8+
pillow~=11.0.0

resources/room_icons/boss_room.png

445 Bytes
Loading

resources/room_icons/item_room.png

456 Bytes
Loading
360 Bytes
Loading

resources/room_icons/shop_room.png

272 Bytes
Loading
520 Bytes
Loading

src/rooms/tkinter/tkinter_room.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def __init__(
2828
super().__init__(x, y, room_id, room_type)
2929
self._width = width
3030
self._height = height
31+
self._image = None
3132

3233
def draw(self, canvas: tk.Canvas):
3334
"""
@@ -40,6 +41,11 @@ def draw(self, canvas: tk.Canvas):
4041
color = room_type.room_colors[self._type].value
4142
hex_color = util_functions.rgb2hex(color[0], color[1], color[2])
4243
canvas.create_rectangle((x0, y0), (x1, y1), fill=hex_color)
44+
center_x = x0 + self._width / 2
45+
center_y = y0 + self._height / 2
46+
self._image = util_functions.get_picture_for_room_type(self._type)
47+
if self._image is not None:
48+
canvas.create_image(center_x, center_y, image=self._image, anchor="center")
4349

4450
@classmethod
4551
def from_room(cls, room: Room):

src/utils/util_functions.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
11
"""
22
This module contains utility functions for the floor generator.
33
"""
4+
import os
45
import random
56
import math
67
from typing import Tuple
78

89
import utils.globals as my_globals
910
from utils.direction import Direction
11+
from utils.room_type import RoomType
12+
from PIL import Image, ImageTk
13+
14+
15+
def get_picture_for_room_type(room_type: RoomType) -> ImageTk:
16+
"""
17+
Gets the picture for the given room type.
18+
@param room_type: room type to get the picture for
19+
@return: picture for the given room type
20+
"""
21+
# Get the directory of the current file
22+
current_dir = os.path.dirname(os.path.abspath(__file__))
23+
# Go up two levels to the project root directory
24+
project_root = os.path.dirname(os.path.dirname(current_dir))
25+
# Define the relative path to the image file
26+
image_file = f"{room_type.name.lower()}.png"
27+
# Join the project root directory with the relative path to get the absolute path
28+
image_path = os.path.join(project_root, "resources", "room_icons", image_file)
29+
try:
30+
return ImageTk.PhotoImage(
31+
Image.open(
32+
image_path
33+
).resize((32, 32))
34+
)
35+
except FileNotFoundError:
36+
return None
1037

1138

1239
def calculate_room_amount(stage_id: int):
@@ -36,7 +63,7 @@ def rgb2hex(r, g, b):
3663

3764

3865
def add_direction_to_coordinates(
39-
direction: Direction, coordinates: Tuple[int, int]
66+
direction: Direction, coordinates: Tuple[int, int]
4067
) -> Tuple[int, int]:
4168
"""
4269
Calculates the room according to the given room and direction
@@ -81,5 +108,5 @@ def calculate_distance(cord1, cord2) -> int:
81108
Calculates the distance between two points.
82109
"""
83110
return (cord1[0] - cord2[0]) * (cord1[0] - cord2[0]) + (cord1[1] - cord2[1]) * (
84-
cord1[1] - cord2[1]
111+
cord1[1] - cord2[1]
85112
)

0 commit comments

Comments
 (0)