I'm trying to test out a game that pits two people together in a D&D-esque dice game. I'm looking for anyone who can see any flaws in my code and see if they can give me some tips to help out.
This was compiled in Dr. Java with JDK 7.0, by the way.
I'm also wondering about how to make defense rolls work as well as to SET the Player's HP to a new amount rather than having it start from 20 every single time.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.Random;
public class savingRoll extends JFrame
//All buttons and labels have been declared here, along with the program's name being set up
{
public JButton roll;
public JRadioButton attackp1;
public JRadioButton defendp1;
public JRadioButton healp1;
public JRadioButton attackp2;
public JRadioButton defendp2;
public JRadioButton healp2;
public JLabel lb1 = new JLabel();
public JLabel lb2 = new JLabel();
public JLabel lb3 = new JLabel();
public JLabel lb4 = new JLabel();
public JLabel p1hp = new JLabel();
public JLabel p2hp = new JLabel();
public JLabel p1name = new JLabel();
public JLabel p2name = new JLabel();
public Random rnd = new Random();
public JPanel board;
public static void main (String [] args)
{
new savingRoll();
}
//The board is created, and everything is added onto it. Along with a button group being created to hold player specific buttons.
public savingRoll()
{
board = new JPanel();
int hpnumber1 = 20;
int hpnumber2 = 20;
attackp1 = new JRadioButton ("Attack (P1)");
defendp1 = new JRadioButton ("Defend (P1)");
healp1 = new JRadioButton ("Heal (P1)");
attackp2 = new JRadioButton ("Attack (P2)");
defendp2 = new JRadioButton ("Defend (P2)");
healp2 = new JRadioButton ("Heal (P2)");
roll = new JButton("Roll");
p1hp = new JLabel("Player 1's HP is currently: "+hpnumber1);
p2hp = new JLabel("Player 2's HP is currently: "+hpnumber2);
p1name = new JLabel("Player 1");
p2name = new JLabel("Player 2");
ButtonGroup p1dicetypes = new ButtonGroup ();
p1dicetypes.add(attackp1);
p1dicetypes.add(defendp1);
p1dicetypes.add(healp1);
ButtonGroup p2dicetypes = new ButtonGroup ();
p2dicetypes.add(attackp2);
p2dicetypes.add(defendp2);
p2dicetypes.add(healp2);
Border etchedBorder = BorderFactory.createEtchedBorder();
JPanel panelP1Dice = new JPanel ();
Border p1Border = BorderFactory.createTitledBorder
(etchedBorder, "P1 Dice");
panelP1Dice.setBorder(p1Border);
panelP1Dice.add(attackp1);
panelP1Dice.add(defendp1);
panelP1Dice.add(healp1);
JPanel panelP2Dice = new JPanel ();
Border p2Border = BorderFactory.createTitledBorder
(etchedBorder, "P2 Dice");
panelP2Dice.setBorder (p2Border);
panelP2Dice.add(attackp2);
panelP2Dice.add(defendp2);
panelP2Dice.add(healp2);
roll.addActionListener (new ButtonListener());
board.add(p1hp);
board.add(p2hp);
board.add(p1name);
board.add(p2name);
board.add(panelP1Dice);
board.add(panelP2Dice);
board.add(roll);
board.add(lb1);
board.add(lb2);
board.add(lb3);
board.add(lb4);
setContentPane(board);
setTitle ("SAVING THROW");
setSize (500, 500);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible (true);
}
public class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if(e.getSource() == roll)
{
//If Player One rolls their attack dice.
if(attackp1.isSelected())
{
int hpnumber2 = 20;
//Get a random number between one and six and store the sum.
int num1 = rnd.nextInt(6)+1;
int sum = num1;
//Find the photo related to the sum and set that as a label.
ImageIcon dice1 = new ImageIcon("dice"+num1+".png");
lb1.setIcon(dice1);
//The sum is displayed and stored in the outcome of the attack, declaring the opponent's current hitpoints.
lb2.setText("P1 attacks with "+sum);
lb2.setFont(new Font("Tahoma", Font.BOLD, 15));
int attackoutcome1 = hpnumber2-sum;
p2hp.setText("Player 2 has "+attackoutcome1+" health points remaining!");
}
//Repeat above procedure, but for other player.
if(attackp2.isSelected())
{
int hpnumber1 = 20;
int num1 = rnd.nextInt(6)+1;
int sum = num1;
ImageIcon dice1 = new ImageIcon("dice"+num1+".png");
lb3.setIcon(dice1);
lb4.setText("P2 attacks with "+sum);
lb4.setFont(new Font("Tahoma", Font.BOLD, 15));
int attackoutcome2 = hpnumber1-sum;
p1hp.setText("Player 1 has "+attackoutcome2+" health points remaining!");
}
if(defendp1.isSelected())
//Similar procedure, but will set a number for the attacking player to roll over. If the attacking player rolls a number higher than the defend value, the attacking value will be subtracted by the defense value and what's left over will be dealt to the defender.
//EX: Defense roll of 4 is defeated by an attack roll of 6, therefore 2 HP will be taken from the defense.
//If Attack roll is less than the Defense, the attack is completely negated altogether.
//Currently WIP.
{
int num1 = rnd.nextInt(6)+1;
int sum = num1;
ImageIcon dice1 = new ImageIcon("die"+num1+".gif");
lb1.setIcon(dice1);
lb2.setText("P1 defends with "+sum);
lb2.setFont(new Font("Tahoma", Font.BOLD, 15));
}
if(defendp2.isSelected())
//Read the above comment.
{
int num1 = rnd.nextInt(6)+1;
int sum = num1;
ImageIcon dice1 = new ImageIcon("die"+num1+".gif");
lb3.setIcon(dice1);
lb4.setText("P2 defends with "+sum);
lb4.setFont(new Font("Tahoma", Font.BOLD, 15));
}
//Similar procedure to all of the other buttons, but it allows the roller to regain HP based on the sum that they've rolled.
if(healp1.isSelected())
{
int hpnumber1 = 20;
int num1 = rnd.nextInt(6)+1;
int sum = num1;
ImageIcon dice1 = new ImageIcon("hdice"+num1+".png");
lb1.setIcon(dice1);
lb2.setText("P1 heals with "+sum);
lb2.setFont(new Font("Tahoma", Font.BOLD, 15));
int healoutcome1 = hpnumber1+sum;
p1hp.setText("Player 1 has healed "+healoutcome1+" of their health!");
}
if(healp2.isSelected())
{
int hpnumber2 = 20;
int num1 = rnd.nextInt(6)+1;
int sum = num1;
ImageIcon dice1 = new ImageIcon("hdice"+num1+".png");
lb3.setIcon(dice1);
lb4.setText("P2 heals with "+sum);
lb4.setFont(new Font("Tahoma", Font.BOLD, 15));
int healoutcome2 = hpnumber2+sum;
p2hp.setText("Player 2 has healed "+healoutcome2+" of their health!");
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire