forked from kevin890703/assign6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assign6.pde
139 lines (127 loc) · 2.85 KB
/
assign6.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
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
130
131
132
133
134
135
136
137
138
class GameState
{
static final int START = 0;
static final int PLAYING = 1;
static final int END = 2;
}
class Direction
{
static final int LEFT = 0;
static final int RIGHT = 1;
static final int UP = 2;
static final int DOWN = 3;
}
class EnemysShowingType
{
static final int STRAIGHT = 0;
static final int SLOPE = 1;
static final int DIAMOND = 2;
static final int STRONGLINE = 3;
}
class FlightType
{
static final int FIGHTER = 0;
static final int ENEMY = 1;
static final int ENEMYSTRONG = 2;
}
int state = GameState.START;
int currentType = EnemysShowingType.STRAIGHT;
int enemyCount = 8;
Enemy[] enemys = new Enemy[enemyCount];
Fighter fighter;
Background bg;
FlameMgr flameMgr;
Treasure treasure;
HPDisplay hpDisplay;
boolean isMovingUp;
boolean isMovingDown;
boolean isMovingLeft;
boolean isMovingRight;
int time;
int wait = 4000;
void setup () {
size(640, 480);
flameMgr = new FlameMgr();
bg = new Background();
treasure = new Treasure();
hpDisplay = new HPDisplay();
fighter = new Fighter(20);
}
void draw()
{
if (state == GameState.START) {
bg.draw();
}
else if (state == GameState.PLAYING) {
bg.draw();
treasure.draw();
flameMgr.draw();
fighter.draw();
//enemys
if(millis() - time >= wait){
addEnemy(currentType++);
currentType = currentType%4;
}
for (int i = 0; i < enemyCount; ++i) {
if (enemys[i]!= null) {
enemys[i].move();
enemys[i].draw();
if (enemys[i].isCollideWithFighter()) {
fighter.hpValueChange(-20);
flameMgr.addFlame(enemys[i].x, enemys[i].y);
enemys[i]=null;
}
else if (enemys[i].isOutOfBorder()) {
enemys[i]=null;
}
}
}
// 這地方應該加入Fighter 血量顯示UI
}
else if (state == GameState.END) {
bg.draw();
}
}
boolean isHit(int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh)
{
// Collision x-axis?
boolean collisionX = (ax + aw >= bx) && (bx + bw >= ax);
// Collision y-axis?
boolean collisionY = (ay + ah >= by) && (by + bh >= ay);
return collisionX && collisionY;
}
void keyPressed(){
switch(keyCode){
case UP : isMovingUp = true ;break ;
case DOWN : isMovingDown = true ; break ;
case LEFT : isMovingLeft = true ; break ;
case RIGHT : isMovingRight = true ; break ;
default :break ;
}
}
void keyReleased(){
switch(keyCode){
case UP : isMovingUp = false ;break ;
case DOWN : isMovingDown = false ; break ;
case LEFT : isMovingLeft = false ; break ;
case RIGHT : isMovingRight = false ; break ;
default :break ;
}
if (key == ' ') {
if (state == GameState.PLAYING) {
fighter.shoot();
}
}
if (key == ENTER) {
switch(state) {
case GameState.START:
case GameState.END:
state = GameState.PLAYING;
enemys = new Enemy[enemyCount];
flameMgr = new FlameMgr();
treasure = new Treasure();
fighter = new Fighter(20);
default : break ;
}
}
}