-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstarfighter.pde
97 lines (76 loc) · 1.99 KB
/
starfighter.pde
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
class Starfighter{
float x;
float y;
float targetX;
float targetY;
boolean keyUp;
boolean keyDown;
boolean keyRight;
boolean keyLeft;
float lionel=10;
float acceleration=20;
float shipWidth = 30;
float shipHeight = 30;
Starfighter(float _x, float _y){
this.x = _x;
this.y = _y;
this.targetX=this.x;
this.targetY=this.y;
}
void keyPressed(){
if(keyCode==UP)
this.keyUp=true;
else if(keyCode==DOWN)
this.keyDown=true;
if(keyCode==LEFT)
this.keyLeft=true;
else if(keyCode==RIGHT)
this.keyRight=true;
}
void keyReleased(){
if(keyCode==UP)
this.keyUp=false;
else if(keyCode==DOWN)
this.keyDown=false;
if(keyCode==LEFT)
this.keyLeft=false;
else if(keyCode==RIGHT)
this.keyRight=false;
if (key == ' ') this.shoot();
}
void move(){
if(this.keyUp){
this.targetY-=this.lionel;
}
if(this.keyDown){
this.targetY+=this.lionel;
}
if(this.keyRight){
this.targetX+=this.lionel;
}
if(this.keyLeft){
this.targetX-=this.lionel;
}
if (this.x - this.shipWidth/2 < 0 && this.targetX < 0) {
this.targetX *= -1;
}
else if (this.x + this.shipWidth/2 > width && this.targetX > width) {
this.targetX = width - (this.targetX - width);
}
if (this.y < 0 && this.targetY < 0) {
this.targetY *= -1;
}
else if (this.y + this.shipHeight > height && this.targetY + this.shipHeight > height) {
this.targetY = height - (this.targetY - height) - this.shipHeight;
}
this.x+=(this.targetX-this.x)/this.acceleration;
this.y+=(this.targetY-this.y)/this.acceleration;
}
void shoot(){
bullets.add(new Bullet(this.x, this.y, this.targetX - this.x, this.targetY - this.y));
}
void draw(){
this.move();
triangle(this.x - this.shipWidth/2, this.y + this.shipHeight, this.x, this.y, this.x + this.shipWidth/2, this.y + this.shipHeight);
}
}