I am making a project for university, and I am currently stuck on something. I need help on how to make a bit of code repeat. I have two ufo's that go left to right (ufo) and right to left across the screen (ufo2) but I need them to do it over and over again.
PImage space;
PImage trader;
PImage trader2;
PImage ship;
PImage ufo;
PImage ufo2;
PImage earth;
PImage uranaus;
PImage mars;
PImage venus;
float x;
float y;
int posx;
int posy;
boolean left, right, up, down;
boolean runOnce = true;
void setup() {
size (1249, 768);
smooth();
space = loadImage("space.png");
trader = loadImage("trader.png");
trader2 = loadImage("trader2.png");
ufo = loadImage("ufo.png");
ufo2 = loadImage("ufo2.png");
earth = loadImage("earth.png");
uranaus = loadImage("uranaus.png");
venus = loadImage("venus.png");
x = 50;
y = 1200;
}
void draw() {
background(space);
imageMode(CENTER);
fill(256, 165, 0);
image(trader2, 600 + posx, 700 + posy);
image(ufo, x, 600);
image(ufo2, y, 500);
image(earth, 1030, 25);
image(uranaus, 630, 25);
image(venus, 230, 25);
x +=4;
y -=4;
}
void keyPressed()
{
if (keyCode== RIGHT)
posx+=10;
if (keyCode== LEFT)
posx-=10;
if (posx<0) posx=0;
if (posx>width) posx=width;
if (keyCode== DOWN)
posy+=10;
if (keyCode== UP)
posy-=10;
if (posy<0) posy=0;
if (posy>height) posy=height;
}
Here is my code. Any help would be greatly appreciated :)!
Hey there, I am currently doing an assignment for a class, and I am having a few troubles trying to get my PImage to move with the arrow keys. The image is a spaceship, and I am creating something similar to frogger. If anyone could tell me a hand that'd be great!
So I'm doing an assignment at the moment and was wondering if anybody could give me assistance on how to change the color of a rect from gray to white with the mousePressed function.
So far this is what i've got, but I'm a little stuck an unsure why it doesn't quite wor
color white = color(255, 255, 255);
color gray = color(105, 105, 105);
int value = gray;
void setup() {
size(500, 500);
}
void draw() {
smooth();
rectMode(CENTER);
background(0);
fill(gray); //Fill Gray
//Row 1
rect(50, 50, 50, 50);
rect(100, 50, 50, 50);
rect(150, 50, 50, 50);
rect(200, 50, 50, 50);
rect(250, 50, 50, 50);
rect(300, 50, 50, 50);
rect(350, 50, 50, 50);
rect(400, 50, 50, 50);
rect(450, 50, 50, 50);
//Row 2
rect(50, 100, 50, 50);
rect(50, 150, 50, 50);
rect(50, 200, 50, 50);
rect(50, 250, 50, 50);
rect(50, 300, 50, 50);
rect(50, 350, 50, 50);
rect(50, 400, 50, 50);
rect(50, 450, 50, 50);
//Row 3
rect(100, 100, 50, 50);
rect(150, 100, 50, 50);
rect(200, 100, 50, 50);
rect(250, 100, 50, 50);
rect(300, 100, 50, 50);
rect(350, 100, 50, 50);
rect(400, 100, 50, 50);
rect(450, 100, 50, 50);
//Row 4
rect(100, 150, 50, 50);
rect(150, 150, 50, 50);
rect(200, 150, 50, 50);
rect(250, 150, 50, 50);
rect(300, 150, 50, 50);
rect(350, 150, 50, 50);
rect(400, 150, 50, 50);
rect(450, 150, 50, 50);
//Row 5
rect(100, 200, 50, 50);
rect(150, 200, 50, 50);
rect(200, 200, 50, 50);
rect(250, 200, 50, 50);
rect(300, 200, 50, 50);
rect(350, 200, 50, 50);
rect(400, 200, 50, 50);
rect(450, 200, 50, 50);
//Row 6
rect(100, 250, 50, 50);
rect(150, 250, 50, 50);
rect(200, 250, 50, 50);
rect(250, 250, 50, 50);
rect(300, 250, 50, 50);
rect(350, 250, 50, 50);
rect(400, 250, 50, 50);
rect(450, 250, 50, 50);
//Row 7
rect(100, 300, 50, 50);
rect(150, 300, 50, 50);
rect(200, 300, 50, 50);
rect(250, 300, 50, 50);
rect(300, 300, 50, 50);
rect(350, 300, 50, 50);
rect(400, 300, 50, 50);
rect(450, 300, 50, 50);
//Row 8
rect(100, 350, 50, 50);
rect(150, 350, 50, 50);
rect(200, 350, 50, 50);
rect(250, 350, 50, 50);
rect(300, 350, 50, 50);
rect(350, 350, 50, 50);
rect(400, 350, 50, 50);
rect(450, 350, 50, 50);
//Row 9
rect(100, 400, 50, 50);
rect(150, 400, 50, 50);
rect(200, 400, 50, 50);
rect(250, 400, 50, 50);
rect(300, 400, 50, 50);
rect(350, 400, 50, 50);
rect(400, 400, 50, 50);
rect(450, 400, 50, 50);
//Row 10
rect(100, 450, 50, 50);
rect(150, 450, 50, 50);
rect(200, 450, 50, 50);
rect(250, 450, 50, 50);
rect(300, 450, 50, 50);
rect(350, 450, 50, 50);
rect(400, 450, 50, 50);
rect(450, 450, 50, 50);
}
void mousePressed() {
if(mousePressed == true) {
fill(white);
rect(50, 50, 50, 50);
rect(100, 50, 50, 50);
rect(150, 50, 50, 50);
rect(200, 50, 50, 50);
rect(250, 50, 50, 50);
rect(300, 50, 50, 50);
rect(350, 50, 50, 50);
rect(400, 50, 50, 50);
rect(450, 50, 50, 50);
} else {
fill(gray);
rect(50, 50, 50, 50);
rect(100, 50, 50, 50);
rect(150, 50, 50, 50);
rect(200, 50, 50, 50);
rect(250, 50, 50, 50);
rect(300, 50, 50, 50);
rect(350, 50, 50, 50);
rect(400, 50, 50, 50);
rect(450, 50, 50, 50);
}
}
Any help would be much appreciated. I will be looking at turning all the rects I have into white squares when you press on them, and I'm hoping they will turn into gray squares when you press on them again. If anybody wants to know what I'm doing to make things more clear, feel free to ask.