Coding Workshop

Talk about whatever!
User avatar
noelle
Posts: 9
Joined: Tue 09 Mar, 2021, 9:46 pm
Location: Lansing, MI
Contact:

Re: Coding Workshop

Post by noelle »

I've been muddling this over in my head for a week, and I think this is the solution I've come up with:

Each player object has a dictionary of slots in which clothing items can appear; each slot is a deque (from collections, as above). A very basic object might look something like this:

Code: Select all

player = {"hat": [], "mask": [], "shirt": [], "pants": [], "shoes": []}
(I'm using "[]" to represent deques, not lists.)

A clothes object would have a list of slots in which it appears, like:

Code: Select all

glasses = Clothing("mask")
balaclava = Clothing("hat", "mask")
dress = Clothing("shirt", "boots")
When you add an item of clothing to a player, you go down the list of slots and add that item to the end of each slot:

Code: Select all

def addClothing(item):
  for slot in item:
    player[slot].append(item)
(You could also give clothing a TooBig attribute, which would mean that additional clothing couldn't be appended after it in a given slot, so for example you couldn't put on a balaclava over glasses, or put on shoes over boots.)

When you want to take off an item of clothing, you go down the list of slots and check each slot the clothing appears in to make sure it's the last item in the list. If it's not, you can't take it off (because you have to take something else off first).

Code: Select all

def removeClothing(item):
  for slot in item:
    if player[slot][-1] != item:
      print(f"You can't take {item.name} off yet; you're wearing something over it.")
      return False
    # we don't have to check again because we already verified it's there
    for slot in item:
      player[slot].pop()
    print(f"You removed {item.name}.")
    return True
I hope that makes sense. This is a suuuuper basic implementation (for instance, you probably want to have 'slots' be an attribute of the Clothing object) but I hope it helps.

Post Reply