Advertisement
SteelGolem

js gamedev cheat sheet

Jul 9th, 2021 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://developer.mozilla.org/en-US/docs/Games/Tutorials/2D_Breakout_game_pure_JavaScript
  2.  
  3. // graphics output
  4.   <canvas id="canvas" width="480" height="320"></canvas>
  5.   var canvas = document.getElementById("canvas");
  6.   var context = canvas.getContext("2d");
  7.   var sprite_sheet = new Image; sprite_sheet.src = "http/link/to/image.png";
  8.   context.drawImage(sprite_sheet, src_x, src_y, src_w, src_h, dest_x, dest_y, dest_w, dest_h);
  9.  
  10. // game loop timing
  11.   setInterval(game_loop, 10); // fixed interval
  12.   requestAnimationFrame(game_loop); // variable interval
  13.   function game_loop() { move_stuff(); draw_stuff(); }
  14.  
  15. // keyboard input
  16.   document.addEventListener("keydown", key_down, false);
  17.   document.addEventListener("keyup", key_up, false);
  18.   function key_down(e) { if(e.key == "Right" || e.key == "ArrowRight") key_r = true; }
  19.   function key_up(e) { if(e.key == "Right" || e.key == "ArrowRight") key_r = false; }
  20.  
  21. // mouse input
  22.   document.addEventListener("mousemove", mouse_move);
  23.   function mouse_move(e) { player_x = e.clientX - canvas.offsetLeft; }
  24.   // mousedown, mouseup, click, dblclick, wheel
  25.   // TODO: https://developer.mozilla.org/en-US/docs/Web/API/Touch_events
  26.  
  27. // sound output
  28.   // TODO: https://www.w3schools.com/graphics/game_sound.asp
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement