JAVA Goal: Design a text adventure game (ex. Zork.) MUST USE THIS LINK: http://t
ID: 3719169 • Letter: J
Question
JAVA
Goal: Design a text adventure game (ex. Zork.)
MUST USE THIS LINK:
http://textadventures.co.uk/games/view/5zyoqrsugeopel3ffhz_vq/zork
The user will navigate through a dungeon that you create, picking up items for their inventory along the way. They must defeat the boss in the final room in order to win.
Throughout this project, you will use many of the programming concepts that you have learned throughout the semester: variables, loops, conditionals, methods, arrays, strings.
Specifications that must be followed:
Initial Rooms:
1.Your dungeon must have at least 4 rooms, which includes the starting room and the final room.
2.Every time the user enters a room, print a message that displays the room’s name, as well as the user options (see slide #8).
3.The user will start with 10/10 health (int) and an empty inventory (array).
4. At least one room must contain a potion and at least one other room must contain a weapon. When the user enters one of these rooms, you must add the item to the user’s inventory. Also, print a message to inform the user of the new item addition.
Final Room:
1.In order to win the game, the user must battle and defeat the boss, reducing the boss to 0 heath. The user will lose if his/her health goes down to 0 first.
2.Every time the user attacks, print how much damage the attack has performed and the boss’s remaining health.
3.The boss starts with X health, where X is a random integer between 5 and 20.
4.The boss’s attack performs Y damage, where Y is a random integer between 1 and 5. This attack damage changes randomly every time the boss attacks.
5.The user may flee at anytime during the battle (see slide #8).
6.Once the user or the boss’s health goes down to 0, print a “winner” or “loser” message to the screen and terminate the game.
The user will enter these numbers in order navigate through the dungeon:
Initial Rooms: User Options
1.Go north
2.Go south
3.Show inventory
Key for initial rooms
1 - Travel to the room immediately to the north
2 - Travel to the room immediately to the south
3 - Print all items in the user’s inventory
Final Room: User Options
Use weapon
Use potion
Show health
Flee
Key for final rooms/numbers:
1- Perform X damage to opponent
2 - Give back Y health to user
3 - Print user’s current health
4 - Teleport user to the first room of the dungeon
Note: The user can only use a potion and/or weapon
if it exists within his/her inventory. Each potion expires after on use.
Explanation / Answer
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Game {
JFrame window;
Container con;
JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel, playerPanel;
JLabel titleNameLabel, hpLabel, hpLabelNumber, weaponLabel, weaponLabelName;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
Font normalFont = new Font("Times New Roman", Font.PLAIN, 28);
JButton startButton, choice1, choice2, choice3, choice4;
JTextArea mainTextArea;
int playerHP, monsterHP, silverRing;
String weapon, position;
TitleScreenHandler tsHandler = new TitleScreenHandler();
ChoiceHandler choiceHandler = new ChoiceHandler();
public static void main(String[] args) {
new Game();
}
public Game(){
window = new JFrame();
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null);
window.setVisible(true);
con = window.getContentPane();
titleNamePanel = new JPanel();
titleNamePanel.setBounds(100, 100, 600, 150);
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("ADVENTURE");
titleNameLabel.setForeground(Color.white);
titleNameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100);
startButtonPanel.setBackground(Color.black);
startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
startButton.setFocusPainted(false);
titleNamePanel.add(titleNameLabel);
startButtonPanel.add(startButton);
con.add(titleNamePanel);
con.add(startButtonPanel);
}
public void createGameScreen(){
titleNamePanel.setVisible(false);
startButtonPanel.setVisible(false);
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 600, 250);
mainTextPanel.setBackground(Color.black);
con.add(mainTextPanel);
mainTextArea = new JTextArea("This is the main text are. This game is going to be great. I'm sure of it!!!!!!!");
mainTextArea.setBounds(100, 100, 600, 250);
mainTextArea.setBackground(Color.black);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextPanel.add(mainTextArea);
choiceButtonPanel = new JPanel();
choiceButtonPanel.setBounds(250, 350, 300, 150);
choiceButtonPanel.setBackground(Color.black);
choiceButtonPanel.setLayout(new GridLayout(4,1));
con.add(choiceButtonPanel);
choice1 = new JButton("Choice 1");
choice1.setBackground(Color.black);
choice1.setForeground(Color.white);
choice1.setFont(normalFont);
choice1.setFocusPainted(false);
choice1.addActionListener(choiceHandler);
choice1.setActionCommand("c1");
choiceButtonPanel.add(choice1);
choice2 = new JButton("Choice 2");
choice2.setBackground(Color.black);
choice2.setForeground(Color.white);
choice2.setFont(normalFont);
choice2.setFocusPainted(false);
choice2.addActionListener(choiceHandler);
choice2.setActionCommand("c2");
choiceButtonPanel.add(choice2);
choice3 = new JButton("Choice 3");
choice3.setBackground(Color.black);
choice3.setForeground(Color.white);
choice3.setFont(normalFont);
choice3.setFocusPainted(false);
choice3.addActionListener(choiceHandler);
choice3.setActionCommand("c3");
choiceButtonPanel.add(choice3);
choice4 = new JButton("Choice 4");
choice4.setBackground(Color.black);
choice4.setForeground(Color.white);
choice4.setFont(normalFont);
choice4.setFocusPainted(false);
choice4.addActionListener(choiceHandler);
choice4.setActionCommand("c4");
choiceButtonPanel.add(choice4);
playerPanel = new JPanel();
playerPanel.setBounds(100, 15, 600, 50);
playerPanel.setBackground(Color.black);
playerPanel.setLayout(new GridLayout(1,4));
con.add(playerPanel);
hpLabel = new JLabel("HP:");
hpLabel.setFont(normalFont);
hpLabel.setForeground(Color.white);
playerPanel.add(hpLabel);
hpLabelNumber = new JLabel();
hpLabelNumber.setFont(normalFont);
hpLabelNumber.setForeground(Color.white);
playerPanel.add(hpLabelNumber);
weaponLabel = new JLabel("Weapon:");
weaponLabel.setFont(normalFont);
weaponLabel.setForeground(Color.white);
playerPanel.add(weaponLabel);
weaponLabelName = new JLabel();
weaponLabelName.setFont(normalFont);
weaponLabelName.setForeground(Color.white);
playerPanel.add(weaponLabelName);
playerSetup();
}
public void playerSetup(){
playerHP = 15;
monsterHP = 20;
weapon = "Knife";
weaponLabelName.setText(weapon);
hpLabelNumber.setText("" + playerHP);
townGate();
}
public void townGate(){
position = "townGate";
mainTextArea.setText("You are at the gate of the town. A guard is standing in front of you. What do you do?");
choice1.setText("Talk to the guard");
choice2.setText("Attack the guard");
choice3.setText("Leave");
choice4.setText("");
}
public void talkGuard(){
position = "talkGuard";
mainTextArea.setText("Guard: Hello stranger. I have never seen your face. I'm sorry but we cannot let a stranger enter our town.");
choice1.setText(">");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void attackGuard(){
position = "attackGuard";
mainTextArea.setText("Guard: Hey don't be stupid! The guard fought back and hit you hard. (You receive 3 damage)");
playerHP = playerHP -3;
hpLabelNumber.setText(""+playerHP);
choice1.setText(">");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void crossRoad(){
position = "crossRoad";
mainTextArea.setText("You are at a crossroad. If you go south, you will go back to the town.");
choice1.setText("Go north");
choice2.setText("Go east");
choice3.setText("Go south");
choice4.setText("Go west");
}
public void north(){
position = "north";
mainTextArea.setText("There is a river. You drink the water and rest at the riverside. (Your HP is recovered by 2)");
playerHP = playerHP + 2;
hpLabelNumber.setText(""+playerHP);
choice1.setText("Go south");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void east(){
position = "east";
mainTextArea.setText("You walked into a forest and found a Long Sword! (You obtained a Long Sword)");
weapon = "Long Sword";
weaponLabelName.setText(weapon);
choice1.setText("Go west");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void west(){
position = "west";
mainTextArea.setText("You encounter a goblin!");
choice1.setText("Fight");
choice2.setText("Run");
choice3.setText("");
choice4.setText("");
}
public void fight(){
position = "fight";
mainTextArea.setText("Monter HP: " + monsterHP + " What do you do?");
choice1.setText("Attack");
choice2.setText("Run");
choice3.setText("");
choice4.setText("");
}
public void playerAttack(){
position = "playerAttack";
int playerDamage = 0;
if(weapon.equals("Knife")){
playerDamage = new java.util.Random().nextInt(3);
}
else if(weapon.equals("Long Sword")){
playerDamage = new java.util.Random().nextInt(12);
}
mainTextArea.setText("You attacked the monster and gave " + playerDamage + " damage!");
monsterHP = monsterHP - playerDamage;
choice1.setText(">");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void monsterAttack(){
position = "monsterAttack";
int monsterDamage = 0;
monsterDamage = new java.util.Random().nextInt(6);
mainTextArea.setText("The monster attacked you and gave " + monsterDamage + " damage!");
playerHP = playerHP - monsterDamage;
hpLabelNumber.setText(""+playerHP);
choice1.setText(">");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void win(){
position = "win";
mainTextArea.setText("You defeated the monster! The monster dropped a ring! (You obtained a Silver Ring)");
silverRing = 1;
choice1.setText("Go east");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void lose(){
position = "lose";
mainTextArea.setText("You are dead! ");
choice1.setText("");
choice2.setText("");
choice3.setText("");
choice4.setText("");
choice1.setVisible(false);
choice2.setVisible(false);
choice3.setVisible(false);
choice4.setVisible(false);
}
public void ending(){
position = "ending";
mainTextArea.setText("Guard: Oh you killed that goblin!? Thank you so much. You are true hero! Welcome to our town! ");
choice1.setText("");
choice2.setText("");
choice3.setText("");
choice4.setText("");
choice1.setVisible(false);
choice2.setVisible(false);
choice3.setVisible(false);
choice4.setVisible(false);
}
public class TitleScreenHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
createGameScreen();
}
}
public class ChoiceHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String yourChoice = event.getActionCommand();
switch(position){
case "townGate":
switch(yourChoice){
case "c1":
if(silverRing==1){
ending();
}
else{
talkGuard();
}
break;
case "c2": attackGuard();break;
case "c3": crossRoad();break;
}
break;
case "talkGuard":
switch(yourChoice){
case "c1": townGate(); break;
}
break;
case "attackGuard":
switch(yourChoice){
case "c1": townGate(); break;
}
break;
case "crossRoad":
switch(yourChoice){
case "c1": north(); break;
case "c2": east();break;
case "c3": townGate(); break;
case "c4": west();break;
}
break;
case "north":
switch(yourChoice){
case "c1": crossRoad(); break;
}
break;
case "east":
switch(yourChoice){
case "c1": crossRoad(); break;
}
break;
case "west":
switch(yourChoice){
case "c1": fight(); break;
case "c2": crossRoad(); break;
}
break;
case "fight":
switch(yourChoice){
case "c1": playerAttack();break;
case "c2": crossRoad(); break;
}
break;
case "playerAttack":
switch(yourChoice){
case "c1":
if(monsterHP<1){
win();
}
else{
monsterAttack();
}
break;
}
break;
case "monsterAttack":
switch(yourChoice){
case "c1":
if(playerHP<1){
lose();
}
else{
fight();
}
break;
}
break;
case "win":
switch(yourChoice){
case "c1": crossRoad();
}
break;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.