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
23 # If the sheep grab it, it scores one point.
28 # Common class for the sheep and the duck
32 # Is the sprite stunned?
33 # 0 if no, >0 if stunned.
34 # The value indicate the number of step that remain to be stunt
36 # If a animal is stunned, it cannot move horizontally
37 var stunt_ttl
: Int writable = 0
39 # Common update for animal
40 # handle stunt and edge collision
50 if self.x
+ self.width
> 7900 then
52 self.x
= 7900 - self.width
53 else if self.x
< 0 then
64 # Which frame to show when walking
65 var leg_state
: Int = 0
67 # Is the sheep currently jumping (or falling)
68 var is_jumping
= false
83 if self.y
+ self.height
> 2000 then
86 self.y
= 2000 - self.height
88 self.vy
+= 10 # gravity
91 if not is_jumping
then
92 self.leg_state
= 1 - self.leg_state
# change leg
96 # Try to jump is possible (ie if walking and not stunt)
99 if is_jumping
or stunt_ttl
> 0 then return
101 self.is_jumping
= true
105 # The nemesis of the sheep.
106 # It just go back and forth
122 var apples
= new LiveGroup[Apple]
124 var sheep
= new Sheep
128 var sprites
= new LiveGroup[LiveObject]
140 # Call update on all sprites
146 a
.x
= (60.rand
+ 10) * 100
153 # Eat apple or fallen apple
155 if not a
.exists
then continue
156 if a
.overlaps
(sheep
) then
166 if sheep
.overlaps
(duck
) then
167 if sheep
.is_jumping
and sheep
.vy
> 0 then
171 if sheep
.x
< duck
.x
then
172 sheep
.x
= duck
.x
- sheep
.width
174 sheep
.x
= duck
.x
+ duck
.width
176 sheep
.vx
= - sheep
.vx