Merge: mongodb: Fixed failing test for `aggregate` method.
[nit.git] / examples / leapfrog / leapfrog.nit
index cfecc7e..6aa6f48 100644 (file)
 # This module is an example of a simple game using a curses backend
 module leapfrog
 
-import curses
-import game
-
-# A simple game view in a curses windows
-class CursesView
-       super View
-
-       # The associated curses window
-       var window: Window
-
-       redef fun draw_sprite(s: Sprite) do s.draw_on_curses(window)
-end
-
-redef class Sprite
-       # Drawing of a sprite in the curse windows
-       fun draw_on_curses(window: Window) is abstract
-end
+import scene2d
 
 # A falling apple
 # If the sheep grab it, it scores one point.
 class Apple
        super Sprite
-
-       redef fun draw_on_curses(window)
-       do
-               var x = self.x/100
-               var y = self.y/100
-               window.mvaddstr(y, x,  "o")
-       end
 end
 
 # Common class for the sheep and the duck
@@ -57,7 +34,7 @@ class Animal
        # The value indicate the number of step that remain to be stunt
        #
        # If a animal is stunned, it cannot move horizontally
-       var stunt_ttl: Int writable = 0
+       var stunt_ttl: Int = 0 is writable
 
        # Common update for animal
        # handle stunt and edge collision
@@ -123,49 +100,6 @@ class Sheep
                self.vy = -100
                self.is_jumping = true
        end
-
-       redef fun draw_on_curses(window)
-       do
-               var x = self.x/100
-               var y = self.y/100
-               if self.is_jumping then
-                       if self.vy > 0 then
-                               # falling
-                               if self.vx > 0 then
-                                       window.mvaddstr(y, x,   "'---@>")
-                                       window.mvaddstr(y+1, x, " \\-\\'")
-                               else
-                                       window.mvaddstr(y, x,   "<@---'")
-                                       window.mvaddstr(y+1, x, " '/-/")
-                               end
-                       else
-                               # jumping
-                               if self.vx > 0 then
-                                       window.mvaddstr(y, x,   ",---@>")
-                                       window.mvaddstr(y+1, x, " /-/'")
-                               else
-                                       window.mvaddstr(y, x,   "<@---,")
-                                       window.mvaddstr(y+1, x, " '\\-\\")
-                               end
-                       end
-               else if self.vx > 0 then
-                       if self.leg_state == 0 then
-                               window.mvaddstr(y, x,   ",---@>")
-                               window.mvaddstr(y+1, x, " /-|'")
-                       else
-                               window.mvaddstr(y, x,   ",---@>")
-                               window.mvaddstr(y+1, x, " |-\\'")
-                       end
-               else
-                       if self.leg_state == 0 then
-                               window.mvaddstr(y, x,   "<@---,")
-                               window.mvaddstr(y+1, x, " '/-|")
-                       else
-                               window.mvaddstr(y, x,   "<@---,")
-                               window.mvaddstr(y+1, x, " '|-\\")
-                       end
-               end
-       end
 end
 
 # The nemesis of the sheep.
@@ -180,109 +114,70 @@ class Duck
                self.width = 400
                self.height = 200
        end
-
-       redef fun draw_on_curses(window)
-       do
-               var x = self.x/100
-               var y = self.y/100
-
-               if self.vx > 0 then
-                       window.mvaddstr(y, x,   "  @<")
-                       window.mvaddstr(y+1, x, "<__)")
-               else
-                       window.mvaddstr(y, x,   ">@")
-                       window.mvaddstr(y+1, x, "(__>")
-               end
-       end
 end
 
-class Game
-       fun run(window: Window)
-       do
-               var main_view = new CursesView(window)
+class PlayScene
+       super Scene
+
+       var apples = new LiveGroup[Apple]
+       var duck = new Duck
+       var sheep = new Sheep
 
-               var apples = new LiveGroup[Apple]
-               var duck = new Duck
-               var sheep = new Sheep
+       var score = 0
 
-               var score = 0
+       var sprites = new LiveGroup[LiveObject]
 
-               var sprites = new LiveGroup[LiveObject]
+       init
+       do
                sprites.add(apples)
                sprites.add(duck)
                sprites.add(sheep)
+       end
 
-               loop
+       redef fun update
+       do
 
-                       # Call update on all sprites
-                       sprites.update
+               # Call update on all sprites
+               sprites.update
+
+               # Need a new apple
+               if 10.rand < 2  then
+                       var a = new Apple
+                       a.x = (60.rand + 10) * 100
+                       a.y = 0
+                       a.vx = 0
+                       a.vy = 70.rand + 30
+                       apples.add(a)
+               end
 
-                       # Need a new apple
-                       if 10.rand < 2  then
-                               var a = new Apple
-                               a.x = (60.rand + 10) * 100
-                               a.y = 0
-                               a.vx = 0
-                               a.vy = 70.rand + 30
-                               apples.add(a)
+               # Eat apple or fallen apple
+               for a in apples do
+                       if not a.exists then continue
+                       if a.overlaps(sheep) then
+                               score += 1
+                               a.exists = false
                        end
-
-                       # Eat apple or fallen apple
-                       for a in apples do
-                               if not a.exists then continue
-                               if a.overlaps(sheep) then
-                                       score += 1
-                                       a.exists = false
-                               end
-                               if a.y > 2000 then
-                                       a.exists = false
-                               end
+                       if a.y > 2000 then
+                               a.exists = false
                        end
+               end
 
-                       # Sheep vs duck
-                       if sheep.overlaps(duck) then
-                               if sheep.is_jumping and sheep.vy > 0 then
-                                       duck.stunt_ttl = 5
-                                       sheep.vy = -150
+               # Sheep vs duck
+               if sheep.overlaps(duck) then
+                       if sheep.is_jumping and sheep.vy > 0 then
+                               duck.stunt_ttl = 5
+                               sheep.vy = -150
+                       else
+                               if sheep.x < duck.x then
+                                       sheep.x = duck.x - sheep.width
                                else
-                                       if sheep.x < duck.x then
-                                               sheep.x = duck.x - sheep.width
-                                       else
-                                               sheep.x = duck.x + duck.width
-                                       end
-                                       sheep.vx = - sheep.vx
-                                       duck.vx = - duck.vx
+                                       sheep.x = duck.x + duck.width
                                end
-                       end
-
-                       # Redraw the screen
-                       window.wclear
-                       sprites.draw(main_view)
-                       window.mvaddstr(0, 0,   "'q' to quit - score: {score}")
-                       window.mvaddstr(20, 0,   "#"*80)
-                       window.refresh
-
-                       # Wait the next frame
-                       sys.nanosleep(0, 48000000)
-
-                       # Keyboard input
-                       while stdin.as(Stdin).poll_in do
-                               if stdin.eof then return
-                               var c = stdin.read_char
-                               if c == 'q'.ascii then return
-                               sheep.jump
+                               sheep.vx = - sheep.vx
+                               duck.vx = - duck.vx
                        end
                end
        end
-end
-
-var game = new Game
 
-var win = new Window
-
-game.run(win)
-
-win.delwin
-win.endwin
-win.refresh
+end