Draw/Setup/Slow Program Help
in
Programming Questions
•
7 months ago
Hi, I've written this code as a variation of the wheres wally game. You search round the wheres wally picture with the mouse. When you find him, you click on him and a tick comes up.
2 Questions: Why does this run so slow? Is it something to do with the mouse searchlight being in draw?
also
How can i get it so that when wally is clicked, the whole picture is revealed for a few seconds, and then a new one comes in and the game restarts?
Thank you in advance.
float wallyx = 160;
float wallyy = 140;
float wallyw = 200;
float wallyh = 200;
PImage img;
PImage img2;
void setup() {
size(1024, 768);
frameRate(50);
img = loadImage("hello.jpg");
img2 = loadImage("correct.png");
}
void draw() {
loadPixels();
for (int x = 0; x < img.width; x++) {
loadPixels();
for (int y = 0; y < img.height; y++ ) {
// Calculate the 1D location from a 2D grid
int loc = x + y*img.width;
// Get the R,G,B values from image
float r, g, b;
r = red (img.pixels[loc]);
g = green (img.pixels[loc]);
b = blue (img.pixels[loc]);
// Calculate an amount to change brightness based on proximity to the mouse
float maxdist = 50;//dist(0,0,width,height);
float d = dist(x, y, mouseX, mouseY);
float adjustbrightness = 200*(maxdist-d)/maxdist;
r += adjustbrightness;
g += adjustbrightness;
b += adjustbrightness;
// Constrain RGB to make sure they are within 0-255 color range
r = constrain(r, 0, 255);
g = constrain(g, 0, 255);
b = constrain(b, 0, 255);
// Make a new color and set pixel in the window
color c = color(r, g, b);
//color c = color(r);
pixels[y*width + x] = c;
}
updatePixels();
}
updatePixels();
}
void mousePressed() {
if (mouseX>wallyx && mouseX <wallyx+wallyw && mouseY <wallyy+wallyh)
{
image(img2, 160, 140);
}
}
1