Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. var img = new Image();
  2. img.src = "http://yourimage.jpg";
  3.  
  4. //snippet
  5. function colorCircle(centerX,centerY, radius, drawColor){
  6. canvasContext.fillStyle = drawColor;
  7. canvasContext.beginPath();
  8. canvasContext.arc(centerX, centerY, radius, 0, Math.PI*2, true);
  9. canvasContext.fill();
  10.  
  11. // ball
  12. colorCircle(ballX, ballY, 10, 'green')
  13.  
  14. <!--Fullgame-->
  15. <html>
  16.  
  17. <canvas id="gameCanvas" width="800" height="600"></canvas>
  18.  
  19.  
  20.  
  21. <script>
  22. // set my vars
  23. var canvas;
  24. var canvasContext;
  25. var ballX = 50;
  26. var ballY = 50;
  27. var ballSpeedX = 10;
  28. var ballSpeedY = 5;
  29.  
  30. var player1Score = 0;
  31. var player2Score = 0;
  32. const WINNING_SCORE = 3;
  33. var showingWinScreen = false;
  34.  
  35. var paddle1Y = 250;
  36. var paddle2Y = 250;
  37.  
  38. // consts cannot be changed when game is played.
  39. const PADDLE_HEIGHT = 100;
  40. const PADDLE_THICKNESS= 10;
  41.  
  42. function calculateMousePos(evt) {
  43. var rect = canvas.getBoundingClientRect();
  44. var root = document.documentElement;
  45. var mouseX = evt.clientX - rect.left - root.scrollLeft;
  46. var mouseY = evt.clientY - rect.top - root.scrollTop;
  47. return {
  48.  
  49. x:mouseX,
  50. y:mouseY
  51. }
  52.  
  53.  
  54.  
  55. }
  56.  
  57. function handleMouseClick(evt) {
  58. if(showingWinScreen) {
  59. player1Score = 0;
  60. player2Score = 0;
  61. showingWinScreen = false;
  62. }
  63.  
  64.  
  65. }
  66.  
  67. window.onload = function() {
  68. canvas = document.getElementById('gameCanvas');
  69. canvasContext = canvas.getContext('2d');
  70.  
  71. var framesPerSecond = 30;
  72. setInterval(function() {
  73. moveEverything();
  74. drawEverything();
  75. }, 1000/framesPerSecond );
  76.  
  77. canvas.addEventListener('mousedown', handleMouseClick);
  78.  
  79. // rewatch section 2 lecture 7
  80. canvas.addEventListener('mousemove',
  81. function(evt) {
  82. var mousePos = calculateMousePos(evt);
  83. paddle1Y = mousePos.y-(PADDLE_HEIGHT/2);
  84.  
  85.  
  86. });
  87. }
  88.  
  89. function ballReset() {
  90. if(player1Score >= WINNING_SCORE ||
  91. player2Score >= WINNING_SCORE) {
  92. showingWinScreen = true;
  93.  
  94. }
  95. ballSpeedX = -ballSpeedX;
  96. ballX = canvas.width/2;
  97. ballY = canvas.height/2;
  98. }
  99.  
  100. function computerMovement() {
  101. var paddle2YCenter = paddle2Y + (PADDLE_HEIGHT/2);
  102. if(paddle2YCenter < ballY-35) {
  103. paddle2Y += 6;
  104. } else if(paddle2YCenter > ballY+35) {
  105. paddle2Y -= 6;
  106. }
  107. }
  108.  
  109. function moveEverything() {
  110. if(showingWinScreen){
  111. return;
  112. }
  113. computerMovement();
  114. ballX += ballSpeedX;
  115. ballY += ballSpeedY;
  116.  
  117. if(ballX < 0) {
  118. if(ballY > paddle1Y &&
  119. ballY < paddle1Y+PADDLE_HEIGHT) {
  120. ballSpeedX = -ballSpeedX
  121. var deltaY = ballY-(paddle1Y+PADDLE_HEIGHT/2);
  122. ballSpeedY = deltaY * .35;
  123. } else {
  124. player2Score += 1; //must be BEFORE ball reset
  125. ballReset();
  126. //++ adds one and
  127.  
  128. }
  129.  
  130. }
  131. if(ballX > canvas.width) {
  132. if(ballY > paddle2Y &&
  133. ballY < paddle2Y+PADDLE_HEIGHT) {
  134. ballSpeedX = -ballSpeedX
  135.  
  136. var deltaY = ballY-(paddle2Y+PADDLE_HEIGHT/2);
  137. ballSpeedY = deltaY * .35;
  138. } else {
  139. player1Score ++;
  140. ballReset();
  141. //--removes one
  142.  
  143. }
  144. }
  145. if(ballY < 0) {
  146. ballSpeedY = -ballSpeedY;
  147. }
  148. if(ballY > canvas.height) {
  149. ballSpeedY = -ballSpeedY;
  150. }
  151.  
  152.  
  153.  
  154.  
  155. }
  156.  
  157. function drawNet() {
  158. for(var i=0;i<canvas.height; i+=40) {
  159. colorRect(canvas.width/2-1, i, 2, 20, 'red');
  160.  
  161.  
  162. }
  163.  
  164.  
  165. }
  166.  
  167. function drawEverything() {
  168.  
  169. // next line blanks out the screen with black
  170. colorRect(0,0,canvas.width,canvas.height,"black");
  171.  
  172.  
  173.  
  174. if(showingWinScreen){
  175. canvasContext.fillStyle = "orange";
  176. if(player1Score >= WINNING_SCORE) {
  177. canvasContext.fillText("Left Player Wins!", 350, 200);
  178.  
  179. } else if( player2Score >= WINNING_SCORE) {
  180. canvasContext.fillText("Right Player Wins!", 350, 200);
  181.  
  182. }
  183.  
  184. canvasContext.fillText("Click to Continue!", 350, 500);
  185. return;
  186. }
  187.  
  188. drawNet();
  189.  
  190. //left player paddle
  191. colorRect(0,paddle1Y,PADDLE_THICKNESS,PADDLE_HEIGHT, 'white');
  192.  
  193. //right player paddle
  194. colorRect(canvas.width-PADDLE_THICKNESS,paddle2Y,PADDLE_THICKNESS,PADDLE_HEIGHT, 'white');
  195.  
  196. // ball
  197. colorCircle(ballX, ballY, 10, 'green')
  198.  
  199. canvasContext.fillText(player1Score, 100, 100);
  200. canvasContext.fillText(player2Score, canvas.width - 100, 100);
  201.  
  202. }
  203.  
  204. function colorCircle(centerX,centerY, radius, drawColor){
  205. canvasContext.fillStyle = drawColor;
  206. canvasContext.beginPath();
  207. canvasContext.arc(centerX, centerY, radius, 0, Math.PI*2, true);
  208. canvasContext.fill();
  209.  
  210. }
  211.  
  212. function colorRect(leftX,topY, width,height, drawColor){
  213. canvasContext.fillStyle = drawColor;
  214. canvasContext.fillRect(leftX,topY, width,height);
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement