-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpaceGame.pde
159 lines (131 loc) · 3.69 KB
/
SpaceGame.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
ArrayList <Bullet> bullets;
ArrayList <Asteroid> asteroids;
ArrayList <Button> buttons;
float bulletSpeed;
Ship ship;
int asteroidTime;
int maxTimeBetweenAsteroids;
int maxTimeBetweenBullets;
int bulletTime;
int score;
boolean keys[];
boolean okToShootBullet;
boolean bulletsDisappearOnImpact;
void setup() {
size(1100,600);
background(0);
fill(255);
rectMode(CORNERS);
bullets = new ArrayList <Bullet>();
asteroids = new ArrayList <Asteroid>();
ship = new Ship(width/2, height/2);
buttons = new ArrayList <Button>();
setupButtons();
bulletSpeed = 10;
maxTimeBetweenAsteroids = 50;
maxTimeBetweenBullets = 10;
score = 0;
keys = new boolean[4];
bulletsDisappearOnImpact = false;
}
void draw() {
background(0);
fill(255);
text("Bullets: " + bullets.size(), 20, 20);
text("Asteroids: " + asteroids.size(), width - 80, 20);
text("Score: " + score, width/2 - 25, 20);
text("Use WASD to move, and click to shoot", 20, height - 25);
text("Click a button to increment a value, right/center click it to decrement.", 20, height - 12);
ship.display();
moveAsteroids();
displayButtons();
okToShootBullet = false;
if(millis() > asteroidTime) {
asteroidTime += maxTimeBetweenAsteroids;
addAsteroid();
}
if(millis() > bulletTime) {
bulletTime += maxTimeBetweenBullets;
okToShootBullet = true;
}
if(mousePressed && okToShootBullet && !buttonsArePressed()) {
addBullet();
}
float[] movement = bufferKeys();
ship.moveShip(movement[0] * ship.speed, movement[1] * ship.speed);
moveBullets();
checkCollisions();
checkBulletsOOB();
checkAsteroidsOOB();
}
void moveBullets() {
for(Bullet b : bullets)
b.moveBullet();
}
void moveAsteroids() {
for(Asteroid a : asteroids)
a.moveAsteroid();
}
void displayButtons() {
for(Button b : buttons) {
if(b instanceof ButtonToggle) {
((ButtonToggle)b).display();
}
else {
b.display(b._nonPressedColor);
}
}
}
void checkBulletsOOB() {
for(int i = 0; i < bullets.size(); i++) {
Bullet b = bullets.get(i);
if(b.posX < 0 || b.posX > width || b.posY < 0 || b.posY > height) {
bullets.remove(i--);
}
}
}
void checkAsteroidsOOB() {
for(int i = 0; i < asteroids.size(); i++) {
Asteroid a = asteroids.get(i);
if(a.xPos < -a.rad || a.xPos > width + a.rad || a.yPos < -a.rad || a.yPos > height + a.rad || a.exploded) {
asteroids.remove(i--);
}
}
}
void addAsteroid() {
float x = random(50, width-50);
float y = random(50, height-50);
asteroids.add(new Asteroid(x, y, (int)random(256), (int)random(256), (int)random(256), 255, "RGB"));
}
void addBullet() {
float angle = degrees(atan((mouseY - ship.gunY()) / (ship.gunX() - mouseX)));
if(mouseX < ship.gunX()) {
angle += 180;
}
bullets.add(ship.shootGun(angle, bulletSpeed));
}
void checkCollisions() {
for(int i = 0; i < asteroids.size(); i++) {
for(int j = 0; j < bullets.size(); j++) {
if(asteroids.get(i).collided(bullets.get(j))) {
asteroids.get(i).exploded = true;
if(bulletsDisappearOnImpact)
bullets.remove(j--);
score++;
}
}
}
}
void setupButtons() {
buttons.add(new ButtonValueChanger(width - 95, height - 50, 85, 40, new DynamicColor(200, 130, 0, 255, "RGB"), " Bullet speed"));
buttons.add(new ButtonValueChanger(width - 185, height - 50, 85, 40, new DynamicColor(200, 130, 0, 255, "RGB"), " Ship speed"));
buttons.add(new ButtonToggle(width - 355, height - 50, 165, 40, new DynamicColor(20, 70, 172, 255, "RGB"), "Bullets disappear on impact"));
}
boolean buttonsArePressed() {
for(Button b : buttons) {
if(b.isClicked()) {
return true;
}
}
return false;
}