MI Python TXT RPG Adventure Tutorial 4 | More Inventory and the Store

First a little house cleaning. We need to update our Players inventory drop function. Let’s add that in now.

Add this to the Player class in the Player.py file. We are updating our nested function within our inventory method. We are doing the same thing we did with items. We just change the Player’s / Room’s object list to remove / append from the weapon and armors lists respectively.


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
def drop(room):
      print("DROP (I)TEM | (W)EAPON | (A)RMOR ?")
      user_input = self.user_input()

      if user_input == "I":
        print("DROPPING ITEM")
        for item in self.items:
          print("DROPPING " + item.name)
          self.items.remove(item)
          room.items.append(item)
 
      elif user_input == "W":
        print("DROPPING WEAPON")
        for weapon in self.weapons:
          print("DROPPING " + weapon.name)
          self.weapons.remove(weapon)
          room.weapons.append(weapon)
       
      elif user_input == "A":
        print("DROPPING ARMOR")
        for armor in self.armors:
          print("DROPPING " + armor.name)
          self.armors.remove(armor)
          room.armors.append(armor)
       
      else:
        pass

Then, the Player can’t “die” in game. Without risk their can be no reward. So, lets add a que_free method to the Player similar to the Character que_free method.


1
2
def que_free(self,room):
    pass

1
 

1
Lets also add experience to the game.  So the player can level up.  We will add the defeated enemy's xp to our players xp variable.  In our Game grid method lets add.

1
2
3
4
5
6
  def grid(self,player):
    ....
              ###   QUE ENEMY FREE ON 0 HP    
              if enemy.hp <=0:
                player.xp += enemy.xp
                enemy.queue_free(enemy,room)

Add a “random” encounter method to our Game class. We will call this method to generate random encounters every “turn”.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def random_encounter(self,player):

    #rooms = [start_room,orc_room,peon_room,rat_room]
    enemies = [orc_peon,orc_peon]

    roll = random.randint(0,100)

    if roll <= 10:
      #print("2 ORC PEONS")
      print(enemies[0].name)
      print(enemies[1].name)

      #enemies[0].pos_x = player.pos_x and enemies[0].pos_y = player.pos_y
     
      #enemies[1]

    elif roll >=11 and roll <= 20:
      print("ORC PEON")
    elif roll >=21 and roll <= 95 :
      print("NO ENCOUNTERS")
    elif roll >= 96:
      print("TREASURE ROOM")

Store Class:

Lets add a “store” class to our game. the Player needs a place to buy and sell off inventory. On further refinement / refactoring we might modify our Room class its self. But, for the scope of this Python Rpg text tutorial we will just initialize a new class. This store class will be instantiated when we are on a certain grid position that we will designate in the Stores pos_x and pos_y attributes. We then pass the store into the Game grid room list just like we do with the Room class.

The Store class will have 4 methods. A status method like all the other classes. A menu method that displays user options. Thirdly a buy method. Lastly a sell method. For the buy and sell methods we will pass in the object list as the argument. Instead of writing out each object list (items,weapons,armors).

Lets make our Store class.


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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from Player import *
from Item import *
#from main import *


class Store():
  def __init__(self,pos_x,pos_y,player,items,weapons,armors):
    self.name = "STORE"
    self.pos_x = pos_x
    self.pos_y = pos_y
    self.items = items
    self.weapons = weapons
    self.armors = armors
 
  def status(self):
    print(self.name + " IS INSTANTIATED")
    self.menu(player)

  def menu(self,player):
    print("STORE MENU")
    print("(B)UY | (S)ELL | (L)EAVE")

    user_input = player.user_input()

    if user_input == "B":
      print("BUYING")
      print("BUY (I)TEM | (W)EAPON | (A)RMOR ?")
      user_input = player.user_input()

      if user_input == "I":
        print("BUYING ITEMS")
        self.buy(player,self.items,player.items)
      elif user_input == "W":
        print("BUYING WEAPONS")
        self.buy(player,self.weapons,player.weapons)
      elif user_input == "A":
        print("BUYING ARMOR")
        self.buy(player,self.armors,player.armors)


    elif user_input == "S":
      print("SELLING")
      print("SELL (I)TEM | (W)EAPON | (A)RMOR ?")
      user_input = player.user_input()

      if user_input == "I":
        print("SELLING ITEMS")
        self.sell(player,self.items,player.items)
      elif user_input == "W":
        print("SELLING WEAPONS")
        self.sell(player,self.weapons,player.weapons)
      elif user_input == "A":
        print("SELLING ARMOR")
        self.sell(player,self.armors,player.armors)

    elif user_input == "L":
      print(player.name + " IS LEAVING")


  ###   PASS IN ITEM WEAPON OR ARMOR LIST AS OBJ_LISTS FOR STORE AND PLAYER
  def buy(self,player,store_obj_list,player_obj_list):
    print(player.name + " IS BUYING")
    i = 0
    for obj in store_obj_list:  
      i += 1
      print(str(i) + " " + obj.name + " " + str(obj.gp_value) + " GP")

    print("BUY WHICH NUMBER 1-" + str(len(store_obj_list)))

    user_input = player.user_input()

    try:
      if user_input == "1":
        if player.gp >= store_obj_list[0].gp_value:
          print("BUYING " + store_obj_list[0].name)
          player.gp -= store_obj_list[0].gp_value
          player_obj_list.append(store_obj_list[0])
          store_obj_list.remove(store_obj_list[0])
        else:
          print("NOT ENOUGH GP")
   
    except:
      print("NO ITEM HERE")  

    try:
      if user_input == "2":
        if player.gp >= store_obj_list[1].gp_value:
          print("BUYING " + store_obj_list[1].name)
          player.gp -= store_obj_list[1].gp_value
          player_obj_list.append(store_obj_list[1])
          store_obj_list.remove(store_obj_list[1])
        else:
          print("NOT ENOUGH GP")
   
    except:
      print("NO ITEM HERE")  

    try:
      if user_input == "3":
        if player.gp >= store_obj_list[2].gp_value:
          print("BUYING " + store_obj_list[2].name)
          player.gp -= store_obj_list[2].gp_value
          player_obj_list.append(store_obj_list[2])
          store_obj_list.remove(store_obj_list[2])
        else:
          print("NOT ENOUGH GP")
   
    except:
      print("NO ITEM HERE")  



  def sell(self,player,store_obj_list,player_obj_list):
    print(player.name + " IS SELLING")

    i = 0
    for obj in player_obj_list:  
      i += 1
      print(str(i) + " " + obj.name + " " + str(obj.gp_value) + " GP")

    print("SELL WHICH NUMBER 1-" + str(len(player_obj_list)))

    user_input = player.user_input()
     
    try:
      if user_input == "1":
        print("SELLING " + player_obj_list[0].name )
        print(player.name + " GETS " + str(player_obj_list[0].gp_value * .70) + " GP")
        player.gp += player_obj_list[0].gp_value * .70
        store_obj_list.append(player_obj_list[0])
        player_obj_list.remove(player_obj_list[0])

    except:
      print("NO ITEM HERE")

    try:
      if user_input == "2":
        print("SELLING " + player_obj_list[1].name )
        print(player.name + " GETS " + str(player_obj_list[1].gp_value * .70) + " GP")
        player.gp += player_obj_list[1].gp_value * .70
        store_obj_list.append(player_obj_list[1])
        player_obj_list.remove(player_obj_list[1])

    except:
      print("NO ITEM HERE")
         
    try:
      if user_input == "3":
        print("SELLING " + player_obj_list[2].name )
        print(player.name + " GETS " + str(player_obj_list[2].gp_value * .70) + " GP")
        player.gp += player_obj_list[2].gp_value * .70
        store_obj_list.append(player_obj_list[2])
        player_obj_list.remove(player_obj_list[2])

    except:
      print("NO ITEM HERE")

store = Store(1,0,player,[gem,gem,gem],[],[])

We also need to add our store to the Game grid rooms list. Then we implement some basic error handling so our game wont crash when we leave our store since there wont be enemies in the store unless a random encounter triggers. The try Python method just “tries” to execute the enemy for loop in that particular room. If it can’t then the except Python method is called. This pass’s (same as breaks ?)


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
  def grid(self,player):
    rooms = [start_room,peon_room,gem_room,store]

    ########################################
    #player.status()
    #self.options(player,rooms)
    ########################################

    player.status()
    self.options(player)

    for room in rooms:

      if player.pos_x == room.pos_x and player.pos_y == room.pos_y:
        room.status()
        player.inventory(room)

        try:

          for enemy in room.enemies:
            enemy.pos_x = room.pos_x
            enemy.pos_y = room.pos_y
           
            if enemy.pos_x == player.pos_x and enemy.pos_y == player.pos_y:

              self.battle(player,enemy)
              ###   QUE ENEMY FREE ON 0 HP    
              if enemy.hp <=0:
                enemy.queue_free(enemy,room)
        except:
          pass

Quest Class:

Lets also add quest capability to our game. This is also similar to the Room class but for the scope of this tutorial we are writing a new class instead of inheriting or using super classes. We will create two new classes below. A Quest and a Quest_Ender class.


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
86
87
88
from Player import *
from Character import *
from Item import *

class Quest():
  def __init__(self,name,desc,pos_x,pos_y,npcs,enemies,items,weapons,armors,ender):
    self.name = name
    self.desc = desc
    self.pos_x = pos_x
    self.pos_y = pos_y
    self.npcs = npcs
    self.enemies = enemies
    self.items = items
    self.armors = armors
    self.weapons = weapons
    self.ender = ender
    #self.complete = False

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


    if self.ender.quest_ended == False:
      self.uncompleted()
    else:
      self.completed()

    print("YOU SEE:")
    for npc in self.npcs:
      print(npc.name)
    for enemy in self.enemies:
      print(enemy.name)
    for item in self.items:
      print(item.name)
    for weapon in self.weapons:
      print(weapon.name)
    for armor in self.armors:
      print(armor.name)
   

  def uncompleted(self):
    print("QUEST STARTS HERE AND IS NOT COMPLETED")

  def completed(self):
    print("QUEST IS COMPLETED")



class Quest_Ender():
  def __init__(self,name,desc,pos_x,pos_y,npcs,enemies,items,armors,weapons):
    self.name = name
    self.desc = desc
    self.pos_x = pos_x
    self.pos_y = pos_y
    self.npcs = npcs
    self.enemies = enemies
    self.items = items
    self.armors = armors
    self.weapons = weapons
    self.quest_ended = False

  def status(self):
    print("YOU SEE:")
    for npc in self.npcs:
      print(npc.name)
    for enemy in self.enemies:
      print(enemy.name)
    for item in self.items:
      print(item.name)
    for weapon in self.weapons:
      print(weapon.name)
    for armor in self.armors:
      print(armor.name)

    if len(self.enemies) == 0:
      print("PLAYER HAS COMPLETED THE QUEST")
      self.quest_ended = True


### QUEST ENDERS    name,desc,pos_x,pos_y,npcs,enemies,items,armors,weapons
quest_1_ender = Quest_Ender("QUEST 1 ENDER","THIS ENDS QUEST 1",3,0,[],[orc_peon],[],[],[])

###  QUESTS
###                name,desc,pos_x,pos_y,npcs,enemies,items,weapons,armors,ender
quest_1 = Quest("QUEST 1","THIS IS QUEST ONE",2,0,[],[],[],[],[],quest_1_ender)

Related posts