xxxxxxxxxx
98
let froggy;
let Car1;
let Car2;
let cars = []
function setup() {
createCanvas(400, 400);
froggy = new Frog();
Car1 = new Car();
Car2 = new Car();
for (let i=0; i < 10; i++){
cars[i] = new Car();
}
noStroke();
}
function draw() {
background(220);
for (let i=0; i < 10; i++){
cars[i].display
}
froggy.move();
froggy.display();
Car1.display();
Car1.move();
Car1.offscreen();
Car2.display();
Car2.move();
Car2.offscreen();
}
class Frog {
constructor() {
this.x = width / 2;
this.y = height - 20;
this.speed = 5;
}
move() {
if (keyIsDown(LEFT_ARROW)) {
this.x -= 3;
}
if (keyIsDown(RIGHT_ARROW)) {
this.x += 3;
}
if (keyIsDown(UP_ARROW)) {
this.y -= 3;
}
if (keyIsDown(DOWN_ARROW)) {
this.y += 3;
}
if (this.y <= 0) {
this.y = height - 20;
}
}
display() {
fill(0, 200, 0);
circle(this.x, this.y, 30);
}
}
class Car {
constructor() {
this.x = width - 20;
this.y = random(0, height - 50);
this.xSpeed = random(1, 3);
}
display() {
fill(200, 0, 0);
rect(this.x, this.y, 30);
}
move() {
this.x = this.x - this.xSpeed;
}
offscreen() {
if (this.x < 0) {
this.x = width;
}
}
}