Created
August 24, 2012 13:18
-
-
Save assertchris/3450452 to your computer and use it in GitHub Desktop.
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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <title>Invaders</title> | |
| <style type="text/css"> | |
| body, html { | |
| width: 100%; | |
| height: 100%; | |
| overflow: hidden; | |
| } | |
| .canvas { | |
| background: #000; | |
| width: 400px; | |
| height: 300px; | |
| position: absolute; | |
| top: 50%; | |
| left: 50%; | |
| margin: -150px 0 0 -200px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="canvas"></div> | |
| <script src="http://ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js"></script> | |
| <script type="text/javascript"> | |
| window.addEvent("domready", function(){ | |
| var Craft = new Class({ | |
| "x": 0, | |
| "y": 0, | |
| "width": 0, | |
| "height": 0, | |
| "dx": 0, | |
| "dy": 0, | |
| "element": null, | |
| "missiles": [], | |
| "initialize": function(x, y, width, height) { | |
| this.x = x; | |
| this.y = y; | |
| this.width = width; | |
| this.height = height; | |
| this.setupEvents(); | |
| this.setupElement(); | |
| }, | |
| "move": function() { | |
| this.x += this.dx; | |
| this.y += this.dy; | |
| if (this.x < 0) { | |
| this.x = 0; | |
| } | |
| if (this.x > this.width - 20) { | |
| this.x = this.width - 20; | |
| } | |
| if (this.y < 0) { | |
| this.y = 0; | |
| } | |
| if (this.y > this.height - 20) { | |
| this.y = this.height - 20; | |
| } | |
| this.element.setStyles({ | |
| "top": this.y, | |
| "left": this.x | |
| }); | |
| }, | |
| "fire": function() {}, | |
| "setupEvents": function() { | |
| var self = this; | |
| window.addEvents({ | |
| "keydown": function(e) { | |
| if (e.key == "space") { | |
| self.fire(); | |
| } | |
| if (e.key == "left") { | |
| self.dx = -1; | |
| } | |
| if (e.key == "right") { | |
| self.dx = 1; | |
| } | |
| if (e.key == "up") { | |
| self.dy = -1; | |
| } | |
| if (e.key == "down") { | |
| self.dy = 1; | |
| } | |
| }, | |
| "keyup": function(e) { | |
| if (e.key == "left" || e.key == "right") { | |
| self.dx = 0; | |
| } | |
| if (e.key == "up" || e.key == "down") { | |
| self.dy = 0; | |
| } | |
| } | |
| }); | |
| }, | |
| "setupElement": function(){ | |
| var self = this; | |
| this.element = new Element("img", { | |
| "src": "craft.png", | |
| "styles": { | |
| "top": self.y, | |
| "left": self.x, | |
| "position": "absolute" | |
| } | |
| }); | |
| }, | |
| "toElement": function(){ | |
| return this.element; | |
| } | |
| }); | |
| var canvas = document.getElement(".canvas"); | |
| var craft = new Craft(100, 100, 400, 300); | |
| canvas.adopt(craft); | |
| setInterval(function(){ | |
| craft.move(); | |
| }, 5); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment