Write a program using an object
Start to create your object-oriented text-based adventure game
Extend other people’s classes
Create an enemy in the room
Recap Week 3
Extending your knowledge of OOP
Finish your game
End of the course
At the moment your role-playing game doesn’t welcome players to the game, or display how it was created and by whom.
In this step you will create a new class to introduce the game, show how it was created, and display the end credits. The information about how it was created and the method to show the credits should never change.
You can create this class by using a combination of instance, static, and class variables.
Create a new file called rpginfo.py containing a new RPGInfo class with a single method that welcomes you to the game.
class RPGInfo():
def __init__(self, game_title):
self.title = game_title
def welcome(self):
print("Welcome to " + self.title)
Import the RPGInfo class into your main program.
from rpginfo import RPGInfo
Instantiate an object for your game and give the game a name.
spooky_castle = RPGInfo("The Spooky Castle")
spooky_castle.welcome()
You should display some information about how the game was created using your RPG game generator. As the game created should always display the same message you can use a static class.
Add a static method called info to your RPGInfo class and add a suitable message.
class RPGInfo():
@staticmethod
def info():
print("Made using the OOP RPG game creator (c) me")
Call this static method from your main program using the name of the class.
RPGInfo.info()
You should also provide a method that will show credits displaying at the end of a game information about who wrote the game.
While the information about your role-playing game generator should always be the same, different authors should be able to use your classes to create games. However, the author will be the same for all games they create, so it should be stored against the class, not the object.
Add a class variable to your class to store the author.
class RPGInfo():
author = "Anonymous"
Create a class method that will output the credits and the author’s name.
@classmethod
def credits(cls):
print("Thank you for playing")
print("Created by " + cls.author)
Set the author’s name and call the credits class method at the end of your game.
RPGInfo.author = "Raspberry Pi Foundation"
RPGInfo.credits()
Your complete RPGInfo class should look like this:
class RPGInfo():
author = "Anonymous"
def __init__(self, game_title):
self.title = game_title
def welcome(self):
print("Welcome to " + self.title)
@staticmethod
def info():
print("Made using the OOP RPG game creator (c) me")
@classmethod
def credits(cls):
print("Thank you for playing")
print("Created by " + cls.author)
Your main program should include the following code to use the RPGInfo class:
from rpginfo import RPGInfo
spooky_castle = RPGInfo("The Spooky Castle")
spooky_castle.welcome()
RPGInfo.info()
RPGInfo.author = "Raspberry Pi Foundation"
RPGInfo.credits()
What other static or class methods could you add to the RPGInfo class? Share your thoughts in the comments section.