jhylands

A bouncing box

Jan 14th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3. <style>
  4. .car
  5. {
  6. background-color:gray;
  7. width:10px;
  8. height:10px;
  9. }
  10. .grass{background-color:green;}
  11. </style>
  12. </head>
  13. <body onload="">
  14. <script>
  15. var cars = new Object();
  16. function CreateCars(){
  17.      writting('<div id="car"><table class="car"><tr><td></td></tr></table></div>');
  18.      cars.visual = document.getElementById('car');
  19.      cars.visual.style.position = "absolute";
  20.      cars.visual.style.top = "100px";
  21.      cars.visual.style.left = "100px";
  22.      cars.p = new Object();
  23.      cars.p.x = 100;
  24.      cars.p.y = 100;
  25.      cars.v = new Object();
  26.      cars.v.x = 7;
  27.      cars.v.y = 9;
  28. }
  29. function sta(){
  30.       if (cars.p.x > 1010 || cars.p.x <10){
  31.          cars.v.x = cars.v.x *-1;
  32.       }
  33.       if (cars.p.y > 200 || cars.p.y <100){
  34.          cars.v.y = cars.v.y *-1;
  35.       }
  36.       cars.p.x = cars.p.x + cars.v.x;
  37.       cars.p.y = cars.p.y + cars.v.y;
  38.       cars.visual.style.left = cars.p.x + "px";
  39.       cars.visual.style.top = cars.p.y + "px";
  40.      
  41.       setTimeout('sta();', 100);
  42. }
  43.  
  44. function writting(text){
  45.       document.getElementById('edspace').innerHTML = document.getElementById('edspace').innerHTML + text;
  46. }
  47. </script>
  48. <input type="button" value="add" onclick="CreateCars();" >
  49. <input type="button" value="move" onclick="sta();">
  50. <div style = "position:absolute;left:10px;top:100px;"><table class="grass"><tr><td width="1000px" height="100px"></td></tr></table>
  51. </div>
  52. <x id="edspace"></x>
  53. </body>
Advertisement
Add Comment
Please, Sign In to add comment