Beginner question. I'm trying to create a simple bullet-dodging game where the data for the number of bullets is pulled from an API. I want to use a few different sets of data and create a group of bullets for each, where each group is a different color. I think I'm struggling with something basic here--I'm seeing the different fill values for each set of bullets mixing, and I'm not sure where exactly the issue is, but I think my code needs to be organized differently to make this work correctly. If someone could point me in the right direction for this, I'd really appreciate it. Thanks!
- import org.json.*;
Bullet[] bullets = new Bullet[5000];
int numBulletsWar;
int numBulletsMurder;
int numBulletsDeath;
void setup() {
size(500, 800);
smooth();
for (int i = 0; i < bullets.length; i++) {
bullets[i] = new Bullet();
}
numBulletsWar = numArticles("war")/1000;
numBulletsMurder = numArticles("murder")/1000;
numBulletsDeath = numArticles("death")/1000;
}
void draw() {
background(0);
for (int i = 0; i < numBulletsWar; i++) {
bullets[i].display(color(0, 0, 255));
bullets[i].move();
}
for (int i = 0; i < numBulletsDeath; i++) {
bullets[i].display(color(0, 255, 0));
bullets[i].move();
}
for (int i = 0; i < numBulletsMurder; i++) {
bullets[i].display(color(255, 0, 0));
bullets[i].move();
}
}
// Get data for terms and apply to number of bullets
int numArticles(String term) {
String[] results = loadStrings("http://api.nytimes.com/svc/search/v1/article?query="+term+"&api-key=90c3326fd3449e3c4cafb8b515ff251b:6:65953071");
println(results);
String finalResult = join(results, "");
JSONObject json = new JSONObject(finalResult);
return json.getInt("total");
}
class Bullet {
float r = 10;
float bx = random(0, width);
float by = random(-1000, 0);
void display(color c) {
fill(c);
noStroke();
ellipse(bx, by, r, r);
}
void move() {
by = by + 2;
}
}
1