Skip to content
This repository was archived by the owner on Feb 17, 2023. It is now read-only.

LNX-64 Bmp map parsing #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pytest==7.2.0
transitions==0.9.0
transitions==0.9.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've switched to StateMachine of @alpinus4, remove that line.

pillow==9.0
7 changes: 7 additions & 0 deletions src/init_files/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to map_settings.json or something more meaningful.
This is not really a Pythonic config.

"floor_colour": "(34, 177, 76)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary for floor to be separate from the rest of the functions?

"functions" : [
{"(0, 0, 0)" : "TrainingDummy"},
{"(237, 28, 36)" : "Portal"},
{"(255, 201, 14)" : "self._init_agent"} ]
}
Binary file added src/maps/map1.bmp
Binary file not shown.
50 changes: 24 additions & 26 deletions src/scene.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import random
from typing import Dict, List

from src.common.point import Point
from src.objects.agent import Agent
from src.objects.floor import Floor
from src.objects.portal import Portal
from src.objects.object import Object
from src.objects.npcs.enemies.training_dummy import TrainingDummy


import json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Group exports in accordance to PEP 8 Python standard.
https://peps.python.org/pep-0008/#imports

1. Standard library imports.
2. Related third party imports.
3. Local application/library specific imports.

Please use more blank lines, also.

from PIL import Image
PATH_TO_CONFIG = "./src/init_files/config.json"
class Scene:
"""
Object representing one level in the game.
Expand All @@ -27,20 +27,16 @@ def __init__(self, runtime) -> None:
self._objects_id_map = {}
self.agent_locals = {}
self.runtime = runtime

self._generate_scene_small()

self.__player = Agent(self)
self._generate_scene()
self.__player = None

def _generate_scene_small(self):
"""
Small scene for tests
"""
points = [Point(0, 0), Point(0, 1), Point(1, 0), Point(1, 1), Point(0, 2), Point(1, 2)]

for point in points:
Floor(self, point)

#Portal(self, random.choice(points))
#TrainingDummy(self, Point(1, 0))

Expand All @@ -49,24 +45,23 @@ def _generate_scene(self):
For now it'll generate scene layout
Subject to be changed
"""
STEPS = 20
pos = Point(0, 0)
points = [pos]
for _ in range(STEPS):
pos += Point(random.choice([-1, 0, 1]), random.choice([-1, 0, 1]))
points.append(pos)
points.append(pos + Point(-1, 0))
points.append(pos + Point(1, 0))
points.append(pos + Point(0, -1))
points.append(pos + Point(0, 1))

points = list(dict.fromkeys(points)) # remove duplicates

for point in points:
Floor(self, point)

Portal(self, random.choice(points))
TrainingDummy(self, Point(1, 0))
with open(PATH_TO_CONFIG) as json_file:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we move it to separate file and class?
so that we can could call here

BMPMap.load(self, "./src/maps/map1.bmp")

or something like that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please!

config = json.load(json_file)
floor_colour = config["floor_colour"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the British English spelling of word colour

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superior American writing requires less less typing with the same clarity of the concept's meaning 🥰

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gigachad ə(r) vs virgin ər

colour_to_function_dict = {}
for dict_row in config["functions"]:
colour_to_function_dict.update(dict_row)
map = Image.open("./src/maps/map1.bmp")
image_width, image_height = map.size
for width in range(image_width):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Divide into helper functions, 57-63 especially.

for height in range(image_height):
pixel_colour = str(map.getpixel((width, height)))
if pixel_colour in colour_to_function_dict or pixel_colour == floor_colour:
Floor(self, Point(width, height))
if pixel_colour != floor_colour:
eval(colour_to_function_dict[pixel_colour])(self, Point(width, height))
map.close()

def run(self):
"""
Expand Down Expand Up @@ -144,3 +139,6 @@ def add_object_to_id_map(self, new_object: Object) -> None:
Add object to per scene storage indexed by objects `id`.
"""
self._objects_id_map[new_object.properties.id] = new_object

def _init_agent(self, _, points):
self.__player = Agent(self, points)