AnonymousNamefag

Pong game (unfinished)

Dec 2nd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4. <head>
  5. <title>HTML5 Canvas</title>
  6.  
  7. <style type="text/css">
  8. * {
  9.     background-color: black;
  10.     border-color: #00ff00;
  11.     color: #00ff00;
  12. }
  13. </style>
  14.  
  15. <script type="text/javascript">
  16.  
  17. var x = 0;
  18. var y = 0;
  19. var xdir = 1;
  20. var ydir = 1;
  21. var cw = 800;
  22. var ch = 500;
  23. var speed = 1;
  24. var lpos = 250;
  25. var rpos = 250;
  26. var pw = 20;
  27. var ph = 100;
  28. var bw = 36;
  29. var bh = 36;
  30. var lscore = 0;
  31. var rscore = 0;
  32.  
  33. function setup(){
  34.     var c = document.getElementById( "Animation" );
  35.     var ctx = c.getContext( "2d" );
  36.     ctx.fillStyle = "#00ff00";
  37.     ctx.fillRect( 0, ch/2-ph/2, pw, ph );
  38.     ctx.fillRect( cw-pw, ch/2-ph/2, pw, ph );
  39.     animate();
  40. }
  41.  
  42. function stop(){
  43.     if( xdir ) lscore++;
  44.     else rscore++;
  45.     refresh();
  46. }
  47.  
  48. function refresh(){
  49.     cw = document.getElementById( "cw" ).value;
  50.     ch = document.getElementById( "ch" ).value;
  51.     speed = document.getElementById( "speed" ).value;
  52.     var c = document.getElementById( "Animation" );
  53.     document.getElementById( "lscore" ).innerHTML = lscore;
  54.     document.getElementById( "rscore" ).innerHTML = rscore;
  55.     c.width = String( cw );
  56.     c.height = String( ch );
  57.     x = 0;
  58.     y = 0;
  59.     xdir = 1;
  60.     ydir = 1;
  61. }
  62.  
  63. function animate(){
  64.     var c = document.getElementById( "Animation" );
  65.     var ctx = c.getContext( "2d" );
  66.     ctx.fillStyle = "#00ff00";
  67.     ctx.clearRect( x, y, bw, bh );
  68.     if( xdir ) x += 2;
  69.     else x -= 2;
  70.     if( ydir ) y += 2;
  71.     else y -= 2;
  72.     if( xdir ){ // traveling right
  73.         if( y+bw > rpos - ph/2 && y < rpos + ph/2 ){ // in range of rpaddle
  74.             if( x+bw+pw >= cw ){ xdir = 0; }
  75.         }
  76.         else{
  77.             if( x+bw >= cw ) stop();
  78.         }
  79.     }
  80.     else{ // traveling left
  81.         if( y+bw > lpos - ph/2 && y < lpos + ph/2 ){ // in range of lpaddle
  82.             if( x-pw <= 0 ){ xdir = 1; }
  83.         }
  84.         else{
  85.             if( x <= 0 ) stop();
  86.         }
  87.     }
  88.     ctx.fillRect( x, y, bw, bh );
  89.     if( Math.abs( y - ch ) <= bh ) ydir = 0;
  90.     if( Math.abs( y ) <= 0 ) ydir = 1;
  91.     window.setTimeout( animate, 20/speed );
  92. }
  93.  
  94. </script>
  95.  
  96. </head>
  97.  
  98. <body onload="setup();">
  99. <center><table><tr><td>
  100. <canvas id="Animation" width="800" height="500" style="border: 1px solid #00ff00;"></canvas>
  101. <h2>Controls:</h2>
  102. <table>
  103. <col></col><col></col>
  104. <col width="200"></col>
  105. <col></col><col></col>
  106. <thead>
  107. <th colspan="2"><h2>Controls:</h2></th>
  108. <th></th>
  109. <th colspan="2"><h2>Scores:</h2></th>
  110. </head>
  111. <tr><td>Speed:</td><td><input id="speed" type="text" value="1"></td><td></td><td>Left:</td><td><span id="lscore">0</span></td></tr>
  112. <tr><td>Container width:</td><td><input id="cw" type="text" value="800"></td><td></td><td>Right:</td><td><span id="rscore">0</span></td></tr>
  113. <tr><td>Container height:</td><td><input id="ch" type="text" value="500"></td></tr>
  114. </table>
  115. <button onclick="refresh();">Refresh</button>
  116. </td></tr></table>
  117. </body>
  118. </html>
Add Comment
Please, Sign In to add comment