所以我正在制作这个游戏,每当点击“攻击”按钮时,敌人和玩家就会失去健康。玩家可以治愈但是当他与怪物进行另一场战斗时,怪物的生命值仍为0或1,同时玩家的健康状况会愈合。我试图制作一个方法来解决它,但它仍然无法正常工作。任何解决方案?
(对不起,如果我的代码很草率。我认为不需要Panel或Label类。)
主要班级:
public static void mainScreen(JPanel p)
{
JButton points = new JButton("stats");
points.setLayout(null);
points.setBounds(smallButton);
points.setLocation(510, 5);
points.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
p.removeAll();
pointsScreen(p);
p.repaint();
}
});
JButton battle = new JButton("battle");
battle.setLayout(null);
battle.setBounds(smallButton);
battle.setLocation(510, 30);
battle.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Battle b = new Battle(strpoints, defpoints, luckpoints, health, currentHealth);
p.removeAll();
p.add(battle);
battleScreen(p,b);
p.repaint();
}
});
p.add(battle);
p.add(points);
}
public static void battleScreen(JPanel p, Battle b)
{
Random r = new Random();
Label enemy = new Label("You've encountered a "+b.e.getName()+"!",200,20, new Dimension(200,30), Color.YELLOW);
p.add(enemy); // enemy encountered
Label enemyHp = new Label("Health: "+b.e.getCurrentEnemyHp()+"/"+b.e.getEnemyHp(),500,100, new Dimension(100,25), Color.GREEN);
p.add(enemyHp); // enemy's hp
Label enemySt = new Label("Strength: "+b.e.getStr(),500,125, new Dimension(100,25), Color.ORANGE);
p.add(enemySt); // enemy's str
Label enemyDf = new Label("Defence: "+b.e.getDef(),500,150, new Dimension(100,25), Color.CYAN);
p.add(enemyDf); // enemy's def
Label enemyloot = new Label("Loot: "+(b.e.getLoot()+r.nextInt(luckpoints + 1)),500,175, new Dimension(100,25), Color.YELLOW);
p.add(enemyloot); // enemy's loot
Label hp = new Label("Health: "+currentHealth+"/"+health,0,100, new Dimension(100,25), Color.GREEN);
p.add(hp); // player hp
Label str = new Label("Strength: "+strpoints,0,125, new Dimension(100,25), Color.ORANGE);
p.add(str); //player str
Label def = new Label("Defence: "+defpoints,0,150, new Dimension(100,25), Color.CYAN);
p.add(def); //player def
Label activity = new Label("What's your next move?",0,340, new Dimension(620,35), Color.WHITE);
p.add(activity); // battle activity feed
Label enemyImg = new Label(null, 390, 100, new Dimension(100,100), null);
enemyImg.setIcon(b.e.getImage());
p.add(enemyImg); // picture of the enemy
Label img = new Label(null, 110, 100, new Dimension(100,100), null);
img.setIcon(new ImageIcon("src\\player.png"));
p.add(img); // picture of the player
JButton done = new JButton("done");
done.setLayout(null);
done.setBounds(smallButton);
done.setLocation(420, 280);
done.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
p.removeAll();
p.repaint();
if(currentHealth <= 0)
{
if(activity.getText().contains("been"))
{
strpoints = 0;
defpoints = 0;
luckpoints = 0;
freepoints = 10;
gold = 100;
health = 10;
currentHealth = 10;
}
p.removeAll();
pointsScreen(p);
p.repaint();
p.add(activity);
p.remove(activity);
}
else
mainScreen(p);
}
});
JButton attack = new JButton("attack");
attack.setLayout(null);
attack.setBounds(smallButton);
attack.setLocation(100, 280);
attack.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int dmg2player = b.e.getStr() + r.nextInt(2) - defpoints/2;
if(dmg2player < 1)
dmg2player = 1;
int dmg2enemy = strpoints + r.nextInt(2) - b.e.getDef()/2;
if(dmg2enemy < 1)
dmg2enemy = 1; // calculates damage dealt by you and enemy
b.e.updateEnemyHp(dmg2enemy);
currentHealth = currentHealth - dmg2player;
hp.setText("Health: "+currentHealth+"/"+health);
enemyHp.setText("Health: "+b.e.getCurrentEnemyHp()+"/"+b.e.getEnemyHp()); //updates player and enemy's current hp
activity.setText("You dealt "+dmg2enemy+" damage to "+b.e.getName()+" and you received "+dmg2player+" damage!"); //updates battle log
if(currentHealth <= 0)
{
p.removeAll();
activity.setText("You have been defeated by a "+b.e.getName()+"!");
p.add(done);
p.add(activity);
p.repaint();
b.e.resetHp();
} // happens whenever the player is ded as fuk
else if(b.e.getCurrentEnemyHp() <= 0)
{
gold = gold + b.e.getLoot();
freepoints = freepoints + (b.e.getLoot()/10);
p.removeAll();
activity.setText("You have defeated the "+b.e.getName()+" and earned "+b.e.getLoot()+" gold along with "+(b.e.getLoot()/10)+" points!");
p.add(done);
p.add(activity);
p.repaint();
} // happens when the enemy is ded as fuk
else
{
p.remove(hp);
p.remove(enemyHp);
p.remove(activity);
p.add(hp);
p.add(enemyHp);
p.add(activity);
p.repaint();
} // happens when nobody is ded as fuk
}
});
p.add(attack);
JButton flee = new JButton("flee (" + (b.e.getLoot()/2)+"g)");
flee.setLayout(null);
flee.setBounds(new Rectangle(120,20));
flee.setLocation(420, 280);
flee.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(gold > b.e.getLoot()/2)
{
gold = gold - (b.e.getLoot()/2);
p.removeAll();
mainScreen(p);
} else
{
activity.setText("You need "+b.e.getLoot()/2+" gold to escape!");
p.remove(activity);
p.add(activity);
}
p.repaint();
}
});
p.add(flee);
}
}
战斗班。
public class Battle
{
int str;
int def;
static int luck;
int hp;
int currenthp;
Enemy e;
public Battle(int s, int d, int l,int hp, int chp)
{
this.hp = hp;
Random r = new Random();
currenthp = chp;
str = s;
def = d;
luck = l;
switch(r.nextInt(5))
{
case 0: e = Enemy.rat;
return;
case 1: e = Enemy.hunter;
return;
case 2: e = Enemy.wolf;
return;
case 3: e = Enemy.troll;
return;
case 4: e = Enemy.demon;
return;
}
}
enum Enemy
{
rat("Rat",2,2,10,10,new ImageIcon("src\\rat.png")),
hunter("Hunter",4,4,20,25,new ImageIcon("src\\hunter.png")),
wolf("Wolf",5,3,15,40,new ImageIcon("src\\wolf.png")),
troll("Troll",4,7,30,75,new ImageIcon("src\\troll.png")),
demon("Demon",6,6,25,100,new ImageIcon("src\\demon.png"));
private String name;
private int estr;
private int edef;
private int ehp;
private int gold;
private int ecHp;
private ImageIcon img;
Enemy(String n, int s, int d, int h, int g, ImageIcon i)
{
name = n;
estr = s;
edef = d;
ehp = h;
gold = g;
ecHp = h;
img = i;
}
public void updateEnemyHp(int dmg)
{
ecHp = ecHp - dmg;
}
public int getCurrentEnemyHp()
{
return ecHp;
}
public int getEnemyHp()
{
return ehp;
}
public int getStr()
{
return estr;
}
public int getDef()
{
return edef;
}
public ImageIcon getImage()
{
return img;
}
public int getLoot()
{
return gold;
}
public String getName() {
return name;
}
public void resetHp() {
ecHp=ehp;
}
}
}