xxxxxxxxxx
111
let gravity = 0.5;
class Player {
constructor() {
this.x = 100;
this.y = 320;
this.d = 30;
this.v = 0.05;
//gravity
this.xg = 0;
this.yg = 0;
}
draw() {
fill(15, 192, 252);
stroke(0, 255, 255);
ellipse(this.x, this.y, this.d);
}
update() {
this.y += this.yg;
if (this.y + this.d + this.yg <= height) {
this.yg += gravity;
} else {
this.yg = 0;
}
// if (this.x < 400){
// this.x += this.xg;
// this.xg += this.v;
// }
}
}
class Platform {
constructor(x,y) {
this.x = x;
this.y = y;
this.w = 100;
this.h = 20;
}
draw() {
fill(0);
rect(this.x, this.y, this.w, this.h);
}
}
let player;
let platforms = [];
function preload() {
title = loadImage("title.png");
bimg = loadImage("gameback.JPG")
}
function setup() {
createCanvas(800, 400);
player = new Player();
for (let i = 0; i < 100; i++) {
platforms[i] = new Platform(i + 500, 300);
}
}
function draw() {
background(bimg);
// menu
// fill(255,229,204);
// rect(width/2,100,250,250);
// image(title, width/2 + 5, 100);
// ban = rect(450,150,150,60);
// rect(450,250,150,60);
player.update();
player.draw();
for (let i = 0; i < 10; i++) {
platforms[i].draw();
}
if (player.x < 400) {
player.x += player.xg;
player.xg += player.v;
for (let i = 0; i < 10; i++) {
platforms[i].x -= 5;
}
}
// platform condition
for (let i = 0; i < 10; i++) {
if (
player.y + player.d <= platforms[i].y &&
player.y + player.d + player.yg >= platforms[i].y &&
player.x + player.d >= platforms[i].x &&
player.x <= platforms[i].x + platforms[i].w
) {
player.yg = 0;
}
}
}
// function mouseClicked(){
// ban.clicked;
// print("true")
// }
function keyPressed() {
if (keyCode === UP_ARROW) {
player.yg -= 10;
}
}