Advertisement
vaakata

Nu_Pogodi_Eier_Problemlösung11.10.16

Oct 11th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by Ivan Minchev on 9.10.2016 г..
  3.  */
  4.  
  5. let buttonPushTopLeft = {x: 6, y: 74, w: 40, h: 40};
  6. let buttonPushTopRight = {x: 255, y: 74, w:40, h: 40};
  7. let buttonPushBotLeft = {x: 6, y: 115, w: 40, h: 40};
  8. let buttonPushBotRight = {x: 255, y: 115, w:40, h: 40};
  9. let wolfTopRight = {x: 150, y:50, w: 50, h: 60, pos: 'topRight'};
  10. let wolfBottomRight = {x: 150, y:50, w: 50, h: 60, pos: 'bottomRight'};
  11. let wolfTopLeft = {x: 100, y: 50, w: 50, h: 60, pos: 'topLeft'};
  12. let wolfBottomLeft = {x: 100, y: 50, w: 50, h: 60, pos:'bottomLeft'};
  13. let eggTopLeft = {x: 73, y: 36, w: 5, h:7, pos: 'topLeft'};
  14. let eggTopRight = {x: 227, y: 34, w: 5, h:7, pos: 'topRight'};
  15. let eggBottomRight = {x: 227, y: 64, w: 5, h:7, pos: 'bottomRight'};
  16. let eggBottomLeft = {x: 73, y: 65, w: 5, h:7, pos: 'bottomLeft'};
  17.  
  18. function init() {
  19.     let ctx = document.getElementById("canvas").getContext("2d");
  20.     let buttonImg = document.getElementById('button');
  21.     //buttonImg.Scale(2, 2);
  22.     let wolfImgTopRight = document.getElementById('wolfTopRight');
  23.     let wolfImgBottomRight = document.getElementById('wolfBottomRight');
  24.     let wolfImgTopLeft = document.getElementById('wolfTopLeft');
  25.     let wolfImgBottomLeft = document.getElementById('wolfBottomLeft');
  26.     let egg = document.getElementById('theEgg');
  27.     //let chickenImgLeft = document.getElementById('chickenLeft');
  28.  
  29.  
  30.     let score = 0;
  31.     let lives = 3;
  32.     let wolfCurrentPos = '';
  33.     // debugger;
  34.  
  35.     window.addEventListener('keydown', kbdHandler);
  36.     window.addEventListener('keyup', kbuHandler);
  37.  
  38.     setInterval(fallingEggs, 400);
  39.     function kbdHandler(event){
  40.         //console.log(event.code);
  41.         switch(event.code){
  42.             case 'Numpad7': drawTopLeft();
  43.                 break;
  44.             case 'Numpad9': drawTopRight();
  45.                 break;
  46.             case 'Numpad1': drawBottomLeft();
  47.                 break;
  48.  
  49.             case 'Numpad3': drawBottomRight();
  50.                 break;
  51.         }
  52.     }
  53.  
  54.     function kbuHandler(event){
  55.         switch(event.code){
  56.             case 'Numpad7':
  57.                 ctx.clearRect(buttonPushTopLeft.x, buttonPushTopLeft.y, buttonPushTopLeft.w, buttonPushTopLeft.h);
  58.                 break;
  59.             case 'Numpad9': console.log('hi');
  60.                 ctx.clearRect(buttonPushTopRight.x, buttonPushTopRight.y, buttonPushTopRight.w, buttonPushTopRight.h);
  61.                 break;
  62.             case 'Numpad1':
  63.                 ctx.clearRect(buttonPushBotLeft.x, buttonPushBotLeft.y, buttonPushBotLeft.w, buttonPushBotLeft.h);
  64.                 break;
  65.             case 'Numpad3':
  66.                 ctx.clearRect(buttonPushBotRight.x, buttonPushBotRight.y, buttonPushBotRight.w, buttonPushBotRight.h);
  67.                 break;
  68.  
  69.         }
  70.     }
  71.  
  72.  
  73.     function drawTopRight() {
  74.         ctx.clearRect(98, 50, 102, 60);
  75.         ctx.drawImage(wolfImgTopRight, wolfTopRight.x, wolfTopRight.y, wolfTopRight.w, wolfTopRight.h);
  76.         ctx.drawImage(buttonImg, buttonPushTopRight.x, buttonPushTopRight.y, buttonPushTopRight.w, buttonPushTopRight.h);
  77.         wolfCurrentPos = wolfTopRight.pos;
  78.     }
  79.  
  80.     function drawBottomRight() {
  81.         ctx.clearRect(98, 50, 102, 60);
  82.         ctx.drawImage(wolfImgBottomRight, wolfBottomRight.x, wolfBottomRight.y, wolfBottomRight.w, wolfBottomRight.h);
  83.         ctx.drawImage(buttonImg, buttonPushBotRight.x, buttonPushBotRight.y, buttonPushBotRight.w, buttonPushBotRight.h);
  84.         wolfCurrentPos = wolfBottomRight.pos
  85.     }
  86.  
  87.     function drawTopLeft(){
  88.         ctx.clearRect(98, 50, 102, 60);
  89.         ctx.drawImage(wolfImgTopLeft, wolfTopLeft.x, wolfTopLeft.y, wolfTopLeft.w, wolfTopLeft.h);
  90.         ctx.drawImage(buttonImg, buttonPushTopLeft.x, buttonPushTopLeft.y, buttonPushTopLeft.w, buttonPushTopLeft.h);
  91.         wolfCurrentPos = wolfTopLeft.pos;
  92.         //c.fillRect(x, y, width, height);
  93.     }
  94.  
  95.     function drawBottomLeft(){
  96.         ctx.clearRect(98, 50, 102, 60);
  97.         ctx.drawImage(wolfImgBottomLeft, wolfBottomLeft.x, wolfBottomLeft.y, wolfBottomLeft.w, wolfBottomLeft.h);
  98.         ctx.drawImage(buttonImg, buttonPushBotLeft.x, buttonPushBotLeft.y, buttonPushBotLeft.w, buttonPushBotLeft.h);
  99.         wolfCurrentPos = wolfBottomLeft.pos;
  100.         //c.fillRect(x, y, width, height);
  101.     }
  102.  
  103.     function fallingEggs(position) {
  104.         position = parseInt(Math.random() * (4)) + 1;
  105.         if (position === 1) {
  106.             animate(eggTopLeft, 5, 3, wolfTopLeft, wolfCurrentPos);
  107.         } else if (position === 2) {
  108.             animate(eggTopRight, -5, 3, wolfTopRight, wolfCurrentPos);
  109.         } else if (position === 3) {
  110.             animate(eggBottomRight, -5, 3, wolfBottomRight, wolfCurrentPos);
  111.         } else {
  112.             animate(eggBottomLeft, 5, 3, wolfBottomLeft, wolfCurrentPos);
  113.         }
  114.  
  115.     }
  116.     function animate(eggToFall, moveRateX, moveRateY, wolfToCheck, wolfPosition) {
  117.         eggToFall.x += moveRateX;
  118.         eggToFall.y += moveRateY;
  119.         if(eggToFall === eggBottomLeft || eggToFall === eggTopLeft) {
  120.             if(eggToFall.x === 98 && wolfToCheck.x === 100){  //Left positions of the wolf have value by axis x == 100;
  121.                 if (!(isEggCaught(eggToFall, wolfToCheck, wolfPosition))){
  122.                     ctx.drawImage(egg, eggToFall.x, eggToFall.y, eggToFall.w, eggToFall.h);
  123.                 }
  124.             } else if(eggToFall.x >= wolfToCheck.x ){
  125.                 isEggCaught(eggToFall, wolfToCheck, wolfPosition);
  126.                 //console.log(lives + '-lives');
  127.                 //console.log(score  + '-score');
  128.                 if (eggToFall === eggBottomLeft) {
  129.                     clear(eggToFall, -moveRateX, -moveRateY);
  130.                     eggToFall.x = 73;
  131.                     eggToFall.y = 65;
  132.  
  133.                 } else {
  134.                     clear(eggToFall, -moveRateX, -moveRateY);
  135.                     eggToFall.x = 73;
  136.                     eggToFall.y = 36;
  137.                 }
  138.                 fallingEggs();
  139.             } else {
  140.                 ctx.drawImage(egg, eggToFall.x, eggToFall.y, eggToFall.w, eggToFall.h);
  141.             }
  142.             clear(eggToFall, -moveRateX, -moveRateY);
  143.         } else if (eggToFall === eggTopRight || eggToFall === eggBottomRight) {
  144.             if(eggToFall.x === 202 && wolfToCheck.x + 50 === 200){
  145.                 if (!(isEggCaught(eggToFall, wolfToCheck, wolfPosition))) {
  146.                     ctx.drawImage(egg, eggToFall.x, eggToFall.y, eggToFall.w, eggToFall.h);
  147.                 }
  148.             } else if(eggToFall.x < wolfToCheck.x + 50 ){
  149.                 isEggCaught(eggToFall, wolfToCheck, wolfPosition);
  150.                 console.log(lives + '-lives');
  151.                 console.log(score  + '-score');
  152.                 if (eggToFall === eggTopRight) {
  153.                     clear(eggToFall, -moveRateX, -moveRateY);
  154.                     eggToFall.x = 227;
  155.                     eggToFall.y = 34;
  156.                 } else {
  157.                     clear(eggToFall, -moveRateX, -moveRateY);
  158.                     eggToFall.x = 227;
  159.                     eggToFall.y = 63;
  160.                 }
  161.                 fallingEggs();
  162.             } else {
  163.                 ctx.drawImage(egg, eggToFall.x, eggToFall.y, eggToFall.w, eggToFall.h);
  164.             }
  165.             clear(eggToFall, -moveRateX, -moveRateY);
  166.         }
  167.  
  168.     }
  169.     function clear(eggToFall, offsetX, offsetY) {
  170.         ctx.clearRect(eggToFall.x + offsetX, eggToFall.y + offsetY, eggToFall.w, eggToFall.h+1);
  171.     }
  172.  
  173.     // The function isEggCaught checks only whether both the wolff and egg are together in top or bottom positions.
  174.     function isEggCaught(eggToCheck, wolfToCheck, wolfCurrentPos) {        
  175.         //let x = (wolfToCheck.x + 25) - (eggToCheck.x + 2.5);
  176.         //let y = (wolfToCheck.y + 30) - (eggToCheck.y + 3.5);
  177.         //let distance = Math.sqrt(x * x + y * y);
  178.         if(eggToCheck.x == 98){ //critical left side position values check
  179.             if(eggToCheck.pos === wolfCurrentPos){
  180.                 score++;
  181.                 return true;
  182.             } else {
  183.                 lives--;
  184.               return false;  
  185.             }            
  186.         } else if(eggToCheck.x == 202){ // critical right side position values check
  187.             if(eggToCheck.pos === wolfCurrentPos){
  188.                 score++;
  189.                 return true;
  190.             } else {
  191.                 lives--;
  192.                 return false;
  193.             }
  194.         } else if (eggToCheck.pos === wolfCurrentPos) {
  195.             score ++
  196.         } else {
  197.             lives --;
  198.         }
  199.     }
  200. }
  201.  
  202. init();
  203.  
  204. //setInterval(render, 20);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement