//setup canvas
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
//initialize variables
var screenWidth = 900;
var screenHeight = 560;
var framerate = 30;
var gravity = 2;
//create map
var map = [ //array of rectangles made up of arrays of points made up of an array
[[0, 0], [900, 30]],
[[0, 530], [900, 560]],
[[0, 0], [30, 560]],
[[870, 0], [900, 560]],
[[190, 310], [710, 350]]
];
function Player(x, y, color, faceDir) { //create the game object of a player
this.x = x;
this.y = y;
this.xVel = 0;
this.yVel = 0;
this.maxSpeed = 8;
this.grappleSpeed = 18;
this.moveSpeed = 6;
this.jumpSpeed = 22;
this.xDir = 0;
this.yDir = 0;
this.faceDir = faceDir;
this.lookDir = this.faceDir;
this.width = 40;
this.height = 65;
this.rope = [];
this.color = color;
this.jumping = false;
this.grappling = false;
this.airborne = false;
}
var playerOne = new Player(200, 250, "red", 1);
var playerTwo = new Player(700, 250, "blue", 3);
function gameCycle() { //the function that actually runs the game
drawBackground();
playerOne.update();
playerTwo.update();
drawMap();
}
function drawBackground() {
context.fillStyle = "black";
context.fillRect(0, 0, screenWidth, screenHeight);
}
function drawMap() {
context.fillStyle = "white";
for (var i = 0; i < map.length; i++) {
context.fillRect(map[i][0][0], map[i][0][1], map[i][1][0] - map[i][0][0], map[i][1][1] - map[i][0][1]);
}
}
function castRay(x, y, angle) { //cast ray and return the first place it hits
while (true) {
for (var i = 0; i < map.length; i++) {
if (x > map[i][0][0] && x < map[i][1][0] && y > map[i][0][1] && y < map[i][1][1]) {
return [Math.floor(x), Math.floor(y)];
}
}
x += Math.cos(angle);
y += Math.sin(angle);
}
}
function distance(x1, y1, x2, y2) { //return the distance between two points
return Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
}
Player.prototype.update = function ()...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.