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);
I'm trying to allow the user to draw over an image so that it reveals partially transparent camera video underneath. I'm able to get an ellipse to show up that can be dragged around, but I want to make it so that the ellipses "stick" and are drawn in a continuous line (that also stays there when the user releases the mouse). Here is my code:
import processing.video.*;
Capture cam;
PImage window1;
PGraphics topLayer;
void setup() {
size(700, 500);
cam = new Capture(this, 700, 500);
window1 = loadImage("window1.png");
topLayer = createGraphics(width, height, P2D);
}
void draw() {
if (cam.available()) {
cam.read();
}
image(window1, 0, 0);
if (mousePressed) {
topLayer.beginDraw();
topLayer.background(0, 0);
topLayer.fill(128);
topLayer.stroke(128);
topLayer.strokeWeight(10);
topLayer.ellipse(mouseX, mouseY, 100, 100);
topLayer.endDraw();
// Camera feed will be unmasked where user paints
cam.mask(topLayer);
image(cam, 0, 0);
}
}
I'm not sure whether this will require the use of an array or dealing with pixels, or if there's an easier way? I would appreciate if I could be pointed in the right direction with this. Thank you!
I'm working on a paint program, and I have a canvas (a blank white PImage) that I want to refresh to white and println() some text when the user clicks a button, but only when there is something drawn on the canvas.
I have some code that scans through the canvas's pixel array. It has a boolean, blankCanvas, that controls the println() and canvas refresh functions.
loadPixels();
for (int i = 0; i < canvas.pixels.length; i ++) {
if (canvas.pixels[i] == color(255)) {
blankCanvas = true;
}
}
This code seems to return blankCanvas as true as long as
any one pixel is white. So, if the user draws something in another color but doesn't fill the whole canvas with it, blankCanvas returns as true. The only time blankCanvas doesn't come back as true is when the entire canvas is filled with a color other than white. I only want it to return as true if
all the pixels are white (i.e., I want blankCanvas to be true only when the canvas is actually blank).
So, I think I'm just having trouble figuring out how to access an array in this way. I think my question is: is there a way to scan through a pixel array and return a true value only if
all the pixels in the array are white? Or, in other words: is there a way to check whether any one or more pixels in a pixel array are
not white and then return a false value? Thank you in advance!
This is a beginner question, and I apologize if it's already been answered elsewhere in the forum. I'm trying to find the best way to access different pieces of text stored in an array. When the user clicks a button, I want the text currently displayed on the screen to be replaced with the text stored in the next array element. I guess what I'm mainly struggling with figuring out how to express is how to get mousePressed() to access these different elements in sequential order. I think this must be a pretty simple thing to do, but I'm having trouble figuring it out just because most of the array examples I can find use "for" loops to access the information contained in the array, rather than show how to cycle through the elements one by one. I think I just need to be pointed in the right direction about how I should approach this. Thanks!
The variables sliderXSpeed and sliderYSpeed are using variables from another class (Ball). SliderXSpeed is working in "float m = map(sliderXSpeed, 0, 10, 0, width);" but not in mouseDragged() below it. I'm not sure if this has to do with how I'm using mouseDragged() or map() or because of how I'm assigning the variables positivemX etc. to sliderXSpeed etc.