xxxxxxxxxx
218
let camera;
let obstacles = [];
let particles = [];
let z;
let y;
let x;
let c;
let boxWidth;
let boxHeight;
let boxThick;
let camX;
let camY;
let song;
let player1;
function preload() {
song = loadSound("scifi.wav");
}
function setup() {
createCanvas(600, 400, WEBGL);
song.play();
song.loop();
camera = createCamera();
setCamera(camera);
camX = 0;
camY = 0;
camZ = 0;
camera.setPosition(camX,camY,camZ);
x = 0;
y = 0;
player1 = new Player();
for (let i = 0; i < 200; i++){
x = random(-1000,1000);
z = random(-6000,-1000);
y = random(-1000,1000);
c = '#ffff';
boxWidth = random(25,200);
boxHeight = random(25,200);
boxThick = random(25,100);
obstacles.push(new boxOb(x, y, z, boxWidth,boxHeight,boxThick,c));
}
for(let i = 0;i<400;i++){
particles.push(new Particle());
}
}
function draw() {
background(100);
player1.show();
player1.update();
player1.control();
for (let i = 0; i < obstacles.length; i++){
obstacles[i].display();
if (player1.pos.x && player1.pos.x + 20 > obstacles[i].x && player1.pos.x && player1.pos.x + 20 < obstacles[i].x + obstacles[i].w && player1.pos.y && player1.pos.y + 20 > obstacles[i].y && player1.pos.y && player1.pos.y + 20 < obstacles[i].y + obstacles[i].h && obstacles[i].z > -302){
background(255,0,0);
myp5.gameOver = 'Game Over';
noLoop();
myp5.noLoop();
song.pause();
}
// if (player1.pos.x > obstacles[i].x && player1.pos.x < obstacles[i].x + obstacles[i].w && player1.pos.y > obstacles[i].y && player1.pos.y < obstacles[i].y + obstacles[i].h && obstacles[i].z > -302){
// noLoop();
// }
}
for(let i = 0;i<particles.length;i++) {
particles[i].display();
particles[i].move();
}
}
class Player {
constructor(x, y) {
this.pos = createVector(x, y);
this.vel = p5.Vector.random2D(0,0);
}
update(){
this.vel.limit(5);
this.pos.add(this.vel);
if(this.pos.x < -260){
this.pos.x = 300;
} else if (this.pos.x > 300){
this.pos.x = -260;
}
if(this.pos.y < -200){
this.pos.y = 180;
} else if (this.pos.y > 180){
this.pos.y = -200;
}
}
control(){
if (keyIsDown(38)){
this.vel.add(0, -1);
} else if (keyIsDown(40)){
this.vel.add(0, 1);
}
if (keyIsDown(37)){
this.vel.add(-1,0);
} else if (keyIsDown(39)){
this.vel.add(1,0);
}
}
show() {
fill('#ffff00');
stroke(2);
push();
translate(this.pos.x, this.pos.y, -300);
box(20,20,20);
pop();
}
}
class boxOb {
constructor(x,y,z,w,h,t,c){
this.x = x;
this.y = y;
this.z = z;
this.w = w;
this.h = h;
this.t = t;
this.c = c;
}
display(){
stroke(2);
push();
fill(this.c);
translate(this.x,this.y,this.z);
this.z = this.z + 10;
box(this.w,this.h,this.t);
pop();
if (this.z > -120){
this.z = random(-6000,-1000);
this.x = random(-1000,1000);
this.y = random(-1000,1000);
this.w = random(25,200);
this.h = random(25,200);
this.t = random(25,100);
}
if (myp5.timer > 1000){
this.z = this.z + 10.5;
} if (myp5.timer > 2000){
this.z = this.z + 11;
} if (myp5.timer > 3000){
this.z = this.z + 11.5;
} if (myp5.timer > 4000){
this.z = this.z + 12;
} if (myp5.timer > 5000){
this.z = this.z + 12.5;
}
}
}
class Particle {
constructor(){
this.x = random(-4000, 4000);
this.y = random(-4000,4000);
this.xSpeed = random(-2,2);
this.ySpeed = random(-1,1.5);
}
display() {
noStroke();
fill(255);
push();
translate(this.x,this.y,-3000);
sphere(10);
pop();
}
move() {
if(this.x < 0 || this.x > width)
this.xSpeed*=-1;
if(this.y < 0 || this.y > height)
this.ySpeed*=-1;
this.x+=this.xSpeed;
this.y+=this.ySpeed;
}
}
var timerSketch = function(t){
let timer;
let gameOver;
t.setup = function(){
t.createCanvas(600,100);
t.timer = 0;
t.gameOver = '';
}
t.draw = function(){
t.background(200);
t.timer++;
t.textSize(30);
t.fill(250);
t.text(t.nf(t.timer),290,55);
t.fill(255,0,0);
t.textSize(20);
t.text(t.gameOver, 265,90);
}
}
var myp5 = new p5(timerSketch);