-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlien.java
More file actions
129 lines (108 loc) · 3.08 KB
/
Alien.java
File metadata and controls
129 lines (108 loc) · 3.08 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* @(#)Alien.java
*
* Sets up an alien; contains methods relating to the alien (eg. making the alien change direction when it reaches the end)
* Calls the AlienBullet class so that each alien has their own AlienBullet; also contains methods relating to the AlienBullet (eg. making the bullet move)
*
* @author
* @version 1.00 2021/11/22
*/
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
public class Alien extends Common{
public int aW,aH; //height and width
private BufferedImage img1, img2; //2 images per alien (for animation)
private String img1Name, img2Name;
private AlienBullet b = new AlienBullet(); //init bullets
/********************** Constructor **********************/
public Alien(int x, int y, int s){
setX(x);
setY(y);
setSpeed(1);
//images
try {
//*names of diff. alien types are set as numbers, with either an 'a' or no 'a' (eg. "2.png", "2a.png")
img1 = ImageIO.read(new File(Integer.toString(s)+".png"));
img2 = ImageIO.read(new File(Integer.toString(s)+"a.png"));
}
catch (IOException e) {
System.out.println(e);
}
aW = img1.getWidth(); //img1 and img2 are same dimensions
aH = img1.getHeight();
//get file names (minus the .png)
img1Name = Integer.toString(s);
img2Name = "" + Integer.toString(s) + "a";
}
/********************** Methods **********************/
//move alien
public void aMove(){
setX(getX() + getSpeed());
}
//check if aliens reached the end
public boolean reachedEnd(int screenW){
if(getX() >= screenW-aW-10 && getSpeed() >0 || getX()<10 && getSpeed()<0){
return true;
}
return false;
}
//make alien move down and switch direction
public void newLine(){
setSpeed(-getSpeed());
setY(getY() + 30);
}
//speed up alien
public void changeSpeed(){
if(getSpeed() > 0){
setSpeed(getSpeed() + 1); //change to <-
}
else{
setSpeed(getSpeed() - 1); //change to ->
}
}
//alien shoots
public void shoot(){
b = new AlienBullet(getX()+aW/2+b.bW,getY()+aH);
b.setVisible(true); //set bullet to visible so it's drawn
}
//get the desired sprite as Image (so that it can be drawn)
public Image toImage(int m, int time){
if(m<time){
return (Image)img1;
}
return (Image)img2;
}
//move alien bullet
public void abMove(){
b.bMove();
}
/***************** Getters *****************/
public String getName(){ //for score keeping, so only the file names w/ only numbers are used (eg. "1" instead of "1a")
return img1Name;
}
//getters for the alien bullet
public boolean isBVis(){
return b.isVisible();
}
public int getBX(){
return b.getX();
}
public int getBY(){
return b.getY();
}
public int getBW(){
return b.getW();
}
public int getBH(){
return b.getH();
}
/***************** Setters (for alien bullet) *****************/
public void setBY(int y){
b.setX(y);
}
public void setBVis(boolean v){
b.setVisible(v);
}
}