Skip to content

Commit 09fae68

Browse files
committed
Add files for OOP
1 parent a00923d commit 09fae68

File tree

5 files changed

+261
-1
lines changed

5 files changed

+261
-1
lines changed

OOP/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ An adaptation of the Zombie-House game using Object-Oriented Programming concept
77
There are many good resources for learning OOP. These examples are based on a course developed by the Raspberry Pi Foundation:
88
[Object-oriented Programming in Python: Create Your Own Adventure Game](https://www.futurelearn.com/courses/object-oriented-principles). This is a free course and enrollment is open several times a year.
99

10-
This library contains classes that can be used to create objects in the game such as rooms and items to be placed into the player's inventory. By using the Adventurelib library, you are beginning to learn the use of Object-Oriented Programming.
10+
In the original Zombie House game, information about the rooms, items in a room and non-playable characters were stored inside a Python dictionary. With OOP, these components of the game are created by constructing an instance of each object contained within a Class. Each object that is created can have attributes (exits in a room, types of items in a room, friendly and enemy characters).
1111

1212
**New Concepts:**
1313

OOP/character.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class Character():
2+
3+
# Create a character
4+
def __init__(self, char_name, char_description):
5+
self.name = char_name
6+
self.description = char_description
7+
self.conversation = None
8+
9+
# Describe this character
10+
def describe(self):
11+
print( self.name + " is here!" )
12+
print( self.description )
13+
14+
# Set what this character will say when talked to
15+
def set_conversation(self, conversation):
16+
self.conversation = conversation
17+
18+
# Talk to this character
19+
def talk(self):
20+
if self.conversation is not None:
21+
print("[" + self.name + " says]: " + self.conversation)
22+
else:
23+
print(self.name + " doesn't want to talk to you")
24+
25+
# Fight with this character
26+
def fight(self, combat_item):
27+
print(self.name + " doesn't want to fight with you")
28+
return False
29+
30+
class Enemy(Character):
31+
32+
enemies_defeated = 0
33+
34+
def __init__(self, char_name, char_description):
35+
36+
super().__init__(char_name, char_description)
37+
self.weakness = None
38+
39+
# Fight with an enemy
40+
def fight(self, combat_item):
41+
if combat_item == self.weakness:
42+
print("You fend " + self.name + " off with the " + combat_item)
43+
Enemy.enemies_defeated += 1
44+
return True
45+
else:
46+
print(self.name + " crushes you, puny adventurer!")
47+
return False
48+
49+
def set_weakness(self, item_weakness):
50+
self.weakness = item_weakness
51+
52+
def get_weakness(self):
53+
return self.weakness
54+
55+
# Getters and setters for the enemies_defeated variable
56+
def get_defeated(self):
57+
return Enemy.enemies_defeated
58+
59+
def set_defeated(self, number_defeated):
60+
Enemy.enemies_defeated = number_defeated
61+
62+
def steal(self):
63+
print("You steal from " + self.name)
64+
# How will you decide what this character has to steal?
65+
66+
67+
class Friend(Character):
68+
69+
def __init__(self, char_name, char_description):
70+
71+
super().__init__(char_name, char_description)
72+
self.feeling = None
73+
74+
def hug(self):
75+
print(self.name + " hugs you back!")

OOP/item.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Item():
2+
3+
def __init__(self, item_name):
4+
self.name = item_name
5+
self.description = None
6+
7+
8+
def set_name(self, item_name):
9+
self.name = item_name
10+
11+
def get_name(self):
12+
return self.name
13+
14+
def set_description(self, item_description):
15+
self.description = item_description
16+
17+
18+
def get_description(self):
19+
return self.description
20+
21+
def describe(self):
22+
print("The [" + self.name + "] is here - " + self.description)

OOP/main.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
'''
2+
Another adaptation of the Zombie House game using OOP concepts
3+
These examples are based on a course developed by the Raspberry Pi Foundation
4+
https://www.futurelearn.com/courses/object-oriented-principles
5+
Sign up and take the free course for a complete set of step-by-step instructions
6+
'''
7+
from room import Room
8+
from character import Enemy, Character
9+
from item import Item
10+
11+
kitchen = Room("Kitchen")
12+
kitchen.set_description("A dank and dirty room buzzing with flies.")
13+
14+
dining_hall = Room("Dining Hall")
15+
dining_hall.set_description("A large room with ornate golden decorations on each wall.")
16+
17+
ballroom = Room("Ballroom")
18+
ballroom.set_description("A vast room with a shiny wooden floor. Huge candlesticks guard the entrance.")
19+
20+
kitchen.link_room(dining_hall, "south")
21+
dining_hall.link_room(kitchen, "north")
22+
dining_hall.link_room(ballroom, "west")
23+
ballroom.link_room(dining_hall, "east")
24+
25+
dave = Enemy("Dave", "A smelly zombie")
26+
dave.set_conversation("What's up, dude! I'm hungry.")
27+
dave.set_weakness("cheese")
28+
dining_hall.set_character(dave)
29+
30+
31+
tabitha = Enemy("Tabitha", "An enormous spider with countless eyes and furry legs.")
32+
tabitha.set_conversation("Sssss....I'm so bored...")
33+
tabitha.set_weakness("book")
34+
ballroom.set_character(tabitha)
35+
36+
37+
cheese = Item("cheese")
38+
cheese.set_description("A large and smelly block of cheese")
39+
ballroom.set_item(cheese)
40+
41+
book = Item("book")
42+
book.set_description("A really good book entitled 'Knitting for dummies'")
43+
dining_hall.set_item(book)
44+
45+
46+
47+
current_room = kitchen
48+
backpack = []
49+
50+
dead = False
51+
52+
while dead == False:
53+
54+
print("\n")
55+
current_room.get_details()
56+
57+
inhabitant = current_room.get_character()
58+
if inhabitant is not None:
59+
inhabitant.describe()
60+
61+
item = current_room.get_item()
62+
if item is not None:
63+
item.describe()
64+
65+
command = input("> ")
66+
67+
if command in ["north", "south", "east", "west"]:
68+
# Move in the given direction
69+
current_room = current_room.move(command)
70+
elif command == "talk":
71+
# Talk to the inhabitant - check whether there is one!
72+
if inhabitant is not None:
73+
inhabitant.talk()
74+
elif command == "fight":
75+
if inhabitant is not None:
76+
# Fight with the inhabitant, if there is one
77+
print("What will you fight with?")
78+
fight_with = input()
79+
80+
# Do I have this item?
81+
if fight_with in backpack:
82+
83+
if inhabitant.fight(fight_with) == True:
84+
# What happens if you win?
85+
print("Hooray, you won the fight!")
86+
current_room.character = None
87+
if inhabitant.get_defeated() == 2:
88+
print("Congratulations, you have vanquished the enemy horde!")
89+
dead = True
90+
else:
91+
# What happens if you lose?
92+
print("Oh dear, you lost the fight.")
93+
print("That's the end of the game")
94+
dead = True
95+
else:
96+
print("You don't have a " + fight_with)
97+
else:
98+
print("There is no one here to fight with")
99+
elif command == "take":
100+
if item is not None:
101+
print("You put the " + item.get_name() + " in your backpack")
102+
backpack.append(item.get_name())
103+
current_room.set_item(None)
104+
else:
105+
print("There's nothing here to take!")
106+
else:
107+
print("I don't know how to " + command)
108+
109+

OOP/room.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Room():
2+
3+
def __init__(self, room_name):
4+
self.name = room_name
5+
self.description = None
6+
self.linked_rooms = {}
7+
self.character = None
8+
self.item = None
9+
10+
def set_character(self, new_character):
11+
self.character = new_character
12+
13+
def get_character(self):
14+
return self.character
15+
16+
def set_description(self, room_description):
17+
self.description = room_description
18+
19+
def get_description(self):
20+
return self.description
21+
22+
def get_name(self):
23+
return self.name
24+
25+
def set_name(self, room_name):
26+
self.name = room_name
27+
28+
def get_item(self):
29+
return self.item
30+
31+
def set_item(self, item_name):
32+
self.item = item_name
33+
34+
def describe(self):
35+
print( self.description )
36+
37+
def link_room(self, room_to_link, direction):
38+
self.linked_rooms[direction] = room_to_link
39+
#print( self.name + " linked rooms: " + repr(self.linked_rooms))
40+
41+
def get_details(self):
42+
print(self.name)
43+
print("--------------------")
44+
print(self.description)
45+
for direction in self.linked_rooms:
46+
room = self.linked_rooms[direction]
47+
print("The " + room.get_name() + " is " + direction)
48+
49+
def move(self, direction):
50+
if direction in self.linked_rooms:
51+
return self.linked_rooms[direction]
52+
else:
53+
print("You can't go that way")
54+
return self

0 commit comments

Comments
 (0)