xxxxxxxxxx
127
let laneSize;
let grassLeaves = [];
let cars = []
let ja
let gameOver
function setup() {
createCanvas(windowWidth, windowHeight);
gameOver = false;
laneSize = 50;
for (let i = 0; i < 1000; i++) {
grassLeaves[i] = new Grass();
}
cars[0] = new Car(10, height / 2 + 10);
cars[1] = new Car(120, height / 2 + 10);
cars[2] = new Car(350, height / 2 + 10);
ja = new Person()
}
function draw() {
background("#4CAF50");
for (let i = 0; i < grassLeaves.length; i++) {
grassLeaves[i].draw();
}
//cesta
noStroke();
fill(100);
rect(0, height / 2 - laneSize, width, laneSize * 2);
// ciara
stroke(255);
strokeWeight(5);
drawingContext.setLineDash([10, 20]);
line(0, height / 2, width, height / 2);
drawingContext.setLineDash([]);
// auto
for (let i = 0; i < cars.length; i++) {
cars[i].draw();
cars[i].move();
cars[i].checkCollision();
}
ja.draw()
ja.move()
}
function keyTyped() {
}
class Car {
constructor(posX, posY) {
this.posX = posX;
this.posY = posY;
this.w = random(30,70)
}
draw() {
fill(255);
stroke(150);
rect(this.posX, this.posY, this.w, 30);
}
move() {
this.posX = this.posX + 3
if (this.posX > width) {
this.posX = -this.w
}
// console.log(this.posX)
}
checkCollision() {
// if "ja" overlaps this object
// gameOver = true
}
}
class Grass {
constructor() {
this.x = random(0, width);
this.y = random(0, height);
}
draw() {
strokeWeight(2);
stroke("#3C883F");
line(this.x, this.y, this.x, this.y + 10);
}
move() {
// @todo: animovat auto
}
}
class Person {
constructor() {
this.x = width / 2;
this.y = height / 2 + laneSize + 40
this.size = 25
}
draw() {
fill('#FFC107')
noStroke()
ellipse(this.x, this.y, this.size)
}
move() {
if (keyIsDown(LEFT_ARROW)) {
ja.x = ja.x - ja.size/4;
} else if (keyIsDown(RIGHT_ARROW)) {
ja.x = ja.x + ja.size/4;
} else if (keyIsDown(UP_ARROW)) {
ja.y = ja.y - ja.size/4;
} else if (keyIsDown(DOWN_ARROW)) {
ja.y = ja.y + ja.size/4;
}
}
}