CristianSW

game HTML

Nov 26th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.onload = init;
  2. //variables
  3. var map;
  4. var ctxMap;
  5.  
  6. var pl;
  7. var ctxPl;
  8.  
  9. var en;
  10. var ctxEn;
  11.  
  12. var drawBtn;
  13. var clearBtn;
  14.  
  15. var gameWidth = 800;
  16. var gameHeight = 500;
  17.  
  18. //background
  19. var background = new Image();
  20. background.src = 'sources/bg1.jpeg';
  21.  
  22. var tux = new Image();
  23. tux.src = 'sources/1.png';
  24.  
  25. var win = new Image();
  26. win.src = '/home/cristian/Рабочий стол/game/sources/2.1.png';
  27. //playerrrss
  28. var player;
  29. var enemy;
  30. var requestAnimFrame = window.requestAnimationFrame ||
  31.                       window.webkitRequestAnimationFrame ||
  32.                       window.mozRequestAnimationFrame ||
  33.                       window.oRequestAnimationFrame ||
  34.                       window.msRequestAnimationFrame ;
  35.  
  36. var isPlaying;
  37.  
  38. function init()
  39. {
  40.   map = document.getElementById('map')
  41.   ctxMap = map.getContext('2d');
  42.  
  43.   pl = document.getElementById("player")
  44.   ctxPl = pl.getContext("2d");
  45.  
  46.   en = document.getElementById('enemy')
  47.   ctxEn = en.getContext("2d");
  48.  
  49.  
  50.   map.width = gameWidth;
  51.   map.height = gameHeight;
  52.   pl.width = gameWidth;
  53.   ctxPl.height = gameHeight;
  54.   en.width = gameWidth;
  55.   ctxEn.height = gameHeight;
  56.  
  57.   drawBtn = document.getElementById("drawBtn")
  58.   clearBtn = document.getElementById("clearBtn")
  59.  
  60.   drawBtn.addEventListener("click", drawRect, false)
  61.   clearBtn.addEventListener("click", clearRect, false)
  62.  
  63.   player = new Player();
  64.   enemy = new Enemy();
  65.  
  66.   drawBg();
  67.   startLoop();
  68.  
  69.   document.addEventListener("keydown", checkKeyDown , false );
  70.   document.addEventListener("keyup", checkKeyUp , false );
  71. }
  72.  
  73. function loop() {
  74.   if (isPlaying) {
  75.     draw();
  76.     update();
  77.     requestAnimFrame(loop);
  78.   }
  79. }
  80.  
  81. function startLoop() {
  82.   isPlaying = true;
  83.   loop();
  84. }
  85.  
  86. function stopLoop() {
  87.   isPlaying = false;
  88. }
  89.  
  90. function draw() {
  91.   player.draw();
  92.   enemy.draw();
  93.  
  94. }
  95. function update() {
  96. player.update();
  97. }
  98. //Objects
  99. function Enemy(){
  100.  
  101.   this.srcX = 0;
  102.   this.srcY = 0;
  103.   this.drawX =400;
  104.   this.drawY =50;
  105.   this.width = 400;
  106.   this.height = 400;
  107.  
  108.   this.speed = 8;
  109. };
  110.  
  111. function Player() {
  112.  
  113.   this.srcX = 0;
  114.   this.srcY = 0;
  115.   this.drawX =0;
  116.   this.drawY = 0;
  117.   this.width = 58;
  118.   this.height = 70;
  119.   this.speed = 3;
  120.  
  121.   //for keys
  122.  
  123.   this.isUp = false;
  124.   this.isDown = false;
  125.   this.isRight = false;
  126.   this.isLeft = false;
  127.  
  128. }
  129.  
  130. Player.prototype.draw = function () {
  131.   clearCtxPlayer();
  132.   ctxPl.drawImage(tux ,this.srcX, this.srcY, this.width, this.height,
  133.                           this.drawX, this.drawY,this.width, this.height);
  134. };
  135.  
  136. Player.prototype.update = function () {
  137.   this.chooseDir();
  138. };
  139.  
  140. Player.prototype.chooseDir = function () {
  141.   if (this.isUp)
  142.     this.drawY -= this.speed;
  143.   if (this.isDown)
  144.     this.drawY += this.speed;
  145.   if (this.isRight)
  146.     this.drawX += this.speed;
  147.   if (this.isLeft)
  148.     this.drawX -= this.speed;
  149.  
  150. };
  151.  
  152. function checkKeyDown(e) {
  153.   var keyID = e.keyCode || e.which;
  154.   var keyChar = String.fromCharCode(keyID);
  155.   if (keyChar == "W") {
  156.     player.isUp = true;
  157.     e.preventDefault();
  158.   }
  159.   if (keyChar == "S") {
  160.     player.isDown = true;
  161.     e.preventDefault();
  162.   }
  163.   if (keyChar == "D") {
  164.     player.isRight = true;
  165.     e.preventDefault();
  166.   }
  167.   if (keyChar == "A") {
  168.     player.isLeft = true;
  169.     e.preventDefault();
  170.   }
  171. }
  172.  
  173. function checkKeyUp(e) {
  174.   var keyID = e.keyCode || e.which;
  175.   var keyChar = String.fromCharCode(keyID);
  176.   if (keyChar == "W") {
  177.     player.isUp = false;
  178.     e.preventDefault();
  179.   }
  180.   if (keyChar == "S") {
  181.     player.isDown = false;
  182.     e.preventDefault();
  183.   }
  184.   if (keyChar == "D") {
  185.     player.isRight = false;
  186.     e.preventDefault();
  187.   }
  188.   if (keyChar == "A") {
  189.     player.isLeft = false;
  190.     e.preventDefault();
  191.   }
  192. }
  193.  
  194. Enemy.prototype.draw = function () {
  195.   ctxMap.drawImage(win ,this.srcX, this.srcY, this.width, this.height,
  196.                           this.drawX, this.drawY,this.width, this.height);
  197. };
  198.  
  199. function drawRect() {
  200.   ctxMap.fillStyle = "mediumaquamarine"
  201.   ctxMap.fillRect(10, 10, 100, 100)
  202. }
  203.  
  204. function clearRect() {
  205.   ctxMap.clearRect(0, 0, 800, 500)
  206. }
  207.  
  208. function clearCtxPlayer() {
  209.   ctxPl.clearRect(0 ,0 ,gameWidth , gameHeight);
  210.  
  211. }
  212.  
  213. function drawBg() {
  214.   ctxMap.drawImage(background,0 ,0 ,1920 ,1080,
  215.                               0, 0, gameWidth ,gameHeight)
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment