< Zurück

02.12.2012 22:46:00 • Categories: Monkey X, 2D Game SDK / Framework, Multiplatform • Tags: Monkey

Monkey: class example with extends, implements and abstract

Strict

Import mojo

Interface IUpdateable   Method OnUpdate:Int() End

Interface IRenderable   Method OnRender:Int() End

Class AbstractEntity Abstract   Global cw:Int   Global ch:Int

  Method New (cw:Int, ch:Int)     Self.cw = cw;     Self.ch = ch;   End

End

Class Ball Extends AbstractEntity Implements IUpdateable, IRenderable   Field x:Int   Field y:Int   Field xs:Int   Field ys:Int

  Method New ()     Self.x = 0     Self.y = 0     Self.xs = 1     Self.ys = 1   End

  Method New (cw:Int, ch:Int)     Super.New(cw, ch)     Self.x = 0     Self.y = 0     Self.xs = 1     Self.ys = 1   End

  Method OnUpdate:Int()     Self.x += xs     Self.y += ys     If Self.x > Self.cw-16 Or Self.x < 0       Self.xs =- Self.xs     End     If Self.y > Self.ch-16 Or Self.y < 0       Self.ys =- Self.ys     End     Return 0   End

  Method OnRender:Int()     DrawCircle Self.x, Self.y, 16     Return 0   End End

Class Pad Extends AbstractEntity Implements IUpdateable, IRenderable   Field x:Int   Field y:Int   Field xs:Int

  Method New ()     Self.x = 0     Self.y = ch - 16     Self.xs = 1   End

  Method New (cw:Int, ch:Int)     Super.New(cw, ch)     Self.x = 0     Self.y = ch - 16     Self.xs = 1   End

  Method OnUpdate:Int()     Self.x += xs     If Self.x > Self.cw-64 Or Self.x < 0       Self.xs =- Self.xs     End   Return 0   End

  Method OnRender:Int()     DrawRect Self.x, Self.y, 64, 8   Return 0   End End

Class game Extends App

  Field ball:Ball   Field pad:Pad

  Method OnCreate:Int()     SetUpdateRate(60)     ball = New Ball(DeviceWidth(), DeviceHeight())     pad = New Pad()     Return 0   End

  Method OnUpdate:Int()     ball.OnUpdate()     pad.OnUpdate()     Return 0   End

  Method OnRender:Int()     Cls 0,0,0     ball.OnRender()     pad.OnRender()     Return 0   End End 

Function Main:Int()   New game   Return 0 End


< Zurück | ^ nach oben