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.

96 lines
2.3 KiB

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 bullets = {};
  35. socket.on("connected", function(data)
  36. {
  37. players = data.player;
  38. bullets = data.bullet;
  39. });
  40. socket.on("update",function(data)
  41. {
  42. });
  43. function doKeyUp(e)
  44. {
  45. socket.emit("keyDown",{"direction":e.keyCode});
  46. }
  47. function doKeyDown(e)
  48. {
  49. socket.emit("keyUp",{"direction":e.keyCode});
  50. }
  51. function draw()
  52. {
  53. context.fillStyle = "#000000";
  54. context.fillRect(0, 0, width, height);
  55. for(var i = 0; i< bullets.length; i++)
  56. {
  57. if(bullets[i] != -1)
  58. {
  59. context.fillStyle = "rgba(255, 0, 199, 1)"; //pink
  60. context.fillRect(bullets[i].x, bullets[i].y, 10,10);
  61. }
  62. }
  63. for(var i = 0; i< players.length; i++)
  64. {
  65. if(players[i] != -1)
  66. {
  67. context.fillStyle = "rgba(0, 45, 160, 1)"; //blue
  68. context.fillRect(players[i].x, players[i].y, 25,25);
  69. }
  70. }
  71. }
  72. setInterval(function ()
  73. {
  74. draw();
  75. }, 1000/60);
  76. });
  77. </script>
  78. </body>
  79. </html>