PageRenderTime 22ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/Snake/win.d

http://github.com/wilkie/djehuty
D | 173 lines | 136 code | 37 blank | 0 comment | 16 complexity | 37c4a15e604ff03de3da9597142f7b0c MD5 | raw file
  1. import djehuty;
  2. import io.console;
  3. import synch.timer;
  4. import cui.window;
  5. import app;
  6. import constants;
  7. import game;
  8. import posn;
  9. import snake;
  10. class SnakeWindow : CuiWindow {
  11. this() {
  12. _frame_wait = FrameWait.Init;
  13. _cod = DeathBy.NotDead;
  14. _timer = new Timer();
  15. setSpeed();
  16. push(_timer);
  17. _game = new SnakeGame(this);
  18. }
  19. void setSpeed() {
  20. uint dirCoef = 1;
  21. if (_game !is null) {
  22. Snake s = _game.snake;
  23. if (s !is null)
  24. dirCoef = s.isVertical ? SpeedCoef.V : SpeedCoef.H;
  25. }
  26. _timer.setInterval(_frame_wait * dirCoef / FrameWait.Multiplier);
  27. }
  28. void gameOver(DeathBy d) {
  29. _cod = d;
  30. _end_time = Time.Now();
  31. _timer.stop();
  32. Console.setColor(fgColor.BrightRed);
  33. Console.putln("DEAD!");
  34. }
  35. override void onInitialize() {
  36. super.onInitialize();
  37. Console.setColor(fgColor.White, bgColor.Black);
  38. string[] instructions = ["move: arrows/wasd", "stop: q or die", "quit: any key"];
  39. foreach (uint y, string s; instructions) {
  40. Console.position(0, y);
  41. Console.put(s);
  42. }
  43. _timer.start();
  44. _start_time = Time.Now();
  45. }
  46. override void onKeyDown(Key k) {
  47. if (_cod != DeathBy.NotDead)
  48. finishUp();
  49. switch(k.code) {
  50. case Key.Up:
  51. case Key.W:
  52. _game.turn(Dir.Up);
  53. speedUp();
  54. break;
  55. case Key.Down:
  56. case Key.S:
  57. _game.turn(Dir.Down);
  58. speedUp();
  59. break;
  60. case Key.Left:
  61. case Key.A:
  62. _game.turn(Dir.Left);
  63. speedUp();
  64. break;
  65. case Key.Right:
  66. case Key.D:
  67. _game.turn(Dir.Right);
  68. speedUp();
  69. break;
  70. case Key.Escape:
  71. case Key.Q:
  72. if (_cod == DeathBy.NotDead) {
  73. gameOver(DeathBy.Quit);
  74. finishUp();
  75. }
  76. break;
  77. default:
  78. break;
  79. }
  80. super.onKeyDown(k);
  81. }
  82. bool onSignal(Dispatcher dsp, uint signal) {
  83. if (dsp is _timer) {
  84. if (_game !is null)
  85. _game.frame();
  86. }
  87. return true;
  88. }
  89. private:
  90. void speedUp() {
  91. _frame_wait -= FrameWait.Step;
  92. if (_frame_wait < FrameWait.Min)
  93. _frame_wait = FrameWait.Min;
  94. }
  95. void finishUp() {
  96. (cast(SnakeApp)(this.application)).shutdown();
  97. Console.clear();
  98. string msg;
  99. uint time = cast(uint)(_end_time - _start_time).seconds;
  100. uint fps = cast(uint)(1000.0 / _frame_wait * FrameWait.Multiplier * 2 / (SpeedCoef.H + SpeedCoef.V));
  101. if (time > 0 && this.width > 0 && this.height > 0 && _game !is null) {
  102. string cod_msg;
  103. switch (_cod) {
  104. case DeathBy.CollisionSelf:
  105. cod_msg = "hit yourself";
  106. break;
  107. case DeathBy.CollisionPortal:
  108. cod_msg = "crashed into a portal";
  109. break;
  110. case DeathBy.CollisionReverse:
  111. cod_msg = "reversed into yourself";
  112. break;
  113. case DeathBy.Quit:
  114. cod_msg = "quit";
  115. break;
  116. default:
  117. cod_msg = "died mysteriously";
  118. break;
  119. }
  120. Snake s = _game.snake;
  121. uint len = 0;
  122. if (s !is null)
  123. len = s.length;
  124. msg = (cod_msg ~ " with {d} segments in {d} seconds on {d} lines and {d} columns at {d} frames per second").format(len, time, this.height, this.width, fps);
  125. } else {
  126. msg = "You didn't even play!";
  127. }
  128. Console.putln(msg);
  129. (cast(SnakeApp)(this.application)).end(0);
  130. }
  131. uint _frame_wait;
  132. DeathBy _cod;
  133. Time _start_time, _end_time;
  134. Timer _timer;
  135. SnakeGame _game;
  136. }