Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. import {Bird} from "./bird.js";
  2. import {InputHandler} from "./input.js";
  3. import {Pipe} from "./pipe.js";
  4.  
  5. export function getRandomInt(min,max){
  6. return Math.floor(min + Math.random()*(max-min+1));
  7. }
  8.  
  9. let canvas = document.getElementById("gameScreen"); //HTMLファイルとの関連付け
  10. let ctx = canvas.getContext("2d"); //getContextメソッドで描写機能をオン
  11.  
  12. const GAME_WIDTH = 800;
  13. const GAME_HEIGHT = 500;
  14.  
  15.  
  16. //クラスからオブジェクトを作成
  17. //変数名は作成したオブジェクトを認識するための名前
  18. let bird = new Bird(GAME_WIDTH,GAME_HEIGHT);
  19. new InputHandler(bird);
  20. let pipe = [];
  21.  
  22. let lastTime =0;
  23. let counter = 0;
  24. let interval = 0;
  25.  
  26. //アップデートして再描写するためのゲームループ関数
  27. function gameLoop(timestamp){
  28. let deltaTime = timestamp - lastTime;
  29. lastTime = timestamp;
  30.  
  31. ctx.clearRect(0,0,GAME_WIDTH,GAME_HEIGHT);
  32.  
  33. bird.update(deltaTime);
  34. bird.draw(ctx);
  35.  
  36. counter += deltaTime;
  37. if(counter > interval ){
  38. pipe.push(new Pipe(GAME_WIDTH,GAME_HEIGHT));
  39. counter = 0;
  40. interval = getRandomInt(200,1000);
  41. }
  42.  
  43. for(var i = pipe.length -1;i >= 0;i--){
  44. pipe[i].update(deltaTime);
  45. pipe[i].draw(ctx);
  46. if(pipe[i].checkHit(bird)){
  47. console.log("HIT");
  48. }
  49. if(pipe[i].offScreen()){
  50. pipe.splice(i,1);
  51. }
  52. }
  53.  
  54.  
  55. requestAnimationFrame(gameLoop);
  56.  
  57. }
  58.  
  59. requestAnimationFrame(gameLoop);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement