Advertisement
Guest User

F

a guest
Jun 10th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Player(){
  2.   this.x = width/2+10;
  3.   this.y = height/2-20;
  4.   this.grounded = true;
  5.   this.show = function(){
  6.     fill(255);
  7.     square(this.x,this.y,20);
  8.   }
  9.   this.testCollisions = function(other){
  10.     if (this.x < other.x+other.w && other.x < this.x+this.w &&
  11.         this.y < other.y+other.h && other.y < this.y+this.h) {
  12.       print("collision")
  13.           this.grounded = true;
  14.     } else {  
  15.           this.grounded = false;
  16.     }
  17.   }
  18.   this.affectGravity = function(){
  19.     if (!this.grounded)
  20.       this.y+=1;
  21.   }
  22. }
  23.  
  24. function Block(x,y,grassed){
  25.   this.grassed = grassed; // bool
  26.   this.x = x; // float
  27.   this.y = y;
  28.   this.h = 40;
  29.   this.w = 40;
  30.   this.gh = 15;
  31.   this.gw = 40;
  32.   this.render = function(){
  33.     if (this.grassed){
  34.       fill("#AF7250");
  35.       rect(this.x,this.y,this.w,this.h);
  36.       fill("#869336");
  37.       rect(this.x,this.y,this.gw,this.gh);
  38.     }
  39.   }
  40. }
  41.  
  42. var block;
  43. var player;
  44. var grounded = true;
  45. function setup() {
  46.   createCanvas(400, 400);
  47.   block = new Block(height/2,width/2,true);
  48.   player = new Player();
  49. }
  50.  
  51. function draw() {
  52.   background(120);
  53.   block.render();
  54.   player.show();
  55.   if (keyIsDown(LEFT_ARROW)){
  56.     player.x --;
  57.   } else if (keyIsDown(RIGHT_ARROW)){
  58.     player.x ++;
  59.   }
  60.   strokeWeight(1);
  61.   player.testCollisions(block);
  62.   player.affectGravity();
  63.   console.log(player.grounded);
  64. }
  65. function keyPressed(){
  66.   if (player.grounded && keyCode === 32){
  67.     for (let i = 0; i < 10; i++)
  68.       player.y-=1;
  69.   }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement