-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.java
More file actions
82 lines (68 loc) · 2.65 KB
/
Copy pathEnemy.java
File metadata and controls
82 lines (68 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Dimension;
import java.util.Random;
import java.awt.image.BufferedImage;
import java.awt.Image.*;
import java.io.*;
import javax.imageio.*;
import java.awt.image.ImageObserver;
public class Enemy{
//variables================================================
public double x;
public double y;
private double xVelocity;
private double yVelocity;
public int size = 20;
public boolean shot;
public int health;
private static double velocity=1.5;
private static BufferedImage enemyDown;
private static BufferedImage enemyUp;
private ImageObserver observer;
//============================================================
//Constructors========================
public Enemy(){
Random rand = new Random();
x=rand.nextInt(Frogger.WIDTH);
y=rand.nextInt(200);
health=10;//right now this equals the hardcoded power value (hardcoded in Frogger where the projectiles are genrated. That is just what is passed into the contructor.)
}
public Enemy(int x, int y){
this.x=x;
this.y=y;
health=10;//right now this equals the hardcoded power value (hardcoded in Frogger where the projectiles are genrated)
}
//================================================
//methods================================================
public void update(int frogX, int frogY){
//This method determines the angle direction the enemies need to go to catch the frog
//It calculates the components of velocity in x and y directions and adds them to position
double theta = Math.atan( ((double) (frogY-y)) / ((double) (frogX-x)) );
if (frogX<x)
theta+=Math.PI;
xVelocity = (velocity*Math.cos(theta));
yVelocity = (velocity*Math.sin(theta));
x+=xVelocity;
y+=yVelocity;
}//update
public void draw(Graphics g){
//Simply draws the enmemies if and only if they are still alive. It was easier to not draw the dead ones than to remove them from the game. They are all purged when a new world is created anyway
if (!shot && yVelocity>=0){
g.drawImage(enemyDown, (int)x-size, (int)y-size, observer);
}
if (!shot && yVelocity<0){
g.drawImage(enemyUp, (int)x-size, (int)y-size, observer);
}
}//draw
public static void readImage(){//Reads in the images of the enemies
try{
enemyDown=ImageIO.read(new File("enemyDown.png"));
enemyUp=ImageIO.read(new File("enemyUp.png"));
}catch (IOException e){}
}//readImage
//================================================================================================
}//Enemy