mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-05-23 07:16:44 +02:00
adjusted fps in simple_factory.py
This commit is contained in:
parent
ac2c8c55e7
commit
b4ad112ea6
@ -13,6 +13,7 @@ class Entity(NamedTuple):
|
|||||||
value: float = 1
|
value: float = 1
|
||||||
value_operation: str = 'none'
|
value_operation: str = 'none'
|
||||||
state: str = None
|
state: str = None
|
||||||
|
id: int = 0
|
||||||
|
|
||||||
|
|
||||||
class Renderer:
|
class Renderer:
|
||||||
@ -21,7 +22,7 @@ class Renderer:
|
|||||||
AGENT_VIEW_COLOR = (9, 132, 227)
|
AGENT_VIEW_COLOR = (9, 132, 227)
|
||||||
ASSETS = Path(__file__).parent / 'assets'
|
ASSETS = Path(__file__).parent / 'assets'
|
||||||
|
|
||||||
def __init__(self, grid_w=16, grid_h=16, cell_size=40, fps=8, grid_lines=True, view_radius=2):
|
def __init__(self, grid_w=16, grid_h=16, cell_size=40, fps=7, grid_lines=True, view_radius=2):
|
||||||
self.grid_h = grid_h
|
self.grid_h = grid_h
|
||||||
self.grid_w = grid_w
|
self.grid_w = grid_w
|
||||||
self.cell_size = cell_size
|
self.cell_size = cell_size
|
||||||
@ -37,7 +38,8 @@ class Renderer:
|
|||||||
self.fill_bg()
|
self.fill_bg()
|
||||||
|
|
||||||
now = time.time()
|
now = time.time()
|
||||||
font1 = pygame.font.Font(None, 24)
|
self.font = pygame.font.Font(None, 20)
|
||||||
|
self.font.set_bold(1.0)
|
||||||
print('Loading System font with pygame.font.Font took', time.time() - now)
|
print('Loading System font with pygame.font.Font took', time.time() - now)
|
||||||
|
|
||||||
|
|
||||||
@ -83,19 +85,23 @@ class Renderer:
|
|||||||
for entity in entities:
|
for entity in entities:
|
||||||
bp = self.blit_params(entity)
|
bp = self.blit_params(entity)
|
||||||
blits.append(bp)
|
blits.append(bp)
|
||||||
if 'agent' in entity.name and self.view_radius > 0:
|
if entity.name.lower() == 'agent':
|
||||||
visibility_rect = bp['dest'].inflate(
|
if self.view_radius > 0:
|
||||||
(self.view_radius*2)*self.cell_size, (self.view_radius*2)*self.cell_size
|
visibility_rect = bp['dest'].inflate(
|
||||||
)
|
(self.view_radius*2)*self.cell_size, (self.view_radius*2)*self.cell_size
|
||||||
shape_surf = pygame.Surface(visibility_rect.size, pygame.SRCALPHA)
|
)
|
||||||
pygame.draw.rect(shape_surf, self.AGENT_VIEW_COLOR, shape_surf.get_rect())
|
shape_surf = pygame.Surface(visibility_rect.size, pygame.SRCALPHA)
|
||||||
shape_surf.set_alpha(64)
|
pygame.draw.rect(shape_surf, self.AGENT_VIEW_COLOR, shape_surf.get_rect())
|
||||||
blits.appendleft(dict(source=shape_surf, dest=visibility_rect))
|
shape_surf.set_alpha(64)
|
||||||
|
blits.appendleft(dict(source=shape_surf, dest=visibility_rect))
|
||||||
if entity.state != 'blank':
|
if entity.state != 'blank':
|
||||||
agent_state_blits = self.blit_params(
|
agent_state_blits = self.blit_params(
|
||||||
Entity(entity.state, (entity.pos[0]+0.11, entity.pos[1]), 0.48, 'scale')
|
Entity(entity.state, (entity.pos[0]+0.12, entity.pos[1]), 0.48, 'scale')
|
||||||
)
|
)
|
||||||
blits.append(agent_state_blits)
|
textsurface = self.font.render(str(entity.id), False, (0, 0, 0))
|
||||||
|
text_blit = dict(source=textsurface, dest=(bp['dest'].center[0]-.07*self.cell_size,
|
||||||
|
bp['dest'].center[1]))
|
||||||
|
blits += [agent_state_blits, text_blit]
|
||||||
|
|
||||||
for blit in blits:
|
for blit in blits:
|
||||||
self.screen.blit(**blit)
|
self.screen.blit(**blit)
|
||||||
|
@ -46,7 +46,7 @@ class SimpleFactory(BaseFactory):
|
|||||||
|
|
||||||
if not self._renderer: # lazy init
|
if not self._renderer: # lazy init
|
||||||
height, width = self._state.shape[1:]
|
height, width = self._state.shape[1:]
|
||||||
self._renderer = Renderer(width, height, view_radius=self.pomdp_radius, fps=9)
|
self._renderer = Renderer(width, height, view_radius=self.pomdp_radius, fps=5)
|
||||||
|
|
||||||
dirt = [Entity('dirt', [x, y], min(0.15 + self._state[DIRT_INDEX, x, y], 1.5), 'scale')
|
dirt = [Entity('dirt', [x, y], min(0.15 + self._state[DIRT_INDEX, x, y], 1.5), 'scale')
|
||||||
for x, y in np.argwhere(self._state[DIRT_INDEX] > h.IS_FREE_CELL)]
|
for x, y in np.argwhere(self._state[DIRT_INDEX] > h.IS_FREE_CELL)]
|
||||||
@ -67,7 +67,7 @@ class SimpleFactory(BaseFactory):
|
|||||||
agents = []
|
agents = []
|
||||||
for i, agent in enumerate(self._agent_states):
|
for i, agent in enumerate(self._agent_states):
|
||||||
name, state = asset_str(agent)
|
name, state = asset_str(agent)
|
||||||
agents.append(Entity(name, agent.pos, 1, 'none', state))
|
agents.append(Entity(name, agent.pos, 1, 'none', state, i+1))
|
||||||
self._renderer.render(dirt+walls+agents)
|
self._renderer.render(dirt+walls+agents)
|
||||||
|
|
||||||
def spawn_dirt(self) -> None:
|
def spawn_dirt(self) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user