Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head> <script src = "https://code.jquery.com/jquery-2.1.1.min.js"></script> </head> <body> </body>
- <script>
- var count = 0
- var game = {
- players : {},
- addPlayer : function(properties) {
- this.properties = properties;
- if (!game.players[this.properties["name"]]) {
- game.players[this.properties["name"]] = this.properties
- $("body").append("<div id = '" + this.properties["id"] + "'></div>")
- $("#" + this.properties["id"]).css(
- {
- "background-color" : "black",
- "width" : "50px",
- "height" : "50px",
- "position" : "absolute",
- "left" : (this.properties["x"] || 0),
- "top" : (this.properties["y"] || 0)
- }
- )
- }
- },
- create : function(object) {
- count++
- if (object["class"] == "player") {
- game.addPlayer({
- name : object["name"],
- speed : object["speed"],
- x : object["x"],
- y : object["y"],
- id : count,
- })
- } else if (object["class"] == "gameObject") {
- }
- },
- move : function(name, keyPressed) {
- var player = game.players["me"]
- var vertical = (keyPressed == 119 ? -player["speed"] : keyPressed == 115 ? player["speed"] : 0)
- var horizontal = (keyPressed == 97 ? -player["speed"] : keyPressed == 100 ? player["speed"] : 0)
- $("#" + player["id"]).animate({
- top: ("+=" + vertical),
- left: ("+=" + horizontal)
- }, 25)
- }
- }
- game.create({
- "class" : "player",
- "name" : "me",
- "speed" : 10,
- "x" : 50,
- "y" : 100
- })
- $(document).keypress(function(key) {
- game.move("me", key.keyCode)
- })
- </script>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment