ecdc64a11ab6e5d8ae5473d6b75e4402d5bf46d8
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.
32 # Half width of the hit box
34 # Heigth of the hit box
41 init (g
: Game, x
, y
: Int)
52 if (20.0*game
.speed_modifier
).to_i
.rand
== 0 then
57 else if (80.0*game
.speed_modifier
).to_i
.rand
== 0 then
61 else if (100.0*game
.speed_modifier
).to_i
.rand
== 0 then
67 fun intercepts
(event
: PointerEvent): Bool
69 if not up
or hitted
then return false
71 var dx
= (dx
*display_scale
).to_i
72 var dy
= (dy
*display_scale
).to_i
73 var ex
= event
.x
.to_i
- display_offset_x
74 var ey
= event
.y
.to_i
- display_offset_y
75 return ex
> x
- dx
and ex
< x
+ dx
and
76 ey
> y
- dy
and ey
< y
86 else abort # should not happen
91 var holes
= new Array[Hole].with_capacity
(4)
94 var modifier_half_life
= 40.0
95 fun rows
: Int do return 4
96 fun columns
: Int do return 5
100 var speed_modifier
= 1.0
103 var dist_between_rows
= 512
104 var dist_between_columns
= 600
105 fun global_speed_modifier
: Float do return 2.0
109 var dx
= (dist_between_columns
.to_f
*display_scale
).to_i
110 var dy
= (dist_between_rows
.to_f
*display_scale
).to_i
111 for x
in [0 .. columns
[ do
112 for y
in [0 .. rows
[ do
113 holes
.add
(new Hole(self, x
*dx
, y
*dy
))
119 for hole
in holes
do hole
.do_turn
121 speed_modifier
= modifier_half_life
/ (modifier_half_life
+points
.to_f
) * global_speed_modifier
125 # Where all the UI stuff is done
130 var numbers
: NumberImages
136 empty_img
= app
.load_image
("images/empty.png")
137 up_img
= app
.load_image
("images/up.png")
138 hit_img
= app
.load_image
("images/hit.png")
139 numbers
= app
.load_numbers
("images/#.png")
142 fun do_frame
(display
: Display)
144 display
.clear
(0.0, 0.7, 0.0)
146 for hole
in game
.holes
do
153 dx
= 256.0*display_scale
154 dy
= 417.0*display_scale
157 dx
= 512.0*display_scale
158 dy
= 830.0*display_scale
161 dx
= 256.0*display_scale
162 dy
= 244.0*display_scale
165 img
.scale
= display_scale
166 display
.blit
(img
, hole
.x-dx
.to_i
+display_offset_x
, hole
.y-dy
.to_i
+display_offset_y
)
169 display
.blit_number
(numbers
, game
.points
, 20, 20)
172 fun input
(event
: InputEvent): Bool
174 if event
isa PointerEvent then
175 for hole
in game
.holes
do
176 if hole
.intercepts
(event
) then
177 if hole
.up
then hole
.hit
190 var screen
: nullable Screen = null
192 var target_dt
= 20000000
196 redef fun init_window
203 fun init_screen_and_game
do screen
= new Screen(self)
205 redef fun frame_core
(display
)
207 var screen
= self.screen
208 if screen
!= null then
209 var clock
= new Clock
212 screen
.do_frame
(display
)
215 if dt
.sec
== 0 and dt
.nanosec
< target_dt
then
216 var sleep_t
= target_dt
- dt
.nanosec
217 sys
.nanosleep
(0, sleep_t
)
225 if ie
isa QuitEvent or
226 (ie
isa KeyEvent and ie
.to_c
== 'q') then
229 else if screen
!= null then
230 return screen
.input
(ie
)
232 print
"unknown input: {ie}"
238 fun display_scale
: Float do return 1.0
240 # Depends on the hole center in the uo image
241 fun display_offset_x
: Int do return (512.0*display_scale
).to_i
243 # Depends on the width of the holes
244 fun display_offset_y
: Int do return (800.0*display_scale
).to_i