Hello I am currently making a weird type of game, and for one part of it you get encountered by a monster which is called a rat and to attack I use Random to give out a number inbetween 0-7 to deal damage and well everytime you click its supposed to attack (deal damage) and use the random above to decide how much damage it deals. Everytime it deals damage I want it to subtract from the rat's overall health.
int rabbithealth = 23;
Random attack = new Random();
rabbithealth = rabbithealth - attack.nextInt(7);
Now, I know why I'm having trouble. rabbithealth is 23 so it subtracts the random numb each time, so its going to just print rabbithealth - attack i.e , but I want it to subtract from that number instead of 23 each time. In an example instead of it being 23 - 4 , or 23 - 6 each time i want it to be 23 - 3 then when clicked again 20 - (random number) here is my code
package com.Game.user;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JLabel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import java.awt.Font;
import java.awt.Canvas;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;
public class Swendiver {
private JFrame frmSwendiver;
private JTextField console;
private JTextField Log;
private JTextField Weapon;
private JTextField Arrows;
private JTextField next;
private JTextField Class;
private JTextField Coin;
private JTextField enemyhealth;
private JTextField myhealth;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Swendiver window = new Swendiver();
window.frmSwendiver.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Swendiver() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmSwendiver = new JFrame();
frmSwendiver.setResizable(false);
frmSwendiver.setTitle("Swendiver 1.0");
frmSwendiver.setAutoRequestFocus(false);
frmSwendiver.setAlwaysOnTop(true);
frmSwendiver.setBounds(100, 100, 623, 406);
frmSwendiver.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmSwendiver.getContentPane().setLayout(null);
console = new JTextField();
console.setBounds(0, 0, 606, 44);
console.setEditable(false);
frmSwendiver.getContentPane().add(console);
console.setColumns(10);
Log = new JTextField();
Log.setBounds(0, 311, 607, 56);
Log.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// KeyEvent.getKeyCode(VK_ENTE-R);
}
});
frmSwendiver.getContentPane().add(Log);
Log.setColumns(10);
JButton button4 = new JButton("Enter");
button4.setBounds(517, 43, 89, 270);
JLabel lblHealth = new JLabel("Your Health:");
lblHealth.setBackground(new Color(50, 205, 50));
lblHealth.setBounds(275, 93, 100, 27);
frmSwendiver.getContentPane().add(lblHealth);
console.setText("Press The enter button to start Swendiver");
JButton Send = new JButton("Enter");
Send.setBounds(517, 43, 89, 270);
Send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
Send.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
boolean isClicked = true;
if (isClicked == true) {
isClicked = false;
lblHealth.setName("Health: 75%");
console.setText("[Hermin] Welcome. Ready for your adventure? (Click Enter)");
Send.setVisible(false);
}
}
@Override
public void mousePressed(MouseEvent arg0) {
Log.setText("");
ArrayList<String> weapon = new ArrayList<String>();
console.setText("Ok, choose your weapon. <SWORD>||<BOW>");
// Log.getText().equalsIgnoreCase("SWORD")
// console.setText("Good choice.");
Log.setText("");
weapon.add("Sword");
if (weapon.get(0).equalsIgnoreCase("Sword")) {
console.setText("[Hermin] A sword? I see. Good choice...");
next.setText("This box is used for Instructions/Actions");
}
}
});
frmSwendiver.getContentPane().add(Send);
JLabel lblWeapon = new JLabel("Weapon:");
lblWeapon.setBounds(441, 145, 56, 27);
frmSwendiver.getContentPane().add(lblWeapon);
JButton button3 = new JButton("Enter");
button3.setBounds(517, 43, 89, 270);
Weapon = new JTextField();
Weapon.setBounds(421, 166, 86, 20);
Weapon.setEditable(false);
frmSwendiver.getContentPane().add(Weapon);
Weapon.setColumns(10);
JButton Enter = new JButton("Enter");
Enter.setBounds(517, 41, 89, 272);
frmSwendiver.getContentPane().add(Enter);
JLabel lblArrows = new JLabel("Arrows:");
lblArrows.setEnabled(false);
lblArrows.setBounds(275, 151, 46, 14);
frmSwendiver.getContentPane().add(lblArrows);
Arrows = new JTextField();
Arrows.setEnabled(false);
Arrows.setBounds(260, 168, 68, 20);
Arrows.setEditable(false);
frmSwendiver.getContentPane().add(Arrows);
Arrows.setColumns(10);
Random attack = new Random();
next = new JTextField();
next.setBounds(0, 43, 346, 25);
next.setEditable(false);
frmSwendiver.getContentPane().add(next);
next.setColumns(10);
JButton Attack = new JButton("Attack");
Attack.setBounds(0, 68, 89, 245);
frmSwendiver.getContentPane().add(Attack);
JLabel lblClass = new JLabel("Class:");
lblClass.setBounds(451, 93, 46, 27);
frmSwendiver.getContentPane().add(lblClass);
Class = new JTextField();
Class.setEditable(false);
Class.setBounds(421, 114, 86, 20);
frmSwendiver.getContentPane().add(Class);
Class.setColumns(10);
JLabel swordpic = new JLabel("");
swordpic.setEnabled(false);
swordpic.setIcon(new ImageIcon("C:\\Users\\Owner\\Desktop\\IDE\\Swendiver\\bin\\sword-icon.png"));
swordpic.setBounds(391, 154, 40, 34);
JLabel bowpic = new JLabel("");
bowpic.setEnabled(false);
bowpic.setIcon(new ImageIcon("C:\\Users\\Owner\\Desktop\\Crossbow-icon.png"));
bowpic.setBounds(352, 152, 40, 34);
frmSwendiver.getContentPane().add(bowpic);
frmSwendiver.getContentPane().add(swordpic);
Coin = new JTextField();
Coin.setText("78");
Coin.setEditable(false);
Coin.setBounds(421, 211, 86, 20);
frmSwendiver.getContentPane().add(Coin);
Coin.setColumns(10);
next.setText("");
JLabel lblCoin = new JLabel("Coin:");
lblCoin.setBounds(451, 198, 46, 14);
frmSwendiver.getContentPane().add(lblCoin);
Enter.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
console.setText("[Hermin]Choose your class <Warrior> or <Archer>");
if (Log.getText().equalsIgnoreCase("Warrior")) {
console.setText("[Hermin]A Swordsman.. I see. Good choice.");
Weapon.setText("Sword");
next.setEnabled(true);
next.setText("Click Enter to continue");
Log.setText("");
Class.setText("Warrior");
Enter.setVisible(false);
Enter.setEnabled(false);
swordpic.setEnabled(true);
}
if (Log.getText().equalsIgnoreCase("Archer")) {
console.setText("[Hermin]An Archer.. Interesting. Here are your Items.");
Weapon.setText("Long-Bow");
Arrows.setText("19");
next.setEnabled(true);
next.setText("Click Enter to continue");
Log.setText("");
Class.setText("Archer");
lblArrows.setEnabled(true);
Arrows.setEnabled(true);
Enter.setVisible(false);
Enter.setEnabled(false);
button3.setEnabled(true);
button3.setVisible(true);
bowpic.setEnabled(true);
}
if (Log.getText().equalsIgnoreCase("/secretcommand")) {
Coin.setText(Integer.toString(300));
int Coin = 300;
next.setText("222 Coin Aquired");
next.setEnabled(true);
Log.setText("");
next.setText("Click Enter to continue!");
Enter.setEnabled(false);
Enter.setVisible(false);
button3.setVisible(true);
button3.setEnabled(true);
}
}
});
button3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
console.setText("[Hermin] Great. Welcome then. Head into SheildsTown to collect some items.");
next.setText("/EquipArmor to get geared and go.");
if (Log.getText().equalsIgnoreCase("/EquipArmor")) {
next.setText("You leave the armory.");
console.setText("");
Log.setText("");
button3.setEnabled(false);
button3.setVisible(false);
button4.setVisible(true);
button4.setEnabled(true);
}
}
});
frmSwendiver.getContentPane().add(button3);
int rathealth = 23;
button4.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
next.setText("You encounter a rat! Attack it! [Rat Health]: 23");
console.setText("[You]: Ahh! I shall kill you wretched beast!");
Attack.setEnabled(true);
boolean mob = true;
if (rathealth == 0) {
next.setText("You Killed the Rat!");
console.setText("I need food for later. </Loot to scavenge monsters or people you kill for items>");
}
}
});
frmSwendiver.getContentPane().add(button4);
JLabel lblNewLabel = new JLabel("Enemies health:");
lblNewLabel.setBounds(125, 119, 100, 27);
frmSwendiver.getContentPane().add(lblNewLabel);
enemyhealth = new JTextField();
enemyhealth.setEditable(false);
enemyhealth.setBounds(113, 139, 96, 20);
frmSwendiver.getContentPane().add(enemyhealth);
enemyhealth.setColumns(10);
JLabel lblEnemyDetails = new JLabel("Enemy details");
lblEnemyDetails.setFont(new Font("Tahoma", Font.PLAIN, 18));
lblEnemyDetails.setBounds(115, 79, 128, 25);
frmSwendiver.getContentPane().add(lblEnemyDetails);
JLabel lblYourDetails = new JLabel("Your details");
lblYourDetails.setFont(new Font("Tahoma", Font.PLAIN, 18));
lblYourDetails.setBounds(389, 55, 129, 41);
frmSwendiver.getContentPane().add(lblYourDetails);
myhealth = new JTextField();
myhealth.setText(" 100%");
myhealth.setEditable(false);
myhealth.setBounds(257, 114, 100, 20);
frmSwendiver.getContentPane().add(myhealth);
myhealth.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Commands/input below");
lblNewLabel_1.setBounds(241, 286, 161, 27);
frmSwendiver.getContentPane().add(lblNewLabel_1);
boolean enemydoesdamage = true;
String middlespacingforhealth = " ";
Attack.addMouseListener(new MouseAdapter() {
boolean attacksituation = true;
int noobiesword = attack.nextInt(7);
@Override
public void mouseClicked(MouseEvent e) {
if (Weapon.getText().equalsIgnoreCase("Sword")) {
// next.setText("You swing your sword and do: "+ Integer.toString(attack.nextInt()) + " damage");
Attack.setEnabled(true);
enemyhealth.setText(Integer.toString(rathealth - attack.nextInt(7)));
// next.setText("Rat bites, and does: " + Integer.toString(attack.nextInt(9))+" damage");
console.setText("[Thought](My arm is dripping blood)");
if(enemydoesdamage){
int Health = 100;
myhealth.setText(" "+ Integer.toString(Health-attack.nextInt(9))+"%");
}
}
if(Weapon.getText().equalsIgnoreCase("Long-Bow")){
next.setText("You shoot your bow and do: " + noobiesword);
enemyhealth.setText(" " + Integer.toString( rathealth - noobiesword));
}
}
});
}}
Aucun commentaire:
Enregistrer un commentaire