Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Player(){
- this.x = width/2+10;
- this.y = height/2-20;
- this.grounded = true;
- this.show = function(){
- fill(255);
- square(this.x,this.y,20);
- }
- this.testCollisions = function(other){
- if (this.x < other.x+other.w && other.x < this.x+this.w &&
- this.y < other.y+other.h && other.y < this.y+this.h) {
- print("collision")
- this.grounded = true;
- } else {
- this.grounded = false;
- }
- }
- this.affectGravity = function(){
- if (!this.grounded)
- this.y+=1;
- }
- }
- function Block(x,y,grassed){
- this.grassed = grassed; // bool
- this.x = x; // float
- this.y = y;
- this.h = 40;
- this.w = 40;
- this.gh = 15;
- this.gw = 40;
- this.render = function(){
- if (this.grassed){
- fill("#AF7250");
- rect(this.x,this.y,this.w,this.h);
- fill("#869336");
- rect(this.x,this.y,this.gw,this.gh);
- }
- }
- }
- var block;
- var player;
- var grounded = true;
- function setup() {
- createCanvas(400, 400);
- block = new Block(height/2,width/2,true);
- player = new Player();
- }
- function draw() {
- background(120);
- block.render();
- player.show();
- if (keyIsDown(LEFT_ARROW)){
- player.x --;
- } else if (keyIsDown(RIGHT_ARROW)){
- player.x ++;
- }
- strokeWeight(1);
- player.testCollisions(block);
- player.affectGravity();
- console.log(player.grounded);
- }
- function keyPressed(){
- if (player.grounded && keyCode === 32){
- for (let i = 0; i < 10; i++)
- player.y-=1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement