Guest User

Brocken

a guest
Mar 3rd, 2019
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. //server.js
  2. var path = require("path");
  3. var express = require('express');
  4. var app = express();
  5. var http = require('http').Server(app);
  6. var io = require('socket.io')(http);
  7.  
  8. class Player {
  9. constructor(x, y, skin) {
  10. self.x = x;
  11. self.y = y;
  12. self.skin = skin;
  13. }
  14. }
  15.  
  16. app.use(express.static('html'))
  17. app.get('/', function(req, res){
  18. res.sendFile(path.resolve(__dirname + '/html/index.html'));
  19. });
  20.  
  21. io.on('connection', function(socket){
  22. socket.on('pos', (player) => {
  23. io.emit('pos', player);
  24. console.log('Received player data with values : ' + player.x + ' for x ' + player.y + ' for y '+ player.skin + 'as a skin')
  25. });
  26. });
  27.  
  28. http.listen(3000, function(){
  29. console.log('listening on *:3000');
  30. });
  31.  
  32. //index.html
  33. <!DOCTYPE html>
  34. <html lang="en">
  35. <head>
  36. <meta charset="UTF-8">
  37. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  38. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  39.  
  40. <title>Oh</title>
  41. <link rel="shortcut icon" href="#" />
  42.  
  43. <script src="socket.io-1.2.0.js"></script>
  44. <script src="jquery.js"></script>
  45. <script src="main.js"></script>
  46. <link rel="stylesheet" href="style.css">
  47. </head>
  48. <body>
  49. <div id="gameContainer">
  50. <h1 id="text-over">Hello, and welcome to <br>Oh !</h1>
  51. <canvas id="gameCanvas" width="800" height="500"></canvas>
  52. </div>
  53. </body>
  54. </html>
  55.  
  56. //main.js
  57. const socket = io.connect('//localhost:3000');
  58.  
  59. class sun {
  60. constructor(suncolor) {
  61. self.suncolor = suncolor;
  62. }
  63.  
  64. draw(ctx) {
  65. ctx.fillStyle = self.suncolor;
  66. ctx.beginPath();
  67. ctx.arc(20, 0, 200, 0 * Math.PI, 1.5 * Math.PI);
  68. ctx.fill();
  69. };
  70. }
  71.  
  72. var fps = 60;
  73. var gameDiv;
  74. var gameCanvas;
  75. var canvasCtx;
  76.  
  77. var defaSun = new sun("#ffff55")
  78.  
  79. var shadow_height = 10;
  80. var shadow_width = 20;
  81. var playerwidth = 20;
  82. var playerheight = 20;
  83. var player = {
  84. name: "player1",
  85. x: 0,
  86. y : 0,
  87. show: true,
  88. ori: "down",
  89. skin: "def"
  90. }
  91.  
  92. window.onload = function() {
  93. gameDiv = document.getElementById("gameContainer");
  94. gameCanvas = document.getElementById("gameCanvas");
  95. canvasCtx = gameCanvas.getContext("2d");
  96.  
  97. player.x = gameCanvas.width/2 - 10;
  98. player.y = gameCanvas.height/2 - 10;
  99.  
  100. setupGameArea();
  101. }
  102.  
  103. function setupGameArea() {
  104. canvasCtx.clearRect(0, 0, gameCanvas.width, gameCanvas.height);
  105.  
  106. start();
  107. }
  108.  
  109. function update() {
  110. canvasCtx.clearRect(0, 0, gameCanvas.width, gameCanvas.height);
  111.  
  112. if (player.show) {
  113.  
  114. if (player.x > gameCanvas.width-player.width) {
  115. player.x = gameCanvas.width-player.width;
  116. }
  117.  
  118. if (player.x < 0) {
  119. player.x = 0;
  120. }
  121.  
  122. if (player.y > gameCanvas.height - player.height) {
  123. player.y = gameCanvas.height - player.height;
  124. }
  125.  
  126. if (player.y < 0) {
  127. player.y = 0;
  128. }
  129. /*if (player.show = true) {
  130. canvasCtx.fillStyle = "#7c5c5c"
  131. canvasCtx.fillRect(player.x, player.y, playerwidth, playerheight);
  132.  
  133. canvasCtx.fillStyle = "grey";
  134. canvasCtx.fillRect(player.x, player.y + 20, shadow_width, shadow_height);
  135. }*/
  136. socket.emit('pos', player);
  137.  
  138. socket.on('pos', (pos) => {
  139. if (player.show = true) {
  140. canvasCtx.fillStyle = "#7c5c5c";
  141. canvasCtx.fillRect(player.x, player.y, playerwidth, playerheight);
  142.  
  143. canvasCtx.fillStyle = "grey";
  144. canvasCtx.fillRect(player.x, player.y + playerheight, shadow_width, shadow_height);
  145. }
  146. })
  147.  
  148. defaSun.draw(canvasCtx);
  149. }
  150. }
  151.  
  152. function keyEvent(evt) {
  153. switch(evt.key) {
  154.  
  155. case "w":
  156. player.y -= 10;
  157. player.ori ="up"
  158. break;
  159.  
  160. case "s":
  161. player.y += 10;
  162. player.ori = "down"
  163. break;
  164.  
  165. case "a":
  166. player.x -= 10;
  167. player.ori = "left"
  168. break;
  169.  
  170. case "d":
  171. player.x += 10;
  172. player.ori = "right"
  173. break;
  174. }
  175. }
  176.  
  177. function start() {
  178. document.addEventListener("keypress", keyEvent);
  179.  
  180. window.setInterval(update, 1000/fps);
  181. }
  182.  
  183. //style.css
  184. #text-over {
  185. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  186. font-size: 50px;
  187. }
  188. #gameCanvas {
  189. background-color: rgb(187, 241, 155);
  190. }
Advertisement
Add Comment
Please, Sign In to add comment