Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use "collections"
- use "time"
- class val Point
- let x: ISize
- let y: ISize
- new val create(x': ISize, y': ISize) =>
- x = x'
- y = y'
- class Notify is TimerNotify
- let _main: Main tag
- new iso create(main: Main tag) =>
- _main = main
- fun ref apply(timer: Timer, count: U64): Bool =>
- _main.step()
- true
- class Handler is StdinNotify
- let _env: Env
- let _main: Main
- new iso create(env: Env, main: Main tag) =>
- env.out.print("Created Handler")
- _env = env
- _main = main
- fun ref apply(
- data: Array[U8 val] iso
- ): None val =>
- let snapshot: Array[U8 val] val = consume data
- let pressed: String val = String.from_array(snapshot)
- // Presumably less performant than getting a char // directly from the first char (the only char we expect)
- // but simple
- match pressed
- | "w" => _main.move(Up)
- | "a" => _main.move(Left)
- | "s" => _main.move(Down)
- | "d" => _main.move(Right)
- | "q" => _main.quit()
- end
- actor Main
- let _env: Env
- var _accepting: Bool = true
- var _timers: Timers
- var _maybeDirection: (Direction| None) = None
- let _snake: List[Point] = List[Point]()
- new create(env: Env) =>
- env.out.print("Starting the program.")
- env.input(Handler(env, this))
- _env = env
- let timers = Timers
- let timer = Timer(Notify(this), 0, 0_100_000_000)
- timers(consume timer)
- _snake.unshift(Point(5,5))
- _snake.unshift(Point(5,6))
- _snake.unshift(Point(5,7))
- _snake.unshift(Point(5,8))
- _snake.unshift(Point(5,9))
- _timers = timers
- be quit() =>
- // quit directive recieved, so finalize main
- _env.out.print("Goodbye! Thanks for playing :)")
- _env.input.dispose()
- _timers.dispose()
- _accepting = false
- fun ref _move_player(d: Direction) => None
- try
- let p = _snake.head()?.apply()?
- let newPoint = match d
- | Up => Point(p.x, p.y - 1)
- | Down => Point(p.x, p.y + 1)
- | Left => Point(p.x - 1, p.y)
- | Right => Point(p.x + 1, p.y)
- end
- _snake.unshift(newPoint)
- _snake.pop()?
- end
- be move(d: Direction) if _accepting => _maybeDirection = d
- be move(d: Direction) => None
- be step() =>
- if(_accepting) then
- match _maybeDirection
- | let d: Direction => _move_player(d)
- end
- end
- _render()
- fun _render() =>
- let buffer: Array[U8 val] iso = recover Array[U8 val]() end
- for y in Range[ISize](0, 30) do
- for x in Range[ISize](0, 30) do
- if _snake.exists({(p: Point val): Bool => (p.x == x) and (p.y == y)})
- then buffer.push('$')
- else buffer.push('.')
- end
- buffer.push(' ')
- end
- buffer.push('\n')
- end
- let final: Array[U8 val] val = consume buffer
- _clear_screen()
- _env.out.print("Use the wasd keys to move around! Or q to quit.")
- _env.out.print(final)
- // only works on Linux.
- fun _clear_screen() =>
- _env.out.print("\ec\e[3J")
- primitive Left
- primitive Right
- primitive Up
- primitive Down
- type Direction is (Left | Right | Up | Down)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement