lib: move game.nit as lib/scene2d.nit
[nit.git] / examples / leapfrog / leapfrog_curses.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # game of leapfrog: be a sheep and avoid the duck to grab apples
16 #
17 # This module is an example of a simple game using a curses backend
18 module leapfrog_curses
19
20 import curses
21 import leapfrog
22
23 # A simple game view in a curses windows
24 class CursesView
25 super View
26
27 # The associated curses window
28 var window: Window
29
30 redef fun draw_sprite(s: Sprite) do s.draw_on_curses(window)
31 end
32
33 redef class Sprite
34 # Drawing of a sprite in the curse windows
35 fun draw_on_curses(window: Window) is abstract
36 end
37
38 redef class Apple
39 redef fun draw_on_curses(window)
40 do
41 var x = self.x/100
42 var y = self.y/100
43 window.mvaddstr(y, x, "o")
44 end
45 end
46
47 redef class Sheep
48 redef fun draw_on_curses(window)
49 do
50 var x = self.x/100
51 var y = self.y/100
52 if self.is_jumping then
53 if self.vy > 0 then
54 # falling
55 if self.vx > 0 then
56 window.mvaddstr(y, x, "'---@>")
57 window.mvaddstr(y+1, x, " \\-\\'")
58 else
59 window.mvaddstr(y, x, "<@---'")
60 window.mvaddstr(y+1, x, " '/-/")
61 end
62 else
63 # jumping
64 if self.vx > 0 then
65 window.mvaddstr(y, x, ",---@>")
66 window.mvaddstr(y+1, x, " /-/'")
67 else
68 window.mvaddstr(y, x, "<@---,")
69 window.mvaddstr(y+1, x, " '\\-\\")
70 end
71 end
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, " /-|'")
76 else
77 window.mvaddstr(y, x, ",---@>")
78 window.mvaddstr(y+1, x, " |-\\'")
79 end
80 else
81 if self.leg_state == 0 then
82 window.mvaddstr(y, x, "<@---,")
83 window.mvaddstr(y+1, x, " '/-|")
84 else
85 window.mvaddstr(y, x, "<@---,")
86 window.mvaddstr(y+1, x, " '|-\\")
87 end
88 end
89 end
90 end
91
92 redef class Duck
93 redef fun draw_on_curses(window)
94 do
95 var x = self.x/100
96 var y = self.y/100
97
98 if self.vx > 0 then
99 window.mvaddstr(y, x, " @<")
100 window.mvaddstr(y+1, x, "<__)")
101 else
102 window.mvaddstr(y, x, ">@")
103 window.mvaddstr(y+1, x, "(__>")
104 end
105 end
106 end
107
108 redef class PlayScene
109 fun draw_on_curses(view: CursesView)
110 do
111 var window = view.window
112
113 # Redraw the screen
114 window.wclear
115 sprites.draw(view)
116 window.mvaddstr(0, 0, "'q' to quit - score: {score}")
117 window.mvaddstr(20, 0, "#"*80)
118 window.refresh
119
120 # Wait the next frame
121 sys.nanosleep(0, 48000000)
122
123 # Keyboard input
124 while stdin.poll_in do
125 if stdin.eof then return
126 var c = stdin.read_char
127 if c == 'q'.ascii then
128 self.exists = false
129 return
130 end
131 sheep.jump
132 end
133 end
134 end
135
136 var game = new PlayScene
137
138 var win = new Window
139 var main_view = new CursesView(win)
140
141 while game.exists do
142 game.update
143 game.draw_on_curses(main_view)
144 end
145
146 win.delwin
147 win.endwin
148 win.refresh