Advertisement
Guest User

Untitled

a guest
Jan 31st, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. use "collections"
  2. use "time"
  3.  
  4. class val Point
  5. let x: ISize
  6. let y: ISize
  7.  
  8. new val create(x': ISize, y': ISize) =>
  9. x = x'
  10. y = y'
  11.  
  12. class Notify is TimerNotify
  13.  
  14. let _main: Main tag
  15.  
  16. new iso create(main: Main tag) =>
  17. _main = main
  18.  
  19. fun ref apply(timer: Timer, count: U64): Bool =>
  20. _main.step()
  21. true
  22.  
  23. class Handler is StdinNotify
  24.  
  25. let _env: Env
  26. let _main: Main
  27.  
  28. new iso create(env: Env, main: Main tag) =>
  29. env.out.print("Created Handler")
  30. _env = env
  31. _main = main
  32.  
  33. fun ref apply(
  34. data: Array[U8 val] iso
  35. ): None val =>
  36.  
  37. let snapshot: Array[U8 val] val = consume data
  38. let pressed: String val = String.from_array(snapshot)
  39.  
  40. // Presumably less performant than getting a char // directly from the first char (the only char we expect)
  41. // but simple
  42.  
  43. match pressed
  44. | "w" => _main.move(Up)
  45. | "a" => _main.move(Left)
  46. | "s" => _main.move(Down)
  47. | "d" => _main.move(Right)
  48. | "q" => _main.quit()
  49. end
  50.  
  51. actor Main
  52.  
  53. let _env: Env
  54. var _accepting: Bool = true
  55. var _timers: Timers
  56. var _maybeDirection: (Direction| None) = None
  57. let _snake: List[Point] = List[Point]()
  58.  
  59. new create(env: Env) =>
  60. env.out.print("Starting the program.")
  61. env.input(Handler(env, this))
  62. _env = env
  63.  
  64. let timers = Timers
  65. let timer = Timer(Notify(this), 0, 0_100_000_000)
  66. timers(consume timer)
  67.  
  68. _snake.unshift(Point(5,5))
  69. _snake.unshift(Point(5,6))
  70. _snake.unshift(Point(5,7))
  71. _snake.unshift(Point(5,8))
  72. _snake.unshift(Point(5,9))
  73.  
  74. _timers = timers
  75.  
  76. be quit() =>
  77. // quit directive recieved, so finalize main
  78. _env.out.print("Goodbye! Thanks for playing :)")
  79. _env.input.dispose()
  80. _timers.dispose()
  81. _accepting = false
  82.  
  83. fun ref _move_player(d: Direction) => None
  84.  
  85. try
  86. let p = _snake.head()?.apply()?
  87.  
  88. let newPoint = match d
  89. | Up => Point(p.x, p.y - 1)
  90. | Down => Point(p.x, p.y + 1)
  91. | Left => Point(p.x - 1, p.y)
  92. | Right => Point(p.x + 1, p.y)
  93. end
  94.  
  95. _snake.unshift(newPoint)
  96. _snake.pop()?
  97. end
  98.  
  99.  
  100. be move(d: Direction) if _accepting => _maybeDirection = d
  101. be move(d: Direction) => None
  102.  
  103. be step() =>
  104.  
  105. if(_accepting) then
  106. match _maybeDirection
  107. | let d: Direction => _move_player(d)
  108. end
  109. end
  110.  
  111. _render()
  112.  
  113. fun _render() =>
  114.  
  115. let buffer: Array[U8 val] iso = recover Array[U8 val]() end
  116.  
  117. for y in Range[ISize](0, 30) do
  118. for x in Range[ISize](0, 30) do
  119.  
  120. if _snake.exists({(p: Point val): Bool => (p.x == x) and (p.y == y)})
  121. then buffer.push('$')
  122. else buffer.push('.')
  123. end
  124.  
  125. buffer.push(' ')
  126. end
  127. buffer.push('\n')
  128. end
  129.  
  130. let final: Array[U8 val] val = consume buffer
  131.  
  132. _clear_screen()
  133. _env.out.print("Use the wasd keys to move around! Or q to quit.")
  134. _env.out.print(final)
  135.  
  136. // only works on Linux.
  137. fun _clear_screen() =>
  138. _env.out.print("\ec\e[3J")
  139.  
  140. primitive Left
  141. primitive Right
  142. primitive Up
  143. primitive Down
  144.  
  145. type Direction is (Left | Right | Up | Down)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement