Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <style>
  6. #canvas{
  7. width:600px;
  8. height:600px;
  9. border:solid red 1px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <canvas id='canvas'>
  15. </canvas>
  16. <script>
  17.  
  18. var programm={
  19. condition:'menu',
  20. clearDevice: function(){
  21.  
  22. },
  23.  
  24. menu: function(){
  25. this .condition = 'menu';//обновили состояние программы
  26. this .clearDevice();//очистка экрана
  27.  
  28. },
  29.  
  30. game: function(){
  31.  
  32. var example = document .getElementById('canvas') ,
  33. canvas = example .getContext('2d'),//настройка канвас
  34. cWidth = 600, //длинна канвас
  35. cHeight = 600;//высота канвас
  36.  
  37. this .condition = 'game';//обновили состояние программы
  38. this .clearDevice();//очистка экрана
  39.  
  40.  
  41. function GameData(map){
  42. this.map = map;
  43. }
  44.  
  45. function Character(x,y){//персонаж
  46. this .x = x;
  47. this .y = y;
  48. this .health = 100;
  49. this .up = function(){
  50. this .y = this .y-1;
  51. }
  52. this .down = function(){
  53. this .y = this .y+1;
  54. }
  55. this .left = function(){
  56. this .x = this .x-1;
  57. }
  58. this .right = function(){
  59. this .x = this .x+1;
  60. }
  61. }
  62.  
  63. var map = [[1,1,1,1,1],[1,2,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]];//карта
  64. var data = new GameData(map);
  65. var x=1,y=1;
  66. var player = new Character(x,y);
  67. var userInput = {//обьект для записи ввода с клавиатуры
  68. up: false,
  69. down: false,
  70. left: false,
  71. right:false
  72. };
  73.  
  74. addEventListener('keydown',function(event){//ввод с клавиатуры
  75. switch (event .keyCode){
  76. case 87: //W
  77. userInput .up = true;
  78. break
  79. case 65: //A
  80. userInput .left = true;
  81. break
  82. case 83: //S
  83. userInput .down = true;
  84. break
  85. case 68: //D
  86. userInput .right = true;
  87. break
  88.  
  89. }
  90. });
  91.  
  92. function inputProcessing(){
  93. if (userInput.up){
  94. player .up();
  95. userInput .up = false;
  96. }
  97.  
  98. if (userInput.down){
  99. player .down();
  100. userInput .down = false;
  101. }
  102.  
  103. if (userInput.left){
  104. player .left();
  105. userInput .left = false;
  106. }
  107.  
  108. if (userInput .right){
  109. player .right();
  110. userInput .right = false;
  111. }
  112. };
  113.  
  114. function update(){
  115. inputProcessing();
  116. };
  117.  
  118. function render(){
  119. canvas.clearRect(0, 0, 600, 600); //чистим поле
  120. var i = 0,j = 0,f = true;
  121. for (i ;i<5;i++){
  122. j=0;
  123. for (j ;j<5; j++){
  124. if (f){canvas .fillRect(i*10, j*10, 10, 10);}
  125. if (f){
  126. f = false;
  127. }
  128. else f = true;
  129. }
  130. } //рисуем карту
  131. };
  132.  
  133. var fps = 15;//частота обновления экрана
  134. function step(){//игровой цикл
  135. update();
  136. render();
  137. setTimeout(function(){
  138. requestAnimationFrame(step);
  139. },1000/fps);
  140.  
  141. };
  142. render();
  143.  
  144. }
  145. }
  146. programm .game();
  147. </script>
  148. </body>
  149. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement