-
Notifications
You must be signed in to change notification settings - Fork 0
/
Missile.java
106 lines (88 loc) · 2.28 KB
/
Missile.java
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
/*
Name Yujin Bae
teacher: Mr. Guglielmi
Date: november, 6, 2020
description: This is the missile class of the final project space invaders game.
this class create the missile object to be shot by the player.
*/
// The "Missile" class.
import java.awt.*;
import hsa.Console;
public class Missile extends Thread
{
//variable declaration
private Console c; // The output console
private int x = 0; //the x coordinate of the missile
private int y = 450; //the y coordinate of the missile
private boolean go = true; //whether the missile should continue or not
private boolean load = true; //whether a missile is loaded or not
//this is the constructor of the missile class
public Missile (Console c)
{
this.c = c;
}
//this method will draw a missile's graphics
private void drawMissile ()
{
y = 450;
//the missile is fired!
while (go)
{
c.setColor (Color.white);
c.fillRect (x, y, 4, 20);
c.setColor (Color.black);
c.fillRect (x, y + 20, 4, 2);
//if the missile does not hit as enemy or goes off screen, it continues ascending
if (y > -20)
{
y-=2;
load = false;
}
else
{
end ();
}
try
{
sleep (10);
}
catch (Exception e)
{
}
} //while(go) loop end
} //drawMissile method end
//this method sets the missile's x coordinate.
//takes in the player's x coordinate as an input
public void setMissileX (int x)
{
this.x = x + 23; //+23 to make the missile com form the missile of the cannon
}
//this method will return the x location of the missile
public int getMissileX ()
{
return x;
}
//this method will return the y location of the missile
public int getMissileY ()
{
return y;
}
//this method will return the load information of the missile
public boolean getLoad ()
{
return load;
}
//this method runs the animation of the missile
public void run ()
{
drawMissile ();
}
//this method will stop the thread
public void end ()
{
go = false;
c.fillRect (x, y, 4, 22);
y = 450;
load = true;
}
} // Missile class