shipsim.js 728 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var stage = new mtm.Stage('c'),
  2. arrow = new Arrow(stage.width/2, stage.height/2),
  3. vr = 0,
  4. thrust = 0,
  5. vx = 0, vy = 0,
  6. left = 37, right = 39, up = 38, down = 40;
  7. stage.shapes.push(arrow);
  8. stage.play(function() {
  9. arrow.radians += vr * Math.PI/180;
  10. var angle = arrow.radians,
  11. ax = Math.cos(angle) * thrust,
  12. ay = Math.sin(angle) * thrust;
  13. vx += ax;
  14. vy += ay;
  15. arrow.x += vx;
  16. arrow.y += vy;
  17. });
  18. document.addEventListener('keydown', function(e) {
  19. switch(e.keyCode) {
  20. case left:
  21. vr = -5;
  22. break;
  23. case right:
  24. vr = 5;
  25. break;
  26. case up:
  27. thrust = 0.2;
  28. break;
  29. }
  30. }, false);
  31. document.addEventListener('keyup', function(e) {
  32. vr = 0;
  33. thrust = 0;
  34. }, false);