Sheerpower Logo
R.5  Simple Cave Game
Here is a very simple cave exploration game. It shows how to use input from the user, conditional logic, and print statements, to make a simple text-based game. Feel free to expand this game to create your own adventures.

// A Simple Cave Adventure Game print 'Welcome to the Cave Adventure!' print 'You find yourself at the entrance of a dark cave.' print 'Your goal is to explore the cave and find the hidden treasure.' print 'Be careful, as danger may lurk in the shadows.' print // Initialize player's choice declare string choice do print 'You have three paths ahead of you:' print '1. Enter the cave to the left.' print '2. Enter the cave to the right.' print '3. Move forward into the darkness.' input 'Which path will you choose? (1, 2, or 3): ': choice // Check the player's choice if choice = '1' then print 'You enter the cave to the left...' print 'Oh no! You have encountered a wild beast!' print 'You try to fight, but the beast is too strong.' print 'You have to retreat back to the entrance.' print elseif choice = '2' then print 'You enter the cave to the right...' print 'You find a small chamber with a mysterious chest.' print 'Do you want to open the chest?' input 'Enter "yes" to open the chest or "no" to leave it alone: ': choice if lcase$(choice[1:1]) = 'y' then print 'Congratulations! You found the hidden treasure!' print 'You carefully take the treasure and exit the cave as a hero.' exit do else print 'You decide to leave the chest alone and return to the entrance.' print end if elseif choice = '3' then print 'You move forward into the darkness...' print 'The path is long and winding, but you find an exit to the cave.' print 'You have escaped the cave safely, but you feel like you missed something.' print exit do else print 'Invalid choice. Please choose 1, 2, or 3.' print end if loop print 'Thank you for playing Cave Adventure!' end

Code Explanation:

1. Comment:

This comment provides a brief description of the program, indicating that it is a simple text-based adventure game.

2. Welcome Message:

Prints a welcome message to the player, introducing them to the game.

3. Scenario Setup:

Describes the initial scenario, placing the player at the entrance of a mysterious cave.

4. Objective:

Informs the player of their objective to explore the cave and find the hidden treasure.

5. Warning:

Adds suspense by warning the player that there may be danger in the cave.

6. Blank Line:

Prints a blank line for better readability, separating the introduction from the rest of the game.

7. Variable Declaration:

Declares a string variable named choice that will store the player's input throughout the game.

8. Start of Loop:

Begins a do loop, which will continue to run until the player successfully completes the game or chooses to exit.

9. Display Options:

Informs the player that they have three paths to choose from.

10. Option 1:

Describes the first path option: entering the cave to the left.

11. Option 2:

Describes the second path option: entering the cave to the right.

12. Option 3:

Describes the third path option: moving forward into the darkness.

13. User Input:

Prompts the player to choose one of the three paths by entering 1, 2, or 3. The player's choice is stored in the choice variable.

14. Comment:

This comment indicates that the following code block will handle the player's choice.

15. Condition for Option 1:

Checks if the player chose option 1.

16. Path 1 Description:

Describes the scenario when the player enters the cave to the left.

17. Encounter:

The player encounters a wild beast in the cave.

18. Outcome:

Informs the player that they are unable to defeat the beast.

19. Retreat:

The player is forced to retreat back to the entrance of the cave.

20. Blank Line:

Prints a blank line to separate this encounter from the next set of options.

21. Condition for Option 2:

Checks if the player chose option 2.

22. Path 2 Description:

Describes the scenario when the player enters the cave to the right.

23. Discovery:

The player finds a small chamber containing a mysterious chest.

24. Prompt:

Asks the player if they want to open the chest.

25. User Decision:

Prompts the player to decide whether to open the chest or leave it alone. The response is stored in the choice variable.

26. Condition to Open Chest:

Converts the first character of the player's input to lowercase using lcase$ and checks if it is 'y' (i.e., the player chose to open the chest).

27. Success:

If the player chooses to open the chest, they are rewarded with the hidden treasure.

28. Ending:

The player successfully exits the cave with the treasure.

29. Exit Loop:

Exits the loop, ending the game since the player has won.

30. Condition to Leave Chest Alone:

If the player chooses not to open the chest, this block is executed.

31. Return to Entrance:

The player decides to leave the chest alone and returns to the entrance of the cave.

32. Blank Line:

Prints a blank line to separate this scenario from the next set of options.

33. End of Chest Decision Block:

Marks the end of the conditional block that handles the player's decision about the chest.

34. Condition for Option 3:

Checks if the player chose option 3.

35. Path 3 Description:

Describes the scenario when the player moves forward into the darkness.

36. Escape:

The player navigates a long, winding path and finds an exit to the cave.

37. Outcome:

The player safely escapes the cave but is left with a feeling of missing out on something (the treasure).

38. Blank Line:

Prints a blank line to separate this scenario from the next.

39. Exit Loop:

Exits the loop, ending the game as the player has left the cave.

40. Invalid Input Handling:

This block handles cases where the player inputs something other than 1

Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen