view on requirebin
Created
July 18, 2013 17:54
-
-
Save sethvincent/6031438 to your computer and use it in GitHub Desktop.
requirebin sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var inherits = require('inherits'); | |
var Game = require('crtrdg-gameloop'); | |
var Entity = require('crtrdg-entity'); | |
var Keyboard = require('crtrdg-keyboard'); | |
var canvas = document.createElement('canvas'); | |
canvas.id = 'game'; | |
var body = document.getElementsByTagName('body')[0]; | |
body.appendChild(canvas); | |
inherits(Player, Entity); | |
function Player(options){ | |
this.position = { | |
x: options.position.x, | |
y: options.position.y | |
}; | |
this.size = { | |
x: options.size.x, | |
y: options.size.y | |
}; | |
this.velocity = { | |
x: options.velocity.x, | |
y: options.velocity.y | |
}; | |
this.speed = options.speed; | |
this.friction = options.friction; | |
this.color = options.color; | |
} | |
Player.prototype.move = function(velocity){ | |
this.position.x += velocity.x; | |
this.position.y += velocity.y; | |
}; | |
Player.prototype.checkBoundaries = function(){ | |
if (this.position.x <= 0){ | |
this.position.x = 0; | |
} | |
if (this.position.x >= this.game.width - this.size.x){ | |
this.position.x = this.game.width - this.size.x; | |
} | |
if (this.position.y <= 0){ | |
this.position.y = 0; | |
} | |
if (this.position.y >= this.game.height - this.size.y){ | |
this.position.y = this.game.height - this.size.y; | |
} | |
}; | |
Player.prototype.keyboardInput = function(){ | |
if ('A' in keyboard.keysDown){ | |
this.velocity.x = -this.speed; | |
} | |
if ('D' in keyboard.keysDown){ | |
this.velocity.x = this.speed; | |
} | |
if ('W' in keyboard.keysDown){ | |
this.velocity.y = -this.speed; | |
} | |
if ('S' in keyboard.keysDown){ | |
this.velocity.y = this.speed; | |
} | |
}; | |
var game = new Game({ | |
canvasId: 'game', | |
width: window.innerWidth, | |
height: window.innerHeight, | |
backgroundColor: '#E187B8' | |
}); | |
var keyboard = new Keyboard(game); | |
var player = new Player({ | |
position: { x: 10, y: 10 }, | |
size: { x: 10, y: 10 }, | |
velocity: { x: 0, y: 0 }, | |
speed: 3, | |
friction: 0.9, | |
color: '#fff' | |
}); | |
player.addTo(game); | |
player.on('update', function(interval){ | |
this.keyboardInput(keyboard); | |
this.move(this.velocity); | |
this.velocity.x *= this.friction; | |
this.velocity.y *= this.friction; | |
this.checkBoundaries(); | |
}); | |
player.on('draw', function(draw){ | |
draw.fillStyle = this.color; | |
draw.fillRect(this.position.x, this.position.y, this.size.x, this.size.y); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Player(e){this.position={x:e.position.x,y:e.position.y},this.size={x:e.size.x,y:e.size.y},this.velocity={x:e.velocity.x,y:e.velocity.y},this.speed=e.speed,this.friction=e.friction,this.color=e.color}require=function(e,t,n){function r(n,s){if(!t[n]){if(!e[n]){var o="function"==typeof require&&require;if(!s&&o)return o(n,!0);if(i)return i(n,!0);throw Error("Cannot find module '"+n+"'")}var a=t[n]={exports:{}};e[n][0].call(a.exports,function(t){var i=e[n][1][t];return r(i?i:t)},a,a.exports)}return t[n].exports}for(var i="function"==typeof require&&require,s=0;n.length>s;s++)r(n[s]);return r}({1:[function(e,t){var n=t.exports={};n.nextTick=function(){var e="undefined"!=typeof window&&window.setImmediate,t="undefined"!=typeof window&&window.postMessage&&window.addEventListener;if(e)return function(e){return window.setImmediate(e)};if(t){var n=[];return window.addEventListener("message",function(e){if(e.source===window&&"process-tick"===e.data&&(e.stopPropagation(),n.length>0)){var t=n.shift();t()}},!0),function(e){n.push(e),window.postMessage("process-tick","*")}}return function(e){setTimeout(e,0)}}(),n.title="browser",n.browser=!0,n.env={},n.argv=[],n.binding=function(){throw Error("process.binding is not supported")},n.cwd=function(){return"/"},n.chdir=function(){throw Error("process.chdir is not supported")}},{}],2:[function(e,t,n){(function(e){function t(e,t){if(e.indexOf)return e.indexOf(t);for(var n=0;e.length>n;n++)if(t===e[n])return n;return-1}e.EventEmitter||(e.EventEmitter=function(){});var r=n.EventEmitter=e.EventEmitter,i="function"==typeof Array.isArray?Array.isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)},s=10;r.prototype.setMaxListeners=function(e){this._events||(this._events={}),this._events.maxListeners=e},r.prototype.emit=function(e){if("error"===e&&(!this._events||!this._events.error||i(this._events.error)&&!this._events.error.length))throw arguments[1]instanceof Error?arguments[1]:Error("Uncaught, unspecified 'error' event.");if(!this._events)return!1;var t=this._events[e];if(!t)return!1;if("function"==typeof t){switch(arguments.length){case 1:t.call(this);break;case 2:t.call(this,arguments[1]);break;case 3:t.call(this,arguments[1],arguments[2]);break;default:var n=Array.prototype.slice.call(arguments,1);t.apply(this,n)}return!0}if(i(t)){for(var n=Array.prototype.slice.call(arguments,1),r=t.slice(),s=0,o=r.length;o>s;s++)r[s].apply(this,n);return!0}return!1},r.prototype.addListener=function(e,t){if("function"!=typeof t)throw Error("addListener only takes instances of Function");if(this._events||(this._events={}),this.emit("newListener",e,t),this._events[e])if(i(this._events[e])){if(!this._events[e].warned){var n;n=void 0!==this._events.maxListeners?this._events.maxListeners:s,n&&n>0&&this._events[e].length>n&&(this._events[e].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[e].length),console.trace())}this._events[e].push(t)}else this._events[e]=[this._events[e],t];else this._events[e]=t;return this},r.prototype.on=r.prototype.addListener,r.prototype.once=function(e,t){var n=this;return n.on(e,function r(){n.removeListener(e,r),t.apply(this,arguments)}),this},r.prototype.removeListener=function(e,n){if("function"!=typeof n)throw Error("removeListener only takes instances of Function");if(!this._events||!this._events[e])return this;var r=this._events[e];if(i(r)){var s=t(r,n);if(0>s)return this;r.splice(s,1),0==r.length&&delete this._events[e]}else this._events[e]===n&&delete this._events[e];return this},r.prototype.removeAllListeners=function(e){return 0===arguments.length?(this._events={},this):(e&&this._events&&this._events[e]&&(this._events[e]=null),this)},r.prototype.listeners=function(e){return this._events||(this._events={}),this._events[e]||(this._events[e]=[]),i(this._events[e])||(this._events[e]=[this._events[e]]),this._events[e]}})(e("__browserify_process"))},{__browserify_process:1}],"crtrdg-gameloop":[function(e,t){t.exports=e("tRnzk7")},{}],tRnzk7:[function(e,t){function n(e){this.canvas=document.getElementById(e.canvasId),this.context=this.canvas.getContext("2d"),this.width=this.canvas.width=e.width,this.height=this.canvas.height=e.height,this.backgroundColor=e.backgroundColor,e.maxListeners?this.setMaxListeners(e.maxListeners):this.setMaxListeners(0),this.loop()}var r=e("events").EventEmitter,i=e("raf"),s=e("inherits");t.exports=n,s(n,r),n.prototype.loop=function(){var e=this;this.ticker=i(this.canvas),this.ticker.on("data",function(t){e.update(t),e.draw()})},n.prototype.pause=function(){this.ticker.pause(),this.emit("pause")},n.prototype.resume=function(){var e=this;this.ticker=i(this.canvas),this.ticker.on("data",function(t){e.update(t),e.draw()}),this.emit("resume")},n.prototype.update=function(e){this.emit("update",e)},n.prototype.draw=function(){this.context.fillStyle=this.backgroundColor,this.context.fillRect(0,0,this.width,this.height),this.emit("draw",this.context)}},{events:2,raf:3,inherits:4}],4:[function(e,t){t.exports="function"==typeof Object.create?function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})}:function(e,t){e.super_=t;var n=function(){};n.prototype=t.prototype,e.prototype=new n,e.prototype.constructor=e}},{}],3:[function(e,t){(function(){function n(e){function t(){var r=n.now(),a=r-i;i=r,s.emit("data",a),s.paused||o(t,e)}var i=n.now(),s=new r;return s.pause=function(){s.paused=!0},s.resume=function(){s.paused=!1},o(t,e),s}t.exports=n;var r=e("events").EventEmitter,i="undefined"==typeof window?this:window,s=i.performance&&i.performance.now?function(){return performance.now()}:Date.now||function(){return+new Date},o=i.requestAnimationFrame||i.webkitRequestAnimationFrame||i.mozRequestAnimationFrame||i.msRequestAnimationFrame||i.oRequestAnimationFrame||(i.setImmediate?function(e){setImmediate(e)}:function(e){setTimeout(e,0)});n.polyfill=o,n.now=s})()},{events:2}]},{},[]),require=function(e,t,n){function r(n,s){if(!t[n]){if(!e[n]){var o="function"==typeof require&&require;if(!s&&o)return o(n,!0);if(i)return i(n,!0);throw Error("Cannot find module '"+n+"'")}var a=t[n]={exports:{}};e[n][0].call(a.exports,function(t){var i=e[n][1][t];return r(i?i:t)},a,a.exports)}return t[n].exports}for(var i="function"==typeof require&&require,s=0;n.length>s;s++)r(n[s]);return r}({inherits:[function(e,t){t.exports=e("n6WjOE")},{}],n6WjOE:[function(e,t){t.exports="function"==typeof Object.create?function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})}:function(e,t){e.super_=t;var n=function(){};n.prototype=t.prototype,e.prototype=new n,e.prototype.constructor=e}},{}]},{},[]),require=function(e,t,n){function r(n,s){if(!t[n]){if(!e[n]){var o="function"==typeof require&&require;if(!s&&o)return o(n,!0);if(i)return i(n,!0);throw Error("Cannot find module '"+n+"'")}var a=t[n]={exports:{}};e[n][0].call(a.exports,function(t){var i=e[n][1][t];return r(i?i:t)},a,a.exports)}return t[n].exports}for(var i="function"==typeof require&&require,s=0;n.length>s;s++)r(n[s]);return r}({1:[function(e,t){var n=t.exports={};n.nextTick=function(){var e="undefined"!=typeof window&&window.setImmediate,t="undefined"!=typeof window&&window.postMessage&&window.addEventListener;if(e)return function(e){return window.setImmediate(e)};if(t){var n=[];return window.addEventListener("message",function(e){if(e.source===window&&"process-tick"===e.data&&(e.stopPropagation(),n.length>0)){var t=n.shift();t()}},!0),function(e){n.push(e),window.postMessage("process-tick","*")}}return function(e){setTimeout(e,0)}}(),n.title="browser",n.browser=!0,n.env={},n.argv=[],n.binding=function(){throw Error("process.binding is not supported")},n.cwd=function(){return"/"},n.chdir=function(){throw Error("process.chdir is not supported")}},{}],2:[function(e,t,n){(function(e){function t(e,t){if(e.indexOf)return e.indexOf(t);for(var n=0;e.length>n;n++)if(t===e[n])return n;return-1}e.EventEmitter||(e.EventEmitter=function(){});var r=n.EventEmitter=e.EventEmitter,i="function"==typeof Array.isArray?Array.isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)},s=10;r.prototype.setMaxListeners=function(e){this._events||(this._events={}),this._events.maxListeners=e},r.prototype.emit=function(e){if("error"===e&&(!this._events||!this._events.error||i(this._events.error)&&!this._events.error.length))throw arguments[1]instanceof Error?arguments[1]:Error("Uncaught, unspecified 'error' event.");if(!this._events)return!1;var t=this._events[e];if(!t)return!1;if("function"==typeof t){switch(arguments.length){case 1:t.call(this);break;case 2:t.call(this,arguments[1]);break;case 3:t.call(this,arguments[1],arguments[2]);break;default:var n=Array.prototype.slice.call(arguments,1);t.apply(this,n)}return!0}if(i(t)){for(var n=Array.prototype.slice.call(arguments,1),r=t.slice(),s=0,o=r.length;o>s;s++)r[s].apply(this,n);return!0}return!1},r.prototype.addListener=function(e,t){if("function"!=typeof t)throw Error("addListener only takes instances of Function");if(this._events||(this._events={}),this.emit("newListener",e,t),this._events[e])if(i(this._events[e])){if(!this._events[e].warned){var n;n=void 0!==this._events.maxListeners?this._events.maxListeners:s,n&&n>0&&this._events[e].length>n&&(this._events[e].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[e].length),console.trace())}this._events[e].push(t)}else this._events[e]=[this._events[e],t];else this._events[e]=t;return this},r.prototype.on=r.prototype.addListener,r.prototype.once=function(e,t){var n=this;return n.on(e,function r(){n.removeListener(e,r),t.apply(this,arguments)}),this},r.prototype.removeListener=function(e,n){if("function"!=typeof n)throw Error("removeListener only takes instances of Function");if(!this._events||!this._events[e])return this;var r=this._events[e];if(i(r)){var s=t(r,n);if(0>s)return this;r.splice(s,1),0==r.length&&delete this._events[e]}else this._events[e]===n&&delete this._events[e];return this},r.prototype.removeAllListeners=function(e){return 0===arguments.length?(this._events={},this):(e&&this._events&&this._events[e]&&(this._events[e]=null),this)},r.prototype.listeners=function(e){return this._events||(this._events={}),this._events[e]||(this._events[e]=[]),i(this._events[e])||(this._events[e]=[this._events[e]]),this._events[e]}})(e("__browserify_process"))},{__browserify_process:1}],"crtrdg-keyboard":[function(e,t){t.exports=e("BKDGmi")},{}],BKDGmi:[function(e,t){function n(e){this.game=e||{},this.keysDown={},this.initializeListeners()}var r=e("events").EventEmitter,i=e("inherits"),s=e("vkey");t.exports=n,i(n,r),n.prototype.initializeListeners=function(){var e=this;document.addEventListener("keydown",function(t){e.emit("keydown",s[t.keyCode]),e.keysDown[s[t.keyCode]]=!0,(40===t.keyCode||38===t.keyCode||37===t.keyCode||39===t.keyCode||32===t.keyCode)&&t.preventDefault()},!1),document.addEventListener("keyup",function(t){e.emit("keyup",s[t.keyCode]),delete e.keysDown[s[t.keyCode]]},!1)}},{events:2,inherits:3,vkey:4}],3:[function(e,t){t.exports="function"==typeof Object.create?function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})}:function(e,t){e.super_=t;var n=function(){};n.prototype=t.prototype,e.prototype=new n,e.prototype.constructor=e}},{}],4:[function(e,t){(function(){var e,n="undefined"!=typeof window?window.navigator.userAgent:"",r=/OS X/.test(n),i=/Opera/.test(n),s=!/like Gecko/.test(n)&&!i,o=t.exports={0:r?"<menu>":"<UNK>",1:"<mouse 1>",2:"<mouse 2>",3:"<break>",4:"<mouse 3>",5:"<mouse 4>",6:"<mouse 5>",8:"<backspace>",9:"<tab>",12:"<clear>",13:"<enter>",16:"<shift>",17:"<control>",18:"<alt>",19:"<pause>",20:"<caps-lock>",21:"<ime-hangul>",23:"<ime-junja>",24:"<ime-final>",25:"<ime-kanji>",27:"<escape>",28:"<ime-convert>",29:"<ime-nonconvert>",30:"<ime-accept>",31:"<ime-mode-change>",27:"<escape>",32:"<space>",33:"<page-up>",34:"<page-down>",35:"<end>",36:"<home>",37:"<left>",38:"<up>",39:"<right>",40:"<down>",41:"<select>",42:"<print>",43:"<execute>",44:"<snapshot>",45:"<insert>",46:"<delete>",47:"<help>",91:"<meta>",92:"<meta>",93:r?"<meta>":"<menu>",95:"<sleep>",106:"<num-*>",107:"<num-+>",108:"<num-enter>",109:"<num-->",110:"<num-.>",111:"<num-/>",144:"<num-lock>",145:"<scroll-lock>",160:"<shift-left>",161:"<shift-right>",162:"<control-left>",163:"<control-right>",164:"<alt-left>",165:"<alt-right>",166:"<browser-back>",167:"<browser-forward>",168:"<browser-refresh>",169:"<browser-stop>",170:"<browser-search>",171:"<browser-favorites>",172:"<browser-home>",173:r&&s?"-":"<volume-mute>",174:"<volume-down>",175:"<volume-up>",176:"<next-track>",177:"<prev-track>",178:"<stop>",179:"<play-pause>",180:"<launch-mail>",181:"<launch-media-select>",182:"<launch-app 1>",183:"<launch-app 2>",186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`",219:"[",220:"\\",221:"]",222:"'",223:"<meta>",224:"<meta>",226:"<alt-gr>",229:"<ime-process>",231:i?"`":"<unicode>",246:"<attention>",247:"<crsel>",248:"<exsel>",249:"<erase-eof>",250:"<play>",251:"<zoom>",252:"<no-name>",253:"<pa-1>",254:"<clear>"};for(e=58;65>e;++e)o[e]=String.fromCharCode(e);for(e=48;58>e;++e)o[e]=e-48+"";for(e=65;91>e;++e)o[e]=String.fromCharCode(e);for(e=96;107>e;++e)o[e]="<num-"+(e-96)+">";for(e=112;136>e;++e)o[e]="F"+(e-111)})()},{}]},{},[]),require=function(e,t,n){function r(n,s){if(!t[n]){if(!e[n]){var o="function"==typeof require&&require;if(!s&&o)return o(n,!0);if(i)return i(n,!0);throw Error("Cannot find module '"+n+"'")}var a=t[n]={exports:{}};e[n][0].call(a.exports,function(t){var i=e[n][1][t];return r(i?i:t)},a,a.exports)}return t[n].exports}for(var i="function"==typeof require&&require,s=0;n.length>s;s++)r(n[s]);return r}({1:[function(e,t){var n=t.exports={};n.nextTick=function(){var e="undefined"!=typeof window&&window.setImmediate,t="undefined"!=typeof window&&window.postMessage&&window.addEventListener;if(e)return function(e){return window.setImmediate(e)};if(t){var n=[];return window.addEventListener("message",function(e){if(e.source===window&&"process-tick"===e.data&&(e.stopPropagation(),n.length>0)){var t=n.shift();t()}},!0),function(e){n.push(e),window.postMessage("process-tick","*")}}return function(e){setTimeout(e,0)}}(),n.title="browser",n.browser=!0,n.env={},n.argv=[],n.binding=function(){throw Error("process.binding is not supported")},n.cwd=function(){return"/"},n.chdir=function(){throw Error("process.chdir is not supported")}},{}],2:[function(e,t,n){(function(e){function t(e,t){if(e.indexOf)return e.indexOf(t);for(var n=0;e.length>n;n++)if(t===e[n])return n;return-1}e.EventEmitter||(e.EventEmitter=function(){});var r=n.EventEmitter=e.EventEmitter,i="function"==typeof Array.isArray?Array.isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)},s=10;r.prototype.setMaxListeners=function(e){this._events||(this._events={}),this._events.maxListeners=e},r.prototype.emit=function(e){if("error"===e&&(!this._events||!this._events.error||i(this._events.error)&&!this._events.error.length))throw arguments[1]instanceof Error?arguments[1]:Error("Uncaught, unspecified 'error' event.");if(!this._events)return!1;var t=this._events[e];if(!t)return!1;if("function"==typeof t){switch(arguments.length){case 1:t.call(this);break;case 2:t.call(this,arguments[1]);break;case 3:t.call(this,arguments[1],arguments[2]);break;default:var n=Array.prototype.slice.call(arguments,1);t.apply(this,n)}return!0}if(i(t)){for(var n=Array.prototype.slice.call(arguments,1),r=t.slice(),s=0,o=r.length;o>s;s++)r[s].apply(this,n);return!0}return!1},r.prototype.addListener=function(e,t){if("function"!=typeof t)throw Error("addListener only takes instances of Function");if(this._events||(this._events={}),this.emit("newListener",e,t),this._events[e])if(i(this._events[e])){if(!this._events[e].warned){var n;n=void 0!==this._events.maxListeners?this._events.maxListeners:s,n&&n>0&&this._events[e].length>n&&(this._events[e].warned=!0,console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",this._events[e].length),console.trace())}this._events[e].push(t)}else this._events[e]=[this._events[e],t];else this._events[e]=t;return this},r.prototype.on=r.prototype.addListener,r.prototype.once=function(e,t){var n=this;return n.on(e,function r(){n.removeListener(e,r),t.apply(this,arguments)}),this},r.prototype.removeListener=function(e,n){if("function"!=typeof n)throw Error("removeListener only takes instances of Function");if(!this._events||!this._events[e])return this;var r=this._events[e];if(i(r)){var s=t(r,n);if(0>s)return this;r.splice(s,1),0==r.length&&delete this._events[e]}else this._events[e]===n&&delete this._events[e];return this},r.prototype.removeAllListeners=function(e){return 0===arguments.length?(this._events={},this):(e&&this._events&&this._events[e]&&(this._events[e]=null),this)},r.prototype.listeners=function(e){return this._events||(this._events={}),this._events[e]||(this._events[e]=[]),i(this._events[e])||(this._events[e]=[this._events[e]]),this._events[e]}})(e("__browserify_process"))},{__browserify_process:1}],"crtrdg-entity":[function(e,t){t.exports=e("davBSO")},{}],davBSO:[function(e,t){function n(){return this}var r=e("events").EventEmitter,i=e("inherits");t.exports=n,i(n,r),n.prototype.addTo=function(e,t){return this.game=e||{},this.game.entities||(this.game.entities=[]),this.game.entities.push(this),this.game.findEntity=this.findEntity,this.initializeListeners(),this.exists=!0,t&&t(this),this},n.prototype.initializeListeners=function(){var e=this;this.findEntity(this,function(t){t&&(e.game.on("update",function(t){e.emit("update",t)}),e.game.on("draw",function(t){e.emit("draw",t)}))})},n.prototype.remove=function(){this.exists=!1,this.removeAllListeners("update"),this.removeAllListeners("draw"),this.findEntity(this,function(e,t,n){e&&t.splice(n,1)})},n.prototype.findEntity=function(e,t){var n;n=void 0===this.game?this.entities:this.game.entities;for(var r=0;n.length>r;r++)n[r]===e&&t(!0,n,r)}},{events:2,inherits:3}],3:[function(e,t){t.exports="function"==typeof Object.create?function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})}:function(e,t){e.super_=t;var n=function(){};n.prototype=t.prototype,e.prototype=new n,e.prototype.constructor=e}},{}]},{},[]);var inherits=require("inherits"),Game=require("crtrdg-gameloop"),Entity=require("crtrdg-entity"),Keyboard=require("crtrdg-keyboard"),canvas=document.createElement("canvas");canvas.id="game";var body=document.getElementsByTagName("body")[0];body.appendChild(canvas),inherits(Player,Entity),Player.prototype.move=function(e){this.position.x+=e.x,this.position.y+=e.y},Player.prototype.checkBoundaries=function(){0>=this.position.x&&(this.position.x=0),this.position.x>=this.game.width-this.size.x&&(this.position.x=this.game.width-this.size.x),0>=this.position.y&&(this.position.y=0),this.position.y>=this.game.height-this.size.y&&(this.position.y=this.game.height-this.size.y)},Player.prototype.keyboardInput=function(){"A"in keyboard.keysDown&&(this.velocity.x=-this.speed),"D"in keyboard.keysDown&&(this.velocity.x=this.speed),"W"in keyboard.keysDown&&(this.velocity.y=-this.speed),"S"in keyboard.keysDown&&(this.velocity.y=this.speed)};var game=new Game({canvasId:"game",width:window.innerWidth,height:window.innerHeight,backgroundColor:"#E187B8"}),keyboard=new Keyboard(game),player=new Player({position:{x:10,y:10},size:{x:10,y:10},velocity:{x:0,y:0},speed:3,friction:.9,color:"#fff"});player.addTo(game),player.on("update",function(){this.keyboardInput(keyboard),this.move(this.velocity),this.velocity.x*=this.friction,this.velocity.y*=this.friction,this.checkBoundaries()}),player.on("draw",function(e){e.fillStyle=this.color,e.fillRect(this.position.x,this.position.y,this.size.x,this.size.y)}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; } | |
body, html { height: 100%; width: 100%; }</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment