xxxxxxxxxx
251
// ==================== Player =================
class Player {
constructor(x, y) {
this.w = 10;
this.h = 40;
this.position = createVector(x, y);
this.canJump = true;
this.velocity = 0;
this.gravity = 0.8;
this.moveLegs = false;
this.phaseLegs = 0.01;
this.phaseArms = 0.024;
this.headColour = color(0, 255, 0)
}
show() {
fill(0, 255, 0);
stroke(255)
strokeWeight(8);
fill(this.headColour);
ellipse(this.position.x + 5 + 2 * sin(this.phaseLegs += 0.1), this.position.y, 30, 35);
strokeWeight(10);
stroke(0, 255, 0);
if (frameRate % 32 == 0)
this.moveLegs = !this.moveLegs
//legs
line(this.position.x + this.w / 2, this.position.y + this.h - 10,
this.position.x + this.w * sin(this.phaseLegs += 0.01), this.position.y + this.h);
line(this.position.x + this.w / 2, this.position.y + this.h - 10,
this.position.x + this.w * 1.5 * cos(this.phaseLegs += 0.01), this.position.y + this.h);
//arms
line(this.position.x + this.w / 2, this.position.y + this.h - 20,
this.position.x + this.w * 1.5 * sin(this.phaseArms += 0.1), this.position.y + this.h - 10);
line(this.position.x + this.w / 2, this.position.y + this.h - 20,
this.position.x + this.w * 1.5 * cos(this.phaseArms += 0.1), this.position.y + this.h - 10);
if (this.canJump == false) {
this.phaseArms += 0.01;
this.phaseLegs += 0.01;
}
}
update() {
this.velocity += this.gravity;
this.position.y += this.velocity;
if (this.position.y + this.h > height - 20) {
this.position.y = height - this.h - 20;
this.velocity = 0;
this.canJump = true;
}
}
jump() {
if (this.canJump) {
this.velocity -= 16;
this.canJump = false;
if (csound)
csound.readScore('i"Jump" 0 .1');
}
}
}
// ==================== Star =================
class Star {
constructor(x, y, w) {
this.position = createVector(x, y);
this.w = w;
this.h = this.w;
this.velocity = createVector(random(-0.3, -0.5), 0);
}
show() {
fill(255);
this.position.add(this.velocity);
ellipse(this.position.x, this.position.y, this.w * random(0.8, 1));
}
}
// ==================== Star =================
class ShootingStar {
constructor(x, y, w) {
this.position = createVector(x, y);
this.w = w;
this.h = this.w;
this.velocity = createVector(random(1, 3), random(0.5, 1));
this.trail = [];
this.a = 0;
this.colour = color(random(100, 255), random(100, 255), random(100, 255));
}
show() {
fill(255);
this.position.add(this.velocity);
this.trail.push([this.position.x, this.position.y]);
for (let i = 0; i < this.trail.length; i++) {
noStroke();
fill(red(this.colour), green(this.colour), blue(this.colour), this.a);
ellipse(this.trail[i][0], this.trail[i][1], map(this.position.x, 0, width, this.w, 0));
if (this.a > 255) {
this.trail.shift();
this.a = 0;
}
this.a += 15;
}
}
}
// ==================== Building =================
class Building {
constructor(x, y, w, h, speed) {
this.w = w;
this.h = h;
this.position = createVector(x, y);
this.velocity = createVector(-2, 0);
this.colour = color(random(100, 255), random(100, 255), random(100, 255));
}
show() {
fill(this.colour);
this.position.add(this.velocity);
strokeWeight(5);
rect(this.position.x, this.position.y, this.w, this.h);
stroke(0);
let xDivs = 4
for (let i = 0; i < xDivs; i++) {
line(this.position.x + i * (this.w / xDivs), this.position.y,
this.position.x + i * (this.w / xDivs), this.position.y + this.h)
}
let yDivs = 8;
for (let i = 0; i < yDivs; i++) {
line(this.position.x, this.position.y + i * (this.h / yDivs),
this.position.x + this.w, this.position.y + i * (this.h / yDivs))
}
strokeWeight(1);
}
}
// ==================== Star =================
class Car {
constructor(x, y, w, h, speed, isFlying = false) {
this.w = w;
this.h = h;
this.position = createVector(x, y);
this.velocity = createVector(-speed, 0);
this.speed = speed;
this.colour = color(random(0, 100), random(0, 100), random(0, 255));
this.phase = 0.01;
this.isFlying = isFlying;
}
setSpeed(speedButtonDown, speed) {
this.speed = speed;
if (this.isFlying === true)
this.velocity.x = -this.speed * 0.75;
else
this.velocity.x = speedButtonDown === true ? -this.speed * 0.25 : -this.speed;
}
show() {
let x = this.position.x;
let y = this.position.y;
this.position.add(this.velocity);
if (this.isFlying === false) {
fill(255 * random(0.8, 1), 255 * random(0.8, 1), 0);
stroke(255 * random(0.8, 1), 255 * random(0.8, 1), 0);
triangle(x - 15, y + this.h * 0.6, x + 5, y + this.h * 0.7, x - 15, y + this.h * .8);
strokeWeight(1);
stroke(0);
fill(this.colour);
rect(x, y + this.h * 0.6, this.w, this.h - this.h * 0.7);
fill(0)
ellipse(x + this.w * 0.2, y + this.h * 0.9, this.h * 0.4);
ellipse(x + this.w * 0.8, y + this.h * 0.9, this.h * 0.4);
fill(this.colour);
quad(x + this.w * 0.15, y + this.h * 0.6, x + this.w * 0.25, y * 1.1, x + this.w * 0.65, y * 1.1, x + this.w * 0.9, y + this.h * 0.5);
} else {
strokeWeight(1);
fill(this.colour);
stroke(0);
triangle(x + this.w * 0.1, y + this.h * 0.5, x + this.w * 1.2, y + this.h * .7, x + this.w * .5, y + this.h * .2)
quad(x + this.w * 0.35, y + this.h * 0.4, x + this.w * 0.45, y * 1.1, x + this.w * 0.6,
y * 1.1, x + this.w * 0.9, y + this.h * Math.abs(sin(this.phase += 0.1)) * 1.1);
fill(255 * random(0.8, 1), 255 * random(0.8, 1), 0);
stroke(255 * random(0.8, 1), 255 * random(0.8, 1), 0);
triangle(x + this.w * 0.1, y + this.h * 0.5, x + this.w * 0.24, y + this.h * 0.4,
x + this.w * 0.26, y + this.h * 0.55)
}
}
}
// ==================== Helicopter =================
class Helicopter {
constructor(x, y, w, h) {
this.position = createVector(x, y);
this.w = w;
this.h = h;
this.phase = 0.01;
this.colour = color(random(255), random(255), random(255));
this.velocity = createVector(0, -1);
this.target = createVector(x - 200, 200);
}
show() {
fill(this.colour)
let targetPosition = createVector(this.target.x, this.target.y);
stroke(red(this.colour) * 0.5, green(this.colour) * 0.5, blue(this.colour) * 0.5);
var distance = targetPosition.dist(this.position);
targetPosition.sub(this.position);
targetPosition.normalize();
this.position.add(targetPosition);
let x = this.position.x;
let y = this.position.y;
strokeWeight(2);
//body
quad(x, y + this.h * 0.6, x + this.w * 0.3, y, x + this.w * 1.1, y + this.h * 0.3,
x + this.w * 0.3, y + this.h);
//back section
triangle(x + this.w * 1.2, y + this.h * 0.55, x + this.w * 1.2, y,
x + this.w * 0.8, y + this.h * .55)
//top blades
strokeWeight(5)
line(x - 30, y + this.h * 0.2 - 10 + 2 * cos(this.phase += 1), x + this.w, y - 10 + 2 * sin(this.phase += 1))
strokeWeight(4)
line(x + this.w * 0.2, y + this.h, x + this.w * 0.6, y + this.h * 0.9)
//back propeller
push()
translate(x + this.w * 1.1, y + this.h * 0.4);
strokeWeight(5)
rotate(this.phase * .7);
rect(0, 0, 40, 2);
pop();
}
returnHome() {
this.target.x = width;
this.target.y = height;
}
}