A basic game code for Thonny Python. The activity is called, "Room Adventures." It is for basic level COSC class so it does not have to be elaborate. I attached the guidelines to this quote.
SAFE Lab CyberIntroduction to Computer Programming Raspberry Pi Activity: Room Adventure In this activity, you will implement a simple text-based game utilizing the object-oriented paradigm. You will need the following items: • Raspberry Pi B v3 with power adapter; • LCD touchscreen; and • Keyboard and mouse. If you wish, you can simply bring your laptop with the Python interpreter (and also perhaps IDLE) installed since you will not be using the GPIO pins on the RPi. The game The first step to designing any kind of computer program is to establish the goal or purpose and to identify all of the necessary details. This is very similar to first understanding a problem before setting about to solve it. Although we will build a very simple game, it can still have (very simple) goals. The setting of the game is a small “mansion” consisting of four rooms. Here's the layout: Gourd 1 Last modified: 01 Dec 2017 Each room has various exits that lead to other rooms in the mansion. In addition, each room has items, some of which can simply be observed, and others that can be picked up and added to the player's inventory. For this activity, there is no actual goal for the player other than to move about the mansion, observe various items throughout the rooms in the mansion, and add various items found in the rooms to inventory. There is an end state that results in death, however! Of course, this doesn't prevent extending the game with a better story and more variety (some ideas will be discussed later). The four rooms are laid out in a simple square pattern. Room 1 is at the top-left of the mansion, room 2 is at the top-right, room 3 is at the bottom-left, and room four is at the bottom-right. Each room has items that can be observed: • Room 1: A chair and a table; • Room 2: A rug and a fireplace; • Room 3: Some bookshelves, a statue, and a desk; and • Room 4: A soda rig (you know, to make some delicious libations). Observable items can provide useful information (once observed) and may reveal new items (some of which can be placed in the player's inventory). In addition, each room may have some items that can be grabbed by the player and placed in inventory: • Room 1: A key; • Room 3: A book; and • Room 4: A 6-pack of a recently made beverage. The rooms have various exits that lead to other rooms in the mansion: • Room 1: An exit to the east that leads to to room 2, and an exit to the south that leads to room 3; • Room 2: An exit to the south that leads to room 4, and an exit to the west that leads to room 1; • Room 3: An exit to the north that leads to room 1, and an exit to the east that leads to room 4; and • Room 4: An exit to the north that leads to room 2, an exit to the west that leads to room 3, and an (unlabeled) exit to the south that leads to...death! Think of it as jumping out of a window. The gameplay The game is text-based (egads, there are no graphics!). Situational awareness is provided by means of meaningful text that describes the current situation in the mansion. Information such as which room the player is located in, what objects are in the current room, and so on, is continually provided throughout the game. The player is prompted for an action (i.e., what to do) after which the current situation is updated. The game supports a simple vocabulary for the player's actions that is composed of a verb followed by a noun. For example, the action “go south” instructs the player to take the south exit in the current room (if that is a valid exit). If the specified exit is invalid (or, for example, if the player misspells an action), an appropriate response is provided, instructing the player of the accepted vocabulary. Supported verbs are: go, look, and take. Supported nouns depend on the verb; for example, for the verb go, the nouns north, east, south, and west are supported. This will allow the player to structure the following go commands: • go north • go east Gourd 2 Last modified: 01 Dec 2017 • go south • go west (young man!) The verbs look and take support a variety of nouns that depend on the actual items located in the rooms of the mansion. The player cannot, for example, “look table” in a room that doesn't have a table! Some examples of look and take actions are: • look table • take key The gameplay depends on the user's input. Rooms change based on valid go actions, meaningful information is provided based on valid look actions, and inventory is accumulated based on valid take actions. For this game, gameplay can continue forever or until the player decides to “go south” in room 4 and effectively jump out of the window to his/her death: At any time, the player may issue the following actions to leave the game: • quit • exit • bye The design By now, you may have already noticed that the four rooms are similar. They each have exits, items that may be observed, and items that may be added to the player's inventory. A good design choice is to utilize the object-oriented paradigm and implement a class that serves as the blueprint for all of the rooms in the game. In fact, let's begin there. Launch IDLE or a text editor and edit a new Python program. Call the document RoomAdventure.py. Let's begin with an informative header and the beginning of the room class: ###################################################################### # Name: # Date: # Description: ###################################################################### ###################################################################### # the blueprint for a room class Room(object): # the constructor Gourd 3 Last modified: 01 Dec 2017 def __init__(self, name): # rooms have a name, exits (e.g., south), exit locations # (e.g., to the south is room n), items (e.g., table), item # descriptions (for each item), and grabbables (things that # can be taken into inventory) self.name = name self.exits = [] self.exitLocations = [] self.items = [] self.itemDescriptions = [] self.grabbables = [] Note that it is wise to periodically save your program in case something happens. Also, it may be best to simply read the source code in this activity so that you understand what is going on instead of actually writing the code as it appears in the document. The entire code will be shown later so that you can write it in its entirety and see the full context. So far, we have covered the constructor of the room class. It does what most constructors do: initializes various instance variables. In this case, a room object must be provided a name in order to be successfully instantiated (note the name parameter in the constructor). The room's name is assigned, and lists that contain its exits, observable items, and items that can be placed in inventory are initialized (all as empty lists). The variables are defined as follows: • name: contains the room's name; • exits: contains the room's exits (e.g., north, south); • exitLocations: contains the rooms found at each exit (e.g., to the south of this room is room 2); • items: contains the observable items in the room; • itemDescriptions: contains the descriptions of the observable items in the room; and • grabbables: contains the items in the room that can be placed in inventory. Note that the lists exits and exitLocations are known as parallel lists. So are the lists items and itemDescriptions. Parallel lists are two or more lists that are associated with each other. That is, they have related data that must be combined to provide meaningful information. The lists are correlated by position (or index). That is, the items at the first index of exits and exitLocations form a meaningful pair. If this were the object reference for room 1, for example, the first item in the list exits could be the string “east”. Consequently, the first item in the list exitLocations would then be the object reference for room 2, since moving east from room 1 should lead to room 2. Similarly, the first item in the list items for room 1 could be “table”. Consequently, the first item in the list itemDescriptions would then be “It is made of oak. A golden key rests on it.” (since this is the description for the table in room 1). The room class should then provide appropriate getters and setters for each instance variable. Recall that this is accomplished by providing getter and setter methods that manipulate (either access or change) instance variables, each of which typically begins with an underscore. For example, the getter for the list exits would be a function named exits that would return the instance variable (a list) _exits. Here are the getters and setters for each instance variable. These are located in the room class, beneath the constructor (make sure to properly indent!): # getters and setters for the instance variables @property Gourd 4 Last modified: 01 Dec 2017 def name(self): return self._name @name.setter def name(self, value): self._name = value @property def exits(self): return self._exits @exits.setter def exits(self, value): self._exits = value @property def exitLocations(self): return self._exitLocations @exitLocations.setter def exitLocations(self, value): self._exitLocations = value @property def items(self): return self._items @items.setter def items(self, value): self._items = value @property def itemDescriptions(self): return self._itemDescriptions @itemDescriptions.setter def itemDescriptions(self, value): self._itemDescriptions = value @property def grabbables(self): return self._grabbables @grabbables.setter def grabbables(self, value): self._grabbables = value Gourd 5 Last modified: 01 Dec 2017 The approach in the game design will be to create an instance of the room class for each room in the mansion. With what has been implemented so far, the room blueprint exists, but there is currently no way to add exits, items, or grabbables. We must therefore provide methods to enable adding each of these things to a room. The first obvious addition is to provide support for adding exits (and their appropriate