Flappy Cow

Материал из Department of Theoretical and Applied Mechanics
Перейти к: навигация, поиск

Описание

Реализация и визуализация аналога игры “Flappy Bird” - “Flappy Cow”

Исполнители: Мальцева О.Н., Клочко Д.А..

Группа 13632/1 Кафедра Теоретической механики

Визуализация

Код программы

 1  window.addEventListener("load", main, false);
 2  function main() {
 3  var ctx = canvas.getContext('2d');
 4  var bird = new Image();
 5  var bg = new Image();
 6  var fg = new Image();
 7  var pipeUp = new Image();
 8  var pipeBottom = new Image();
 9  var score = 0;
10  bird.src = 'img/korova.png';
11  bg.src = 'img/nebo.jpg'
12  fg.src = 'img/flappy_bird_fg.png';
13  pipeUp.src = 'img/flappy_bird_pipeUp.png'; 
14  pipeBottom.src = 'img/flappy_bird_pipeBottom.png'; 
15  var gap = 70;
16  var gravitation = 1;
17  var Posx = 10;
18  var Posy = 150;
19  //нажатие
20  document.addEventListener("keydown", moveUp);
21  function moveUp(){
22    gravitation = -7;
23  }
24  var pipe = [];
25  pipe[0] = {
26   x : canvas.width,
27   y : 0
28  }
29  function draw(){
30  	ctx.clearRect(0,0, 288,512);
31      ctx.drawImage(bg, -bg.width/2-90, 0);
32      for (var i = 0; i < pipe.length; i++){
33         ctx.drawImage(pipeUp, pipe[i].x, pipe[i].y);
34         ctx.drawImage(pipeBottom, pipe[i].x, pipe[i].y + pipeUp.height + gap);
35        pipe[i].x--;
36        if (pipe[i].x == 125){
37          pipe.push({
38            x: canvas.width,
39            y: Math.floor(Math.random() * pipeUp.height) - pipeUp.height
40          });
41        }
42        if (Posx + bird.width >= pipe[i].x
43          && Posx <= pipe[i].x + pipeUp.width
44          && (Posy <= pipe[i].y + pipeUp.height
45          || Posy + bird.height >= pipe[i].y + pipeUp.height + gap)
46          || Posy + bird.height >= canvas.height - fg.height){
47            location.reload();
48          }
49      }
50      ctx.drawImage(fg, 0, canvas.height - fg.height);
51      ctx.drawImage(bird, Posx, Posy);
52      Posy += gravitation;
53      if (gravitation != 1){
54        gravitation += 1;
55      }
56           score++; some_name.innerHTML = score;
57      console.log(pipe);
58      requestAnimationFrame(draw);
59  }
60  pipeBottom.onload = draw;
61  }

</div>