game.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2012 David Geary. This code is from the book
  3. * Core HTML5 Canvas, published by Prentice-Hall in 2012.
  4. *
  5. * License:
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation files
  9. * (the "Software"), to deal in the Software without restriction,
  10. * including without limitation the rights to use, copy, modify, merge,
  11. * publish, distribute, sublicense, and/or sell copies of the Software,
  12. * and to permit persons to whom the Software is furnished to do so,
  13. * subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * The Software may not be used to create training material of any sort,
  19. * including courses, books, instructional videos, presentations, etc.
  20. * without the express written consent of David Geary.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  24. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  26. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  27. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  29. * OTHER DEALINGS IN THE SOFTWARE.
  30. */
  31. // ---------------------------------------------------------------------
  32. // --------------------------- DECLARATIONS ----------------------------
  33. // ---------------------------------------------------------------------
  34. var canvas = document.getElementById('game-canvas'),
  35. context = canvas.getContext('2d'),
  36. // Constants............................................................
  37. PLATFORM_HEIGHT = 8,
  38. PLATFORM_STROKE_WIDTH = 2,
  39. PLATFORM_STROKE_STYLE = 'rgb(0,0,0)',
  40. STARTING_RUNNER_LEFT = 50,
  41. STARTING_RUNNER_TRACK = 1,
  42. // Track baselines...................................................
  43. TRACK_1_BASELINE = 323,
  44. TRACK_2_BASELINE = 223,
  45. TRACK_3_BASELINE = 123,
  46. // Images............................................................
  47. background = new Image(),
  48. runnerImage = new Image(),
  49. // Platforms.........................................................
  50. platformData = [ // One screen for now
  51. // Screen 1.......................................................
  52. {
  53. left: 10,
  54. width: 230,
  55. height: PLATFORM_HEIGHT,
  56. fillStyle: 'rgb(250,250,0)',
  57. opacity: 0.5,
  58. track: 1,
  59. pulsate: false,
  60. },
  61. { left: 250,
  62. width: 100,
  63. height: PLATFORM_HEIGHT,
  64. fillStyle: 'rgb(150,190,255)',
  65. opacity: 1.0,
  66. track: 2,
  67. pulsate: false,
  68. },
  69. { left: 400,
  70. width: 125,
  71. height: PLATFORM_HEIGHT,
  72. fillStyle: 'rgb(250,0,0)',
  73. opacity: 1.0,
  74. track: 3,
  75. pulsate: false
  76. },
  77. { left: 633,
  78. width: 100,
  79. height: PLATFORM_HEIGHT,
  80. fillStyle: 'rgb(250,250,0)',
  81. opacity: 1.0,
  82. track: 1,
  83. pulsate: false,
  84. },
  85. ];
  86. // ------------------------- INITIALIZATION ----------------------------
  87. function initializeImages() {
  88. background.src = 'images/background_level_one_dark_red.png';
  89. runnerImage.src = 'images/runner.png';
  90. background.onload = function (e) {
  91. startGame();
  92. };
  93. }
  94. function drawBackground() {
  95. context.drawImage(background, 0, 0);
  96. }
  97. function calculatePlatformTop(track) {
  98. var top;
  99. if (track === 1) { top = TRACK_1_BASELINE; }
  100. else if (track === 2) { top = TRACK_2_BASELINE; }
  101. else if (track === 3) { top = TRACK_3_BASELINE; }
  102. return top;
  103. }
  104. function drawPlatforms() {
  105. var pd, top;
  106. context.save(); // Save context attributes
  107. for (var i=0; i < platformData.length; ++i) {
  108. pd = platformData[i];
  109. top = calculatePlatformTop(pd.track);
  110. context.lineWidth = PLATFORM_STROKE_WIDTH;
  111. context.strokeStyle = PLATFORM_STROKE_STYLE;
  112. context.fillStyle = pd.fillStyle;
  113. context.globalAlpha = pd.opacity;
  114. // If you switch the order of the following two
  115. // calls, you get a different effect.
  116. context.strokeRect(pd.left, top, pd.width, pd.height);
  117. context.fillRect (pd.left, top, pd.width, pd.height);
  118. }
  119. context.restore(); // Restore context attributes
  120. }
  121. function drawRunner() {
  122. context.drawImage(runnerImage,
  123. STARTING_RUNNER_LEFT,
  124. calculatePlatformTop(STARTING_RUNNER_TRACK) - runnerImage.height);
  125. }
  126. function draw(now) {
  127. drawBackground();
  128. drawPlatforms();
  129. drawRunner();
  130. }
  131. function startGame() {
  132. draw();
  133. }
  134. // Launch game.........................................................
  135. initializeImages();