ForeverDev

Untitled

Oct 14th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <head> <script src = "https://code.jquery.com/jquery-2.1.1.min.js"></script> </head> <body> </body>
  3.     <script>
  4.         var count = 0
  5.         var game = {
  6.             players : {},
  7.             addPlayer : function(properties) {
  8.                 this.properties = properties;
  9.                 if (!game.players[this.properties["name"]]) {
  10.                     game.players[this.properties["name"]] = this.properties
  11.                     $("body").append("<div id = '" + this.properties["id"] + "'></div>")
  12.                     $("#" + this.properties["id"]).css(
  13.                         {
  14.                             "background-color" : "black",
  15.                             "width" : "50px",
  16.                             "height" : "50px",
  17.                             "position" : "absolute",
  18.                             "left" : (this.properties["x"] || 0),
  19.                             "top" : (this.properties["y"] || 0)
  20.                         }
  21.                     )
  22.                 }
  23.             },
  24.             create : function(object) {
  25.                 count++
  26.                 if (object["class"] == "player") {
  27.                     game.addPlayer({
  28.                         name : object["name"],
  29.                         speed : object["speed"],
  30.                         x : object["x"],
  31.                         y : object["y"],
  32.                         id : count,
  33.                     })
  34.                 } else if (object["class"] == "gameObject") {
  35.  
  36.                 }
  37.             },
  38.             move : function(name, keyPressed) {
  39.                 var player = game.players["me"]
  40.                 var vertical = (keyPressed == 119 ? -player["speed"] : keyPressed == 115 ? player["speed"] : 0)
  41.                 var horizontal = (keyPressed == 97 ? -player["speed"] : keyPressed == 100 ? player["speed"] : 0)
  42.                 $("#" + player["id"]).animate({
  43.                     top: ("+=" + vertical),
  44.                     left: ("+=" + horizontal)
  45.                 }, 25)
  46.             }
  47.         }
  48.         game.create({
  49.             "class" : "player",
  50.             "name" : "me",
  51.             "speed" : 10,
  52.             "x" : 50,
  53.             "y" : 100
  54.         })
  55.         $(document).keypress(function(key) {
  56.             game.move("me", key.keyCode)
  57.         })
  58.     </script>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment