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.
21 app_version
(1, 0, git_revision
)
22 app_name
("Crazy Groundhogs")
36 # Half width of the hit box
38 # Heigth of the hit box
46 init (g
: Game, x
, y
: Int)
57 if (20.0*game
.speed_modifier
).to_i
.rand
== 0 then
62 else if (80.0*game
.speed_modifier
).to_i
.rand
== 0 then
66 else if (100.0*game
.speed_modifier
).to_i
.rand
== 0 then
70 # shot traps only at 50 points and up
72 if game
.points
> 50 then
74 # After 50 points we have more and more traps until point 1000
75 var d
= 1250-(game
.points
- 50)
76 if d
< 200 then d
= 200
78 if d
.rand
< 100 then trap
= true
83 fun intercepts
(event
: PointerEvent): Bool
85 if not up
or hitted
then return false
87 var dx
= (dx
*display_scale
).to_i
88 var dy
= (dy
*display_scale
).to_i
89 var ex
= event
.x
.to_i
- display_offset_x
90 var ey
= event
.y
.to_i
- display_offset_y
91 return ex
> x
- dx
and ex
< x
+ dx
and
92 ey
> y
- dy
and ey
< y
102 if game
.points
< 0 then game
.points
= 0
111 var holes
= new Array[Hole].with_capacity
(4)
114 var modifier_half_life
= 1000.0
115 fun rows
: Int do return 4
116 fun columns
: Int do return 5
120 var speed_modifier
= 1.0
123 var dist_between_rows
= 512
124 var dist_between_columns
= 600
125 fun global_speed_modifier
: Float do return 2.0
129 var dx
= (dist_between_columns
.to_f
*display_scale
).to_i
130 var dy
= (dist_between_rows
.to_f
*display_scale
).to_i
131 for x
in [0 .. columns
[ do
132 for y
in [0 .. rows
[ do
133 holes
.add
(new Hole(self, x
*dx
, y
*dy
))
139 for hole
in holes
do hole
.do_turn
141 speed_modifier
= modifier_half_life
/ (modifier_half_life
+points
.to_f
) * global_speed_modifier
145 # Where all the UI stuff is done
149 fun do_frame
(display
: Display)
151 display
.clear
(0.1, 0.65, 0.2)
153 app
.assets
.sign_warning
.scale
= display_scale
154 app
.assets
.sign_cute
.scale
= display_scale
155 app
.assets
.sign_hits
.scale
= display_scale
156 for img
in app
.numbers
.imgs
do img
.scale
= display_scale
158 display
.blit
(app
.assets
.sign_warning
, (0.0*display_scale
).to_i
, (0.0*display_scale
).to_i
)
159 display
.blit
(app
.assets
.sign_cute
, (740.0*display_scale
).to_i
, (0.0*display_scale
).to_i
)
160 display
.blit
(app
.assets
.sign_hits
, (1640.0*display_scale
).to_i
, (55.0*display_scale
).to_i
)
161 display
.blit_number
(app
.numbers
, game
.points
, (1720.0*display_scale
).to_i
, (170.0*display_scale
).to_i
)
163 for hole
in game
.holes
do
165 var img
= app
.assets
.empty
166 var dx
= 300.0*display_scale
167 var dy
= 256.0*display_scale
168 img
.scale
= display_scale
169 display
.blit
(img
, hole
.x-dx
.to_i
+display_offset_x
, hole
.y-dy
.to_i
+display_offset_y
)
175 dx
= 250.0*display_scale
176 dy
= 512.0*display_scale
179 img
= app
.assets
.trap
180 dx
= 212.0*display_scale
181 dy
= 830.0*display_scale
184 dx
= 212.0*display_scale
185 dy
= 820.0*display_scale
190 img
.scale
= display_scale
191 display
.blit
(img
, hole
.x-dx
.to_i
+display_offset_x
, hole
.y-dy
.to_i
+display_offset_y
)
196 fun input
(event
: InputEvent): Bool
198 if event
isa PointerEvent then
199 for hole
in game
.holes
do
200 if hole
.intercepts
(event
) then
201 if hole
.up
then hole
.hit
213 var screen
: nullable Screen = null
215 # Image set generate by inkscape_tools
216 var assets
= new DrawingImages
218 # Numbers to display the score
219 var numbers
= new NumberImages(assets
.n
)
235 fun init_screen_and_game
do screen
= new Screen
237 redef fun frame_core
(display
)
239 var screen
= self.screen
240 if screen
!= null then
242 screen
.do_frame
(display
)
249 if ie
isa QuitEvent or
250 (ie
isa KeyEvent and ie
.to_c
== 'q') then
253 else if screen
!= null then
254 return screen
.input
(ie
)
256 print
"unknown input: {ie}"
262 fun display_scale
: Float do return 1.0
264 # Depends on the hole center in the uo image
265 fun display_offset_x
: Int do return (512.0*display_scale
).to_i
267 # Depends on the width of the holes
268 fun display_offset_y
: Int do return (800.0*display_scale
).to_i