package games; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; /// Player represents the a user-controlled /// object within the Game. May be subclassed /// to define new kinds of Players. public class Player extends Visible { private int _radius = 10; private int _health = 50; // Draw the Player; default representation is fairly // boring. public void draw(Graphics g) { g.setColor(Color.green); g.fillRect(getX()-_radius, getY()-_radius, _radius*2,_radius*2); drawHealth(g); } /// Displays player's health on screen public void drawHealth(Graphics g) { g.setColor(Color.white); g.drawString(Integer.toString(_health),getX()-_radius,getY()-_radius); } /// Calculate damage the player can inflict on a Monster public int calcAttackDamage() { return (int)( Math.random() * 30 )+5; } /// Adjust player's health public void adjustHealth(int delta) { _health += delta; System.out.println("Player health: "+Integer.toString(_health)); if (_health <= 0) { System.out.println("Player has been vanquished!"); System.exit(0); } } }