1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # game of leapfrog: be a sheep and avoid the duck to grab apples
17 # This module is an example of a simple game using a curses backend
18 module leapfrog_curses
23 # A simple game view in a curses windows
27 # The associated curses window
30 redef fun draw_sprite
(s
: Sprite) do s
.draw_on_curses
(window
)
34 # Drawing of a sprite in the curse windows
35 fun draw_on_curses
(window
: Window) is abstract
39 redef fun draw_on_curses
(window
)
43 window
.mvaddstr
(y
, x
, "o")
48 redef fun draw_on_curses
(window
)
52 if self.is_jumping
then
56 window
.mvaddstr
(y
, x
, "'---@>")
57 window
.mvaddstr
(y
+1, x
, " \\-\\'")
59 window
.mvaddstr
(y
, x
, "<@---'")
60 window
.mvaddstr
(y
+1, x
, " '/-/")
65 window
.mvaddstr
(y
, x
, ",---@>")
66 window
.mvaddstr
(y
+1, x
, " /-/'")
68 window
.mvaddstr
(y
, x
, "<@---,")
69 window
.mvaddstr
(y
+1, x
, " '\\-\\")
72 else if self.vx
> 0 then
73 if self.leg_state
== 0 then
74 window
.mvaddstr
(y
, x
, ",---@>")
75 window
.mvaddstr
(y
+1, x
, " /-|'")
77 window
.mvaddstr
(y
, x
, ",---@>")
78 window
.mvaddstr
(y
+1, x
, " |-\\'")
81 if self.leg_state
== 0 then
82 window
.mvaddstr
(y
, x
, "<@---,")
83 window
.mvaddstr
(y
+1, x
, " '/-|")
85 window
.mvaddstr
(y
, x
, "<@---,")
86 window
.mvaddstr
(y
+1, x
, " '|-\\")
93 redef fun draw_on_curses
(window
)
99 window
.mvaddstr
(y
, x
, " @<")
100 window
.mvaddstr
(y
+1, x
, "<__)")
102 window
.mvaddstr
(y
, x
, ">@")
103 window
.mvaddstr
(y
+1, x
, "(__>")
108 redef class PlayScene
109 fun draw_on_curses
(view
: CursesView)
111 var window
= view
.window
116 window
.mvaddstr
(0, 0, "'q' to quit - score: {score}")
117 window
.mvaddstr
(20, 0, "#"*80)
120 # Wait the next frame
121 sys
.nanosleep
(0, 48000000)
124 while sys
.stdin
.poll_in
do
125 if sys
.stdin
.eof
then return
126 var c
= sys
.stdin
.read_char
136 var game
= new PlayScene
139 var main_view
= new CursesView(win
)
143 game
.draw_on_curses
(main_view
)