Advertisement
Guest User

Untitled

a guest
Dec 19th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
  6.  
  7. <meta name="apple-mobile-web-app-capable" content="yes">
  8. <meta name="mobile-web-app-capable" content="yes">
  9.  
  10. <title>FlappyJS</title>
  11.  
  12. <script src="sprite.js"></script>
  13.  
  14. <style>
  15. canvas {
  16. display: block;
  17. position: absolute;
  18. margin: auto;
  19. top: 0;
  20. bottom: 0;
  21. left: 0;
  22. right: 0;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27.  
  28.  
  29.  
  30. <!-- START OF SCRIPT -->
  31. <script>
  32.  
  33. var
  34.  
  35. canvas,
  36. ctx,
  37. width,
  38. height,
  39. fgpos = 0,
  40.  
  41. frames = 0,
  42. score = 0,
  43. best = 0,
  44.  
  45. currentstate,
  46. states = {
  47. Splash: 0, Game: 0, Score: 2
  48. },
  49.  
  50.  
  51. bird = {
  52.  
  53. x: 60,
  54. y: 0,
  55. frame: 0,
  56. velocity:0,
  57. animation: [0,1,2,1],
  58. rotation: 0,
  59. gravity: 0.25,
  60. _jump: 4.6,
  61.  
  62. jump: function (){
  63.  
  64. this.velocity = -this._jump;
  65. },
  66.  
  67. update : function (){
  68. var n = currentstate === states.Splash ? 10 : 5;
  69. this.frame += frames % n === 0 ? 1 : 0;
  70. this.frame %= this.animation.length;
  71.  
  72. if(currentstate === states.Splash){
  73. this.y = height - 280 + 5*Math.cos(frames/10);
  74. this.rotation = 0;
  75. }
  76. },
  77.  
  78. draw : function (ctx){
  79. ctx.save();
  80. ctx.translate(this.x, this.y);
  81. ctx.rotate (this.rotation);
  82.  
  83.  
  84. var n = this.animation[this.frame];
  85. s_bird[n].draw(ctx, -s_bird[n].width/2, -s_bird[n].height/2);
  86.  
  87. ctx.restore();
  88. }
  89.  
  90. },
  91.  
  92.  
  93.  
  94. pipes = {
  95. update : function() {},
  96. draw : function(ctx){}
  97. };
  98.  
  99.  
  100. function onpress(evt) {
  101.  
  102. switch (currentstate) {
  103.  
  104. case states.Splash:
  105. currentstate = states.Game;
  106. bird.jump();
  107. break;
  108.  
  109. case states.Game:
  110. bird.jump();
  111. break;
  112.  
  113. case states.Score:
  114. break;
  115.  
  116.  
  117.  
  118. }
  119. }
  120.  
  121.  
  122. //START OF MAIN
  123.  
  124. function main(){
  125. canvas = document.createElement("canvas");
  126.  
  127. width = window.innerWidth;
  128. height = window.innerHeight;
  129.  
  130. var evt = "touchstart"
  131. if (width >= 500) {
  132. width = 320;
  133. height = 480;
  134. canvas.style.border = "1px solid #000";
  135. evt = "mousedown";
  136. }
  137.  
  138. document.addEventListener(evt, onpress);
  139. canvas.width = width;
  140. canvas.height = height;
  141. ctx = canvas.getContext("2d");
  142.  
  143. currentstate = states.Splash;
  144.  
  145. document.body.appendChild(canvas);
  146.  
  147. var img = new Image();
  148. img.onload = function(){
  149. initSprites(this);
  150. ctx.fillStyle = s_bg.color;
  151. run();
  152.  
  153. }
  154.  
  155. img.src = "sheet2.png";
  156.  
  157. }
  158.  
  159. //START OF RUN
  160.  
  161.  
  162.  
  163. function run(){
  164. var loop = function (){
  165. update();
  166. render();
  167. window.requestAnimationFrame(loop, canvas);
  168. }
  169. window.requestAnimationFrame(loop, canvas);
  170.  
  171. }
  172.  
  173. //START OF UPDATE
  174.  
  175. function update(){
  176. frames++;
  177. fgpos = (fgpos - 2) % 14;
  178.  
  179. bird.update();
  180. pipes.update();
  181. }
  182. //START OF RENDER
  183.  
  184.  
  185. function render(){
  186. ctx.fillRect(0, 0, width, height);
  187. s_bg.draw(ctx, 0, height - s_bg.height);
  188. s_bg.draw(ctx, s_bg.width, height - s_bg.height);
  189.  
  190. bird.draw(ctx);
  191. pipes.draw(ctx);
  192.  
  193.  
  194. s_fg.draw(ctx, fgpos, height - s_fg.height);
  195. s_fg.draw(ctx, fgpos+s_fg.width, height - s_fg.height);
  196.  
  197.  
  198. var width2 = width/2;
  199.  
  200. if (currentstate === states.Splash) {
  201. s_splash.draw(ctx, width2 - s_splash.width/2, height - 300);
  202. s_text.GetReady.draw(ctx, width2 - s_text.GetReady.width/2, height - 380);
  203. }
  204. }
  205.  
  206.  
  207. main();
  208.  
  209.  
  210. </script> <!-- START OF SCRIPT -->
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. </body>
  219. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement