Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4. <meta charset='utf-8'>
  5. <title>Meu Primeiro Jogo Multiplayer</title>
  6. <style>
  7. #screen {
  8. border: 10px solid #CCC;
  9. image-rendering: pixelated;
  10. image-rendering: crisp-edges;
  11. image-rendering: -moz-crisp-edges;
  12. width: 400px;
  13. height: 400px;
  14. }
  15. </style>
  16. </head>
  17.  
  18. <body>
  19. <canvas id='screen' width='10' height='10'></canvas>
  20.  
  21. <script>
  22. const screen = document.getElementById('screen')
  23. const context = screen.getContext('2d')
  24. const currentPlayerId = 'player1'
  25.  
  26.  
  27. function createGame() {
  28. const state = {
  29. players: {
  30. 'player1': { x: 1, y: 1 },
  31. 'player2': { x: 9, y: 9 },
  32.  
  33. },
  34. fruits: {
  35. 'fruit1': { x: 3, y: 1 }
  36. }
  37. }
  38.  
  39. function movePlayer(command) {
  40. console.log(`Moving ${command.playerId} with ${command.keyPressed}`)
  41.  
  42. const keyPressed = command.keyPressed
  43. const player = state.players[command.playerId]
  44.  
  45. if (keyPressed === 'ArrowUp' && player.y - 1 >= 0) {
  46. player.y = player.y - 1
  47. return
  48. }
  49.  
  50. if (keyPressed === 'ArrowRight' && player.x + 1 < screen.width) {
  51. player.x = player.x + 1
  52. return
  53. }
  54.  
  55. if (keyPressed === 'ArrowDown' && player.y + 1 <= screen.height) {
  56. player.y = player.y + 1
  57. return
  58. }
  59.  
  60. if (keyPressed === 'ArrowLeft' && player.x - 1 >= 0) {
  61. player.x = player.x - 1
  62. return
  63. }
  64.  
  65. }
  66.  
  67. return {
  68. movePlayer, state
  69. }
  70. }
  71.  
  72. const game = createGame()
  73.  
  74.  
  75. const keyboardListener = createKeyboardListener()
  76.  
  77. function createKeyboardListener() {
  78. const state = {
  79. observers: []
  80. }
  81.  
  82. function subscribe(observerFunction){
  83. state.observers.push(observerFunction)
  84.  
  85. }
  86.  
  87. function notifyAll(command){
  88. console.log(`Notifying ${state.observers.length} observers`)
  89. for (const observerFunction of state.observers)
  90. observerFunction(command)
  91.  
  92. }
  93. document.addEventListener('keydown', handleKeyDown)
  94.  
  95. function handleKeyDown(event) {
  96. const keyPressed = event.key
  97. const command = {
  98. playerId: 'player1', keyPressed
  99. }
  100. game.movePlayer(command)
  101. notifyAll(command)
  102. }
  103.  
  104. return{
  105. subscribe
  106. }
  107.  
  108. }
  109. renderScreen()
  110.  
  111. function renderScreen() {
  112. context.fillStyle = 'white'
  113. context.clearRect(0, 0, 10, 10)
  114. for (const playerId in game.state.players) {
  115. const player = game.state.players[playerId]
  116. context.fillStyle = 'black'
  117. context.fillRect(player.x, player.y, 1, 1)
  118. }
  119.  
  120. for (const fruitId in game.state.fruits) {
  121. const fruit = game.state.fruits[fruitId]
  122. context.fillStyle = 'green'
  123. context.fillRect(fruit.x, fruit.y, 1, 1)
  124. }
  125. requestAnimationFrame(renderScreen)
  126. }
  127.  
  128.  
  129. </script>
  130. </body>
  131.  
  132. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement