1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2012-2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 # This is a minimal practical example of the mnit framework.
35 init (g
: Game, x
, y
: Int)
47 if (20.0*game
.speed_modifier
).to_i
.rand
== 0 then
52 else if (80.0*game
.speed_modifier
).to_i
.rand
== 0 then
56 else if (100.0*game
.speed_modifier
).to_i
.rand
== 0 then
62 fun intercepts
(event
: PointerEvent): Bool
66 return ex
> x
and ex
< x
+ dim
and
67 ey
> y
and ey
< y
+ dim
77 else abort # should not happen
82 var holes
= new Array[Hole].with_capacity
(4)
85 var modifier_half_life
= 40.0
91 var speed_modifier
= 1.0
94 var img_ori_dim
: Int = 256
95 fun img_dim
: Int do return 210
96 fun global_speed_modifier
: Float do return 2.0
101 for x
in [0 .. rows
[do
102 for y
in [0 .. columns
[do
103 holes
.add
(new Hole(self, x
*d
, y
*d
))
109 for hole
in holes
do hole
.do_turn
111 speed_modifier
= modifier_half_life
/ (modifier_half_life
+points
.to_f
) * global_speed_modifier
115 # Where all the UI stuff is done
120 var numbers
: NumberImages
126 empty_img
= app
.load_image
("images/empty.png")
127 up_img
= app
.load_image
("images/up.png")
128 hit_img
= app
.load_image
("images/hit.png")
129 numbers
= app
.load_numbers
("images/#.png")
131 var scale
= game
.img_dim
.to_f
/ game
.img_ori_dim
.to_f
132 empty_img
.scale
= scale
134 hit_img
.scale
= scale
137 fun do_frame
(display
: Display)
139 display
.clear
(0.0, 0.7, 0.0)
141 for hole
in game
.holes
do
152 display
.blit
(img
, hole
.x
, hole
.y-64
)
155 display
.blit_number
(numbers
, game
.points
, 20, 20)
158 fun input
(event
: InputEvent): Bool
160 if event
isa PointerEvent then
161 for hole
in game
.holes
do
162 if hole
.intercepts
(event
) then
163 if hole
.up
then hole
.hit
176 var screen
: nullable Screen = null
178 var target_dt
= 20000000
182 redef fun init_window
186 screen
= new Screen(self)
189 redef fun frame_core
(display
)
191 var screen
= self.screen
192 if screen
!= null then
193 var clock
= new Clock
196 screen
.do_frame
(display
)
199 if dt
.sec
== 0 and dt
.nanosec
< target_dt
then
200 var sleep_t
= target_dt
- dt
.nanosec
201 sys
.nanosleep
(0, sleep_t
)
209 if ie
isa QuitEvent or
210 (ie
isa KeyEvent and ie
.to_c
== 'q') then
213 else if screen
!= null then
214 return screen
.input
(ie
)
216 print
"unknown input: {ie}"