Matter.Engine.clear(engine)
Matter.World.clear(world, false)
Matter.Engine.clear(engine)
hits.push(new Hit(0,-450,50,2000,false))
hits.push(new Hit(1950,-450,50,2000,false))
hits.push(new Hit(1000, 600-40, 2000, 80))
addBox(500,500,random(20,60),random(20,60))
addBox(550,500,random(20,60),random(20,60))
addBox(600,500,random(20,60),random(20,60))
addBox(650,500,random(20,60),random(20,60))
addBox(525,450,random(20,60),random(20,60))
addBox(575,450,random(20,60),random(20,60))
addBox(625,450,random(20,60),random(20,60))
addBox(550,400,random(20,60),random(20,60))
addBox(600,400,random(20,60),random(20,60))
addBox(575,350,random(20,60),random(20,60))
addCir(1000+random(-30,30),400+random(-50,100),random(20,80))
addSpike(800,495,80,50,true)
addSpike(800,530,700,50,true)
addDoor(300,460,800,100,700,50)
for(let x=0;x<=600;x+=50){
addBox(500+x,50,50,50)}else{addCir(500+x,50,50)}}
addHit(550, 550, 200, 200, true);
addHit(750, 550, 200, 400, true);
addHit(950, 550, 200, 600, true);
addHit(950, 550, 300, 1000, true);
addBoost(950, 0, 300, 100, 3, 5);
addHit(1050, -500, 200, 100, true);
addHit(850, -750, 200, 100, true);
addBoost(850, -850, 200, 100, 3, 10);
for(var i = 0; i < 50; i++) {
addBox(random(0, 2020), random(-1500, 550), random(50,100), random(50,100));}
else{addCir(random(0, 2020), random(-1500, 550), random(50,100), random(50,100))}
addDoor(500,100,900,200,50,800)
addDoor(700,100,1500,200,50,800)
addBoost(400,300,100,500,3,1.5)
addBoost(300,450,200,100,0,3)
addDoor(1250,500,300,400,200,20)
addHit(200,300,20,300,false)
addHit(400,300,20,300,false)
addHit(1300,400,20,300,false)
for(let x=0;x<200;x+=50){
for(let y=0;y<200;y+=50){
addBox(230+x,170+y,random(30,60),random(30,60))
addHit(150, 250, 50, 50);
addHit(100, 200, 50, 50);
addHit(400, 50, 400, 100);
addHit(600, -100, 400, 100);
addHit(400, -350, 400, 100);
addHit(200, -500, 400, 100);
addHit(400, -750, 400, 100);
addHit(600, -900, 400, 100);
addHit(400, -1150, 400, 100);
addHit(200, -1300, 400, 100);
addBoost(75, -1400, 100, 100, 0, 5);
addSpike(1000, 500, 2000, 100,true);
addBoost(1700,-350,100,300,3,6)
addHit(1400,-1500,200,50)
addSpike(random(300,2000),random(-1800,500),50,50,false)
addHit(400+100,350,20,400,false)
addHit(550+100,350,20,400,false)
addDoor(500,-325,475+100,300-20,150,20)
addDoor(500,-1525,475+100,375-20,150,20)
addDoor(1850,-1425,475+100,450-20,150,20)
addDoor(575,500,750,350,200,20)
addBoost(700,-800,100,200,3,5)
addHit(1100,-1700,200,20)
addHit(1900,-1400,200,20)
addHit(1950,-1950,50,1000,false)
addHit(1000, 500, 400, 100);
addHit(970, 200, 340, 100);
addHit(1030, -100, 340, 100);
addHit(970, -400, 340, 100);
addHit(1030, -700, 340, 100);
addSpike(550, -1000, 700, 100,true);
addHit(950, -1000, 100, 100);
addHit(1200, -1200, 340, 100, false);
addBoost(-300, 800, 600, 50, 3, 12);
addHit(1500,-300,30,1700,false)
addHit(1200,-1400,200,30)
Matter.Engine.clear(engine)
p5.disableFriendlyErrors = true;
var Engine = Matter.Engine,
var door_colors=[60,70,100,150,120,250,24,300,85,220,320,310,330,15,78]
createCanvas(windowWidth, windowHeight);
engine = Engine.create();
box1 = Matter.Bodies.rectangle(100, 100, 80, 80,{restitution:0,mass:15,chamfer:
ground = Bodies.rectangle(width/2, height-40, width, 80,{ isStatic: true,
Engine.update(engine, 1000/30);
if(keyIsDown(77)){Matter.Engine.clear(engine)}
text("Welcome to Physics Game",(width/2+100)+tmx/2,(height/2-100)+tmy/3)
text("Arrow keys/WASD to move",(width/2+80)+tmx/2.5,(height/2-25)+tmy/4)
text("Go to the yellow circle to get to the next level",(width/2+60)+
tmx/3,(height/2+20)+tmy/5)
if(frameCount%r3==0){if(r3%2==0){addBox(575,-100,random(20,80),random(20,80))}
else{addCir(1000,-100,random(20,80))}r3=round(random(60,120))}}
text("Red will kill you",(width/2)+tmx/2,(height/2-100)+tmy/3)
text("Press R to reset",(500)+tmx/2,(height/2)+tmy/3)
text("Watch out!",(width/2)+tmx/2,(height/2-100)+tmy/3)
if(round(random(0,5))==2){
addSpike(random(0,1500),0,50,50,false)}
if(rise>=sr){rise=0;addSpike(hits[2].body.position.x,hits[2].body.position.y+(sy-500),2000,100,true);sy-=100;if(sr>20){sr-=2}}
text("Good Job!",(width/2+100)+tmx/2,(height/2-100)+tmy/3)
text("69",(width/2+100)+tmx/2,(height/2-700)+tmy/3)
if(round(random(0,5))==2){
addBox(random(0,1500),0,random(40,200),random(40,200),false)}
if(keyIsDown(67)&&st6==0){st6=1; transX=0
Matter.Body.setPosition(box1,{x:hits[2].body.position.x-900,y:hits[2].body.position.y-500 });
Matter.Body.setVelocity( box1, {x:0,y:0});Clear()}
if(!keyIsDown(67)){st6=0}
Matter.Body.setPosition(box1,{x:hits[2].body.position.x-900,y:hits[2].body.position.y-500 })
;Matter.Body.setVelocity( box1, {x:0,y:0});Clear()}
if(keyIsDown(38)&&st==0){clear=false;st=1}else{clear=true}
Matter.Engine.clear(engine);ellipse(20,20,20,20)}}
Matter.Body.translate(ground,{x:transX,y:transY})
if(frameCount%50==0){st5=0}
if(hits[0].body.position.x>=0){transX=0;
if(box1.position.x>width/2){transX=-box1.position.x+width/2}}else{transX=-box1.position.x+width/2}
if(hits[1].body.position.x<=width){transX=0;
if(box1.position.x<width/2){transX=-box1.position.x+width/2}}
if(hits[2].body.position.y<=height){transY=0} else{transY=-box1.position.y+height/2}
if(box1.position.y<height/2){transY=-box1.position.y+height/2}
Matter.Body.translate(box1,{x:transX,y:transY})
translate(box1.position.x,box1.position.y)
translate(box1.position.x,box1.position.y)
if(t2>=15){rr-=rr/5;rrr-=rr/5}
if(oldR==round(box1.angle*20)/20){t2++}else{t2=0;rr=box1.angle+rrr}
rotate(rr=box1.angle+rrr)
if(t>4){t=0;if(oldR!==round(box1.angle*20)/20){o=true}else{o=false}}
if(doBox){ellipse(0,15,20,20)}
line (-10*2,5*2,-5*2,8*2)
line (-5*2,8*2,4*2,8*2)}}
oldR=round(box1.angle*20)/20
Matter.Events.on(engine, 'collisionActive', ({ pairs }) => {
if(keyIsDown(71)){print(pairs)}
pairs.forEach(({ bodyA, bodyB,i=0 }) => {
if(bodyA.label=="player"&&bodyB.label=="Rectangle Body"&&(keyIsDown(38)||keyIsDown(87))&&
bodyA.position.y<bodyB.position.y){st=0;Matter.Body.setVelocity( box1,
{x: box1.velocity.x, y: -25});}
if(bodyA.label=="player"&&bodyB.label=="spike"){die()}
if(bodyA.label=="player"&&bodyB.label=="fin"){if(st5==0){
Matter.Body.setPosition(box1,{x:80,y:0});Matter.Body.setVelocity
( box1, {x:0,y:0});Clear();st5=1}}
if(bodyA.label=="spike"&&bodyB.label=="player"){die()}
if(bodyA.label=="player"&&bodyB.label=="boost0"){Matter.Body.setVelocity( box1,
{x:bodyB.speed*10, y:box1.velocity.y });}
if(bodyB.label=="Rectangle Body"&&bodyA.label=="boost0"){Matter.Body.setVelocity
( bodyB, {x:bodyA.speed*10, y:bodyB.velocity.y });}
if(bodyA.label=="player"&&bodyB.label=="boost1"){Matter.Body.setVelocity
( box1, {x:box1.velocity.x, y:bodyB.speed*10 });}
if(bodyB.label=="Rectangle Body"&&bodyA.label=="boost1"){Matter.Body.setVelocity
( bodyB, {x:bodyB.velocity.x, y:bodyA.speed*10 });}
if(bodyA.label=="player"&&bodyB.label=="boost2"){Matter.Body.setVelocity
( box1, {x:-bodyB.speed*10, y:box1.velocity.y });}
if(bodyB.label=="Rectangle Body"&&bodyA.label=="boost2"){Matter.Body.setVelocity
(bodyB, {x:-bodyA.speed*10, y:bodyB.velocity.y });}
if(bodyA.label=="player"&&bodyB.label=="boost3"){
Matter.Body.setVelocity( box1, {x:box1.velocity.x, y:-bodyB.speed*10 });}
if(bodyB.label=="Rectangle Body"&&bodyA.label=="boost3"){
Matter.Body.setVelocity( bodyB, {x:bodyB.velocity.x, y:-bodyA.speed*10 });}
for (let i=0; i< fins.length;i++){
for (let i=0; i< boxs.length;i++){
if (boxs[i].d==1) {boxs.splice(i,1);i--}
for (let i=0; i< cirs.length;i++){
if (cirs[i].d==1) {cirs.splice(i,1);i--}
for (let i=0; i< hits.length;i++){
for (let i=0; i< boosts.length;i++){
for (let i=0; i< doors.length;i++){
for (let i=0; i< spikes.length;i++){
for (let i=0; i< parts.length;i++){
parts[i].move();if (parts[i].d==1) {parts.splice(i,1);i--}}
text('Be like "'+fol+'" and follow me!',tmx+250,tmy+370)
if (keyIsDown(37)&&abs(box1.velocity.x)<=maxspeed){Matter.Body.setVelocity
( box1, {x: box1.velocity.x-1, y: box1.velocity.y})}
if (keyIsDown(39)&&abs(box1.velocity.x)<=maxspeed){Matter.Body.setVelocity
( box1, {x: box1.velocity.x+1, y: box1.velocity.y})}
text("x: "+round(tmx)+", y: "+round(tmy)+" FPS: "+round(frameRate())+" level: "+level+", "+rise+", "+sr,70,20)
text("level by: George Moangher",width/2,height-30)
text("level by: George Moangher",width/2,height-30)
text("level by: George Moangher",width/2,height-30)
cirs.push(new Cir(x,y,w,w))
function addSpike(x,y,w,h,s){
spikes.push(new Spike(x,y,w,h,s))
function addBox(x,y,w,h){
boxs.push(new Box(x,y,w,h))
function addFin(x,y,w,h){
fins.push(new Fin(x,y,w,h))
function addBoost(x,y,w,h,d,s){
boosts.push(new boost(x,y,w,h,d,s))
function addDoor(x,y,x2,y2,w,h){
doors.push(new Door(x,y,x2,y2,w,h))
function Box (xx,yy,ww,hh) {
this.ir=round(random(0,50))
this.body=Bodies.rectangle(xx, yy, ww, hh,{restitution:0.3,friction:0.05,isStatic:false,chamfer: { radius: 4 }});
World.add(world, this.body);
Matter.Body.translate(this.body,{x:transX,y:transY})
if(keyIsDown(32)){World.remove(world, this.body);this.d=1}
var pos=this.body.position
var angle=this.body.angle
fill(217+this.r, 188+this.r, 22+this.r)
stroke(173+this.r, 146+this.r, 38+this.r)
if(this.ir==2&&this.img.width>0&&this.w==this.h){
rect(0,0,this.w,this.h,4)
line(-ww/2+2,-hh/2+2,ww/2-2,hh/2-2)}else{
line(ww/2-2,-hh/2+2,-ww/2+2,hh/2-2)}
function Cir (xx,yy,ww,hh) {
this.body=Bodies.circle(xx, yy, ww/2,{restitution:0.3,friction:0.1,mass:3});
World.add(world, this.body);
this.body.label="Rectangle Body"
Matter.Body.translate(this.body,{x:transX,y:transY})
if(keyIsDown(32)){World.remove(world, this.body);this.d=1}
translate(this.body.position.x,this.body.position.y)
fill(217+this.r, 188+this.r, 22+this.r)
stroke(173+this.r, 146+this.r, 38+this.r)
ellipse(0,0,this.w,this.w)
line(-ww/2+(ww/6),-hh/2+(ww/6),ww/2-(ww/6),hh/2-(ww/6))}else{
line(ww/2-(ww/6),-hh/2+(ww/6),-ww/2+(ww/6),hh/2-(ww/6))}
function Fin (xx,yy,ww,hh) {
this.body=Bodies.circle(xx, yy, 50,{restitution:0.3,friction:0.05,isStatic: true,isSensor:true});
World.add(world, this.body);
Matter.Body.translate(this.body,{x:transX,y:transY})
var pos=this.body.position
var angle=this.body.angle
this.body=Bodies.rectangle(xx, yy, 20, 20,{restitution:0.3,friction:0.05});
World.add(world, this.body);
Matter.Body.setVelocity( this.body, {x:random(-25,25),y:random(-25,25)})
Matter.Body.translate(this.body,{x:transX,y:transY})
var pos=this.body.position
var angle=this.body.angle
this.d=1;World.remove(world, this.body);
if(re==0){transX=0;transY=0;tmx=0;tmy=0;re=1;
doBox=true;Matter.Body.setPosition
(box1,{x:hits[2].body.position.x-900,y:hits[2].body.position.y-500 })
Matter.Body.setVelocity(box1,{x:0,y:0})
fill(0, 98, 255,this.fade)
function Spike (xx,yy,ww,hh,ss) {
this.body=Bodies.rectangle(this.x, this.y, this.w, this.h,{restitution:0.3,
friction:0.05,chamfer: { radius: 4 },isStatic: ss});
World.add(world, this.body);
Matter.Body.translate(this.body,{x:transX,y:transY})
translate(this.body.position.x,this.body.position.y)
rect(0,0,this.w,this.h,4)
function Hit (xx,yy,ww,hh,hit) {
this.body=Bodies.rectangle(xx, yy, ww, hh,{ isStatic: true ,restitution:1,chamfer: { radius: 4 }});
World.add(world, this.body);
if(hit==false){this.body.label=" "}
Matter.Body.translate(this.body,{x:transX,y:transY})
var pos=this.body.position
var angle=this.body.angle
rect(0,0,this.w,this.h,4)
function Door (x2,y2,xx,yy,ww,hh) {
this.r=random(door_colors)
this.body=Bodies.rectangle(xx, yy, ww, hh,{ isStatic: true ,restitution:0,chamfer: { radius: 4 }});
this.body2=Bodies.rectangle(x2, y2, 40, 40,{ isStatic: true, isSensor:true ,restitution:0,chamfer: { radius: 4 }});
this.body2.label="doorA"+this.id
World.add(world, this.body);
World.add(world, this.body2);
Matter.Body.translate(this.body,{x:transX,y:transY})
Matter.Body.translate(this.body2,{x:transX,y:transY})
var pos=this.body.position
var angle=this.body.angle
if(this.re){fill(this.r,999,999)}else{fill(this.r,999,999,250)}
if(this.re){stroke(this.r,350,350)}else{stroke(this.r,350,350,250)}
rect(0,0,this.w,this.h,4)
translate(this.body2.position.x,this.body2.position.y)
if(this.re){rect(0,0,40,40,4)}else
Matter.Events.on(engine, 'collisionEnd', ({ pairs }) => {
pairs.forEach(({ bodyA, bodyB }) => {
if(bodyA.label=="player"&&bodyB.label=="doorA"+this.id||bodyA.label=="doorA"+this.id&&bodyB.label=="Rectangle Body"||
bodyA.label=="Rectangle Body"&&bodyB.label=="doorA"+this.id)
{World.add(world, this.body);this.re=true}
Matter.Events.on(engine, 'collisionActive', ({ pairs }) => {
pairs.forEach(({ bodyA, bodyB }) => {
if(bodyA.label=="player"&&bodyB.label=="doorA"+this.id||bodyA.label=="doorA"+this.id&&bodyB.label=="Rectangle Body"||
bodyA.label=="Rectangle Body"&&bodyB.label=="doorA"+this.id)
{World.remove(world, this.body);this.re=false}
var op={ isStatic: true ,restitution:0,isSensor:true}
function boost (xx,yy,ww,hh,dd,ss) {
this.body=Bodies.rectangle(xx, yy, ww, hh,op);
World.add(world, this.body);
this.body.label="boost"+dd
Matter.Body.translate(this.body,{x:transX,y:transY})
var pos=this.body.position
var angle=this.body.angle
translate(this.body.position.x-ww/2,pos.y)
for(let i=0;i<(ww/20)-1;i++){
line(-20+((this.xm+(i*20))%this.ww)+20,-hh/2,0+((this.xm+(i*20))%this.ww)+20,0)
line(0+((this.xm+(i*20))%this.ww)+20,0,-20+((this.xm+(i*20))%this.ww)+20,hh/2)}
for(let i=0;i<(ww/20)-1;i++){
line(0-((this.xm+(i*20))%this.ww)+ww,-hh/2,-20-((this.xm+(i*20))%this.ww)+ww,0)
line(-20-((this.xm+(i*20))%this.ww)+ww,0,0-((this.xm+(i*20))%this.ww)+ww,hh/2)}
for(let i=0;i<(hh/20)-1;i++){
line(0,((this.xm+(i*20))%this.hh)-hh/2,ww/2,((this.xm+(i*20))%this.hh)+20-hh/2)
line(ww/2,(((this.xm+(i*20))%this.hh)+20)-hh/2,ww,(((this.xm+(i*20))%this.hh))-hh/2)
for(let i=0;i<(hh/20)-1;i++){
line(0,-((this.xm+(i*20))%this.hh)+hh/2,ww/2,-((this.xm+(i*20))%this.hh)+hh/2-20)
line(ww/2,-(((this.xm+(i*20))%this.hh))+hh/2-20,ww,-(((this.xm+(i*20))%this.hh))+hh/2)
World.remove(world, box1);doBox=false
parts.push(new Part((box1.position.x-40)+x,(box1.position.y-40)+y))
function addHit(x,y,w,h,s){
hits.push(new Hit(x, y, w, h,s))