I have been working with a book called Make Your Own Python Text Adventure by Phillip Johnson. Up until Chapter 12 I had little problems. There were some error messages that I got but was able to fix them on my own. Now I'm getting an error message:
SyntaxError: unexpected EOF while parsing
and I don't know where the problem is. I'm not 100% certain what it means either. I'm relatively new to programming and I've just started out with Python. I am currently taking an Intro to Programming course at my community college. Anyway, I will post the game code from several different modules and ask you to check it out! Can someone please let me know where the problem lies and how to resolve it? Thank you!
game.py
# this program is a text based rpg
# import the player module
from player import Player
# import the world module
import world
# prompt the user for a direction
def get_player_command():
return input("Action, (Type a letter): ")
def main():
# display Yradellian name examples
print("Some examples of female Yradellian names include Nelsi, Drew, Casey, Ilya, etc.")
print("Some examples of male Yradellian names include Toreth, Daren, Attel, Mayes, etc.")
# get the character name from the user
yesName = False
while yesName == False:
charName = input("What can I call you? ")
nameInput = input("Your name is, " + charName + "? Type Yes or No: ")
if nameInput == "Yes":
yesName = True
else:
print("Sorry...")
# create a player object
player = Player()
# display the welcome message
print()
player.say_hello(charName)
# display current inventory
print()
print("Here's your starting game Inventory:")
player.print_inventory()
while True:
# display the intro text for each tile
print()
room = world.tile_at(player.x, player.y)
print(room.intro_text())
# modify the player depending on the tile type
room.modify_player(player)
# get the action input from the user
print()
print("Choose what to do...")
action_input = get_player_command()
# display the chosen action
if action_input in ["n", "N"]:
print()
print("Go North!")
player.move_north()
elif action_input in ["e", "E"]:
print()
print("Go East!")
player.move_east()
elif action_input in ["s", "S"]:
print()
print("Go South!")
player.move_south()
elif action_input in ["w", "W"]:
print()
print("Go West!")
player.move_west()
elif action_input in ["i", "I"]:
print()
player.print_inventory()
elif action_input in ["a", "A"]:
player.attack()
elif action_input in ["h", "H"]:
player.heal()
else:
print("That is invalid input.")
print()
# call the main function
main()
enemies.py
# create an Enemy class
class Enemy:
def __init__(self):
raise NotImplementedError("Do not create raw Enemy objects.")
def __str__(self):
return self.name
def is_alive(self):
return self.hp > 0
# create two Enemies
class Wolf(Enemy):
def __init__(self):
self.name = "Wolf"
self.hp = 50
self.damage = 15
class Goblin(Enemy):
def __init__(self):
self.name = "Goblin"
self.hp = 75
self.damage = 20
items.py
# make a parent class Weapon for the weapons classes
class Weapon:
def __init__(self):
raise NotImplementedError("Do not create raw Weapon objects.")
def __str__(self):
return self.name
# create classes for three different types of weapons
class Dagger(Weapon):
def __init__(self):
self.name = "Dagger"
self.description = "A small bladed weapon used for stabbing the enemy."
self.damage = 10
class DaggerSword(Weapon):
def __init__(self):
self.name = "Dagger Sword"
self.description = "A bladed weapon bigger than a dagger but smaller than a sword."
self.damage = 15
class Sword(Weapon):
def __init__(self):
self.name = "Sword"
self.description = "A bladed weapon meant for striking down the enemy."
self.damage = 20
# make a parent class Consumable for the consumables classes
class Consumable:
def __init__(self):
raise NotImplementedError("Do not create raw Consumable objects.")
def __str__(self):
return "{} (+{} HP)".format(self.name, self.healing_value)
# create classes for different types of consumables
class BreadRoll(Consumable):
def __init__(self):
self.name = "Bread Roll"
self.healing_value = 20
class FancyChocolate(Consumable):
def __init__(self):
self.name = "Fancy Chocolate"
self.healing_value = 25
class Potion(Consumable):
def __init__(self):
self.name = "Potion"
self.healing_value = 50
player.py
# import the items module
import items
# import the world module
import world
# create a class for Player
class Player:
# define the inventory
def __init__(self):
self.inventory = [items.Dagger(), "Gold(100)", BreadRoll(), Potion()]
self.x = 1
self.y = 2
self.hp = 100
# print the inventory and the best weapon
def print_inventory(self):
print("Inventory:")
for item in self.inventory:
print("* " + str(item))
best_weapon = self.most_powerful_weapon()
print("Your best weapon is your {}".format(best_weapon))
# create a function to heal the player
def heal(self):
consumables = [item for item in self.inventory if isinstance(item.Consumable)]
if not consumables:
print("You don't have any items to heal you!")
return
for i, item in enumerate(consumables, 1):
print("Choose an item to use to heal: ")
print("{}. {}".format(i, item))
valid = False
while not valid:
choice = input("")
try:
to_eat = consumables[int(choice) - 1]
self.hp = min(100, self.hp + to_eat.healing_value)
self.inventory.remove(to_eat)
print("Current HP: {}".format(self.hp))
valid = True
except (ValueError, IndexError):
print("Invalid choice, try again.")
# welcome the user to the game
def say_hello(self, name):
print("Hello, " + name, ", welcome to the world of Yradel.")
# create a function to determine the best weapon
def most_powerful_weapon(self):
max_damage = 0
best_weapon = None
for item in self.inventory:
try:
if item.damage > max_damage:
best_weapon = item
max_damage = item.damage
except AttributeError:
pass
return best_weapon
# create functions to let the player move
def move(self, dx, dy):
self.x += dx
self.y += dy
def move_north(self):
self.move(dx = 0, dy = -1)
def move_east(self):
self.move(dx = 1, dy = 0)
def move_south(self):
self.move(dx = 0, dy = 1)
def move_west(self):
self.move(dx = -1, dy = 0)
# create a function to attack the enemy
def attack(self):
best_weapon = self.most_powerful_weapon()
room = world.tile_at(self.x, self.y)
enemy = room.enemy
print("You use {} against the {}!".format(best_weapon.name, enemy.name))
enemy.hp -= best_weapon.damage
if not enemy.is_alive():
print("You killed {}!".format(enemy.name))
else:
print("{} HP is {}.".format(enemy.name, enemy.hp)
world.py
# import the enemies module
import enemies
# import the random module
import random
# create a parent class for the map tiles
class MapTile:
def __init__(self, x, y):
self.x = x
self.y = y
def intro_text(self):
raise NotImplementedError("Create a subclass instead!")
def modify_player(self, player):
pass
# create the tile subclasses
class StartTile(MapTile):
def intro_text(self):
return """You find yourself in a forest, sunlight trickling through the leaves overhead. Your feet crunch over the underbrush. You can see four paths through the trees.\n"""
class BoringTile(MapTile):
def intro_text(self):
return """The trees all look the same here...\n"""
class CityTile(MapTile):
def intro_text(self):
return """You made it out of the forest into a small town known as Burenburg. The people greet you warmly and you are filled with a sense of accomplishment.\n"""
class EnemyTile(MapTile):
# have enemies randomly appear
def __init__(self, x, y):
r = random.random()
if r < 0.50:
self.enemy = enemies.Wolf()
self.alive_text = "A lone Wolf approaches you baring its fangs."
self.dead_text = "The Wolf keels over, dead before you."
else:
self.enemy = enemies.Goblin()
self.alive_text = "A Goblin tries to steal your gold, you must defend yourself against his blade."
self.dead_text = "The Goblin sticks its tongue out at you as it falls over dead."
super().__init__(x, y)
# display their alive/dead message
def intro_text(self):
text = self.alive_text if self.enemy.is_alive() else self.dead_text
return text
# have enemies attack the player
def modify_player(self, player):
if self.enemy.is_alive():
player.hp = player.hp - self.enemy.damage
print("Enemy does {} damage. You have {} HP remaining.".format(self.enemy.damage, player.hp))
# create the basic world map
world_map = [
[None, CityTile(1,0), None],
[None, EnemyTile(1,1), None],
[BoringTile(0,2), StartTile(1,2), BoringTile(2,2)],
[None, EnemyTile(1,3), None]
]
# create a function that locates the tile at a specific coordinate
def tile_at(x, y):
if x < 0 or y < 0:
return None
try:
return world_map[y][x]
except IndexError:
return None