Inventory Pane
Hello, loves!
Finally! Let’s get back to displaying Dot’s inventory. Tout le monde déteste l’IA.
Back a while ago I had a spike displaying a column on the right side of the screen, intended to be the place where inventory will display. Let’s see if I can find that spike code and pop it in. No, let’s think first. Couldn’t hurt, might help.
-
We’ll make a column of some handy width on the right, beside the map, and we’ll display inventory items in that column.
-
I recall thinking that if, when we take an item into inventory, if we were to change the position of the sprite for that item, the display might “just work”. We’ll see about that, but that’s a ways down the road.
-
The “right” thing to do will probably be to have a separate Camera2D for inventory, and if there’s special code associated with it, perhaps a Section, which seems to be how Arcade wants to do things like this.
-
We’ll experiment this morning, trying to learn what we have to adjust to get the picture more or less correct.
Now to find that code.
The first thing that I did in the spike was to make the window one cell larger:
def main():
# random.seed(35)
# random.seed(234)
layout = DungeonLayout(55, 55)
dungeon = Dungeon(layout)
stepper = make_build_table(layout, dungeon)
screen_multiplier = 16
screen_width = screen_multiplier*dungeon.max_x
screen_height = screen_multiplier*dungeon.max_y
arcade.Window(screen_width + 2*screen_multiplier, screen_height, 'Caveat Emptor')
...
This may not work as intended: it didn’t last time. It also doesn’t this time: the map fills the screen, no blank space on the right. Someone is looking at the window size.
In the article linked above, I changed the viewport on the dungeon camera, like this:
class Cameras:
def __init__(self, view, max_x, max_y, zoom):
self.view = view
self.max_x = max_x
self.max_y = max_y
width = self.max_x*cell_size
height = self.max_y*cell_size
viewport = arcade.rect.LRBT(0,width,0,height)
self.dungeon_cam = arcade.Camera2D(viewport=viewport)
self.init_zoom(zoom)
Now it seems to me that that should have left a wider margin than it did. But I think the extra space isn’t right. I think it should be more like 128 than 32. Wrong multiplier I think.
Let me do one more thing before I take a screenshot.
class DungeonView:
def on_draw(self):
self.clear()
player_cell = self.dungeon.player_cell
if player_cell:
self.cameras.scroll_dungeon_cam(self.dungeon.player_cell)
with self.cameras.dungeon_cam.activate():
self.keyed_sprites.draw()
self.draw_adventurer()
self.draw_flood()
with self.cameras.inventory_cam.activate():
cam = self.cameras.inventory_cam
w = cam.width
h = cam.height
x = w / 2
y = h / 2
rectangle = arcade.rect.XYWH(x,y,w, h)
arcade.draw.draw_rect_filled(rectangle, color=arcade.color.RUST)
That gives me what I wanted:

I’m not sure what approach we should take to drawing the inventory items. We do have the items in Dot’s inventory, so we could loop over them and draw them. Let’s see if we can spike that in, and then we’ll have Dot pick up some stuff and see what we get.
After a bit of messing about I finally discover that, somehow, the sprites have their scale set to zero, perhaps something having to do with being in the SpriteList. Anyway this ad-hoc code:
class DungeonView:
def on_draw(self):
self.clear()
player_cell = self.dungeon.player_cell
if player_cell:
self.cameras.scroll_dungeon_cam(self.dungeon.player_cell)
with self.cameras.dungeon_cam.activate():
self.keyed_sprites.draw()
self.draw_adventurer()
self.draw_flood()
with self.cameras.inventory_cam.activate():
cam = self.cameras.inventory_cam
w = cam.width
h = cam.height
x = w / 2
y = h / 2
cam.position = (x,y)
rectangle = arcade.rect.XYWH(x,y,w, h)
arcade.draw_rect_filled(rectangle, color=arcade.color.DARK_SLATE_GRAY)
sprite_y = 64
for item in self.dungeon.player_inventory:
sprite = self.keyed_sprites.sprite_at(item)
sprite.scale = item.scale
sprite.position = x,sprite_y
sprite.visible = True
arcade.draw_sprite(sprite)
sprite_y += 64
… gives us this picture:

I’m tired: discovering the need to set scale took quite a lot of time and hammering. Let’s sum up.
Summary
That is just about what I intended. The numbers are a bit too magical, but not terrible. There is an issue with the scaling, which is that while item.scale is OK for the three items shown, the other key and the brilliant torch display, respectively, too large and way too large using that value. I’m not sure how we’ll get the values we need but there will be a way, I’m sure.
I think this is a decent spike, and we’ll commit it. We’ll probably want to move this code to a Section just to clean up DungeonView a bit, but we’ll see what we do when we get there.
Main thing for study and next time is to figure out what is going on with the sprite scale.
A decent result overall. See you next time!