Making the process repeat itself.
in
Programming Questions
•
29 days ago
I wrote this, but i cant seem to get the rectangle to start over again. If you visualize this as a road, I want the rectangle to change back to the bottom lane after it goes out of bounds in the top lane. I realise that putting:
- if (x > width){
- x=x2;
- y=y2;
- speed=speed2;
- }
screwed everything up, but I can't think of an alternative.
- float x = 0;
- float x2 = 1000;
- float y = 375;
- float y2 = 250;
- float speed = 5;
- float speed2 = -5;
- void setup() {
- size(1000,700);
- }
- void draw() {
- background(177);
- move();
- display();
- line(0,500,1000,500);
- line(0,250,1000,250);
- line(0,375,1000,375);
- }
- void move() {
- x = x + speed;
- if (x > width) {
- x = x2;
- y = y2;
- speed=speed2;
- }
- else if (x > width){
- x2 = x;
- y2 = y;
- speed2 = speed;
- }
- if (keyPressed){
- if (key == 'b' || key == 'B') {
- x = x - speed*4;
- }
- if (key == 'a' || key == 'A') {
- x = x + speed*4;
- }
- }
- }
- void display() {
- fill(124, 200, 142);
- rect(x, y, 250,125);
- }
1