how to use keyReleased? and how to move down 50 pixels every time
in
Programming Questions
•
2 years ago
I have drawn this bus that moves from left to right! But I don't know what codes to use so when it hits the right boundary, it "wraps around" to the left and moves down 50 pixels???
I also want to use "keyReleased" so that when you press and release the b key, this will toggle bus motion. When Pressing the key repeatedly to stop and continue the vehicle
the code is below, I need your guidelines please
thank U
int x = 0;
int speed = 1;
int y=0;
Bus myBus;
// Setup does not change
void setup() {
size(500,500);
myBus = new Bus();
smooth();
}
void draw() {
background(255);
road();
myBus.move();
myBus.display();
}
public class Bus {
Bus() { //bus class
}
// A function to display the bus
void display() {
fill(255,0,0); //bus color
stroke(0);
rect(x, y, 100, 40);
fill(0);
ellipse(x+25, y+40, 20, 20);
ellipse(x+75, y+40, 20, 20);
}
void move() {
// Change the x location by speed
x = x + speed;
}
}
void road(){
for(int yRoad=0; yRoad < height; yRoad+=50){
fill(0);
stroke(255);
strokeWeight(4);
rect(0,yRoad,width,50);
}
}
1