Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The World Prototype
#6
If you want to create a text adventure the easiest way to do this in my opinion is to use a two dimensional array that can be navigated like a maze.

I whipped up the text adventure below to give you an idea of where to start. It's not a full game but simply allows you to navigate the game's world using basic commands.

Go East, Go North, Go South, Go West, Quit, Exit

(You can simply type east, west, north, or south as well)

Each grid in the two dimensional array contains its own descriptions that are displayed as you enter each one. The game ends either when you ask to quit or exit, something kills you, or you leave the world successfully.

Code: (Select All)
OPTION _EXPLICIT

' Quick demonstration of using a two-dimensional array as a map for a text adventure.
'
' Valid commands are: north, south, east, west, quit, exit
'
' You can enter commands such as:
' - I want to quit
' - Exit game
' - or simply quit or exit
' - go east
' - go south
' - go north
' - go west
' - or simply enter the direction you wish to go
'
'  ------------------------
' | The game's maze design |
'  ------------------------
'
' The game is made up of a 5x5 grid. An asterisk (*) indicates a grid square can be entered.
'
'                          NORTH
'
'        +-------+-------+-------+-------+-------+           +----  1  ----+
'        |   *   |       |       |   *   |       |           |             |    Room exits can have the following values:
'        |  1,1  |  2,1  |  3,1  |  4,1  |  5,1  |           |  All doors  |
'        |       |       |       |       |       |             are present       0 - no doors present
'+-------+--   --+-------+-------+--   --+-------+           8             2     1 - North door present
'|   *   |   *   |   *   |   *   |   *   |   *   |                               2 - East door present
'|  0,2 --> 1,2     2,2     3,2     4,2     5,2  |           |  1+2+4+8=15 |     3 - North & East doors present
'| ENTER |       |       |       |       |       |           |             |     4 - South door present
'+-------+--   --+-------+-------+-------+-------+           +----  4  ----+     5 - North & South doors present
'        |   *   |       |   *   |   *   |   *   |                               6 - East & South doors present
'  WEST  |  1,3  |  2,3  |  3,3     4,3     5,3  |  EAST     +----  1  ----+     7 - North & East & South doors present
'        |       |       |       |       |       |           |             |     8 - West door present
'        +--   --+-------+--   --+-------+--   --+           | Only North  |     9 - North & West doors present
'        |   *   |   *   |   *   |       |   *   |           | and East         10 - East & West doors present
'        |  1,4     2,4  |  3,4  |  4,4  |  5,4 --> EXIT     | are present 2    11 - North & East & West doors present
'        |       |       |       |       |       |           |                  12 - South & West doors present
'        +-------+--   --+--   --+-------+--   --+           |    1+2=3    |    13 - North & South & West doors present
'        |       |   *   |   *   |       |   *   |           |             |    14 - East & South & West doors present
'        |  1,5  |  2,5     3,5  |  4,5  |  5,5  |           +-------------+    15 - All doors present
'        |       |       |       |       |       |
'        +-------+-------+-------+-------+-------+
'
'                          SOUTH

CONST FALSE = 0 '           truth detector
CONST TRUE = NOT FALSE '    truth detector
CONST NORTHDOOR = 1 '       north door value
CONST EASTDOOR = 2 '        east door value
CONST SOUTHDOOR = 4 '       south door value
CONST WESTDOOR = 8 '        west door value
CONST MAZEWIDTH = 5 '       width of game maze
CONST MAZEHEIGHT = 5 '      height of game maze

TYPE TYPE_ROOM '            ROOM DEFINITIONS
    Description AS STRING ' room description
    Door AS INTEGER '       doors that exist (1 through 15, see explanation above)
    North AS STRING '       description of north door
    East AS STRING '        description of east door
    South AS STRING '       description of south door
    West AS STRING '        description of west door
END TYPE

DIM Room(MAZEWIDTH, MAZEHEIGHT) AS TYPE_ROOM ' create the game maze playing field
DIM px AS INTEGER '                            player's x location in maze (horizontal)
DIM py AS INTEGER '                            player's y location in maze (vertical)
DIM Response AS STRING '                       player's next response
DIM Valid AS INTEGER '                         did player give a valid response? (TRUE or FALSE)
DIM North AS INTEGER '                         north is a valid direction (TRUE or FALSE)
DIM East AS INTEGER '                          east  is a valid direction (TRUE or FALSE)
DIM South AS INTEGER '                         south is a valid direction (TRUE or FALSE)
DIM West AS INTEGER '                          west  is a valid direction (TRUE or FALSE


'  -----------------------
' | Set up the ENTER room |
'  -----------------------

Room(0, 2).Description = "You are standing outside of a dark sinister looking building."
Room(0, 2).North = "you see a scary forest."
Room(0, 2).East = "is an entrance into the building."
Room(0, 2).South = "you see a swamp."
Room(0, 2).West = "you see a pack of hungry wild dogs."
Room(0, 2).Door = 15 ' all exits exist

'  -----------------------
' | Set up the maze rooms |
'  -----------------------

Room(1, 1).Description = "This walls in this room have an odd glow to them."
Room(1, 1).South = "you see a passage way."
Room(1, 1).Door = 4 ' only exit is to the SOUTH

Room(4, 1).Description = "This room gives you a feeling of dread!"
Room(4, 1).South = "you see an unlocked door."
Room(4, 1).Door = 4 ' only exit is to the SOUTH

Room(1, 2).Description = "You are standing in the lobby of the sinister building."
Room(1, 2).North = "you see a dimly lit passage way."
Room(1, 2).East = "you see a long hallway."
Room(1, 2).South = "you hear sounds coming from a passage way."
Room(1, 2).West = "you see an exit to the oustide world."
Room(1, 2).Door = 15 ' all exits exist

Room(2, 2).Description = "You are in a hallway and hear faint voices."
Room(2, 2).East = "you see the hallway continuing on."
Room(2, 2).West = "you see the hallway continuing on."
Room(2, 2).Door = 10 ' only exits are EAST and WEST

Room(3, 2).Description = "You are in a hallway. The voices are louder."
Room(3, 2).East = "you see the hallway continuing on."
Room(3, 2).West = "you see the hallway continuing on."
Room(3, 2).Door = 10 ' only exits are EAST and WEST

Room(4, 2).Description = "There is a walkie talkie on the ground. You smell something foul."
Room(4, 2).North = "you see an unlocked door."
Room(4, 2).East = "you see the hallway continuing on."
Room(4, 2).West = "you see the hallway continuing on."
Room(4, 2).Door = 11 ' only exits are NORTH, EAST, and WEST

Room(5, 2).Description = "There is a dead security guard laying here!"
Room(5, 2).West = "you see a long hallway."
Room(5, 2).Door = 8 ' only exit is to the WEST

Room(1, 3).Description = "A television is playing a movie in the corner of the room."
Room(1, 3).North = "you see a passage way leading to the building's lobby."
Room(1, 3).South = "you see mist entering from a passage way."
Room(1, 3).Door = 5 ' only exits are NORTH and SOUTH

Room(3, 3).Description = "There appears to be blood smeared on the walls!"
Room(3, 3).East = "you see an open door leading to dark hallway."
Room(3, 3).South = "you see a passage way with a flickering light."
Room(3, 3).Door = 6 ' only exits are EAST and SOUTH

Room(4, 3).Description = "You are in a dark hallway. Once again you feel watched."
Room(4, 3).East = "you see an open door leading to a room lit in red."
Room(4, 3).West = "you see an open door."
Room(4, 3).Door = 10 ' only exits are EAST and WEST

Room(5, 3).Description = "You are in a red room with portraits you swear are watching you!"
Room(5, 3).South = "you see a passage way to escape through."
Room(5, 3).West = "you see a dark hallway."
Room(5, 3).Door = 12 ' only exists are SOUTH and WEST

Room(1, 4).Description = "You feel as though you are being watched through the mist in this room."
Room(1, 4).North = "you see a room with flickering light."
Room(1, 4).East = "you see mist entering through a passage way."
Room(1, 4).Door = 3 ' only exits are NORTH and EAST

Room(2, 4).Description = "Blood appears to be seeping from the north wall!"
Room(2, 4).South = "you see flickering light coming from a passge way."
Room(2, 4).West = "you see mist entering from a passage way."
Room(2, 4).Door = 12 ' only exits are SOUTH and WEST

Room(3, 4).Description = "There is a table against the west wall with candles burning."
Room(3, 4).North = "you see a passage way."
Room(3, 4).South = "you seen an unlocked door."
Room(3, 4).Door = 5 ' only exits are NORTH and SOUTH

Room(5, 4).Description = "You here chains rattling in the distance."
Room(5, 4).North = "you see a passage way to a dimly lit room in red."
Room(5, 4).East = "you see a doorway with sunlight shining in."
Room(5, 4).South = "you see a staircase leading down."
Room(5, 4).Door = 7 ' only exits are NORTH, EAST, and SOUTH

Room(2, 5).Description = "There is a pentagram on the floor surrounded by burning candles."
Room(2, 5).North = "you see a passage way."
Room(2, 5).East = "you see a salt trail leading through a doorway."
Room(2, 5).Door = 3 ' only exits are NORTH and EAST

Room(3, 5).Description = "A dead goat and chicken lie on the floor. The goat is staring at you."
Room(3, 5).North = "you see a passage way with a flickering light."
Room(3, 5).West = "you see a salt trail leading through a doorway."
Room(3, 5).Door = 9 ' only exits are NORTH and WEST

Room(5, 5).Description = "A rotting corpse is chained to the south wall. It's moving!"
Room(5, 5).North = "you see a staircase leading up."
Room(5, 5).Door = 1 ' only exist is NORTH

'  -----------------
' | Begin game here |
'  -----------------

px = 0 ' player's x starting location
py = 2 ' player's y starting location

CLS
PRINT
PRINT " You awaken to find yourself in a strange place. How did I get here?"
PRINT " There is a dark sinister looking building surrounded by a forest and"
PRINT " misty swamp land. You hear dogs barking in the distance."
PRINT

DO '                                                                                             begin main game loop
    DO '                                                                                         begin player response loop
        North = FALSE: East = FALSE: South = FALSE: West = FALSE '                               reset valid directions
        PRINT " ------------------------------------------------------------------------------"
        COLOR 15, 0 '                                                                            bright white on black text
        PRINT " "; Room(px, py).Description '                                                    print current room description
        COLOR 7, 0 '                                                                             white on black text
        IF (Room(px, py).Door AND NORTHDOOR) = NORTHDOOR THEN '                                  is there a door to the north?
            PRINT " - To the north "; Room(px, py).North '                                       yes, print its description
            North = TRUE '                                                                       north is a valid move
        END IF
        IF (Room(px, py).Door AND EASTDOOR) = EASTDOOR THEN '                                    is there a door to the east?
            PRINT " - To the east "; Room(px, py).East '                                         yes, print its description
            East = TRUE '                                                                        east is a valid move
        END IF
        IF (Room(px, py).Door AND SOUTHDOOR) = SOUTHDOOR THEN '                                  is there a door to the south?
            PRINT " - To the south "; Room(px, py).South '                                       yes, print its description
            South = TRUE '                                                                       south is a valid move
        END IF
        IF (Room(px, py).Door AND WESTDOOR) = WESTDOOR THEN '                                    is there a door to the west?
            PRINT " - To the west "; Room(px, py).West '                                         yes, print its description
            West = TRUE '                                                                        west is a valid move
        END IF
        PRINT " ------------------------------------------------------------------------------"
        LINE INPUT " What next? > "; Response '                                                  ask player for input
        PRINT
        Valid = TRUE '                                                                           assume player's input is valid
        Response = UCASE$(Response) '                                                            capitalize player's input
        IF INSTR(Response, "QUIT") OR INSTR(Response, "EXIT") THEN SYSTEM '                      leave game if player wishes
        IF INSTR(Response, "NORTH") AND North THEN '                                             is north a valid move?
            py = py - 1 '                                                                        yes, move player's position up in maze
        ELSEIF INSTR(Response, "EAST") AND East THEN '                                           is east a valid move?
            px = px + 1 '                                                                        yes, move player's position to the right in maze
        ELSEIF INSTR(Response, "SOUTH") AND South THEN '                                         is south a valid move?
            py = py + 1 '                                                                        yes, move player's position down in maze
        ELSEIF INSTR(Response, "WEST") AND West THEN '                                           is west a valid move?
            px = px - 1 '                                                                        yes, move player's position to the left in maze
        ELSE '                                                                                   player entered invalid input
            COLOR 15, 0 '                                                                        bright white on black text
            PRINT " You can't go in that direction." '                                           inform player of error
            COLOR 7, 0 '                                                                         white on black text
            Valid = FALSE '                                                                      player's input was invalid
        END IF
    LOOP UNTIL Valid = TRUE '                                                                    loop back until player enters valid input

    IF px < 1 THEN '                                                                             is player to the left of maze?
        COLOR 15, 0 '                                                                            yes, bright white on black text
        IF py = 1 THEN '                                                                         player went north into the forest
            PRINT " You wander around in the forest for days and die from dehydration." '        player is dead
        ELSEIF py = 3 THEN '                                                                     player went south into the swamp
            PRINT " You enter the swamp and get eaten by an alligator." '                        player is dead
        ELSEIF px = -1 THEN '                                                                    player went west into the dogs
            PRINT " The hungry wild dogs feast on your bones for days." '                        player is dead
        END IF
        END '                                                                                    end the game
    ELSEIF px > 5 THEN '                                                                         no, is player to the right of maze?
        PRINT " ------------------------------------------------------------------------------"
        COLOR 15, 0 '                                                                            yes, bright white on black text
        PRINT " You exit the building wondering what in the hell all that was about?" '          inform player success through maze
        COLOR 7, 0 '                                                                             white on black text
        PRINT " ------------------------------------------------------------------------------"
        END '                                                                                    end game
    END IF
LOOP '                                                                                           loop back to display new room
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply


Messages In This Thread
The World Prototype - by WriterHash - 08-31-2024, 08:12 AM
RE: The World Prototype - by Petr - 08-31-2024, 09:04 AM
RE: The World Prototype - by WriterHash - 08-31-2024, 10:03 AM
RE: The World Prototype - by WriterHash - 08-31-2024, 10:10 AM
RE: The World Prototype - by Pete - 08-31-2024, 04:47 PM
RE: The World Prototype - by TerryRitchie - 08-31-2024, 06:56 PM
RE: The World Prototype - by Pete - 08-31-2024, 07:29 PM



Users browsing this thread: 7 Guest(s)