Journey With the Python Text Adventure Empire Fable

PLAY THE WORK IN PROGRESS GAME EMPIRE FABLE HERE:

https://PYTHONTEXTADVENTURE.mandrews85.repl.run

Hey everybody, I’ve seen some questions lately. I would like to share my methods (pun intended) that helped me better understand what I want to do with my programs .

I think Python is an excellent language for a text adventure. If I want to scale it up later with graphics and menus. Tkinter is an easy option or of course Pygame. For this project though I wanted to stick with pure Python. I didn’t want to have a bunch of dependencies. I want to make my own text adventure frame work eventually.

A cool Ascii font for a text adventure in Python is the Efti Wall font here:

http://patorjk.com/software/taag/#p=display&h=2&v=3&f=Efti%20Wall&t=H%0A

I wanted to do Ascii for the room, character and player “avatars”. I’m calling them “portraits”. Eventually it would be cool to have ascii “images” for the items too.

I was always into D&D (Dungeons and Dragons) and was fascinated with text adventures back in the day. So the next logical step for me would be to write a simulation that encapsulates the basic algorithm from D&D that Gary Gygax developed. I’ve simplified the THAC0 system a little bit but a more faithful reproduction would be cool to do.

I’ve refactored this many many times now looking for the simplest, logical way to execute this. For player movement. I’ve come up with a “grid” method. The grid method passes the player in with a list of “rooms”. The rooms are the areas of the game. Rooms are stored as objects in a 1 dimensional array. Each room has an x and y positional attribute. The grid method loops through the list of rooms. If the players position X and position Y attributes = the rooms position x and position y attributes. The player see’s that rooms status. I’m always looking for more efficient ways of doing things though.

Let’s get to the code.

GAME CLASS:

Below, we initialize the game class. The other objects, Player, Rooms, Characters and Items are imported into the “main.py” file. With the grid method we are passing the player object in with a list of rooms. We then loop through the rooms. If the players pos x and pos y = any room pos x and pos y, that rooms status is printed. If enemies are in the room.enemies list the game.battle_options() method is triggered. Enemies position x and position y = the rooms position x and position y.


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
from Player import *
from Room import *
from Character import *
from Item import *

class Game():
  def __init__(self):
    self.name = "GAME"

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


  def grid(self,player):

   
    print("INITIALIZE GRID")
    rooms = [start_room]

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

        room.status()

        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:
            while enemy.hp >= 0:
              self.battle_options(player,enemy)
             

    ###   PLAYER OPTIONS ON TEH GRID
    self.options(player)
 

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

    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 battle_options(self,attacker,defender):

    ###  TO HIT ROLL PHASE
 
    attacker_roll = attacker.roll_die(100)
    defender_roll = defender.roll_die(100)

    if attacker_roll >= defender_roll:
      print(attacker.name + " HITS " + defender.name)
      defender.hp -= attacker.dmg
    else:
      print(attacker.name + " MISSES " + defender.name)

Now lets create our objects that will appear in the game.


1
 

[quote]

[code]

[/code]

[/quote]

Related posts