package game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
public class Enemy implements Runnable
{
int x;
int y;
int xDirection;
int yDirection;
int health;
int p1Score;
Player p1 = new Player(290, 15, 10);
Rectangle enemy;
public Enemy(int x, int y, int health)
{
p1Score = 0;
this.x = x;
this.y = y;
this.health = health;
Random r = new Random();
int rDir = r.nextInt(1);
if(rDir == 0)
{
rDir--;
setXDirection(rDir);
}
int yrDir = r.nextInt(1);
if(yrDir == 0)
{
yrDir--;
setYDirection(yrDir);
}
enemy = new Rectangle(this.x, this.y, 30, 30);
}
public void setXDirection(int direction) {
xDirection = direction;
}
public void setYDirection(int direction) {
yDirection = direction;
}
public void draw(Graphics g)
{
//draw the enemy initially
g.setColor(Color.white);
g.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
//if the enemy dies remove it
//Need to delete the current one and make a new enemy
if(health <= 0)
{
g.clearRect(enemy.x, enemy.y, enemy.width, enemy.height);
}
}
public void collision()
{
//Checks first if the player bullet is null
//Collision between the bullet and the player.
//Checks if the bounds of bullet and enemy intersect
if(p1.bullet != null && enemy.getBounds().contains(p1.bullet.getBounds()))
{
setYDirection(-1);
p1Score+=100;
health-=1;
}
}
public void move()
{
//enemy.x+=xDirection;
enemy.y+=yDirection;
//Checks if the enemy hits the left side of the screen
//if so reverse the direction to the right
if(enemy.x <= 0)
{
setXDirection(1);
}
//Checks if the enemy hits the right side of the screen
//if so reverse the direction to the left
if(enemy.x >= 780)
{
setXDirection(-1);
//Add to score
p1Score++;
}
//Checks if the enemy hits the top of the screen
//if so reverse the direction to the bottom
if(enemy.y <= 30)
{
setYDirection(1);
}
//Checks if the enemy hits the bottom of the screen
//if so reverse the direction to the top
if(enemy.y >= 570)
{
setYDirection(-1);
}
//collision();
}
public void run()
{
try
{
while(true)
{
move();
collision();
Thread.sleep(3);
}
}
catch(Exception e){System.err.println(e.getMessage());}
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getP1Score() {
return p1Score;
}
public void setP1Score(int score) {
p1Score = score;
}
public Player getP1() {
return p1;
}
public void setP1(Player p1) {
this.p1 = p1;
}
public Rectangle getEnemy() {
return enemy;
}
public void setEnemy(Rectangle enemy) {
this.enemy = enemy;
}
public int getXDirection() {
return xDirection;
}
public int getYDirection() {
return yDirection;
}
}
Edited by Gearslayer360, 25 July 2012 - 08:27 PM.






