The Text based adventure game in java have been a popular genre since the early days of computer gaming. These games challenge players to navigate through a story by making choices that affect the outcome. In this blog post, we’ll explore a simple text-based adventure game implemented in Java using Swing for the graphical user interface.
Overview
The game in java with GUI we’ll build is a basic adventure where the player finds themselves in a series of rooms, each with its own set of challenges and choices. The player’s goal is to navigate through these rooms, collect items, and avoid dangers to reach the final treasure room.
Setting Up the Game
To begin, we’ll create a class TextBasedAdventureGameGUI
to represent our game. This class will manage the game’s state, handle player interactions, and update the graphical user interface accordingly.
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
//java game project
class TextBasedAdventureGameGUI {
private List<String> inventory = new ArrayList<>();
private JFrame frame;
private JTextArea textArea;
private JButton leftDoorButton;
private JButton rightDoorButton;
private JButton takeKeyButton;
private JButton leaveButton;
private JButton playAgainButton;
// ...
}
NOTE
you can also get the code of text based adventure game in java from GitHub by clicking on this button
Source Code
Before download code please make sure to disable AD-BLOCKER because the ad revenue is used for hosting our Website. The download will automatically starts after 10 seconds of stay on redirected page.
Download CodeBuilding the GUI
The graphical user interface (GUI) is an essential part of any game, providing players with visual feedback and interactive controls. Swing is a powerful Java GUI toolkit that provides a collection of components to create desktop applications. In our game, we’ll utilize Swing to build a user-friendly interface comprising a text area and buttons.
- Text Area: The text area will display the game’s narrative, providing players with feedback on their actions and decisions.
- Buttons: We’ll use buttons to represent various choices available to the player, such as which door to enter or whether to pick up an item.
//text based adventure game in java
public TextBasedAdventureGameGUI() {
// Initialize frame and set properties
frame = new JFrame("Text-based Adventure Game");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
// Initialize text area
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setFont(new Font("Arial", Font.PLAIN, 14));
frame.add(new JScrollPane(textArea), BorderLayout.CENTER);
// Initialize buttons and button panel
// ...
}
Logic of game project in java
Our game’s logic revolves around different rooms that the player can explore. Each room will have its own set of actions, challenges, and outcomes.
room1()
: This method represents the initial room where players start. Here, they might encounter a locked chest and a key, setting the stage for subsequent choices.room2()
: In this room, players may encounter a sleeping dragon. If they have the key from the previous room, they can unlock the door and proceed; otherwise, they face a perilous end.room3()
: The final room holds the treasure. Players who successfully navigate the previous challenges will find their efforts rewarded here.
private void room1() {
textArea.append("You enter a room with a locked chest.\n");
textArea.append("You notice a key on the table.\n");
takeKeyButton.setEnabled(true);
leaveButton.setEnabled(true);
}
private void room2() {
textArea.append("You enter a room with a sleeping dragon.\n");
if (inventory.contains("key")) {
textArea.append("You use the key to
Player Choices
Players interact with the game by clicking on buttons corresponding to different actions. These choices invoke specific methods that update the game state and display relevant messages in the text area.
- Go Left/Go Right: Players must decide which door to enter, leading them down different paths.
- Take Key: Players can collect items like keys to unlock doors or solve puzzles.
- Leave Room: Players can choose to exit a room, potentially leading to new challenges or opportunities.
Gameplay Flow of java game project
Starting the Game
The game begins with an introductory message that sets the scene and provides context for the player’s adventure. This initial narrative establishes the player’s starting point and introduces the central objective: to explore the game world, overcome challenges, and ultimately discover the treasure.
Navigating the Rooms
The game text based adventure game in java begins with an introductory message that sets the scene and provides context for the player’s adventure. This initial narrative establishes the player’s starting point and introduces the central objective: to explore the game world, overcome challenges, and ultimately discover the treasure.
Winning and Losing
- Victory: Players achieve victory by successfully navigating the game’s challenges, solving puzzles, and reaching the final treasure room. Victory represents the culmination of the player’s efforts and signifies their mastery of the game’s mechanics and challenges.
- Defeat: Conversely, players may face defeat if they make poor choices, fail to overcome obstacles, or succumb to hazards within the game world. Defeat serves as a learning opportunity, encouraging players to reflect on their decisions and strategies, and motivating them to try again with newfound knowledge and experience.
Conclusion
Creating a text based adventure game in Java offers a great opportunity to practice programming skills while exploring game design concepts. By combining Java’s object-oriented programming capabilities with Swing’s GUI components, we can build a functional and engaging game that challenges players’ decision-making skills.
Whether you’re a beginner looking to hone your programming skills or a game enthusiast eager to create your own interactive experiences, this project provides a fun and educational journey into the world of game development. So, roll up your sleeves, unleash your creativity, and embark on the exciting adventure of building your own text-based game!
Code of text based adventure game in java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
//text based adventure game in java
class TextBasedAdventureGameGUI {
private List<String> inventory = new ArrayList<>();
private JFrame frame;
private JTextArea textArea;
private JButton leftDoorButton;
private JButton rightDoorButton;
private JButton takeKeyButton;
private JButton leaveButton;
private JButton playAgainButton;
public TextBasedAdventureGameGUI() {
frame = new JFrame("Text-based Adventure Game");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setFont(new Font("Arial", Font.PLAIN, 14));
frame.add(new JScrollPane(textArea), BorderLayout.CENTER );
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
leftDoorButton = new JButton("Go Left");
rightDoorButton = new JButton("Go Right");
takeKeyButton = new JButton("Take Key");
leaveButton = new JButton("Leave Room");
playAgainButton = new JButton("Play Again");
leftDoorButton.addActionListener(e -> {
textArea.append("\nYou chose to go left.\n");
room1();
});
rightDoorButton.addActionListener(e -> {
textArea.append("\nYou chose to go right.\n");
room2();
});
takeKeyButton.addActionListener(e -> {
inventory.add("key");
textArea.append("You took the key.\n");
takeKeyButton.setEnabled(false);
});
leaveButton.addActionListener(e -> {
textArea.append("You left the room.\n");
rightDoor();
});
playAgainButton.addActionListener(e -> resetGame());
buttonPanel.add(leftDoorButton);
buttonPanel.add(rightDoorButton);
buttonPanel.add(takeKeyButton);
buttonPanel.add(leaveButton);
buttonPanel.add(playAgainButton);
frame.add(buttonPanel, BorderLayout.SOUTH);
showInstructions();
frame.setVisible(true);
}
private void showInstructions() {
textArea.setText("Welcome to the Text-based Adventure Game!\n");
textArea.append("You find yourself in a dark room with two doors.\n");
textArea.append("Your goal is to navigate through rooms, collect items, and avoid dangers to win.\n");
textArea.append("Follow the on-screen instructions and click the buttons to make choices.\n");
textArea.append("Good luck!\n\n");
}
private void room1() {
textArea.append("You enter a room with a locked chest.\n");
textArea.append("You notice a key on the table.\n");
takeKeyButton.setEnabled(true);
leaveButton.setEnabled(true);
}
private void rightDoor() {
room2();
}
private void room2() {
textArea.append("You enter a room with a sleeping dragon.\n");
if (inventory.contains("key")) {
textArea.append("You use the key to unlock the door and escape quietly.\n");
room3();
} else {
textArea.append("The dragon wakes up and eats you!\n");
playAgainButton.setEnabled(true);
}
}
private void room3() {
textArea.append("You enter a room filled with treasures!\n");
textArea.append("Congratulations, you win!\n");
playAgainButton.setEnabled(true);
}
private void resetGame() {
inventory.clear();
showInstructions();
takeKeyButton.setEnabled(false);
leaveButton.setEnabled(false);
playAgainButton.setEnabled(false);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TextBasedAdventureGameGUI());
}
}
FAQ
How do I start playing the game?
To start the text based adventure game in java, simply run the Java program. Once launched, you’ll be presented with the game’s interface, where you can read the introductory message and begin your adventure by clicking on the available options
What are the controls for playing the game?
The Game project in java is primarily controlled using the buttons displayed on the GUI. You can click on these buttons to make choices and interact with the game world. For example, you can click on buttons like “Go Left,” “Go Right,” “Take Key,” or “Leave Room” to navigate through the rooms and progress in the game.
How do I know what choices are available to me?
The available choices are presented as clickable buttons on the GUI. Each button represents a specific action or decision that you can make within the game. Additionally, the text area in the GUI provides descriptive messages that guide you through the game and inform you of your current situation and available options.
Thanks for sharing this helpful resource! I’ve bookmarked it for future reference.