2
\$\begingroup\$

I wrote a small "game" in Python for practice. I would like if someone could go over it and tell me if there is anything wrong or if there are any improvements I can make.

import time
import sys
class Creature:
    def __init__(self, name, hp, inventory):
        self.name = name
        self.hp = hp
        self.inventory = inventory
    def get_hp(self):
        return self.hp
    def get_name(self):
        return self.name
    def get_inventory(self):
        return self.inventory
    def add_item(self, item):
        self.inventory.append(item)
class Item:
    def __init__(self, name, damage, special):
        self.name=name
        self.damage=damage
        self.special=special

staff = Item("heroStaff", 2, "Nothing")
heroinv = [] # Hero inventory
Hero = Creature("A hero",100,heroinv)
print("You wake up in a dark room")
time.sleep(1)
print("1: Sleep more")
print("2: Look around")
key = input()
if key == "1":
    print("You keep sleeping. Good night")
    time.sleep(1)
    print(".")
    time.sleep(1)
    print(".")
    time.sleep(1)
    print(".")
    print("You wake up, feeling restored, you decide to look around")
time.sleep(1.5)
out = False
emptybox = False
door1key=False
while not out:
    print("")
    print("You look around and find a door, a window, and a box")
    time.sleep(1)
    print("1: Check door")
    print("2: Check window")
    print("3: Check box")
    key = input()
    if key == "1":
        time.sleep(0.5)
        print("trying to unlock door")
        time.sleep(1)
        print(".")
        time.sleep(1)
        if not out:
          for items in Hero.get_inventory():
                if items.name == "Door1Key":
                    print("CLICK!")
                    time.sleep(1)
                    print("*Door opened*")
                    time.sleep(1)
                    out = True
                    door1key=True
        if not door1key:
            print("Door Could not be opened")
            time.sleep(1)
    if key=="2":
        print("The window is locked. Can't break it")
        time.sleep(0.5)
    if key=="3":
        if not emptybox:
            time.sleep(0.5)
            print("You open the chest and find a staff(2 damage) and a key, you take them")
            key=Item("Door1Key",0,"Nothing")
            Hero.add_item(key)
            Hero.add_item(staff)
            time.sleep(2)
            emptybox = True
        else:
            print("The box is empty")
            time.sleep(0.5)

print("You step out of the room")
time.sleep(1)
print("You see an infected cat in front of you, with death in his eyes.")
time.sleep(2)
strkill="HE WANTS TO KILL YOU."
for char in strkill:
    if char==' ':
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(0.5);
    else:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(0.2);
print("")
time.sleep(1)
print("*Infected cat engages*")
\$\endgroup\$
1
\$\begingroup\$

One improvement would be to simplify this:

    if not out:
        for items in Hero.get_inventory():
            if items.name == "Door1Key":
                print("CLICK!")
                time.sleep(1)
                print("*Door opened*")
                time.sleep(1)
                out = True
                door1key=True
    if not door1key:
        print("Door Could not be opened")
        time.sleep(1)

into this:

    if "Door1Key" in [_.name for _ in Hero.get_inventory()]:
        print("CLICK!")
        time.sleep(1)
        print("*Door opened*")
        time.sleep(1)
        break
    else:
        print("Door could not be opened")
        time.sleep(1)

You don't need "if not out:" at the top of this -- you're in a "while not out" loop and out hasn't had a chance to change since the top of the loop.

The "for items" loop just scans through the inventory looking to see if you have the door key. That can be done in a single statement, as I show.

Once you've opened the door, you're out, so we can just break out of the "while not out" loop and carry on from there.

Several of your descriptions are hard-coded:

print("You look around and find a door, a window, and a box")      
print("You open the chest and find a staff(2 damage) and a key, you take them")
print("You see an infected cat in front of you, with death in his eyes.")

For the room, you could do something like

room = {'entrances': ['door', 'window'],
        'contents': ['box']}
print("You look around and find " +
      ', '.join(['a ' + _ for _ in room.entrances] +
      'and ' + ', '.join(room.contents))

The idea is that rather than embedding your data in the code, you should structure it in a meaningful way, then use generic code to present the data from the structures in the way you want the player to see it.

I think you're on the right track with your Creature and Item classes. I think maybe you want more classes like Place (or Room), Container (to represent boxes, chests, hidey holes, etc.). If you have to write specific logic for each step in the story, your game is going to be very linear and you'll be limited in how much branching and looping you can manage.

On the other hand, if your game is just a network of Places connected to each other with Items, Containers, other Creatures and such scattered throughout for the Hero to encounter and either fight or befriend, and the Hero is free to wander from Place to Place backward and forward and in loops, then the game becomes much less predictable and more interesting.

What would the code look like to process such a network of Places, Items, Containers, Creatures, and whatever else you can dream up?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.