-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.pde
165 lines (135 loc) · 3.52 KB
/
game.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
int HP_BAR_WIDTH = 30;
int HP_MAX = 10;
int BAR_BUFFER = 20;
int PADDLE_WIDTH = 30;
int PADDLE_HEIGHT = 82;
int PADDLE_SPEED = 5;
int SHOOT_INTERVAL = 2000; // ms
class Game {
Player players[] = new Player[2];
int BULLET_SPEED = 5;
int nextShootTime;
// int winCount = 0;
// int winnerId = 1;
int winCount = -1;
int winnerId;
int startFrame;
void setup() {
players[0] = new Player(0, "A", #FF0000, true);
players[1] = new Player(1, "B", #0000FF, false);
startFrame = frameCount;
link.output(1, "start");
link.output(6, 1);
}
void draw() {
int ms = millis();
if (winCount >= 0) {
int frame = frameCount - winCount;
// boolean blinkOn = (frame % 10) / 5 == 0;
boolean blinkOn = true;
if (frame >= 100) {
gameOn = false;
setup();
} else if (blinkOn) {
image(winnerImage[winnerId], 0, 0);
}
return;
}
int frame = frameCount - startFrame;
if (frame > 900) {
BULLET_SPEED = 14;
SHOOT_INTERVAL = 800;
link.output(6, 4);
} else if (frame > 600) {
BULLET_SPEED = 11;
SHOOT_INTERVAL = 1200;
link.output(6, 3);
} else if (frame > 300) {
BULLET_SPEED = 8;
SHOOT_INTERVAL = 1600;
link.output(6, 2);
}
//println("speed " + BULLET_SPEED + " interval " + SHOOT_INTERVAL);
if (!gameOn) {
if (players[0].y < 50 && players[1].y < 50) {
gameOn = true;
players[0].nextShootTime = ms;
players[1].nextShootTime = ms + (SHOOT_INTERVAL / 2);
}
}
if (!gameOn) {
imageMode(CENTER);
image(title, 400, 300, 350, 171);
imageMode(CORNER);
image(subtitle, 0, height-45);
}
if (gameOn) {
// if it's time, shoot a new object from player 1/2
for (int i = 0; i < 2; i++) {
if (ms >= players[i].nextShootTime) {
players[i].nextShootTime += SHOOT_INTERVAL;
players[i].shootBullet();
}
}
}
if (gameOn) {
// draw bullets
for (int i = 0; i < 2; i++) {
ArrayList destroy = new ArrayList();
for (int j = 0; j < players[i].bullets.size(); j++) {
Bullet bullet = (Bullet) players[i].bullets.get(j);
// increment bullets
bullet.move();
if ((bullet.speed > 0 && bullet.x >= width - HP_BAR_WIDTH) || (bullet.speed < 0 && bullet.x <= HP_BAR_WIDTH)) {
// it hits the HP bar
players[1-i].hit();
destroy.add(bullet);
} else {
// draw bullets
bullet.draw();
}
}
for (int j = 0; j < destroy.size(); j++) {
Bullet bullet = (Bullet) destroy.get(j);
boolean done = players[i].bullets.remove(bullet);
}
}
}
// draw player 1 HP
// draw player 2 HP
if (gameOn) {
for (int i = 0; i < 2; i++) {
players[i].drawHP();
}
}
// draw player 1 block
// draw player 2 block
for (int i = 0; i < 2; i++) {
players[i].drawPaddle();
}
}
void keyPressed() {
switch (key) {
case 'q':
case 'Q':
players[0].up();
break;
case 'a':
case 'A':
players[0].down();
break;
case 'o':
case 'O':
players[1].up();
break;
case 'l':
case 'L':
players[1].down();
break;
}
}
void gameOver() {
winCount = frameCount;
link.output(5, "victory");
}
}