xxxxxxxxxx
let dino;
let world;
let obstacles = [];
let caught = false;
let over_an_obstacle = false;
let over_an_obstacle_swithc = false;
let overSound;
let score = 0;
let sp = 8;
class World{
constructor(){
this.floor_height = 50;
this.floor_pos = height - this.floor_height;
this.gravity = 9.8;
}
draw(){
background(200, 230, 255);
//background(255);
fill(150, 200, 100);
stroke(100);
rect(0, height - this.floor_height, width, this.floor_height);
fill(250, 250, 50);
stroke(250, 250, 50);
circle(width - 60, 60, 40);
}
}
class Dino{
constructor(world){
this.dh = 80;
this.dw = 70;
this.weight = 500.0; //dino's body weight
this.world = world;
this.run_imgs = [loadImage('run1.png'), loadImage('run2.png')];
this.jump_img = loadImage('jump.png');
this.jumpSounds = [loadSound('hop.mp3'), loadSound('xoba.mp3'), loadSound('opa.mp3')];
this.caughtSound = loadSound('caught.mp3');
this.run_switch = true;
this.run_counter = 0;
this.dx = 100; //dino's x position
this.dy = 100; //dino's y position
this.dyv = 0; //dino's y velocity
this.is_flying = this.dy < this.world.floor_pos;
}
caught(){
this.run_switch = true;
this.run_counter = 0;
this.dx = 100; //dino's x position
this.dy = 100; //dino's y position
this.dyv = 0; //dino's y velocity
this.is_flying = this.dy < this.world.floor_pos;
this.caughtSound.play();
}
draw(){
if (this.dy > this.world.floor_pos){
this.dy = this.world.floor_pos;
this.dyv = 0;
}
if(this.dy < this.world.floor_pos)
this.dyv += this.world.gravity * deltaTime / 100.;
if(this.dy < this.world.floor_pos)
image(this.jump_img, this.dx, this.dy - this.dh, this.dw, this.dh);
else
image(this.run_imgs[this.run_switch ? 0 : 1], this.dx, this.dy - this.dh, this.dw, this.dh);
this.run_counter += 1;
if(this.run_counter > 7){
this.run_counter = 0;
this.run_switch = !this.run_switch;
}
this.dy += this.dyv;
}
jump(){
var r = Math.floor(Math.random() * 3);
this.jumpSounds[r].play();
this.dyv = -26;
this.is_flying = true;
}
check_collision(obstacle){
return Math.min(this.right, obstacle.right) > Math.max(this.left, obstacle.left) &&
Math.min(this.bottom, obstacle.bottom) > Math.max(this.top, obstacle.top);
}
over_an_obstacle(obstacle){
return Math.min(this.right, obstacle.right) > Math.max(this.left, obstacle.left);
}
get right(){
return this.dx + this.dw;
}
get left(){
return this.dx;
}
get top(){
return this.dy - this.dh;
}
get bottom(){
return this.dy;
}
}
class Obstacle{
constructor(world){
this.world = world;
this.img = loadImage('cactus1.png');
this.h = 80;
this.w = 40
this.y = this.world.floor_pos;
this.x = width - 20;
this.xv = sp;
}
draw(){
image(this.img, this.x, this.y - this.h, this.w, this.h);
// noFill();
// stroke(0);
// rect(this.x, this.y - this.h, this.w, this.h);
this.x -= this.xv;
}
is_offscreen(){
return (this.x < -this.w);
}
get right(){
return this.x + this.w;
}
get left(){
return this.x;
}
get top(){
return this.y - this.h;
}
get bottom(){
return this.y;
}
}
function preload(){
soundFormats('mp3', 'ogg');
overSound = loadSound('over.mp3');
}
let obs_g = 0;
function setup() {
createCanvas(500, 400);
// frameRate(25);
world = new World();
dino = new Dino(world);
textSize(24);
}
function draw() {
if (!caught){
caught = obstacles.some((ob)=>{return dino.check_collision(ob);});
over_an_obstacle = obstacles.some((ob)=>{return dino.over_an_obstacle(ob);});
if (over_an_obstacle & !over_an_obstacle_swithc){
over_an_obstacle_swithc = true;
score ++;
overSound.play();
sp = Math.floor(score / 10) * 2 + 8;
}
if (!over_an_obstacle){
over_an_obstacle_swithc = false;
}
world.draw();
fill(0);
noStroke();
text(score, 30, 30);
dino.draw();
obstaclesGenerator();
for(obstacle of obstacles){
obstacle.draw();
}
obstaclesDestroyer();
}
else{
caught = false;
score = 0;
obstacles = []
dino.caught();
}
}
function keyPressed(){
if (key == ' ')
dino.jump();
}
function obstaclesGenerator(){
if(obstacles.length < 3 & obs_g > 40){
obstacles.push(new Obstacle(world));
obs_g = 0;
}
obs_g ++;
}
function obstaclesDestroyer(){
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacle = obstacles[i];
if(obstacle.is_offscreen()){
obstacles.splice(i, i + 1);
break;
}
}
}