Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Tip Tap!</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no">
  7. <link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
  8. <style>
  9. body{
  10. margin: 0;
  11. height: 100vh;
  12. display: flex;
  13. align-items: center;
  14. justify-content: center;
  15. background: linear-gradient(to top, rgba(169, 207, 19, 0.2) 0%,
  16. rgba(39, 210, 36, 0.2) 100%),
  17. url("img/paper.png");
  18. }
  19. canvas{
  20. background: url(img/background.png);
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <canvas></canvas>
  26. <script>
  27.  
  28. // Création d'un Sprite
  29. var Sprite = {
  30. // Propriétés
  31. x: null,
  32. y: null,
  33. scale: 1,
  34. width: 100,
  35. height: 100,
  36.  
  37. sW: 100,
  38. sH: 100,
  39. sX: 0,
  40. sY: 0,
  41.  
  42.  
  43. column: 10,
  44. numberOfFrames: 10,
  45. currentFrame:0,
  46.  
  47. // Méthode init de positionnement
  48. init: function(x, y){
  49. this.x = x;
  50. this.y = y;
  51. this.sX = 0;
  52. this.sY = 0;
  53. this.width = 100;
  54. this.height= 100;
  55.  
  56. this.scale = Math.random()*2+0.1;
  57. this.width = Math.floor(this.scale*this.width);
  58. this.height = Math.floor(this.scale*this.height);
  59.  
  60. },
  61. // Update l'image du Sprite
  62. updateSprite: function(){
  63. this.sX = (this.currentFrame%this.column)*this.sW;
  64. this.sY = Math.floor(this.currentFrame/this.column)*this.sH;
  65. this.currentFrame++;
  66.  
  67. if(this.currentFrame==this.numberOfFrames){
  68. this.currentFrame=0;
  69. }
  70. },
  71.  
  72. hit: function(mouse){
  73. return(mouse.x>=this.x && mouse.x<=(this.x+this.width)
  74. && mouse.y>=this.y && mouse.y<=(this.y+this.height)
  75. )
  76. }
  77. }
  78.  
  79. var GameTimer = {
  80. time: null,
  81. interval: null,
  82. start: function(t){
  83. this.time = t;
  84. this.interval = setInterval(function(){GameTimer.tick()}, 1000);
  85. },
  86.  
  87. thick: function(){
  88. this.time--;
  89. },
  90.  
  91. stop: function(){
  92. clearInterval(this.interval);
  93. }
  94. }
  95.  
  96. var model = {
  97. sprites: [],
  98. width: 600,
  99. height: 800,
  100. fpsLimit:10,
  101. nb: 10,
  102. score: 0,
  103. timer: GameTimer,
  104.  
  105. // ÉTATS DU JEU
  106. INIT: 0,
  107. MENU: 1,
  108. PLAYING: 2,
  109. OVER: 3,
  110. gameState: 0,
  111. gameMessage:'',
  112.  
  113. init: function(){
  114. // Creation d'une 'Instance de Sprite' + init
  115. for(var i=0; i<this.nb; i++){
  116. var tmp = Object.create(Sprite);
  117. tmp.init(Math.floor(Math.random()*500+50),Math.floor(Math.random()*500+50));
  118. this.sprites.push(tmp);
  119. }
  120. },
  121. update: function(){
  122. // update les sprites
  123. this.sprites.forEach(function(tmp){
  124. tmp.updateSprite();
  125. })
  126. },
  127.  
  128. setScore: function(){
  129. this.score++;
  130. },
  131.  
  132. setFps: function(){
  133. this.fpsLimit++;
  134. }
  135. }
  136.  
  137. var octopus = {
  138.  
  139. previousDelta: 0,
  140.  
  141. init: function(){
  142. model.init();
  143. octopus.gameLoop();
  144. },
  145.  
  146. gameLoop: function(currentDelta){
  147. // requestAnimationFrame : en moyenne 60 fois par 1000ms
  148. // ce qui est un peu rapide pour nous...
  149. window.requestAnimationFrame(octopus.gameLoop);
  150.  
  151. // Le paramètre currentDelta correspond au temps écoulé en ms
  152. // depuis le lancement de requestAnimationFrame
  153. // Nous pouvons calculer le laps de temps écoulé depuis notre dernière loop (tjs en ms)
  154. var delta = currentDelta - octopus.previousDelta;
  155.  
  156. // return si celui-ci est inférieur au laps de temps de notre fps
  157. if (model.fpsLimit && delta < 1000 / model.fpsLimit) {
  158. return;
  159. }
  160.  
  161. switch(model.gameState){
  162. case model.INIT:
  163. view.init();
  164. model.gameMessage="LOADING";
  165. view.loadImage();
  166. break;
  167. case model.MENU:
  168. model.gameMessage="CLICK!";
  169. model.update();
  170. view.bindEvents();
  171. break;
  172. case model.PLAYING:
  173. model.gameMessage="";
  174. model.update();
  175. if(model.timer.time<0){
  176.  
  177. }
  178. break;
  179. case model.OVER:
  180. break;
  181. }
  182. view.render();
  183. octopus.previousDelta = currentDelta;
  184. },
  185.  
  186. getSprites: function(){
  187. var sprites = model.sprites;
  188. return sprites;
  189. },
  190.  
  191. getWidth: function(){
  192. var width = model.width;
  193. return width;
  194. },
  195. getHeight: function(){
  196. var height = model.height;
  197. return height;
  198. },
  199. getGameMessage: function(){
  200. var message = model.gameMessage;
  201. return message;
  202. },
  203.  
  204. getScore: function(){
  205. var score = model.score;
  206. if(score<10)
  207. score="0"+score;
  208. return score;
  209. },
  210.  
  211. handlerLoad: function(){
  212. // Changement d'état du jeu
  213. model.gameState=1;
  214. },
  215. handlerMouseDown: function(e){
  216. if(model.gameState == model.MENU){
  217. model.gameState = model.PLAYING;
  218. model.timer.start(10);
  219. return;
  220. }
  221. var mouse={};
  222. mouse.x= e.pageX - e.target.offsetLeft;
  223. mouse.y= e.pageY - e.target.offsetTop;
  224. for(var i=model.sprites.length-1; i>=0; i--){
  225. if(model.sprites[i].hit(mouse)){
  226. model.sprites[i].init(Math.floor(Math.random()*500+50),Math.floor(Math.random()*500+50));
  227. model.setScore();
  228. model.setFps();
  229. return;
  230. }
  231. }
  232. }
  233. }
  234.  
  235. var view = {
  236. canvas: null,
  237. ctx: null,
  238. image: null,
  239. init: function(){
  240. view.canvas = document.querySelector("canvas");
  241. view.ctx = view.canvas.getContext('2d');
  242. view.canvas.width = octopus.getWidth();
  243. view.canvas.height = octopus.getHeight();
  244. },
  245.  
  246. loadImage: function(){
  247. view.image = new Image();
  248. view.image.src = "img/sprite.png";
  249. view.image.addEventListener("load", octopus.handlerLoad)
  250. },
  251.  
  252. bindEvents: function(){
  253. view.canvas.addEventListener('mousedown',octopus.handlerMouseDown)
  254. },
  255.  
  256. render: function(){
  257. view.ctx.clearRect(0,0,view.canvas.width, view.canvas.height);
  258.  
  259. // Render sprites
  260. var sprites = octopus.getSprites();
  261.  
  262. for(var i=0; i< sprites.length; i++){
  263. var tmpSprite = sprites[i];
  264. view.ctx.drawImage(view.image,
  265. tmpSprite.sX,
  266. tmpSprite.sY,
  267. tmpSprite.sW,
  268. tmpSprite.sH,
  269. tmpSprite.x,
  270. tmpSprite.y,
  271. tmpSprite.width,
  272. tmpSprite.height)
  273. }
  274.  
  275.  
  276.  
  277. // Dessine le gameMessage
  278. view.ctx.font = "40px 'Press Start 2P'";
  279. view.ctx.fillStyle = "rgba(0,0,0,1)";
  280. view.ctx.fillText(octopus.getScore(), 70, 80);
  281. view.ctx.fillText(octopus.getGameMessage(),160,400);
  282.  
  283.  
  284. }
  285. }
  286. octopus.init();
  287.  
  288. </script>
  289.  
  290. </body>
  291. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement