Home > Software design >  Why does my movement system randomly break?
Why does my movement system randomly break?

Time:11-08

Here is the full code:

import random
import numpy as np

world = {}

class Player():
  def __init__(self, health, maxHealth, baseDmg, dmg, name, weapons, items, isAlive, previousRoom, roomName):
    self.health = health
    self.maxHealth = maxHealth
    self.baseDmg = baseDmg
    self.dmg = dmg
    self.name = name
    self.weapons = weapons
    self.items = items
    self.isAlive = isAlive
    self.previousRoom = previousRoom
    self.room = world[roomName]

  def Move(self, direction):
    if direction not in self.room.exits:
      print("Cannot Move In That Direction!")
      return
    newRoomName = self.room.exits[direction]
    self.previousRoom = world[self.room.name]
    print("Moving to", newRoomName)
    self.room = world[newRoomName]

  def MoveBack(self):
    self.room = world[self.previousRoom.name]
    print("Moving to", self.room.name)

class Enemy():
  def __init__(self, health, dmg, hasLoot, lootItem, isAlive):
    self.health = health
    self.dmg = dmg
    self.hasLoot = hasLoot
    self.lootItem = lootItem
    self.isAlive = isAlive

class Weapon():
  def __init__(self, name, dmg, description):
    self.name = name
    self.dmg = dmg
    self.description = description

class Item():
  def __init__(self, name, amt, description):
    self.name = name
    self.amt = amt
    self.description = description

class Room():
  def __init__(self, name, description, exits, hasWeapon, weapon, hasItem, item, hasEnemy, enemy, isFirstVisit, coords):
    self.name = name
    self.description = description
    self.exits = exits
    self.hasWeapon = hasWeapon
    self.weapon = weapon
    self.hasItem = hasItem
    self.item = item
    self.hasEnemy = hasEnemy
    self.enemy = enemy
    self.isFirstVisit = isFirstVisit
    self.coords = coords

#######################Dungeon Generation###################
rooms = np.zeros((11, 11))
maxRooms = 7
possibleNextRoom = []

def startLevel():
  for r in range(len(rooms[0])):
    for c in range(len(rooms[1])):
      rooms[r][c] = 0

  possibleNextRoom.clear()

  halfHeight = int(len(rooms[1]) / 2)
  halfWidth = int(len(rooms[0]) / 2)
  rooms[halfWidth][halfHeight] = 1

def resetLevel():
  for r in range(len(rooms[0])):
    for c in range(len(rooms[1])):
      rooms[r][c] = 0
  
  possibleNextRoom.clear()

def countRooms():

  roomCount = 0

  for r in range(len(rooms)):
    for c in range(len(rooms)):
      if rooms[r][c] == 1:
        roomCount  = 1

  return roomCount

def findPossibleRooms():

  for r in range(len(rooms) - 1):
    for c in range(len(rooms) - 1):
      if rooms[r][c] == 1:
        if rooms[r][c 1] != 1:
          possibleNextRoom.append((r, c 1))
        if rooms[r][c-1] != 1:
          possibleNextRoom.append((r, c-1))
        if rooms[r-1][c] != 1:
          possibleNextRoom.append((r-1, c))
        if rooms[r 1][c] != 1:
          possibleNextRoom.append((r 1, c))

def addRoom():
  x = random.randrange(0, len(possibleNextRoom))

  rooms[possibleNextRoom[x][0]][possibleNextRoom[x][1]] = 1
  possibleNextRoom.pop(x)

def generateLevel():

  global x, possibleNextRoom

  startLevel()

  while countRooms() < maxRooms:
    countRooms()
    findPossibleRooms()
    addRoom()

def makeRoomsForLevel():

  counter = 1

  for r in range(len(rooms)):
    for c in range(len(rooms)):
      if rooms[c][r] == 1:
        world[f"room{counter}"] = Room(
          f"room{counter}",
          "",
          {},
          False,
          None,
          False,
          None,
          False,
          None,
          True,
          (r, c)
        )
        counter  = 1

def findRoom(x, y):
  for i in range(len(world)):
      if world[f"room{i 1}"].coords == (x, y):
        return f"room{i 1}"
  return None

def findExits():
  for i in range(len(world)):
      x, y = world[f"room{i 1}"].coords
      exits = dict()
      
      #east
      if rooms[x, y 1] == 1:
          exits["E"] = findRoom(x, y 1)
      
      #south
      if rooms[x 1, y] == 1:
          exits["S"] = findRoom(x 1, y)
      
      #west
      if rooms[x, y-1] == 1:
          exits["W"] = findRoom(x, y-1)
      
      #north
      if rooms[x-1, y] == 1:
          exits["N"] = findRoom(x-1, y)
          
      world[f"room{i 1}"].exits = exits

############################################################

generateLevel()
makeRoomsForLevel()
findExits()

WoodenSword = Weapon("Wooden Sword", 5, "A wooden sword. Looks like a kid's toy.")
IronDagger = Weapon("Iron Dagger", 8, "Small, sharp, and pointy. Good for fighting monsters!")

HealthPot = Item("Health Potion", 1, "A Potion of Instant Health. Restores 10 Health.")

goblin1 = Enemy(25, 2, True, [HealthPot, IronDagger], True)

player = Player(10, 10, 5, 5, "", [], [], True, "room1", "room1")

def ShowInv():
  print("*******************************")
  print("Name:", player.name)
  print("Health:", player.health)
  print("Weapons:")
  for i in player.weapons:
    print(" ===============================")
    print(" Weapon:", i.name)
    print(" Description:", i.description)
    print(" Damage:", i.dmg)
    print(" ===============================")
  print("Items:")
  for i in player.items:
    print(" ===============================")
    print(" Item:", i.name)
    print(" Amount:", i.amt)
    print(" Description:", i.description)
    print(" ===============================")
  print("*******************************")

def testItems(item):
  exists = item in player.items
  return exists

def fight(enemy):
  print("Your Health:", player.health)
  print("Enemy Health:", enemy.health)

  ans = input("What would you like to do?\n>>")
  if ans == "attack":
    chance = random.randrange(1, 20)
    if chance >= 10:
      enemy.health -= player.dmg
    else:
      print("You did not roll high enough...\nYour turn has been passed...")

  if ans == "heal":
    chance = random.randrange(1, 20)
    if testItems(HealthPot):
      if chance >= 10:
        x = 0
        for item in player.items:
          if item == HealthPot:
            player.health  = 10
            if player.health > player.maxHealth:
              player.health = player.maxHealth
            item.amt -= 1
            if item.amt <= 0:
              player.items.pop(x)
              break
          x  = 1
      else:
        print("You did not roll high enough...\nYour turn has been passed...")

  if ans == "run":
    chance = random.randrange(1, 20)
    if chance >= 10:
      player.MoveBack()
    else:
      print("You did not roll high enough...\nYour turn has been passed...")

  if enemy.health > 0 and player.health > 0:
    chance  = random.randrange(1, 20)
    if chance >= 10:
      player.health -= enemy.dmg
  else:
    if enemy.health <= 0:
      enemy.isAvile = False;

def testRoom():
  if player.room.hasWeapon:
    if player.room.isFirstVisit:
      player.weapons.append(player.room.weapon)
  
  if player.room.hasItem:
    if player.room.isFirstVisit:
      player.items.append(player.room.item)

  if player.room.hasEnemy:
    if player.room.isFirstVisit:
      while player.room.enemy.health > 0:
        fight(player.room.enemy)

  player.room.isFirstVisit = False


while True:
  command = input(">>")

  if command in {"N", "S", "E", "W"}:
    player.Move(command)
    testRoom()

  elif command == "look":
    print(player.room.description)
    print("Exits:", *','.join(list(player.room.exits.keys())))

  elif command == "inv":
    ShowInv()

  elif command == "heal":
    if testItems(HealthPot):
      player.health  = 10
      if player.health > player.maxHealth:
        player.health = player.maxHealth
    else:
      print("You don't have any", HealthPot.name, "\bs")

  else:
    print("Invalid Command")

And here is the problem:

Everything seems to work fine, until a couple moves in when it breaks and gives me a "KeyError: None" for the room I just walked in. I have no idea what could be causing this, and I am fairly new, so please simplify the explanations for me. I think it has something to do with the findExits() function and findRoom() function, but idk.

Here is the TraceBackError:

Traceback (most recent call last):
  File "main.py", line 287, in <module>
    player.Move(command)
  File "main.py", line 26, in Move
    self.room = world[newRoomName]
KeyError: None

I've tried fiddling with the function, but nothing i've tried worked. Here is the repl.it link: https://replit.com/@samsonsbrother0/Dungeon-Crawler#main.py

CodePudding user response:

your code is accessing a key before it exists, thus the error. to fix it, you need to assign the newroomname to the world dict as you move, like so:

print("Moving to", newRoomName)
world[newRoomName] = newRoomName
self.room = world[newRoomName]
  • Related