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.

99 lines
2.4 KiB

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. var canvas = document.getElementById("mycanvas");
  28. canvas.addEventListener('keydown', doKeyDown, true);
  29. canvas.addEventListener('keyup', doKeyUp, true);
  30. var context = canvas.getContext("2d");
  31. var width = 700;
  32. var height = 700;
  33. var players = {};
  34. var blocks = {};
  35. socket.on("connected", function(data)
  36. {
  37. console.log("got message connected");
  38. console.log(data);
  39. players = data.players;
  40. blocks = data.blocks;
  41. });
  42. socket.on("update",function(data)
  43. {
  44. });
  45. function doKeyUp(e)
  46. {
  47. socket.emit("keyDown",{"direction":e.keyCode});
  48. }
  49. function doKeyDown(e)
  50. {
  51. console.log("key down");
  52. socket.emit("keyUp",{"direction":e.keyCode});
  53. }
  54. function draw()
  55. {
  56. context.fillStyle = "#000000";
  57. context.fillRect(0, 0, width, height);
  58. for(var i = 0; i< blocks.length; i++)
  59. {
  60. if(bullets[i] != -1)
  61. {
  62. context.fillStyle = "rgba(255, 0, 199, 1)"; //pink
  63. context.fillRect(blocks[i].x, blocks[i].y, 10,10);
  64. }
  65. }
  66. for(var i = 0; i< players.length; i++)
  67. {
  68. if(players[i] != -1)
  69. {
  70. context.fillStyle = "rgba(0, 45, 160, 1)"; //blue
  71. context.fillRect(players[i].x, players[i].y, 25,25);
  72. }
  73. }
  74. }
  75. setInterval(function ()
  76. {
  77. draw();
  78. }, 1000/60);
  79. });
  80. </script>
  81. </body>
  82. </html>