Multiplayer Java Script game
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <html>
  2. <head>
  3. <title>Block Battle Beta</title>
  4. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  5. <style type="text/css">
  6. canvas{
  7. border: 1px solid black;
  8. }
  9. .row {
  10. margin: 0; padding: 0;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div class="row">
  16. <div class="col-md-6">
  17. <canvas width="700" height="700" ID="mycanvas" tabindex="0" >Canvas tag not supported</canvas>
  18. </div>
  19. </div>
  20. <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  21. <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
  22. <script>
  23. $(document).ready(function()
  24. {
  25. var socket = io('http://localhost:3000');
  26. io.connect();
  27. socket.emit('register', "Jrtechs");
  28. var canvas = document.getElementById("mycanvas");
  29. canvas.addEventListener('keydown', doKeyDown, true);
  30. canvas.addEventListener('keyup', doKeyUp, true);
  31. var context = canvas.getContext("2d");
  32. var width = 700;
  33. var height = 700;
  34. var players = {};
  35. var blocks = new Array();
  36. var name = "Jrtechs";
  37. socket.on("connected", function(data)
  38. {
  39. console.log("got message connected");
  40. console.log(data);
  41. players = data.players;
  42. blocks = data.blocks;
  43. });
  44. socket.on("updateClient",function(data)
  45. {
  46. console.log(data);
  47. players[data.name] = data.player;
  48. });
  49. function doKeyUp(e)
  50. {
  51. socket.emit("keyDown",{"direction":e.keyCode});
  52. }
  53. function doKeyDown(e)
  54. {
  55. console.log("key down");
  56. socket.emit("keyUp",{"direction":e.keyCode});
  57. }
  58. function draw()
  59. {
  60. context.fillStyle = "#000000";
  61. context.fillRect(0, 0, width, height);
  62. console.log(blocks);
  63. for(var i = 0; i< blocks.length; i++)
  64. {
  65. if(blocks[i] != -1)
  66. {
  67. context.fillStyle = "rgba(255, 0, 199, 1)"; //pink
  68. context.fillRect(blocks[i].x, blocks[i].y, 10,10);
  69. }
  70. }
  71. Object.keys(players).forEach(function(key)
  72. {
  73. context.fillStyle = "rgba(0, 45, 160, 1)"; //blue
  74. context.fillRect(players[key].x, players[key].y, 25,25);
  75. });
  76. }
  77. var speed = 5;
  78. setInterval(function ()
  79. {
  80. Object.keys(players).forEach(function(key)
  81. {
  82. console.log(key);
  83. if(players[key].upB)
  84. {
  85. players[key].y -= speed;
  86. }
  87. else if(players[key].downB)
  88. {
  89. players[key].y += speed;
  90. }
  91. else if(players[key].leftB)
  92. {
  93. players[key].x -= speed;
  94. }
  95. else if(players[key].rightB)
  96. {
  97. players[key].x += speed;
  98. }
  99. });
  100. draw();
  101. }, 1000/60);
  102. });
  103. </script>
  104. </body>
  105. </html>