MI Python Text Adventure RPG Tutorial 1 |Create The Game, Objects, and Grid

CREATING THE GAME AND GRID

Repl for this section

https://pythontextadventuretutorial1.mandrews85.repl.run

Hey every one, Thought I would share my methodology for making a text adventure / RPG in Python. I wanted to make this in pure Python with no added dependencies, other then built in Python dependencies. I wanted to create a simple and logical text adventure frame work. Each Python class is its own “module”. I wanted each “module” to handle its own functions to keep everything clear and concise.

Today we will be implementing our Game Grid in the Game class. This is the “world ” we will be putting our game objects in. It is really just a one dimensional list of room objects. Each room object has a position x and a position y attribute. The grid method will parse the rooms list and if the players position x and position y match the rooms. That rooms status will be printed.

Below we create the Game class that will be our game world. Initialize it with the name “Game”. Next we create a status function. I tend to create a status function for every object.


1
2
3
4
5
6
class Game():
  def __init__(self):
    self.name = "GAME"

  def status(self):
    print(self.name + " IS INSTANTIATED")

Creating the Game Grid:

We create the grid function for our game. This will be the method that holds the player and room objects in the game class. The game is going to be broken down into areas named “Rooms”. Each room will have it’s own position x and position y attribute. Along with a list of enemies and items. We will get to more of this in a bit though. For now we are passing the player in as an argument along with a list of rooms to our game grid method. The grid method loops through a list of rooms with a for statement. If the player is on the same x / y coordinates on the grid as a room that rooms status will be displayed.


1
2
3
4
5
6
7
8
9
def grid(self,player):

    print("INSTANTIATE GRID")
    rooms =  [start_room,empty_room]

    for room in rooms:
      if player.pos_x == room.pos_x and player.pos_y == room.pos_y:

        room.status()

Create Game Objects:

Lets quickly create a player and room class so we can instantiate a player and a room in our game. We will create a name and position x / y attributes for now. In our player status we will print the current x/y coords.


1
2
3
4
5
6
7
8
9
class Player():
  def __init__(self):
    self.name = "PLAYER"
    self.pos_x = 0
    self.pos_y = 0
  def status():
     print(self.name)
     print( self.name + " POS X: " + str(self.pos_x))
     print( self.name + " POS Y: " + str(self.pos_y))

Now lets create our rooms that will serve as the x,y areas on the grid. Below we initialize the Room class with : name, desc, pos_x,pos_y attributes. We are adding arguments to each instance of a room by passing the variables in when we instantiate our room objects. Then we make a status method. That is what the player will see upon interaction with a room.


1
2
3
4
5
6
7
8
9
10
11
12
class Room():
  def __init__(self,name,desc,pos_x,pos_y):
    self.name = name
    self.desc = desc
    self.pos_x = pos_x
    self.pos_y = pos_y

   def status():
     print(self.name)
     print(self.desc)
     print("ROOM: X: " + str(self.pos_x))
     print("ROOM Y: " + str(self.pos_y))

Instance Our Objects:

Lets instantiate our objects now. Below we instance the: game , player and room objects. For the rooms we pass in individual arguments. Don’t forget to add the room instances to the game.grid rooms list!!! Then we execute the game grid method with the player passed in as an argument.


1
2
3
4
5
6
7
8
9
10
11
12
###   INSTANCE GAME
game = Game()

###    INSTANCE PLAYER
player = Player()

###   ROOMS        name,desc,pos_x,pos_y
start_room = Room("START ROOM","THIS IS THE STARTING ROOM",0,0)
room_2 = Room("ROOM 2", "THIS IS ROOM 2",0,1)

###   RUN GAME GRID METHOD
game.grid(player)

The Python RPG text adventure code so far should look like below.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Game():
  def __init__(self):
    self.name = "GAME"

  def status(self):
    print(self.name + " IS INSTANTIATED")

  def grid(self,player):


    print("INSTANTIATE GRID")
    rooms = [start_room,room_2]

    for room in rooms:
      if player.pos_x == room.pos_x and player.pos_y == room.pos_y:

        room.status()

class Player():
  def __init__(self):
    self.name = "PLAYER"
    self.pos_x = 0
    self.pos_y = 0
  def status(self):
     print(self.name)
     print( self.name + " POS X: " + str(self.pos_x))
     print( self.name + " POS Y: " + str(self.pos_y))

class Room():
  def __init__(self,name,desc,pos_x,pos_y):
    self.name = name
    self.desc = desc
    self.pos_x = pos_x
    self.pos_y = pos_y

  def status(self):
    print(self.name)
    print(self.desc)
    print("ROOM: X: " + str(self.pos_x))
    print("ROOM Y: " + str(self.pos_y))

###   INSTANCE GAME
game = Game()

###    INSTANCE PLAYER
player = Player()

###   ROOMS        name,desc,pos_x,pos_y
start_room = Room("START ROOM","THIS IS THE STARTING ROOM",0,0)
empty_room = Room("EMPTY ROOM", "THIS IS AN EMPTY ROOM",0,1)

###   RUN GAME GRID METHOD
game.grid(player)

Execute this code and then change the players pos_y to 1. Execute the code again. You should see different room statuses displayed. One for the starting room and one for the empty room in our Python RPG text adventure .

User Interaction:

Of course this is no fun with out user interaction from the player. So lets add an input method for the player. I’ve debated whether to put the user input in the game class or the player class. My rational being that a player should always have self control over their actions within a game, specially a RPG. The game should dictate the options yet the user should be self aware and with at least illusory control. Just a little philosophical rant.

Lets add an input method to our Player class. we are adding the .upper() method to the input so everything imputed is in upper case.


1
2
3
def user_input(self):
    user_input = input(">>> ").upper()
    return user_input

Now lets add an options method for our Game class. “N”, “E”, “S”, and W” are our movement options for now. we will expand on this later. If the Player imputes “N” the players.pos_y is increased by one, etc..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  def options(self,player):
    print("MOVE: (N), (E), (S), (W)")
    ###   PLAYER USER INPUT
    user_input = player.user_input()

    if user_input == "N":
      player.pos_y += 1
    elif user_input == "E":
      player.pos_x += 1
    elif user_input == "S":
      player.pos_y -= 1
    elif user_input == "W":
      player.pos_x -= 1
    else:
      print("INVALID CHOICE")
    return

Lets also write a main loop method for our Game class. With the Player passed in as an argument.


1
2
3
4
5
  def main_loop(self,player):
    game.status()
    while True:
      game.grid(player)
      player.status()

Final Code For This Section:

The updated code is below. Add the Game option method to the Game grid method then we start the Game main loop. The Game options method prints a string of game options and imputes the Players user_input and Player status methods. When the Player imputes “N”, “E”,” S” or “W” the Players pos x / y is updated accordingly. If the players pos x/y equals a rooms pos x/y in the grid room list. That rooms status is printed.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class Game():
  def __init__(self):
    self.name = "GAME"

  def status(self):
    print(self.name + " IS INSTANTIATED")

  def grid(self,player):
    rooms = [start_room,room_2,room_3]

    for room in rooms:
     
      if player.pos_x == room.pos_x and player.pos_y == room.pos_y:
        room.status()


  def options(self,player):
     #player.status()
     print("MOVE: (N), (E), (S), (W)")
     ###   PLAYER USER INPUT
     user_input = player.user_input()

     if user_input == "N":
       player.pos_y += 1
     elif user_input == "E":
       player.pos_x += 1
     elif user_input == "S":
       player.pos_y -= 1
     elif user_input == "W":
       player.pos_x -= 1
     else:
       print("INVALID CHOICE")
     return

  def main_loop(self,player):
      self.status()
      while True:
        self.grid(player)
        player.status()
        self.options(player)
       


class Player():
  def __init__(self):
    self.name = "PLAYER"
    self.pos_x = 0
    self.pos_y = 0
  def status(self):
     print(self.name)
     print(self.name + " POS X: " + str(self.pos_x))
     print(self.name + " POS Y: " + str(self.pos_y))
     
  def user_input(self):
     user_input = input(">>> ").upper()
     return user_input

class Room():
  def __init__(self,name,desc,pos_x,pos_y):
    self.name = name
    self.desc = desc
    self.pos_x = pos_x
    self.pos_y = pos_y

  def status(self):
    print(self.name)
    print(self.desc)
    print("ROOM: X: " + str(self.pos_x))
    print("ROOM Y: " + str(self.pos_y))

###   INSTANCE GAME
game = Game()

###    INSTANCE PLAYER
player = Player()

###   ROOMS        name,desc,pos_x,pos_y
start_room = Room("START ROOM","THIS IS THE STARTING ROOM",0,0)
room_2 = Room("ROOM 2", "THIS IS ROOM 2",0,1)
room_3 = Room("ROOM 3", "THIS IS ROOM 3",0,2)



###   RUN GAME MAIN LOOP
game.main_loop(player)

Run the main.py file and impute a direction. The player is able to interact with the grid at this point.

Check out MI Python Text RPG Adventure Tutorial 2 at MIPYTHON. Next time we will be adding enemies and item objects to our rooms. We will also populate our game grid and organize our program structure more logically.

Related posts