├── 2d Platformer Action Rpg.monkey2 ├── 2d Platformer RPG - 01.monkey2 ├── 2d Platformer RPG 02.monkey2 ├── 2d Platformer RPG 03.monkey2 ├── 2d Platformer RPG 04.monkey2 ├── 2d Platformer RPG 05.monkey2 ├── 2d Platformer RPG 06.monkey2 ├── Army Camouflage Pattern Generator.monkey2 ├── BattleDungeons001.monkey2 ├── BattleRoom.monkey2 ├── Beginners - Angle between two points.monkey2 ├── Beginners - Array of Arrays Tilemap.monkey2 ├── Beginners - Arrays creating.monkey2 ├── Beginners - Arrays length GetSize.monkey2 ├── Beginners - BitWise tiles.monkey2 ├── Beginners - Blendmode and Linewidth.monkey2 ├── Beginners - Canvas getcolor r g b.monkey2 ├── Beginners - Clamp.monkey2 ├── Beginners - Class Array.monkey2 ├── Beginners - Class inside Class.monkey2 ├── Beginners - ClearColor.monkey2 ├── Beginners - Color Vector manipulation.monkey2 ├── Beginners - Color and DrawText and Clear.monkey2 ├── Beginners - Confirm.monkey2 ├── Beginners - Creating Ints and Floats and Strings.monkey2 ├── Beginners - DesktopDir.monkey2 ├── Beginners - Distance Uclidean and Manhattan.monkey2 ├── Beginners - DrawPoly.monkey2 ├── Beginners - DrawTriangle and Mouse.monkey2 ├── Beginners - Elvis Operator.monkey2 ├── Beginners - Enum.monkey2 ├── Beginners - Font Text Width and Height.monkey2 ├── Beginners - For loop step.monkey2 ├── Beginners - For loop.monkey2 ├── Beginners - Fullscreen 2 mode.monkey2 ├── Beginners - Fullscreen Mode.monkey2 ├── Beginners - Function T return resizearray.monkey2 ├── Beginners - Getting int value of string.monkey2 ├── Beginners - IsAlpha and IsDigit.monkey2 ├── Beginners - IsHexDigit and IsSpace.monkey2 ├── Beginners - Lambda Example 1.monkey2 ├── Beginners - Lambda Example 2.monkey2 ├── Beginners - Lambda Example 3.monkey2 ├── Beginners - LineWidth.monkey2 ├── Beginners - Lists creating and looping.monkey2 ├── Beginners - LoadString SaveString.monkey2 ├── Beginners - Max.monkey2 ├── Beginners - Microsecs and Millisecs.monkey2 ├── Beginners - Mid Command.monkey2 ├── Beginners - Min.monkey2 ├── Beginners - Mouse PointerVisible.monkey2 ├── Beginners - Now.monkey2 ├── Beginners - OpenUrl.monkey2 ├── Beginners - Outlinemode outlinecolor outlinewidth.monkey2 ├── Beginners - Printing Boolean variables.monkey2 ├── Beginners - Proceed.monkey2 ├── Beginners - Quit End Terminate App.monkey2 ├── Beginners - Rectf and DrawRect.monkey2 ├── Beginners - RequestDir.monkey2 ├── Beginners - RequestFile.monkey2 ├── Beginners - Return List from Function.monkey2 ├── Beginners - Return Stack Class from Function.monkey2 ├── Beginners - Return Stack from Function.monkey2 ├── Beginners - Rnd.monkey2 ├── Beginners - RndUlong.monkey2 ├── Beginners - SeedRnd.monkey2 ├── Beginners - Select case.monkey2 ├── Beginners - Stack.monkey2 ├── Beginners - String Create and Compare.monkey2 ├── Beginners - String FromChar.monkey2 ├── Beginners - Vec2f (ship movement).monkey2 ├── Beginners - Window Title.monkey2 ├── Beginners - Window Width and Height.monkey2 ├── Beginners - getangle Get angle between two points.monkey2 ├── Beginners - lerp function - linear interpolation.monkey2 ├── Brute Force Pathfinding Lookup.monkey2 ├── Brute Force Pathfinding.monkey2 ├── CivClone 0.1.monkey2 ├── CivClone 0.2.monkey2 ├── CivClone 0.3.monkey2 ├── CivClone 0.4.monkey2 ├── CivClone 0.4b.monkey2 ├── CivClone 0.5.monkey2 ├── CivClone 0.6.monkey2 ├── CivClone 0.65.monkey2 ├── CivClone 0.70.monkey2 ├── CivClone 0.75.monkey2 ├── Exampel - Mouse Released and Pressed and Down.monkey2 ├── Example - 2d Fireworks.monkey2 ├── Example - 2d Homing Missiles.monkey2 ├── Example - 2d Spaceship controls.monkey2 ├── Example - 4 Way Flood Fill Pathfinding.monkey2 ├── Example - AI taking Cover shooter.monkey2 ├── Example - Ai cities on random map.monkey2 ├── Example - App FPS Command.monkey2 ├── Example - Astar Pathfinding.monkey2 ├── Example - Background - 3 rlines.monkey2 ├── Example - Background - Blue Cloudy.monkey2 ├── Example - Background - Tinted Gradient.monkey2 ├── Example - Background - Yellow wormy grain.monkey2 ├── Example - Background - red gradient.monkey2 ├── Example - Binary Addition.monkey2 ├── Example - Boids Flocking.monkey2 ├── Example - Bresenham Line algorithm - code example.monkey2 ├── Example - Brownian caves.monkey2 ├── Example - Brownian map test.monkey2 ├── Example - C64 Maze Generator.monkey2 ├── Example - Chunks system.monkey2 ├── Example - Circle Morphing.monkey2 ├── Example - Clear and Color and DrawRect.monkey2 ├── Example - Closest turn Angle vs target angle.monkey2 ├── Example - Collision - Circle Rect overlap Function.monkey2 ├── Example - Collision - Circles overlap.monkey2 ├── Example - Collision - Line Segment Circle.monkey2 ├── Example - Collision - Lines segments intersect.monkey2 ├── Example - Collision - Point in Triangle.monkey2 ├── Example - Collision - Rectsoverlap function.monkey2 ├── Example - Convex Hull - Gift Wrapping.monkey2 ├── Example - Creating Rects and Intersects.monkey2 ├── Example - Desktop Width and Height.monkey2 ├── Example - Different Framerate.monkey2 ├── Example - Draw to Image at mouse pos.monkey2 ├── Example - DrawQuad.monkey2 ├── Example - Drawpoly and Scissor and Image.monkey2 ├── Example - Eachin Split.monkey2 ├── Example - File Requester.monkey2 ├── Example - Flood Fill Pathfinding.monkey2 ├── Example - Flow Fields.monkey2 ├── Example - Game Window Food.monkey2 ├── Example - Game Window Improvements.monkey2 ├── Example - Game Window Population.monkey2 ├── Example - Game Window Production.monkey2 ├── Example - Game Window Resources.monkey2 ├── Example - Generator - 2d Fantasy maps.monkey2 ├── Example - Generator - 2d Maps using lines.monkey2 ├── Example - Generator - Bush fire map.monkey2 ├── Example - Generator - Cartoon clouds.monkey2 ├── Example - Generator - DLA Maps.monkey2 ├── Example - Generator - Dungeon with halls.monkey2 ├── Example - Generator - Hall Of Rooms.monkey2 ├── Example - Generator - Maze Recursive backtracker.monkey2 ├── Example - Generator - Maze r Tilemap.monkey2 ├── Example - Generator - One Room Map or Cave.monkey2 ├── Example - Generator - Rick Tiles.monkey2 ├── Example - Generator - Rogue Basin Conway maps.monkey2 ├── Example - Generator - Roguelike Maps tiled.monkey2 ├── Example - Generator - Roguelike maps.monkey2 ├── Example - Generator - Rooms Piling Maze.monkey2 ├── Example - Generator - Rooms piling.monkey2 ├── Example - Generator - Tileable Textured Rock.monkey2 ├── Example - Generator - Tiles Stone ux.monkey2 ├── Example - Generator - World easy.monkey2 ├── Example - Generator - Zones Map Outlines.monkey2 ├── Example - Generator - Zones Map.monkey2 ├── Example - Generator Map Sketch Lines.monkey2 ├── Example - Generator Mines 1.monkey2 ├── Example - Gui - Listbox.monkey2 ├── Example - Gui - Menu.monkey2 ├── Example - Gui - Pulldown.monkey2 ├── Example - Gui - Text Editor v1.monkey2 ├── Example - Image Width and Height.monkey2 ├── Example - Increase variable in OnRender.monkey2 ├── Example - Keyboard KeyDown.monkey2 ├── Example - Metaballs.monkey2 ├── Example - Microsecs() and Millisecs().monkey2 ├── Example - Mouse X and Mouse Y.monkey2 ├── Example - MulitDimensional Arrays.monkey2 ├── Example - Notify requester.monkey2 ├── Example - Orientation - point left or right vs line.monkey2 ├── Example - Pi and Cos and Sin.monkey2 ├── Example - Pixmap and Clear and Image.monkey2 ├── Example - Pixmap and SetPixel.monkey2 ├── Example - Player Map Collision Simple.monkey2 ├── Example - Player swinging on a rope.monkey2 ├── Example - Player vs Monsters Main.monkey2 ├── Example - Player vs Monsters on Empty Map.monkey2 ├── Example - Player vs Monsters on Random Map.monkey2 ├── Example - Point vs Polygon Collision.monkey2 ├── Example - Polygon vs Polygon Collision.monkey2 ├── Example - Read and Write ClipboardText.monkey2 ├── Example - Recursion Fractal.monkey2 ├── Example - Replace.monkey2 ├── Example - Rnd and Image.monkey2 ├── Example - Rpg - Battle Screen.monkey2 ├── Example - Rpg - Fetching Quests.monkey2 ├── Example - Save png screenshot of window.monkey2 ├── Example - Smoke Points Poly.monkey2 ├── Example - Smooth values or terrain.monkey2 ├── Example - Starfield Class List.monkey2 ├── Example - Testing BSP.monkey2 ├── Example - Tilemap Data.monkey2 ├── Example - Timers.monkey2 ├── Example - Touch Released X and Y.monkey2 ├── Example - Triangle Fill.monkey2 ├── Example - Vector - Blob fixed speed to mouse.monkey2 ├── Example - Vector - Blob moves to mouse.monkey2 ├── Example - Vector Distance.monkey2 ├── Example - Vectory acc to mouse.monkey2 ├── Example - Water 1.monkey2 ├── Example - Water 2 simple.monkey2 ├── Example - Water 3.monkey2 ├── Example - Water tile count.monkey2 ├── Example - Zooming in canvas center.monkey2 ├── Example - brownian map test 2.monkey2 ├── Example - brownian map test 3.monkey2 ├── Example - canvas Width and Height.monkey2 ├── Example - texteffect stack class erase.monkey2 ├── Example - turret turn left or right.monkey2 ├── Fire_Power_0_1.monkey2 ├── Generator - 2d Simple Pixelart Trees.monkey2 ├── Generator - Sci Fy Weapon images.monkey2 ├── Generator - Seamless grow tiles.monkey2 ├── Generator - Simple 2d Houses.monkey2 ├── Generator - Spaceships or Items Generator.monkey2 ├── Generator - Various rock tiles.monkey2 ├── Generator - World Map part 1.monkey2 ├── Mojox - Example - Button.monkey2 ├── Platformer jump up through tile.monkey2 ├── README.md ├── SpriteEditor ├── spriteeditor14-6-2019.monkey2 ├── spriteeditor3-11-2019.monkey2 ├── spriteeditor5-6-2019.monkey2 ├── spriteeditor6-6-2019.monkey2 ├── spriteeditor7-6-2019.monkey2 ├── spriteeditor8-6-2019.monkey2 └── spriteeditor9-6-2019.monkey2 ├── Template Minimum and keys.monkey2 ├── ai ├── Collision avoidance.monkey2 ├── GeneticAlgotanksrandombase.monkey2 ├── HideandCoverPositions.monkey2 ├── edgegrowfloodpath.monkey2 ├── geneticbeatmap.monkey2 └── geneticbeatmaprandomlevels.monkey2 ├── example - 2d spaceship tilemap.monkey2 ├── games ├── Electroid.monkey2 ├── Swingrope Sister.monkey2 ├── dudespacemining.monkey2 ├── dudespacemining2.monkey2 ├── dudespacemining3.monkey2 ├── dudespacemining4.monkey2 ├── dudespacemining5.monkey2 └── tinytank.monkey2 ├── generators ├── BaseFirePowertiled.monkey2 ├── BaseForFirePower.monkey2 ├── BrushAdditiveClouds.monkey2 ├── Brushadditiveclouds2.monkey2 ├── GeneticAlgorithm_mapgenerator.monkey2 ├── PixelArt Tree.monkey2 ├── PixelPinetree.monkey2 ├── Snippet2DRiverGenerator.monkey2 ├── Wavylines or roads map.monkey2 ├── asteroidmapgen.monkey2 ├── farmworkersmap.monkey2 ├── godstrytiles.monkey2 ├── grow shapes - Shaded edges.monkey2 ├── growvisualiser map.monkey2 ├── growvisualiser.monkey2 ├── texturestonebarklike.monkey2 ├── topdownpixeltree.monkey2 ├── tree2d.monkey2 ├── watertopdown.monkey2 ├── wavefunctioncollapse.monkey2 ├── wavefunctioncollapse2.monkey2 └── wavefunctioncollapse3.monkey2 ├── random mine maps with water.monkey2 ├── randomwalk.monkey2 ├── rpgbook ├── cave.monkey2 ├── charactercreation.monkey2 ├── classselection.monkey2 ├── combat.monkey2 ├── cutscenes.monkey2 ├── dialogtree.monkey2 ├── effects.monkey2 ├── hud.monkey2 ├── inventory.monkey2 ├── monsterleveling.monkey2 ├── overworld.monkey2 ├── playerclass.monkey2 ├── quests.monkey2 ├── shop.monkey2 ├── shopinterface.monkey2 ├── skilltree.monkey2 └── speechwindow.monkey2 └── rts ├── Group Movement Drag Movement.monkey2 ├── Group Movement.monkey2 ├── Multiple Units Avoidance.monkey2 ├── Multiple Units Drag and Move.monkey2 ├── Multiple Units Select and move.monkey2 ├── Single Unit Select And Path.monkey2 └── Single Unit Select and Move.monkey2 /2d Platformer Action Rpg.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class Tilemap 8 | Field tilewidth:Int 9 | Field tileheight:Int 10 | Field minimapimage:Image 11 | Field minimapcanvas:Canvas 12 | Field image:= New Image[10] 13 | Field canvas:= New Canvas[10] 14 | Field width:Int,height:Int 15 | Method New(width:int,height:Int,tilewidth:Int,tileheight:Int) 16 | 17 | Self.width=width 18 | Self.height=height 19 | Self.tilewidth=tilewidth 20 | Self.tileheight=tileheight 21 | initminimap(128,128) 22 | image[0] = New Image(tilewidth,tileheight) 23 | canvas[0] = New Canvas(image[0]) 24 | canvas[0].Clear(Color.Black) 25 | 26 | canvas[0].Color = Color.White 27 | canvas[0].DrawLine(0,0,tilewidth,0) 28 | canvas[0].DrawLine(0,0,0,tileheight) 29 | canvas[0].Flush() 30 | End Method 31 | Method update() 32 | End Method 33 | Method draw(canvas:Canvas) 34 | 'draw the map 35 | For Local y:=0 Until height 36 | For Local x:=0 Until width 37 | canvas.DrawImage(image[0],x*tilewidth,y*tileheight) 38 | Next 39 | Next 40 | 'draw the minimap 41 | canvas.DrawImage(minimapimage,width-minimapimage.Width,0) 42 | End Method 43 | Method updateminimap() 44 | 45 | End Method 46 | Method initminimap(w:Int,h:int) 47 | minimapimage = New Image(w,h) 48 | minimapcanvas = New Canvas(minimapimage) 49 | minimapcanvas.Clear(Color.Black) 50 | updateminimap() 51 | End Method 52 | End Class 53 | 54 | Global tilemap:Tilemap 55 | 56 | Class MyWindow Extends Window 57 | 58 | Method New() 59 | Super.New("Action Rpg",1366,768) 60 | tilemap = New Tilemap(Width,Height,32,32) 61 | End Method 62 | 63 | Method OnRender( canvas:Canvas ) Override 64 | App.RequestRender() ' Activate this method 65 | tilemap.draw(canvas) 66 | ' if key escape then quit 67 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 68 | End Method 69 | 70 | End Class 71 | 72 | Function Main() 73 | New AppInstance 74 | New MyWindow 75 | App.Run() 76 | End Function 77 | -------------------------------------------------------------------------------- /Army Camouflage Pattern Generator.monkey2: -------------------------------------------------------------------------------- 1 | ' Camouflage pattern generator 2 | 3 | #Import "" 4 | #Import "" 5 | 6 | Using std.. 7 | Using mojo.. 8 | 9 | Class camopattern 10 | Field maplayer:Int[,] 11 | Field mapwidth:Int,mapheight:Int 12 | Field screenwidth:Int,screenheight:Int 13 | Field im:Image 14 | Field can:Canvas 15 | Method New(w:Int,h:Int) 16 | mapwidth = w 17 | mapheight = h 18 | im = New Image(mapwidth,mapheight) 19 | can = New Canvas(im) 20 | ' Create a green background 21 | can.Clear(New Color(1.0/255.0*80.0,1.0/255.0*120.0,1.0/255.0*80.0)) 22 | can.Flush() 23 | maplayer = New Int[mapwidth,mapheight] 24 | ' Create the brown pattern 25 | createpattern(New Color(1.0/255.0*100.0,1.0/255.0*86.0,1.0/255.0*60.0)) 26 | ' Create the Yellow pattern 27 | createpattern(New Color(1.0/255.0*200.0,1.0/255.0*185.0,1.0/255.0*130.0)) 28 | ' Create the Black pattern 29 | createpattern(New Color(1.0/255.0*50.0,1.0/255.0*60.0,1.0/255.0*65.0)) 30 | 31 | End Method 32 | ' Here we create the pattern 33 | Method createpattern(mycol:Color) 34 | maplayer = New Int[mapwidth,mapheight] 35 | 36 | ' Create random dots on the layer1 37 | For Local i:Int=0 Until (mapwidth*mapheight)/2000 38 | Local x:Int=Rnd(mapwidth) 39 | Local y:Int=Rnd(mapheight) 40 | maplayer[x,y] = 1 41 | Next 42 | ' Grow the layers 43 | For Local i:Int=0 Until (mapwidth*mapheight)*2 44 | Local x:Int=Rnd(mapwidth) 45 | Local y:Int=Rnd(mapheight) 46 | If maplayer[x,y] = 1 47 | For Local y2:Int=y-Rnd(2) To y+Rnd(2) 48 | For Local x2:Int=x-Rnd(5) To x+Rnd(5) 49 | If x2<0 Or y2<0 Or x2>=mapwidth Or y2>=mapheight Then Continue 50 | maplayer[x2,y2] = 1 51 | Next 52 | Next 53 | End If 54 | Next 55 | ' Put the layer on the image 56 | can.Color = mycol 57 | For Local y:Int=0 Until mapheight 58 | For Local x:Int=0 Until mapwidth 59 | If maplayer[x,y] = 1 60 | can.DrawPoint(x,y) 61 | End If 62 | Next 63 | Next 64 | can.Flush() 65 | End Method 66 | ' Draw the image 67 | Method draw(canvas:Canvas) 68 | canvas.Color = Color.White 69 | canvas.DrawImage(im,0,0) 70 | End Method 71 | End Class 72 | 73 | Global mycamopattern:camopattern 74 | 75 | Class MyWindow Extends Window 76 | 77 | Method New() 78 | mycamopattern = New camopattern(Width,Height) 79 | End method 80 | 81 | Method OnRender( canvas:Canvas ) Override 82 | App.RequestRender() ' Activate this method 83 | mycamopattern.draw(canvas) 84 | If Keyboard.KeyReleased(Key.Space) Then 85 | SeedRnd(Millisecs()) 86 | mycamopattern = New camopattern(Width,Height) 87 | End If 88 | ' if key escape then quit 89 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 90 | End Method 91 | 92 | End Class 93 | 94 | Function Main() 95 | New AppInstance 96 | New MyWindow 97 | App.Run() 98 | End Function 99 | -------------------------------------------------------------------------------- /Beginners - Angle between two points.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End Method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | canvas.Color = Color.White 16 | Local x1:Int=Width/2 17 | Local y1:Int=Height/2 18 | Local x2:Int=Mouse.X 19 | Local y2:Int=Mouse.Y 20 | Local angle:Float=getangle(x1,y1,x2,y2) 21 | canvas.DrawRect(x1,y1,2,2) 22 | canvas.DrawRect(x2,y2,2,2) 23 | Local x3:Int=x1+Cos(angle)*50 24 | Local y3:Int=y1+Sin(angle)*50 25 | canvas.DrawLine(x1,y1,x3,y3) 26 | 'press escape to end 27 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 28 | End Method 29 | End Class 30 | 31 | Function getangle:float(x1:Int,y1:Int,x2:Int,y2:Int) 32 | Return ATan2(y2-y1, x2-x1) 33 | End Function 34 | 35 | Function Main() 36 | New AppInstance 37 | New MyWindow 38 | App.Run() 39 | End Function 40 | -------------------------------------------------------------------------------- /Beginners - Array of Arrays Tilemap.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field map := New Int[][] ( 10 | New Int[]( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ), 11 | New Int[]( 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 ), 12 | New Int[]( 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 ), 13 | New Int[]( 1, 0, 0, 0, 0, 0, 1, 1, 1, 1 ), 14 | New Int[]( 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 ), 15 | New Int[]( 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 ), 16 | New Int[]( 1, 1, 1, 1, 1, 0, 0, 0, 0, 1 ), 17 | New Int[]( 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 ), 18 | New Int[]( 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 ), 19 | New Int[]( 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 ), 20 | New Int[]( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ) ) 21 | Field tilewidth:Int 22 | Field tileheight:Int 23 | Method New() 24 | ' Set The title of the window... 25 | Title="Tilemap example Array of Arrays....." 26 | tilewidth = Width/map[0].Length 27 | tileheight = Height/map.Length 28 | End Method 29 | 30 | Method OnRender( canvas:Canvas ) Override 31 | App.RequestRender() ' Activate this method 32 | ' Clear with black color 33 | canvas.Clear(Color.Black) 34 | canvas.Color = Color.White 35 | For Local y:Int=0 Until map.Length 36 | For Local x:Int=0 Until map[0].Length 37 | If map[y][x] = 1 38 | canvas.DrawRect(x*tilewidth,y*tileheight,tilewidth,tileheight) 39 | End If 40 | Next 41 | Next 42 | ' if key escape then quit 43 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 44 | End Method 45 | 46 | End Class 47 | 48 | Function Main() 49 | New AppInstance 50 | New MyWindow 51 | App.Run() 52 | End Function 53 | -------------------------------------------------------------------------------- /Beginners - Arrays creating.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Field myarray1:Int[] = New Int[](0,1,2,3,4,5,6,7,8,9) 11 | Field myarray2:String[,] = New String[10,10] 12 | Field myarray3:String[,,] = New String[10,10,10] 13 | Method New() 14 | myarray1[0] = 10 15 | myarray2[0,0] = "test1" 16 | myarray3[0,0,0] = "test2" 17 | End Method 18 | 19 | Method OnRender( canvas:Canvas ) Override 20 | App.RequestRender() ' Activate this method 21 | canvas.Color = Color.White 22 | canvas.DrawText(myarray1[0],0,20) 23 | canvas.DrawText(myarray2[0,0],0,40) 24 | canvas.DrawText(myarray3[0,0,0],0,60) 25 | ' if key escape then quit 26 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 27 | End Method 28 | 29 | End Class 30 | 31 | Function Main() 32 | New AppInstance 33 | New MyWindow 34 | App.Run() 35 | End Function 36 | -------------------------------------------------------------------------------- /Beginners - Arrays length GetSize.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | ' no idea yet on how to do length withmultidimensional arrays. 10 | ' documentation points to do it yourself. 11 | Field myarray1:Int[] = New Int[30] 12 | Field myarray2:Int[,] = New Int[10,20] 13 | Method New() 14 | End Method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | canvas.Color = Color.White 19 | canvas.DrawText("myarray1 Length : " +myarray1.Length,0,20) 20 | canvas.DrawText("myarray2 size 0 : " + myarray2.GetSize(0),0,40) 21 | canvas.DrawText("myarray2 size 1 : " + myarray2.GetSize(1),0,60) 22 | 23 | ' if key escape then quit 24 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 25 | End Method 26 | 27 | End Class 28 | 29 | Function Main() 30 | New AppInstance 31 | New MyWindow 32 | App.Run() 33 | End Function 34 | -------------------------------------------------------------------------------- /Beginners - Blendmode and Linewidth.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | canvas.BlendMode=BlendMode.Opaque 16 | canvas.LineWidth = 10 17 | canvas.DrawLine(0,0,100,100) 18 | canvas.BlendMode=BlendMode.None 19 | canvas.LineWidth = 20 20 | canvas.DrawLine(100,0,200,100) 21 | ' if key escape then quit 22 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 23 | End Method 24 | 25 | End Class 26 | 27 | Function Main() 28 | New AppInstance 29 | New MyWindow 30 | App.Run() 31 | End Function 32 | -------------------------------------------------------------------------------- /Beginners - Canvas getcolor r g b.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | Title="canvas getcolor r g b" 12 | End method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | ' if key escape then quit 17 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 18 | canvas.Clear(Color.Black) 19 | canvas.Color = New Color(0.9,0.7,0.1) 20 | Local r:Float=canvas.Color.r 21 | Local g:Float=canvas.Color.g 22 | Local b:Float=canvas.Color.b 23 | canvas.DrawText("Current canvas.color.r:"+r,0,0) 24 | canvas.DrawText("Current canvas.color.g:"+g,0,20) 25 | canvas.DrawText("Current canvas.color.b:"+b,0,40) 26 | End Method 27 | 28 | End Class 29 | 30 | Function Main() 31 | New AppInstance 32 | New MyWindow 33 | App.Run() 34 | End Function 35 | -------------------------------------------------------------------------------- /Beginners - Clamp.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' if key escape then quit 16 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 17 | ' 18 | canvas.DrawText("Clamp example",0,0) 19 | canvas.DrawText("a value = -10. b value = Clamp(a,0,10)",0,20) 20 | Local a:Int=-10 21 | ' If the value of the clamp is lower then the lowest clamp value is set 22 | ' if the value of the clamp is higher then the highest clamp value is set 23 | Local b:Int=Clamp(a,0,10) 24 | canvas.DrawText("value b after clamping = "+b,0,40) 25 | End Method 26 | 27 | End Class 28 | 29 | Function Main() 30 | New AppInstance 31 | New MyWindow 32 | App.Run() 33 | End Function 34 | -------------------------------------------------------------------------------- /Beginners - Class Array.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | ' This the enemy class 8 | Class enemy 9 | ' position x and y 10 | Field px:Int,py:Int 11 | ' This is called when the class is created 12 | Method New(x:Int,y:Int) 13 | ' Set the position 14 | Self.px = x 15 | Self.py = y 16 | End Method 17 | ' Method to modify the px and py 18 | Method move(x:Int,y:Int) 19 | px += x 20 | py += y 21 | End Method 22 | End Class 23 | 24 | Class MyWindow Extends Window 25 | ' How many enemies are there 26 | Field numenemies:Int=10 27 | ' The array class 28 | Field myenemy:enemy[] 29 | ' 30 | Method New() 31 | ' Create our class array 32 | myenemy = New enemy[numenemies] 33 | ' Create our enemies inside the class array 34 | For Local i:Int=0 Until numenemies 35 | myenemy[i] = New enemy(Rnd(Width),Rnd(Height)) 36 | Next 37 | End method 38 | 39 | Method OnRender( canvas:Canvas ) Override 40 | App.RequestRender() ' Activate this method 41 | ' 42 | ' We can read/modify/call anything inside the class array 43 | myenemy[0].move(5,0) 44 | If myenemy[0].px > Width Then myenemy[0].px = -32 45 | ' 46 | ' Draw the enemies to the screen 47 | canvas.Clear(Color.Black) 48 | canvas.Color = Color.White 49 | ' Loop through everything inside the array 50 | For Local i:=Eachin myenemy 51 | ' i is used to run/read/modify the class array 52 | canvas.DrawRect(i.px,i.py,32,32) 53 | Next 54 | ' if key escape then quit 55 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 56 | End Method 57 | 58 | End Class 59 | 60 | Function Main() 61 | New AppInstance 62 | New MyWindow 63 | App.Run() 64 | End Function 65 | -------------------------------------------------------------------------------- /Beginners - Class inside Class.monkey2: -------------------------------------------------------------------------------- 1 | ' Class inside Class Example 2 | 3 | #Import "" 4 | #Import "" 5 | 6 | Using std.. 7 | Using mojo.. 8 | 9 | Class master 10 | ' The s variable is used to create the slave with 11 | Field s:slave 12 | ' This is a class inside a class 13 | Class slave 14 | ' Fields 15 | Field x:Int,y:Int 16 | ' New method 17 | Method New(x:Int,y:Int) 18 | ' Assign self x and y with new x'es and y'es 19 | Self.x = x 20 | Self.y = y 21 | End Method 22 | ' Draw method which draws on canvas 23 | Method draw(canvas:Canvas) 24 | canvas.DrawCircle(x,y,100) 25 | End Method 26 | End Class 27 | ' new method for main class 28 | Method New(x:Int,y:Int) 29 | ' Assign into s from class inside the main 30 | ' class. 31 | s = New slave(x,y) 32 | End Method 33 | ' update class in main class which passes 34 | ' the canvas(draw) 35 | Method update(canvas:Canvas) 36 | s.draw(canvas) 37 | End Method 38 | End Class 39 | 40 | ' make a global variable which we put the master 41 | ' class into. We create it here also. 42 | Global mymaster:master = New master(200,200) 43 | 44 | 45 | Class MyWindow Extends Window 46 | 47 | Method New() 48 | End Method 49 | 50 | Method OnRender( canvas:Canvas ) Override 51 | App.RequestRender() ' Activate this method 52 | ' Here we call the mymaster update method 53 | ' canvas which we draw on is passed into the call 54 | mymaster.update(canvas) 55 | ' We can modify variables from classes in classes 56 | ' also. 57 | ' mymaster is the variable we created the master class 58 | ' with. 59 | ' s is the variable inside the mymaster class we created 60 | ' the slave class with. 61 | mymaster.s.x = Rnd(Width) 62 | ' if key escape then quit 63 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 64 | End Method 65 | 66 | End Class 67 | 68 | Function Main() 69 | New AppInstance 70 | New MyWindow 71 | App.Run() 72 | End Function 73 | -------------------------------------------------------------------------------- /Beginners - ClearColor.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | ' Set the current clear color 12 | ClearColor = Color.Black 13 | End method 14 | 15 | Method OnRender( canvas:Canvas ) Override 16 | App.RequestRender() ' Activate this method 17 | ' if key escape then quit 18 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 19 | End Method 20 | 21 | End Class 22 | 23 | Function Main() 24 | New AppInstance 25 | New MyWindow 26 | App.Run() 27 | End Function 28 | -------------------------------------------------------------------------------- /Beginners - Color Vector manipulation.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | 16 | canvas.Color = Color.Red 17 | canvas.DrawRect(0,10,90,90) 18 | canvas.Color = Color.Red*5 19 | canvas.DrawRect(100,10,90,90) 20 | canvas.Color = Color.Red/2 21 | canvas.DrawRect(200,10,90,90) 22 | canvas.Color = Color.Blue+.2 23 | canvas.DrawRect(300,10,90,90) 24 | canvas.Color = Color.Blue.Blend(Color.White,.7) 25 | canvas.DrawRect(400,10,90,90) 26 | 27 | ' if key escape then quit 28 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 29 | End Method 30 | 31 | End Class 32 | 33 | Function Main() 34 | New AppInstance 35 | New MyWindow 36 | App.Run() 37 | End Function 38 | -------------------------------------------------------------------------------- /Beginners - Color and DrawText and Clear.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' if key escape then quit 16 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 17 | ' The color goes from 0.0 to 1.0 18 | ' red,green,blue,alpha 19 | ' you can also set color by 20 | ' canvas.Color = Color.Black 'ect 21 | canvas.Clear(New Color(0,0,0,1)) 22 | canvas.Color = New Color(0.5,0,0,1) 23 | canvas.DrawText("Test....",0,0) 24 | End Method 25 | 26 | End Class 27 | 28 | Function Main() 29 | New AppInstance 30 | New MyWindow 31 | App.Run() 32 | End Function 33 | -------------------------------------------------------------------------------- /Beginners - Confirm.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field r:bool 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' 16 | If Keyboard.KeyReleased(Key.F1) 17 | r=Confirm("Title","Is this true?",False) 18 | End If 19 | canvas.DrawText("Press F1 for Notify Request",0,0) 20 | If r=False then canvas.DrawText("Last request return value r = false",0,20) 21 | If r=true then canvas.DrawText("Last request return value r = true",0,20) 22 | ' if key escape then quit 23 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 24 | End Method 25 | 26 | End Class 27 | 28 | Function Main() 29 | New AppInstance 30 | New MyWindow 31 | App.Run() 32 | End Function 33 | -------------------------------------------------------------------------------- /Beginners - Creating Ints and Floats and Strings.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | Global myi:=10 10 | 11 | Class MyWindow Extends Window 12 | 13 | 14 | Field myf:=0.10 15 | Field mys:="hello" 16 | 17 | Method New() 18 | End Method 19 | 20 | 21 | Method OnRender( canvas:Canvas ) Override 22 | ' if key escape then quit 23 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 24 | App.RequestRender() ' Loop with sync 25 | canvas.Clear(Color.Black) 26 | canvas.Color = Color.White 27 | canvas.DrawText("Integer : "+myi,0,0) 28 | canvas.DrawText("Float : "+myf,0,15) 29 | canvas.DrawText("String : "+mys,0,30) 30 | End Method 31 | 32 | End Class 33 | 34 | Function Main() 35 | instance = New AppInstance 36 | 37 | New MyWindow 38 | 39 | App.Run() 40 | 41 | End Function 42 | -------------------------------------------------------------------------------- /Beginners - DesktopDir.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Method New() 10 | End Method 11 | 12 | Method OnRender( canvas:Canvas ) Override 13 | App.RequestRender() ' Activate this method 14 | ' 15 | canvas.DrawText("DesktopDir = "+DesktopDir(),0,60) 16 | ' if key escape then quit 17 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 18 | End Method 19 | 20 | End Class 21 | 22 | Function Main() 23 | New AppInstance 24 | New MyWindow 25 | App.Run() 26 | End Function 27 | -------------------------------------------------------------------------------- /Beginners - Distance Uclidean and Manhattan.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Method New() 10 | End method 11 | 12 | Method OnRender( canvas:Canvas ) Override 13 | App.RequestRender() ' Activate this method 14 | 15 | canvas.Color = Color.White 16 | Local a:Float=edistance(Mouse.X,Mouse.Y,320,240) 17 | Local b:Float=distance(Mouse.X,Mouse.Y,320,240) 18 | canvas.DrawText("Uclidean distance :"+a,0,0) 19 | canvas.DrawText("Manhattan distance :"+b,0,20) 20 | canvas.DrawLine(Mouse.X,Mouse.Y,320,240) 21 | 22 | ' if key escape then quit 23 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 24 | End Method 25 | Function edistance:Float(x1:Float,y1:Float,x2:Float,y2:Float) 26 | Return Sqrt( (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) ) 27 | End Function 28 | ' Manhattan Distance (less precise) 29 | Function distance:Float(x1:Float,y1:Float,x2:Float,y2:Float) 30 | Return Abs(x2-x1)+Abs(y2-y1) 31 | End Function 32 | End Class 33 | 34 | Function Main() 35 | New AppInstance 36 | New MyWindow 37 | App.Run() 38 | End Function 39 | -------------------------------------------------------------------------------- /Beginners - DrawPoly.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | Class MyWindow Extends Window 11 | 12 | Method New() 13 | End Method 14 | 15 | Method OnRender( canvas:Canvas ) Override 16 | App.RequestRender() ' Activate this method 17 | ' if key escape then quit 18 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 19 | ' Draw the Poly 20 | canvas.Clear(Color.Black) 21 | canvas.Color = Color.Blue 22 | Local pol:= new Float[6] 23 | pol[0] = Mouse.X 24 | pol[1] = Mouse.Y 25 | pol[2] = 500 26 | pol[3] = 200 27 | pol[4] = 100 28 | pol[5] = 200 29 | canvas.DrawPoly(pol) 30 | End Method 31 | 32 | End Class 33 | 34 | Function Main() 35 | instance = New AppInstance 36 | 37 | New MyWindow 38 | 39 | App.Run() 40 | 41 | End Function 42 | -------------------------------------------------------------------------------- /Beginners - DrawTriangle and Mouse.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | Class MyWindow Extends Window 11 | 12 | Method New() 13 | End Method 14 | 15 | Method OnRender( canvas:Canvas ) Override 16 | App.RequestRender() ' Activate this method 17 | ' if key escape then quit 18 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 19 | ' Draw the Triangle 20 | canvas.Clear(Color.Black) 21 | canvas.Color = Color.Blue 22 | canvas.DrawTriangle(New Vec2f(Mouse.X,Mouse.Y),New Vec2f(200,200),New Vec2f(50,200)) 23 | End Method 24 | 25 | End Class 26 | 27 | Function Main() 28 | instance = New AppInstance 29 | 30 | New MyWindow 31 | 32 | App.Run() 33 | 34 | End Function 35 | -------------------------------------------------------------------------------- /Beginners - Elvis Operator.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field a:Int = 10 10 | Field b:Int = 5 11 | Field c:Int=10 12 | Field d:Int=0 13 | Method New() 14 | ' Elvis Operator. 15 | ' 16 | ' If b = any other value of 0 then a gets 17 | ' value of b else it gets value of -1 . 18 | a=b?Else-1 19 | ' Here c gets the value of -1 because d = 0. 20 | ' 21 | c=d?Else-1 22 | End method 23 | 24 | Method OnRender( canvas:Canvas ) Override 25 | App.RequestRender() ' Activate this method 26 | canvas.Color = Color.Black 27 | canvas.DrawText("a = "+a,0,0) 28 | canvas.DrawText("c = "+c,0,15) 29 | ' if key escape then quit 30 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 31 | End Method 32 | 33 | End Class 34 | 35 | Function Main() 36 | New AppInstance 37 | New MyWindow 38 | App.Run() 39 | End Function 40 | -------------------------------------------------------------------------------- /Beginners - Enum.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | ' Here we create the enum 10 | ' it gives a number to each variable in the enum section 11 | Enum test 12 | a,b,c 13 | End Enum 14 | 15 | Enum test2 16 | a = 10 17 | b = 20 18 | c = 30 19 | End Enum 20 | 21 | Class MyWindow Extends Window 22 | 23 | Method New() 24 | End Method 25 | 26 | Method OnRender( canvas:Canvas ) Override 27 | App.RequestRender() ' Activate this method 28 | ' if key escape then quit 29 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 30 | ' 31 | if test.a = 0 then canvas.DrawText("Enum a = 0",0,0) 32 | If test.b = 1 then canvas.DrawText("Enum b = 1",0,15) 33 | If test.c = 2 then canvas.DrawText("Enum c = 2",0,30) 34 | if test2.a = 10 then canvas.DrawText("Enum 2 a = 10",110,0) 35 | If test2.b = 20 then canvas.DrawText("Enum 2 b = 20",110,15) 36 | If test2.c = 30 then canvas.DrawText("Enum 2 c = 30",110,30) 37 | 38 | End Method 39 | 40 | End Class 41 | 42 | Function Main() 43 | instance = New AppInstance 44 | 45 | New MyWindow 46 | 47 | App.Run() 48 | 49 | End Function 50 | -------------------------------------------------------------------------------- /Beginners - Font Text Width and Height.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | 12 | End Method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | canvas.Clear(Color.Black) 17 | 18 | ' Create a screen filled with random 19 | ' text. 20 | SeedRnd(0) 21 | ' Here the font height is used. 22 | For Local y:Int=0 To Height Step canvas.Font.Height 23 | Local x:Int=0 24 | While x" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | Local loc:=0 16 | For local i:=10 Until 0 Step -1 17 | canvas.DrawText("integer negative i:"+i,100,100+loc) 18 | loc+=10 19 | Next 20 | loc=0 21 | For Local i:=10.0 To 0 Step -0.5 22 | canvas.DrawText("float negative i:"+i,320,100+loc) 23 | loc+=10 24 | Next 25 | ' if key escape then quit 26 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 27 | End Method 28 | 29 | End Class 30 | 31 | Function Main() 32 | New AppInstance 33 | New MyWindow 34 | App.Run() 35 | End Function 36 | -------------------------------------------------------------------------------- /Beginners - For loop.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' for loop 16 | For Local i:=0 Until 10 17 | canvas.DrawText("For loop i:"+i,10,100+i*10) 18 | Next 19 | ' grid 20 | For Local y:=0 To 10 21 | For Local x:=0 To 10 22 | canvas.DrawLine(100+x*42,100+y*32,100+x*42+42,100+y*32) 23 | canvas.DrawLine(100+x*42,100+y*32,100+x*42,100+y*32+32) 24 | canvas.DrawText(""+x+","+y,100+x*42,100+y*32) 25 | Next 26 | Next 27 | ' if key escape then quit 28 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 29 | End Method 30 | 31 | End Class 32 | 33 | Function Main() 34 | New AppInstance 35 | New MyWindow 36 | App.Run() 37 | End Function 38 | -------------------------------------------------------------------------------- /Beginners - Fullscreen 2 mode.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | Super.New("test",1024,768,WindowFlags.Fullscreen) 12 | End Method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | ' if key escape then quit 17 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 18 | ' 19 | If Keyboard.KeyReleased(Key.F) 20 | If Fullscreen = True Then Fullscreen = False Else Fullscreen = True 21 | End If 22 | ' 23 | canvas.Clear(Color.Black) 24 | canvas.Color = Color.White 25 | canvas.DrawText("Press Escape to End.",0,0) 26 | canvas.DrawText("Press key f to switch between fullscreen/Windowed",0,20) 27 | End Method 28 | 29 | End Class 30 | 31 | Function Main() 32 | New AppInstance 33 | New MyWindow() 34 | App.Run() 35 | End Function 36 | -------------------------------------------------------------------------------- /Beginners - Fullscreen Mode.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | Super.New("test",1024,768) 12 | Fullscreen = True 13 | End Method 14 | 15 | Method OnRender( canvas:Canvas ) Override 16 | App.RequestRender() ' Activate this method 17 | ' if key escape then quit 18 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 19 | End Method 20 | 21 | End Class 22 | 23 | Function Main() 24 | New AppInstance 25 | New MyWindow() 26 | App.Run() 27 | End Function 28 | -------------------------------------------------------------------------------- /Beginners - Function T return resizearray.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | ' create a integer 2 dimensional array sized 10x10 10 | Field myarray:Int[,] = New Int[10,10] 11 | Method New() 12 | ' fill the array with random numbers 13 | ' getSize returns the size of the array. (0-first collumn 1 is second ..) 14 | For Local y:=0 Until myarray.GetSize(1) 15 | For Local x:=0 Until myarray.GetSize(0) 16 | myarray[x,y] = Int(Rnd(10,1000)) 17 | Next 18 | Next 19 | 'resize the array to 5x5 20 | myarray = resize2darray(myarray,5,5) 21 | End Method 22 | 23 | Method OnRender( canvas:Canvas ) Override 24 | App.RequestRender() ' Activate this method 25 | 'Draw the contents of the array to the screen. 26 | For Local y:=0 Until myarray.GetSize(1) 27 | For Local x:=0 Until myarray.GetSize(0) 28 | canvas.DrawText(myarray[x,y],x*50,y*20) 29 | Next 30 | Next 31 | 32 | 33 | ' if key escape then quit 34 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 35 | End Method 36 | 37 | End Class 38 | 39 | ' 40 | ' Here we create a function that returns a list and takes in 41 | ' a list. 42 | ' 43 | Function resize2darray:T[,](a:T[,],x:int,y:Int) 44 | ' Create a local array 45 | Local out:= New T[x,y] 46 | ' Loop though the local array 47 | For Local i:=0 Until out.GetSize(0) 48 | For Local j:=0 Until out.GetSize(1) 49 | out[i,j] = a[i,j] 50 | Next 51 | Next 52 | 'return the local array 53 | Return out 54 | End Function 55 | 56 | Function Main() 57 | New AppInstance 58 | New MyWindow 59 | App.Run() 60 | End Function 61 | -------------------------------------------------------------------------------- /Beginners - Getting int value of string.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' if key escape then quit 16 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 17 | ' 18 | canvas.DrawText("Alphabet/Characters integer value example",0,0) 19 | canvas.DrawText("int value of letter a="+"a"[0],0,20) 20 | canvas.DrawText("int value of letter A="+"A"[0],0,40) 21 | End Method 22 | 23 | End Class 24 | 25 | Function Main() 26 | New AppInstance 27 | New MyWindow 28 | App.Run() 29 | End Function 30 | -------------------------------------------------------------------------------- /Beginners - IsAlpha and IsDigit.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End Method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' 16 | If IsAlpha("a"[0]) 17 | canvas.DrawText("a - Is a alpa",0,0) 18 | End If 19 | if IsAlpha("0"[0]) = False 20 | canvas.DrawText("0 - Is not a alpha",0,20) 21 | End If 22 | If IsDigit("0"[0]) 23 | canvas.DrawText("0 - is a Digit",0,40) 24 | End If 25 | If IsDigit("!"[0]) = False 26 | canvas.DrawText("! - is not a Digit",0,60) 27 | End If 28 | ' if key escape then quit 29 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 30 | End Method 31 | 32 | End Class 33 | 34 | Function Main() 35 | New AppInstance 36 | New MyWindow 37 | App.Run() 38 | End Function 39 | -------------------------------------------------------------------------------- /Beginners - IsHexDigit and IsSpace.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End Method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' 16 | If IsHexDigit("F"[0]) 17 | canvas.DrawText("F - is a hex digit",0,0) 18 | End If 19 | If IsHexDigit("G"[0]) = False 20 | canvas.DrawText("G - is not a hex digit",0,20) 21 | End If 22 | If IsSpace(" "[0]) 23 | canvas.DrawText("' ' - is a space character",0,40) 24 | End If 25 | If IsSpace("-"[0]) = False 26 | canvas.DrawText("'-' - is not a space character",0,60) 27 | End If 28 | ' if key escape then quit 29 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 30 | End Method 31 | 32 | End Class 33 | 34 | Function Main() 35 | New AppInstance 36 | New MyWindow 37 | App.Run() 38 | End Function 39 | -------------------------------------------------------------------------------- /Beginners - Lambda Example 1.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | Title="Lambda example 1" 12 | End Method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | ' 17 | ' Here you create a lambda function nearby where you want to use it. 18 | ' You do not have to create a different function for this elsewere in the 19 | ' code. 20 | ' 21 | Local add := Lambda:Int(a:Int, b:Int) 22 | Return a + b 23 | End Lambda 24 | 25 | canvas.DrawText("Lambda function result : "+add(10,10),0,40) 26 | 27 | ' if key escape then quit 28 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 29 | End Method 30 | 31 | End Class 32 | 33 | Function Main() 34 | New AppInstance 35 | New MyWindow 36 | App.Run() 37 | End Function 38 | -------------------------------------------------------------------------------- /Beginners - Lambda Example 2.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | Title="Lambda example 1" 12 | End Method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | ' 17 | ' Here you create a lambda function nearby where you want to use it. 18 | ' Here we create a lambda that returns a string. 19 | ' 20 | Local append := Lambda:String(a:String,b:String) 21 | Return a+b 22 | End lambda 23 | 24 | canvas.DrawText("Lambda function result : "+append("conn","-ect"),0,40) 25 | 26 | ' if key escape then quit 27 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 28 | End Method 29 | 30 | End Class 31 | 32 | Function Main() 33 | New AppInstance 34 | New MyWindow 35 | App.Run() 36 | End Function 37 | -------------------------------------------------------------------------------- /Beginners - Lambda Example 3.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | Title="Lambda example 3" 12 | End Method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | ' 17 | ' Here you create a lambda function nearby where you want to use it. 18 | ' You can modify global things inside lambda functions 19 | ' 20 | global z:Int 21 | Local tst := Lambda() 22 | z=10 23 | End Lambda 24 | tst() 25 | 26 | canvas.DrawText("Lambda function result : "+z,0,40) 27 | 28 | ' if key escape then quit 29 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 30 | End Method 31 | 32 | End Class 33 | 34 | Function Main() 35 | New AppInstance 36 | New MyWindow 37 | App.Run() 38 | End Function 39 | -------------------------------------------------------------------------------- /Beginners - LineWidth.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Method New() 10 | End method 11 | 12 | Method OnRender( canvas:Canvas ) Override 13 | App.RequestRender() ' Activate this method 14 | 15 | 16 | canvas.LineWidth = 1 17 | canvas.DrawLine(50,50,100,100) 18 | canvas.LineWidth = 3 19 | canvas.DrawLine(50,50,200,100) 20 | canvas.LineWidth = 10 21 | canvas.DrawLine(50,50,300,100) 22 | 23 | 24 | ' if key escape then quit 25 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 26 | End Method 27 | End Class 28 | 29 | Function Main() 30 | New AppInstance 31 | New MyWindow 32 | App.Run() 33 | End Function 34 | -------------------------------------------------------------------------------- /Beginners - Lists creating and looping.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | Global mylist := New List 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | For Local i:=0 Until 10 15 | mylist.AddLast(i) 16 | Next 17 | End Method 18 | 19 | 20 | Method OnRender( canvas:Canvas ) Override 21 | ' if key escape then quit 22 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 23 | App.RequestRender() ' Loop with sync 24 | canvas.Clear(Color.Black) 25 | canvas.Color = Color.White 26 | Local y:=0 27 | For Local value:=Eachin mylist 28 | canvas.DrawText(value,0,y) 29 | y+=15 30 | Next 31 | End Method 32 | 33 | End Class 34 | 35 | Function Main() 36 | instance = New AppInstance 37 | 38 | New MyWindow 39 | 40 | App.Run() 41 | 42 | End Function 43 | -------------------------------------------------------------------------------- /Beginners - LoadString SaveString.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | ' This is the string which we save 10 | Field mystring:String="This could be a string filled with tile data...." 11 | Method New() 12 | ' Set The title of the window... 13 | Title="LoadString and SaveString" 14 | ' Save/create the field 'mystring' 15 | savefile(mystring) 16 | ' Put the data from the file into mystring. 17 | mystring = readfile() 18 | End Method 19 | 20 | Method OnRender( canvas:Canvas ) Override 21 | App.RequestRender() ' Activate this method 22 | ' Clear with black color 23 | canvas.Clear(Color.Black) 24 | ' Draw the text at 100,100 25 | canvas.DrawText(mystring,100,100) 26 | ' if key escape then quit 27 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 28 | End Method 29 | 30 | End Class 31 | 32 | Function savefile(savedata:String) 33 | ' Save text into mydata.txt 34 | SaveString(savedata,AssetsDir()+"mydata.txt" ) 35 | End Function 36 | 37 | Function readfile:String() 38 | ' Load text from mydata.txt 39 | Return LoadString( AssetsDir()+"mydata.txt" ) 40 | End Function 41 | 42 | Function Main() 43 | New AppInstance 44 | New MyWindow 45 | App.Run() 46 | End Function 47 | -------------------------------------------------------------------------------- /Beginners - Max.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' if key escape then quit 16 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 17 | ' 18 | canvas.DrawText("Max example",0,0) 19 | Local a:Int=10 20 | Local b:Int=20 21 | Local c:Int=Max(a,b) 22 | canvas.DrawText("Max of a("+a+") and b("+b+") = "+c,0,40) 23 | End Method 24 | 25 | End Class 26 | 27 | Function Main() 28 | New AppInstance 29 | New MyWindow 30 | App.Run() 31 | End Function 32 | -------------------------------------------------------------------------------- /Beginners - Microsecs and Millisecs.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Method New() 10 | End Method 11 | 12 | Method OnRender( canvas:Canvas ) Override 13 | App.RequestRender() ' Activate this method 14 | ' 15 | canvas.DrawText("Microsecs since app start = "+Microsecs(),0,60) 16 | canvas.DrawText("Millisecs since app start = "+Millisecs(),0,40) 17 | ' if key escape then quit 18 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 19 | End Method 20 | 21 | End Class 22 | 23 | Function Main() 24 | New AppInstance 25 | New MyWindow 26 | App.Run() 27 | End Function 28 | -------------------------------------------------------------------------------- /Beginners - Mid Command.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' if key escape then quit 16 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 17 | Local a:="abcdefghijklmnopqrstuvwxyz" 18 | canvas.DrawText("a = "+a,0,0) 19 | canvas.DrawText("a.Mid(0,1) = "+a.Mid(0,1),0,20) 20 | canvas.DrawText("a.Mid(1,5) = "+a.Mid(1,5),0,40) 21 | End Method 22 | 23 | End Class 24 | 25 | Function Main() 26 | New AppInstance 27 | New MyWindow 28 | App.Run() 29 | End Function 30 | -------------------------------------------------------------------------------- /Beginners - Min.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' if key escape then quit 16 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 17 | ' 18 | canvas.DrawText("Min example",0,0) 19 | Local a:Int=10 20 | Local b:Int=20 21 | Local c:Int=Min(a,b) 22 | canvas.DrawText("Lowest of a("+a+") and b("+b+") = "+c,0,40) 23 | End Method 24 | 25 | End Class 26 | 27 | Function Main() 28 | New AppInstance 29 | New MyWindow 30 | App.Run() 31 | End Function 32 | -------------------------------------------------------------------------------- /Beginners - Mouse PointerVisible.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End Method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' 16 | ' If visible or invisible draw the text 17 | If Mouse.PointerVisible = True 18 | canvas.DrawText("Mouse Pointer is Visible",0,0) 19 | Else 20 | canvas.DrawText("Mouse Pointer is not Visible",0,0) 21 | End If 22 | ' 23 | ' With space pressed hide or show mouse cursor 24 | If Keyboard.KeyReleased(Key.Space) 25 | If Mouse.PointerVisible = True Then 26 | Mouse.PointerVisible = False 27 | Else 28 | Mouse.PointerVisible = True 29 | End If 30 | End If 31 | ' 32 | canvas.DrawText("Press Space to hide/Show MouseCursor",Width*.5,Height-10,.5,.5) 33 | ' if key escape then quit 34 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 35 | End Method 36 | 37 | End Class 38 | 39 | Function Main() 40 | New AppInstance 41 | New MyWindow 42 | App.Run() 43 | End Function 44 | -------------------------------------------------------------------------------- /Beginners - Now.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Method New() 10 | End Method 11 | 12 | Method OnRender( canvas:Canvas ) Override 13 | App.RequestRender() ' Activate this method 14 | ' 15 | canvas.DrawText("Seconds since app start = "+Now(),0,60) 16 | ' if key escape then quit 17 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 18 | End Method 19 | 20 | End Class 21 | 22 | Function Main() 23 | New AppInstance 24 | New MyWindow 25 | App.Run() 26 | End Function 27 | -------------------------------------------------------------------------------- /Beginners - OpenUrl.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Method New() 10 | End method 11 | 12 | Method OnRender( canvas:Canvas ) Override 13 | App.RequestRender() ' Activate this method 14 | ' 15 | If Keyboard.KeyReleased(Key.F1) 16 | OpenUrl("www.google.com") 17 | End If 18 | canvas.DrawText("Press F1 To open standard browser (google.com)",0,0) 19 | ' if key escape then quit 20 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 21 | End Method 22 | 23 | End Class 24 | 25 | Function Main() 26 | New AppInstance 27 | New MyWindow 28 | App.Run() 29 | End Function 30 | -------------------------------------------------------------------------------- /Beginners - Outlinemode outlinecolor outlinewidth.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Method New() 10 | End Method 11 | 12 | Method OnRender( canvas:Canvas ) Override 13 | App.RequestRender() ' Activate this method 14 | 15 | ' 16 | ' Outline mode. Draw a border around things. 17 | ' 18 | canvas.Color = Color.Black 19 | ' smooth outline 20 | canvas.OutlineMode=OutlineMode.Smooth 21 | canvas.OutlineColor = Color.Blue 22 | canvas.OutlineWidth = 4 23 | ' Draw things with the new drawing settings 24 | canvas.DrawRect(50,50,100,100) 25 | canvas.DrawOval(50,50,20,20) 26 | canvas.DrawQuad(100,100,200,150,200,200,100,200) 27 | Local b:Float[] = New Float[](10,10,20,20,10,60) 28 | canvas.DrawPoly(b) 29 | ' solid outline 30 | canvas.OutlineMode = OutlineMode.Solid 31 | canvas.DrawRect(200,50,100,100) 32 | 'Set back the outline mode to none 33 | canvas.OutlineMode = OutlineMode.None 34 | canvas.DrawRect(350,50,100,100) 35 | 36 | 37 | ' if key escape then quit 38 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 39 | End Method 40 | 41 | End Class 42 | 43 | Function Main() 44 | New AppInstance 45 | New MyWindow 46 | App.Run() 47 | End Function 48 | -------------------------------------------------------------------------------- /Beginners - Printing Boolean variables.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Method New() 10 | End Method 11 | 12 | Method OnRender( canvas:Canvas ) Override 13 | App.RequestRender() ' Activate this method 14 | ' 15 | ' From Monkey 1.1.08 you can print boolean values. They get 16 | ' converted to string automatically. (Handy for debugging) 17 | ' 18 | Local a:Bool=True 19 | canvas.Color = Color.Black 20 | canvas.DrawText("'a' boolean value is : "+a,0,0) 21 | ' if key escape then quit 22 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 23 | End Method 24 | 25 | End Class 26 | 27 | Function Main() 28 | New AppInstance 29 | New MyWindow 30 | App.Run() 31 | End Function 32 | -------------------------------------------------------------------------------- /Beginners - Proceed.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field r:Int 10 | Method New() 11 | End Method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' 16 | If Keyboard.KeyReleased(Key.F1) 17 | r=Proceed("Message","Do you wish to proceed?",False) 18 | End If 19 | canvas.DrawText("Last Proceed value was :"+r,0,20) 20 | canvas.DrawText("Press F1 for Proceed Requester",0,0) 21 | ' if key escape then quit 22 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 23 | End Method 24 | 25 | End Class 26 | 27 | Function Main() 28 | New AppInstance 29 | New MyWindow 30 | App.Run() 31 | End Function 32 | -------------------------------------------------------------------------------- /Beginners - Quit End Terminate App.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class MyWindow Extends Window 8 | 9 | Method New() 10 | End Method 11 | 12 | Method OnRender( canvas:Canvas ) Override 13 | App.RequestRender() ' Activate this method 14 | ' if key escape then quit 15 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 16 | End Method 17 | 18 | End Class 19 | 20 | Function Main() 21 | New AppInstance 22 | New MyWindow 23 | App.Run() 24 | End Function 25 | -------------------------------------------------------------------------------- /Beginners - Rectf and DrawRect.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | Local x:=Mouse.X 16 | Local y:=Mouse.Y 17 | Local w:=100 18 | Local h:=100 19 | Local r := new Rectf(x,y,x+w,y+h) 20 | canvas.DrawRect(r) 21 | ' 22 | canvas.DrawText("Left:"+r.Left,10,10) 23 | canvas.DrawText("Top:"+r.Top,10,30) 24 | canvas.DrawText("Right:"+r.Right,10,50) 25 | canvas.DrawText("Bottom:"+r.Bottom,10,70) 26 | canvas.DrawText("Width:"+r.Width,10,90) 27 | canvas.DrawText("Height:"+r.Height,10,110) 28 | Local s:=New Vec2f() 29 | s = r.Size 30 | canvas.DrawText("Size vec2f width:"+s.X,10,130) 31 | canvas.DrawText("Size vec2f Height:"+s.Y,10,150) 32 | ' if key escape then quit 33 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 34 | End Method 35 | 36 | End Class 37 | 38 | Function Main() 39 | New AppInstance 40 | New MyWindow 41 | App.Run() 42 | End Function 43 | -------------------------------------------------------------------------------- /Beginners - RequestDir.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field r:String 10 | Method New() 11 | End Method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' 16 | If Keyboard.KeyReleased(Key.F1) 17 | r=RequestDir("Select Dir","C:") 18 | End If 19 | canvas.DrawText("Last dir value was :"+r,0,20) 20 | canvas.DrawText("Press F1 for Dir Requester",0,0) 21 | ' if key escape then quit 22 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 23 | End Method 24 | 25 | End Class 26 | 27 | Function Main() 28 | New AppInstance 29 | New MyWindow 30 | App.Run() 31 | End Function 32 | -------------------------------------------------------------------------------- /Beginners - RequestFile.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field r:String 10 | Method New() 11 | End Method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' 16 | If Keyboard.KeyReleased(Key.F1) 17 | r=RequestFile("Select File","Image:jpg,png,bmp",False,"default.png") 18 | End If 19 | canvas.DrawText("Last File value was :"+r,0,20) 20 | canvas.DrawText("Press F1 for File Requester",0,0) 21 | ' if key escape then quit 22 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 23 | End Method 24 | 25 | End Class 26 | 27 | Function Main() 28 | New AppInstance 29 | New MyWindow 30 | App.Run() 31 | End Function 32 | -------------------------------------------------------------------------------- /Beginners - Return List from Function.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | 'Create a new int list 12 | Local mylist:List 13 | mylist = New List 14 | ' add some values to the list 15 | mylist.AddLast(10) 16 | mylist.AddLast(20) 17 | mylist.AddLast(30) 18 | ' increase the ints using this function 19 | mylist = inclist(mylist) 20 | ' print the output to the console 21 | For Local i:=Eachin mylist 22 | Print i 23 | Next 24 | End Method 25 | 26 | Method OnRender( canvas:Canvas ) Override 27 | App.RequestRender() ' Activate this method 28 | ' if key escape then quit 29 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 30 | End Method 31 | 32 | End Class 33 | 34 | ' 35 | ' Here we create a function that returns a list and takes in 36 | ' a list. 37 | ' 38 | Function inclist:List(in:List) 39 | ' Create a local list 40 | Local out:List = New List 41 | 'loop through the inputted list 42 | For Local i:=Eachin in 43 | ' add new value based on old value to the out list 44 | out.AddLast(i+1) 45 | Next 46 | ' return the out list from the function 47 | Return out 48 | End Function 49 | 50 | Function Main() 51 | New AppInstance 52 | New MyWindow 53 | App.Run() 54 | End Function 55 | -------------------------------------------------------------------------------- /Beginners - Return Stack Class from Function.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class enemy 8 | Field x:Int,y:Int 9 | Method New(x:int,y:Int) 10 | Self.x = x 11 | Self.y = y 12 | End Method 13 | End Class 14 | 15 | Class MyWindow Extends Window 16 | field mystack:Stack 17 | Method New() 18 | 'Create a new int Stack 19 | 20 | mystack = New Stack 21 | ' Create some data in the stack class 22 | mystack.Add(New enemy(Rnd(Width),Rnd(Height))) 23 | mystack.Add(New enemy(Rnd(Width),Rnd(Height))) 24 | mystack.Add(New enemy(Rnd(Width),Rnd(Height))) 25 | End Method 26 | 27 | Method OnRender( canvas:Canvas ) Override 28 | App.RequestRender() ' Activate this method 29 | canvas.Clear(Color.Black) 30 | ' create a new stack (change the x variable) 31 | mystack = modifystack(mystack) 32 | 33 | canvas.Color = Color.White 34 | For Local i:=Eachin mystack 35 | canvas.DrawOval(i.x,i.y,10,10) 36 | Next 37 | 38 | ' if key escape then quit 39 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 40 | End Method 41 | 42 | End Class 43 | 44 | ' Here we create a function that returns a stack and takes in 45 | ' a stack 46 | ' 47 | Function modifystack:Stack(in:Stack) 48 | ' Create a local stack 49 | Local out:Stack = New Stack 50 | 'loop through the inputted stack 51 | For Local i:=Eachin in 52 | Local x:Int=i.x+Rnd(-100,100) 53 | Local y:Int=i.y+Rnd(-100,100) 54 | If x<0 Or x>640 Then x=320 55 | If y<0 Or y>480 Then y=240 56 | out.Add(New enemy(x,y)) 57 | Next 58 | ' return the out stack from the function 59 | Return out 60 | End Function 61 | 62 | 63 | Function Main() 64 | New AppInstance 65 | New MyWindow 66 | App.Run() 67 | End Function 68 | -------------------------------------------------------------------------------- /Beginners - Return Stack from Function.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | 'Create a new int list 12 | Local mystack:Stack 13 | mystack = New Stack 14 | ' add some values to the list 15 | mystack.Add(10) 16 | mystack.Add(20) 17 | mystack.Add(30) 18 | ' increase the ints using this function 19 | mystack = incstack(mystack) 20 | ' print the output to the console 21 | For Local i:=Eachin mystack 22 | Print i 23 | Next 24 | End method 25 | 26 | Method OnRender( canvas:Canvas ) Override 27 | App.RequestRender() ' Activate this method 28 | ' if key escape then quit 29 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 30 | End Method 31 | 32 | End Class 33 | 34 | ' Here we create a function that returns a list and takes in 35 | ' a list. (it increases the ints in the in list bu 1) 36 | ' 37 | Function incstack:Stack(in:Stack) 38 | ' Create a local list 39 | Local out:Stack = New Stack 40 | 'loop through the inputted list 41 | For Local i:=Eachin in 42 | ' add new value based on old value to the out list 43 | out.Add(i+1) 44 | Next 45 | ' return the out list from the function 46 | Return out 47 | End Function 48 | 49 | 50 | Function Main() 51 | New AppInstance 52 | New MyWindow 53 | App.Run() 54 | End Function 55 | -------------------------------------------------------------------------------- /Beginners - Rnd.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field r:Double 10 | Method New() 11 | End Method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' 16 | If Keyboard.KeyReleased(Key.F1) 17 | r=Rnd() 18 | End If 19 | If Keyboard.KeyReleased(Key.F2) 20 | r=Rnd(5) 21 | End If 22 | If Keyboard.KeyReleased(Key.F3) 23 | r=Rnd(2,5) 24 | End If 25 | canvas.DrawText("Last value was :"+r,0,60) 26 | canvas.DrawText("Press F1 for rnd 0/1",0,0) 27 | canvas.DrawText("Press F2 for rnd 0<5",0,20) 28 | canvas.DrawText("Press F3 for rnd 2><5",0,40) 29 | ' if key escape then quit 30 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 31 | End Method 32 | 33 | End Class 34 | 35 | Function Main() 36 | New AppInstance 37 | New MyWindow 38 | App.Run() 39 | End Function 40 | -------------------------------------------------------------------------------- /Beginners - RndUlong.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field r:ULong 10 | Method New() 11 | End Method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' 16 | If Keyboard.KeyReleased(Key.F1) 17 | r=RndULong() 18 | End If 19 | canvas.DrawText("Last value was :"+r,0,60) 20 | canvas.DrawText("Press F1 for RndUlong",0,0) 21 | ' if key escape then quit 22 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 23 | End Method 24 | 25 | End Class 26 | 27 | Function Main() 28 | New AppInstance 29 | New MyWindow 30 | App.Run() 31 | End Function 32 | -------------------------------------------------------------------------------- /Beginners - SeedRnd.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field r:Double 10 | Method New() 11 | End Method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' 16 | If Keyboard.KeyReleased(Key.F1) 17 | ' same seed same rnd result 18 | SeedRnd(1) 19 | r=Rnd() 20 | End If 21 | canvas.DrawText("Last value was :"+r,0,60) 22 | canvas.DrawText("Press F1 for Seed 1 rnd()",0,0) 23 | ' if key escape then quit 24 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 25 | End Method 26 | 27 | End Class 28 | 29 | Function Main() 30 | New AppInstance 31 | New MyWindow 32 | App.Run() 33 | End Function 34 | -------------------------------------------------------------------------------- /Beginners - Select case.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | Title="Select Case Example" 12 | End Method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | Local out1:String 17 | Local out2:string 18 | Local a:String="ab" 19 | ' 20 | Select a 21 | Case "a" 22 | out1 = "string was value :"+a 23 | Case "ab","abc" 24 | out1 = "string was value :"+a 25 | Default 26 | out1 = "string was value :"+a 27 | End Select 28 | ' 29 | Local b:Int=1 30 | ' 31 | Select b 32 | Case 1,2,3,4 33 | out2 = "integer was value : "+b 34 | Case 10 35 | out2 = "integer was value : "+b 36 | Default 37 | out2 = "integer was value : "+b 38 | End Select 39 | ' 40 | canvas.DrawText("a was : "+out1+" - b was : "+out2,0,50) 41 | ' if key escape then quit 42 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 43 | End Method 44 | 45 | End Class 46 | 47 | Function Main() 48 | New AppInstance 49 | New MyWindow 50 | App.Run() 51 | End Function 52 | -------------------------------------------------------------------------------- /Beginners - Stack.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class MyWindow Extends Window 8 | 9 | Field mystack:= New Stack 10 | 11 | Method New() 12 | For Local i:=0 To 10 13 | mystack.Push(i) 14 | Next 15 | mystack.Add(500) ' add is like push 16 | mystack.Insert(0,1000) ' insert at any point (0) = at starting point of list 17 | End Method 18 | 19 | Method OnRender( canvas:Canvas ) Override 20 | App.RequestRender() ' Activate this method 21 | 22 | If Keyboard.KeyReleased(Key.Space) 23 | mystack.Erase(0) 24 | Print mystack.Length 25 | End if 26 | For Local i:=0 until mystack.Length 27 | canvas.DrawText(mystack.Get(i),10,i*20) 28 | Next 29 | 30 | ' 31 | ' top reads the last value added 32 | canvas.DrawText("Stack.Top value :"+mystack.Top,320,40) 33 | 34 | ' if key escape then quit 35 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 36 | End Method 37 | 38 | End Class 39 | 40 | Function Main() 41 | New AppInstance 42 | New MyWindow 43 | App.Run() 44 | End Function 45 | -------------------------------------------------------------------------------- /Beginners - String Create and Compare.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' if key escape then quit 16 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 17 | Local a:="test1" 18 | Local b:="test2" 19 | canvas.DrawText("a="+a,0,0) 20 | canvas.DrawText("b="+b,0,20) 21 | If a=b Then 22 | canvas.DrawText("a and b is the same",0,50) 23 | Else 24 | canvas.DrawText("a and b is different",0,70) 25 | End If 26 | Local c:="test3" 27 | Local d:="test3" 28 | canvas.DrawText("c="+c,0,110) 29 | canvas.DrawText("d="+d,0,130) 30 | If c=d Then 31 | canvas.DrawText("c and d is the same",0,170) 32 | Else 33 | canvas.DrawText("c and d is different",0,190) 34 | End If 35 | End Method 36 | 37 | End Class 38 | 39 | Function Main() 40 | New AppInstance 41 | New MyWindow 42 | App.Run() 43 | End Function 44 | -------------------------------------------------------------------------------- /Beginners - String FromChar.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' if key escape then quit 16 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 17 | ' 18 | canvas.DrawText("FromChar example",0,0) 19 | canvas.DrawText("String.FromChar value of letter 97="+String.FromChar(97),0,20) 20 | canvas.DrawText("String.FromChar value of letter 65="+String.FromChar(65),0,40) 21 | End Method 22 | 23 | End Class 24 | 25 | Function Main() 26 | New AppInstance 27 | New MyWindow 28 | App.Run() 29 | End Function 30 | -------------------------------------------------------------------------------- /Beginners - Vec2f (ship movement).monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | Class MyWindow Extends Window 11 | 12 | ' Here we create the vectors for the ship 13 | ' and the velocity 14 | Field ship:Vec2f 15 | Field velocity:Vec2f 16 | 17 | Method New() 18 | ship.X = 100 19 | ship.Y = 100 20 | End Method 21 | 22 | 23 | Method OnRender( canvas:Canvas ) Override 24 | ' if key escape then quit 25 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 26 | App.RequestRender() ' Loop with sync 27 | 28 | ' Input 29 | If Keyboard.KeyDown(Key.Right) 30 | velocity.X += 0.1 31 | End If 32 | If Keyboard.KeyDown(Key.Left) 33 | velocity.X -= 0.1 34 | End If 35 | If Keyboard.KeyDown(Key.Up) 36 | velocity.Y -= 0.1 37 | End If 38 | If Keyboard.KeyDown(Key.Down) 39 | velocity.Y += 0.1 40 | End If 41 | 42 | 'Limit speed 43 | If velocity.X < -5 Then velocity.X = -5 44 | If velocity.X > 5 Then velocity.X = 5 45 | If velocity.Y < -5 Then velocity.Y = -5 46 | If velocity.Y > 5 Then velocity.Y = 5 47 | 48 | ' keep the ship inside the viewport 49 | If ship.X > canvas.Viewport.Width Then ship.X = 0 50 | If ship.X < 0 Then ship.X = canvas.Viewport.Width 51 | If ship.Y > canvas.Viewport.Height Then ship.Y = 0 52 | If ship.Y < 0 Then ship.Y = canvas.Viewport.Height 53 | 54 | ' Update the ship position 55 | ship+=velocity 56 | 57 | 58 | canvas.Clear(Color.Black) 59 | canvas.Color = Color.White 60 | canvas.DrawText("Use cursor keys to move rectangle around the screen.",0,0) 61 | ' Draw the ship 62 | canvas.DrawRect(ship.X,ship.Y,20,20) 63 | End Method 64 | 65 | End Class 66 | 67 | Function Main() 68 | instance = New AppInstance 69 | 70 | New MyWindow 71 | 72 | App.Run() 73 | 74 | End Function 75 | -------------------------------------------------------------------------------- /Beginners - Window Title.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Method New() 10 | ' Set The title of the window... 11 | Title="Title of the Window....." 12 | End Method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | ' Clear with black color 17 | canvas.Clear(Color.Black) 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | -------------------------------------------------------------------------------- /Beginners - Window Width and Height.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | canvas.DrawText("This windows width:"+Width+" and height:"+Height,0,40) 16 | ' if key escape then quit 17 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 18 | End Method 19 | 20 | End Class 21 | 22 | Function Main() 23 | New AppInstance 24 | New MyWindow 25 | App.Run() 26 | End Function 27 | -------------------------------------------------------------------------------- /Beginners - getangle Get angle between two points.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field point1:Vec2i,point2:Vec2i 10 | Method New() 11 | point2.x = 320 12 | point2.y = 240 13 | End method 14 | 15 | Method OnRender( canvas:Canvas ) Override 16 | App.RequestRender() ' Activate this method 17 | 18 | point1.x = Mouse.X 19 | point1.y = Mouse.Y 20 | 21 | canvas.Color = Color.White 22 | canvas.DrawCircle(point2.x,point2.y,20) 23 | 24 | canvas.LineWidth = 3 25 | canvas.DrawLine(point1.x,point1.y,point2.x,point2.y) 26 | 27 | canvas.DrawText(getangle(point2.x,point2.y,point1.x,point1.y),0,0) 28 | canvas.DrawText("Pi : "+Pi,0,20) 29 | canvas.DrawText("TwoPi : "+TwoPi,0,40) 30 | 31 | ' if key escape then quit 32 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 33 | End Method 34 | ' 35 | ' Return the angle from - to in float 36 | Function getangle:Float(x1:Int,y1:Int,x2:Int,y2:Int) 37 | Return ATan2(y2-y1, x2-x1) 38 | End Function 39 | End Class 40 | 41 | Function Main() 42 | New AppInstance 43 | New MyWindow 44 | App.Run() 45 | End Function 46 | -------------------------------------------------------------------------------- /Beginners - lerp function - linear interpolation.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | ' 8 | ' The Linear Interpolation function lerp featured here is a function 9 | ' that takes 0.0 to 1.0 as its first value, this will tell the function 10 | ' to return a value between the second and third value. This on the percentage 11 | ' of the first. 0.0 is 0 percent and 1.0 is 100 percent. 12 | ' 13 | ' lerp(0,0,10) would return 0 14 | ' lerp(0.5,0.10) would return 5 15 | ' lerp(1,0,10) would return 10 16 | ' 17 | ' You can use it for example moving a player from start to goal location 18 | ' x = lerp(percentage,x1,x2) 19 | ' y = lerp(percentage,y1,y2) 20 | 21 | Class MyWindow Extends Window 22 | 23 | Field percentage:Float=0 'will go from 0.0 to 1.0 24 | Field stp:Float=.05 'step value smaller is smoother 25 | Field playerx:Int,playery:Int 26 | Field x1:Int=100,y1:Int=100 'our path start 27 | Field x2:Int=200,y2:Int=250 'our path end 28 | Method New() 29 | ' set the player start location 30 | playerx = x1 31 | playery = y1 32 | End method 33 | 34 | Method OnRender( canvas:Canvas ) Override 35 | App.RequestRender() ' Activate this method 36 | canvas.Clear(Color.Black) 37 | 38 | canvas.Color = Color.White 39 | canvas.DrawRect(playerx,playery,32,32) 40 | 41 | ' move the player (ea:patrol) 42 | playerx = lerp(percentage,x1,x2) 43 | playery = lerp(percentage,y1,y2) 44 | 45 | percentage+=stp 46 | If percentage>=1 Or percentage<=0 Then stp=-stp 47 | 48 | 49 | 50 | ' if key escape then quit 51 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 52 | End Method 53 | Function lerp:double(t:double , a:double, b:double) 54 | return a + t * (b - a) 55 | End Function 56 | End Class 57 | 58 | Function Main() 59 | New AppInstance 60 | New MyWindow 61 | App.Run() 62 | End Function 63 | -------------------------------------------------------------------------------- /Exampel - Mouse Released and Pressed and Down.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End Method 15 | 16 | 17 | Method OnRender( canvas:Canvas ) Override 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 20 | App.RequestRender() ' Loop with sync 21 | canvas.Clear(Color.Black) 22 | canvas.Color = Color.White 23 | canvas.DrawText("Mouse Button Left Down :"+Mouse.ButtonDown(MouseButton.Left),0,0) 24 | canvas.DrawText("Mouse Button Left Released :"+Mouse.ButtonReleased(MouseButton.Left),0,15) 25 | canvas.DrawText("Mouse Button Left Pressed :"+Mouse.ButtonPressed(MouseButton.Left),0,30) 26 | 27 | End Method 28 | 29 | End Class 30 | 31 | Function Main() 32 | instance = New AppInstance 33 | 34 | New MyWindow 35 | 36 | App.Run() 37 | 38 | End Function 39 | -------------------------------------------------------------------------------- /Example - 2d Fireworks.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | ' This is the particle class - Each particle is a part 8 | ' of the fireworks. It starts a light and heads in a 9 | ' direction and dims out... 10 | Class particle 11 | 'fields 12 | Field x:Float,y:Float 13 | Field direction:Double 14 | Field speed:Float 15 | Field col:Color 16 | Field deleteme:Bool 17 | Field time:Int 18 | Method New(x:Int,y:Int,direction:Double) 19 | Self.x = x 20 | Self.y = y 21 | Self.speed = 3 22 | If Rnd(20)<3 Then Self.speed += Rnd(-.5,.5) 23 | Self.direction = direction 24 | Self.col = New Color(1,1,1) 25 | End Method 26 | Method update() 27 | time+=1 28 | ' If burn out then flag for delete 29 | If time > 60 Then deleteme = True 30 | ' update the position 31 | x+=Cos(direction)*speed 32 | y+=Sin(direction)*speed 33 | ' Dim out the color. 34 | col = New Color(col.R-1.0/60,col.G-1.0/60,col.B-1.0/60) 35 | ' Slow the speed by a fraction 36 | speed -= speed/60 37 | End Method 38 | Method draw(canvas:Canvas) 39 | canvas.Color = col 40 | canvas.DrawCircle(x,y,2) 41 | End Method 42 | End Class 43 | 44 | Global myparticle:List = New List 45 | 46 | Class MyWindow Extends Window 47 | 48 | Method New() 49 | Title="Fireworks..." 50 | ' 51 | ClearColor = Color.Black 52 | ' 53 | ' 54 | End Method 55 | Method update() 56 | ' Here we create the fireworks. 57 | ' At a random situation 58 | If Rnd(100)<10 59 | ' create a point (x,y) 60 | Local x:Int=Rnd(0,Width) 61 | Local y:Int=Rnd(0,Height) 62 | ' 20 or so particles in a random direction 63 | For Local i:Int=0 Until 20 64 | myparticle.Add(New particle(x,y,Rnd(TwoPi))) 65 | Next 66 | End If 67 | ' Update each particle 68 | For Local i:=Eachin myparticle 69 | i.update() 70 | Next 71 | ' Delete flagged out particles 72 | For Local i:=Eachin myparticle 73 | If i.deleteme = True Then myparticle.Remove(i) 74 | Next 75 | End Method 76 | Method OnRender( canvas:Canvas ) Override 77 | App.RequestRender() ' Activate this method 78 | ' 79 | ' Here we call the update method 80 | update() 81 | ' 82 | ' Draw the particles 83 | For Local i:=Eachin myparticle 84 | i.draw(canvas) 85 | Next 86 | ' if key escape then quit 87 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 88 | End Method 89 | 90 | End Class 91 | 92 | Function Main() 93 | New AppInstance 94 | New MyWindow 95 | App.Run() 96 | End Function 97 | 98 | -------------------------------------------------------------------------------- /Example - 2d Homing Missiles.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class missile 8 | Field x:Double,y:Double 9 | Field currentangle:Double 10 | Field deleteme:Bool 11 | Method New(x:Double,y:Double) 12 | Self.x = x 13 | Self.y = y 14 | End Method 15 | Method update(mx:Double,my:Double) 16 | 'Home the missile 17 | local targetx:Double = mx - x 18 | local targety:Double = my - y 19 | currentangle = ATan2(targety, targetx) * 180 / Pi 20 | '2 here below is the movement speed 21 | Local vx:Double = 2 * (90 - Abs(currentangle)) / 90 22 | local vy:Double 23 | if (currentangle < 0) 24 | vy = -2 + Abs(vx) 25 | Else 26 | vy = 2 - Abs(vx) 27 | End if 28 | x += vx 29 | y += vy 30 | 'if collide then set remove flag 31 | If rectsoverlap(mx-5,my-5,10,10,x-5,y-5,10,10) Then deleteme = true 32 | End Method 33 | Method draw(canvas:Canvas) 34 | canvas.Color = Color.Black 35 | canvas.DrawCircle(x,y,5) 36 | End Method 37 | Function rectsoverlap:Bool(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int) 38 | If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then Return False 39 | If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then Return False 40 | Return True 41 | End Function 42 | End Class 43 | 44 | Global mymissile:List = New List 45 | 46 | Class MyWindow Extends Window 47 | 48 | Method New() 49 | For Local i:Int=0 until 5 50 | mymissile.Add(New missile(Rnd(Width),Rnd(Height))) 51 | Next 52 | End Method 53 | 54 | Method OnRender( canvas:Canvas ) Override 55 | App.RequestRender() ' Activate this method 56 | ' 57 | 'Update the missiles 58 | For Local i:=Eachin mymissile 59 | i.update(Mouse.X,Mouse.Y) 60 | Next 61 | For Local i:=Eachin mymissile 62 | If i.deleteme Then 63 | mymissile.Remove(i) 64 | mymissile.Add(New missile(Rnd(Width),Height+50)) 65 | End If 66 | Next 67 | 68 | 'Draw the missile 69 | For Local i:=Eachin mymissile 70 | i.draw(canvas) 71 | Next 72 | ' if key escape then quit 73 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 74 | End Method 75 | 76 | End Class 77 | 78 | Function Main() 79 | New AppInstance 80 | New MyWindow 81 | App.Run() 82 | End Function 83 | 84 | -------------------------------------------------------------------------------- /Example - App FPS Command.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End Method 15 | 16 | 17 | Method OnRender( canvas:Canvas ) Override 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 20 | App.RequestRender() ' Loop with sync 21 | canvas.Clear(Color.Black) 22 | canvas.Color = Color.White 23 | canvas.DrawText("Frames Per Second :"+App.FPS,0,0) 24 | End Method 25 | 26 | End Class 27 | 28 | Function Main() 29 | instance = New AppInstance 30 | 31 | New MyWindow 32 | 33 | App.Run() 34 | 35 | End Function 36 | -------------------------------------------------------------------------------- /Example - Background - Tinted Gradient.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class background 8 | Field w:Int,h:Int 9 | Field barwidth:Int=16 10 | Field col1:Color 11 | Field col2:Color 12 | Field col3:Color 13 | Field col4:Color 14 | Field bh:Float 15 | Method New(w:Int,h:int) 16 | Self.w = w 17 | Self.h = h 18 | col1 = New Color(Rnd(),Rnd(),Rnd()) 19 | col2 = New Color(Rnd(),Rnd(),Rnd()) 20 | col3 = New Color(Rnd(),Rnd(),Rnd()) 21 | col4 = New Color(Rnd(),Rnd(),Rnd()) 22 | bh = h/3 23 | End Method 24 | Method draw(canvas:Canvas) 25 | Local cnt:Float=0 26 | Local pos:Float=0 27 | Local r1:float,g1:Float,b1:Float 28 | Local r2:Float,g2:Float,b2:Float 29 | 30 | For Local y:=0 Until h 31 | Select pos 32 | Case 0 33 | Local r1:Float = col1.r 34 | Local g1:Float = col1.g 35 | Local b1:Float = col1.b 36 | Local r2:Float = col2.r 37 | Local g2:Float = col2.g 38 | Local b2:Float = col2.b 39 | Local cstepr:Float = (r2-r1)/bh 40 | Local cstepg:Float = (g2-g1)/bh 41 | Local cstepb:Float = (b2-b1)/bh 42 | canvas.Color = New Color(r1+(cstepr*cnt),g1+(cstepg*cnt),b1+(cstepb*cnt)) 43 | Case 1 44 | Local r1:Float = col2.r 45 | Local g1:Float = col2.g 46 | Local b1:Float = col2.b 47 | Local r2:Float = col3.r 48 | Local g2:Float = col3.g 49 | Local b2:Float = col3.b 50 | Local cstepr:Float = (r2-r1)/bh 51 | Local cstepg:Float = (g2-g1)/bh 52 | Local cstepb:Float = (b2-b1)/bh 53 | canvas.Color = New Color(r1+(cstepr*cnt),g1+(cstepg*cnt),b1+(cstepb*cnt)) 54 | Case 2 55 | Local r1:Float = col3.r 56 | Local g1:Float = col3.g 57 | Local b1:Float = col3.b 58 | Local r2:Float = col4.r 59 | Local g2:Float = col4.g 60 | Local b2:Float = col4.b 61 | Local cstepr:Float = (r2-r1)/bh 62 | Local cstepg:Float = (g2-g1)/bh 63 | Local cstepb:Float = (b2-b1)/bh 64 | canvas.Color = New Color(r1+(cstepr*cnt),g1+(cstepg*cnt),b1+(cstepb*cnt)) 65 | End Select 66 | canvas.DrawLine(0,y,w,y) 67 | cnt+=1 68 | If cnt>= bh Then pos+=1 ; cnt=0 69 | Next 70 | End Method 71 | end Class 72 | 73 | Global mybackground:background 74 | 75 | Class MyWindow Extends Window 76 | 77 | Method New() 78 | Title = "Tinted Gradient background Example.." 79 | mybackground = New background(Width,Height) 80 | End Method 81 | 82 | Method OnRender( canvas:Canvas ) Override 83 | App.RequestRender() ' Activate this method 84 | ' 85 | If Keyboard.KeyReleased(Key.Space) 86 | mybackground = New background(Width,Height) 87 | End If 88 | ' 89 | mybackground.draw(canvas) 90 | ' if key escape then quit 91 | canvas.Color = Color.White 92 | canvas.DrawText("Press space for new screen..",0,0) 93 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 94 | End Method 95 | 96 | End Class 97 | 98 | Function Main() 99 | New AppInstance 100 | New MyWindow 101 | App.Run() 102 | End Function 103 | -------------------------------------------------------------------------------- /Example - Background - red gradient.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class background 8 | Field w:Int,h:Int 9 | Field barwidth:Int=16 10 | Method New(w:Int,h:int) 11 | Self.w = w 12 | Self.h = h 13 | End Method 14 | Method draw(canvas:Canvas) 15 | Local cnt:Float=0.0 16 | Local cd:Int=0 17 | For Local x:=0 Until w*2 18 | canvas.Color = New Color(cnt,0,0) 19 | If cd = 1 Then cnt+=1.0/barwidth 20 | If cd = 0 Then cnt-=1.0/barwidth 21 | If cnt>1 Then cd=0;cnt=1 22 | If cnt<0 Then cd=1;cnt=0 23 | canvas.DrawLine(x,0,x-w,h) 24 | Next 25 | End Method 26 | end Class 27 | 28 | Global mybackground:background 29 | 30 | Class MyWindow Extends Window 31 | 32 | Method New() 33 | Title = "Red Gradient background Example.." 34 | mybackground = New background(Width,Height) 35 | End Method 36 | 37 | Method OnRender( canvas:Canvas ) Override 38 | App.RequestRender() ' Activate this method 39 | ' 40 | mybackground.draw(canvas) 41 | ' if key escape then quit 42 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 43 | End Method 44 | 45 | End Class 46 | 47 | Function Main() 48 | New AppInstance 49 | New MyWindow 50 | App.Run() 51 | End Function 52 | -------------------------------------------------------------------------------- /Example - Binary Addition.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | ' How long should the binary be 8 | Global binlen:Int=14 9 | ' This contains the binary data. bin[0] is the most left 10 | Global bin:Int[] 11 | 12 | 13 | Class MyWindow Extends Window 14 | 15 | ' keep track of the number 16 | Field cnt:Int=0 17 | 18 | Method New() 19 | ' create our binary array 20 | bin = New Int[binlen] 21 | 22 | End Method 23 | 24 | Method OnRender( canvas:Canvas ) Override 25 | App.RequestRender() ' Activate this method 26 | 27 | If Keyboard.KeyReleased(Key.Space) Or Mouse.ButtonReleased(MouseButton.Left) 28 | ' Add one to the binary 29 | incbin() 30 | ' Add up one into our count 31 | cnt+=1 32 | ' if all values are zero then reset cnt 33 | Local res:Bool=True 34 | For Local i:Int=0 Until binlen 35 | If bin[i] = 1 Then res=False 36 | Next 37 | If res Then cnt=0 38 | End If 39 | 40 | canvas.Scale(2,2) 41 | canvas.DrawText("Press Space or LMB(touch) to increase",0,0) 42 | ' Draw the binary on the screen 43 | For Local i:Int=0 Until binlen 44 | canvas.DrawText(bin[i],i*20,100) 45 | Next 46 | canvas.DrawText(cnt,0,120) 47 | 48 | ' if key escape then quit 49 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 50 | End Method 51 | 52 | End Class 53 | 54 | 'We go from right to left turning each 1 into a zero until 55 | 'or if we find a 0 and then we then turn this into a 1 and then return 56 | Function incbin() 57 | ' Loop from right to left 58 | For Local i:Int=binlen-1 To 0 Step -1 59 | ' if we encounter a 0 then turn it into a 1 and return 60 | If bin[i] = 0 Then 61 | bin[i] = 1 62 | Return 63 | End If 64 | ' turn the next bit into a 0 65 | bin[i] = 0 66 | Next 67 | End Function 68 | 69 | Function Main() 70 | New AppInstance 71 | New MyWindow 72 | App.Run() 73 | End Function 74 | -------------------------------------------------------------------------------- /Example - Bresenham Line algorithm - code example.monkey2: -------------------------------------------------------------------------------- 1 | ' 2 | ' Bresenham line algorithm 3 | ' 4 | 5 | #Import "" 6 | #Import "" 7 | ' 8 | 9 | Using std.. 10 | Using mojo.. 11 | 12 | 13 | Class MyWindow Extends Window 14 | 15 | Method New() 16 | End Method 17 | 18 | Method OnRender( canvas:Canvas ) Override 19 | App.RequestRender() ' Activate this method 20 | ' Draw some text 21 | canvas.Color = Color.Black 22 | ' 23 | For Local i:Int=0 Until 50 24 | Local x1:Int=Rnd(0,Width) 25 | Local y1:Int=Rnd(0,Height) 26 | Local x2:Int=Rnd(0,Width) 27 | Local y2:Int=Rnd(0,Height) 28 | bline(canvas,x1,y1,x2,y2) 29 | Next 30 | ' if key escape then quit 31 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 32 | End Method 33 | 34 | End Class 35 | 36 | Function bline:Void(canvas:Canvas,x1:Int,y1:Int,x2:Int,y2:Int) 37 | Local dx:Int, dy:Int, sx:Int, sy:Int, e:Int 38 | dx = Abs(x2 - x1) 39 | sx = -1 40 | If x1 < x2 Then sx = 1 41 | dy = Abs(y2 - y1) 42 | sy = -1 43 | If y1 < y2 Then sy = 1 44 | If dx < dy Then 45 | e = dx / 2 46 | Else 47 | e = dy / 2 48 | End If 49 | Local exitloop:Bool=False 50 | While exitloop = False 51 | canvas.DrawPoint(x1,y1) 52 | 53 | If x1 = x2 54 | If y1 = y2 55 | exitloop = True 56 | End If 57 | End If 58 | If dx > dy Then 59 | x1 += sx ; e -= dy 60 | If e < 0 Then e += dx ; y1 += sy 61 | Else 62 | y1 += sy ; e -= dx 63 | If e < 0 Then e += dy ; x1 += sx 64 | Endif 65 | 66 | Wend 67 | 68 | End Function 69 | 70 | 71 | Function Main() 72 | New AppInstance 73 | New MyWindow 74 | App.Run() 75 | End Function 76 | -------------------------------------------------------------------------------- /Example - Brownian caves.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Field range:Int=6 11 | 12 | Class node 13 | Field x:Int 14 | Field y:Int 15 | Method New(x:Int,y:Int) 16 | Self.x = x 17 | Self.y = y 18 | End Method 19 | End Class 20 | 21 | Field brownian:Stack 22 | 23 | Method New() 24 | make() 25 | End method 26 | 27 | Method make() 28 | brownian = New Stack 29 | brownian.Push(New node(100,100)) 30 | For Local i:Int=0 Until 500 31 | Local nx:Int=brownian.Get(i).x 32 | Local ny:Int=brownian.Get(i).y 33 | nx += Rnd(-range,range) 34 | ny += Rnd(-range,range) 35 | nx = Clamp(nx,0,200) 36 | ny = Clamp(ny,0,200) 37 | brownian.Push(New node(nx,ny)) 38 | Next 39 | End Method 40 | 41 | Method OnRender( canvas:Canvas ) Override 42 | App.RequestRender() ' Activate this method 43 | canvas.Clear(Color.Black) 44 | canvas.Color = Color.White 45 | For Local i:Int=0 Until brownian.Length-1 46 | Local x1:Int=brownian.Get(i).x 47 | Local y1:Int=brownian.Get(i).y 48 | Local x2:Int=brownian.Get(i+1).x 49 | Local y2:Int=brownian.Get(i+1).y 50 | x1*=2 51 | y1*=2 52 | x2*=2 53 | y2*=2 54 | For Local y:Int=-2 To 2 55 | For Local x:Int=-2 To 2 56 | canvas.DrawLine(x1+x+100,y1+y+100,x2+x+100,y2+y+100) 57 | Next 58 | Next 59 | Next 60 | ' If key pace then new map 61 | If Keyboard.KeyReleased(Key.Space) Then make() 62 | ' if key escape then quit 63 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 64 | End Method 65 | 66 | End Class 67 | 68 | Function Main() 69 | New AppInstance 70 | New MyWindow 71 | App.Run() 72 | End Function 73 | -------------------------------------------------------------------------------- /Example - Brownian map test.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Field range:Int=6 11 | Field len:Int=300 12 | 13 | Class node 14 | Field x:Int 15 | Field y:Int 16 | Method New(x:Int,y:Int) 17 | Self.x = x 18 | Self.y = y 19 | End Method 20 | End Class 21 | 22 | Field brownian:Stack 23 | 24 | Method New() 25 | make() 26 | End method 27 | 28 | Method make() 29 | brownian = New Stack 30 | brownian.Push(New node(100,100)) 31 | For Local i:Int=0 Until len 32 | Local nx:Int=brownian.Get(i).x 33 | Local ny:Int=brownian.Get(i).y 34 | SeedRnd(Microsecs()) 35 | nx += Rnd(-range,range) 36 | ny += Rnd(-range,range) 37 | nx = Clamp(nx,0,200) 38 | ny = Clamp(ny,0,200) 39 | brownian.Push(New node(nx,ny)) 40 | Next 41 | End Method 42 | 43 | Method OnRender( canvas:Canvas ) Override 44 | App.RequestRender() ' Activate this method 45 | canvas.Clear(Color.Black) 46 | canvas.Color = Color.White 47 | For Local i:Int=0 Until brownian.Length-1 48 | Local x1:Int=brownian.Get(i).x 49 | Local y1:Int=brownian.Get(i).y 50 | Local x2:Int=brownian.Get(i+1).x 51 | Local y2:Int=brownian.Get(i+1).y 52 | x1*=2 53 | y1*=2 54 | x2*=2 55 | y2*=2 56 | SeedRnd(100) 57 | Local l:Float=4 58 | For Local y:Int=Rnd(-l,-1) To Rnd(1,l) 59 | For Local x:Int=Rnd(-l,-1) To Rnd(1,l) 60 | canvas.DrawLine(x1+x+100,y1+y+100,x2+x+100,y2+y+100) 61 | l+=Rnd(1) 62 | If l>20 Then l=4 63 | Next 64 | Next 65 | Next 66 | ' If key pace then new map 67 | If Keyboard.KeyReleased(Key.Space) Then make() 68 | ' if key escape then quit 69 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 70 | End Method 71 | 72 | End Class 73 | 74 | Function Main() 75 | New AppInstance 76 | New MyWindow 77 | App.Run() 78 | End Function 79 | -------------------------------------------------------------------------------- /Example - C64 Maze Generator.monkey2: -------------------------------------------------------------------------------- 1 | ' 2 | ' From the coding train - based on a commodore 64 line of code. 3 | ' 10 print chr 4 | ' 5 | 6 | #Import "" 7 | #Import "" 8 | 9 | Using std.. 10 | Using mojo.. 11 | 12 | 13 | Class MyWindow Extends Window 14 | Field x:Int,y:Int 15 | Field spacing:Int=10 16 | Field iimage:Image 17 | Field icanvas:Canvas 18 | Method New() 19 | iimage = New Image(Width,Height,TextureFlags.Dynamic) 20 | icanvas = New Canvas(iimage) 21 | icanvas.Clear(Color.Black) 22 | icanvas.Color = Color.White 23 | End method 24 | 25 | Method OnRender( canvas:Canvas ) Override 26 | App.RequestRender() ' Activate this method 27 | ' 28 | draw(icanvas) 29 | canvas.DrawImage(iimage,0,0) 30 | ' if key escape then quit 31 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 32 | End Method 33 | Method draw(canvas:Canvas) 34 | 35 | If Rnd(1)<.5 Then 36 | canvas.DrawLine(x,y,x+spacing,y+spacing) 37 | Else 38 | canvas.DrawLine(x+spacing,y,x,y+spacing) 39 | End If 40 | 41 | x+=spacing 42 | If x>Width Then x=0 ; y+=spacing 43 | If y>Height Then 44 | canvas.Clear(Color.Black) 45 | y=0 46 | End If 47 | canvas.Flush() 48 | End Method 49 | End Class 50 | 51 | Function Main() 52 | New AppInstance 53 | New MyWindow 54 | App.Run() 55 | End Function 56 | -------------------------------------------------------------------------------- /Example - Clear and Color and DrawRect.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | Class MyWindow Extends Window 10 | 11 | Method New() 12 | End method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Loop with sync 16 | canvas.Clear(Color.Black) 17 | canvas.Color = Color.Red 18 | canvas.DrawRect(App.MouseLocation.x,App.MouseLocation.y,100,100) 19 | End Method 20 | 21 | Method OnKeyEvent( event:KeyEvent ) Override 22 | Select event.Type 23 | Case EventType.KeyDown 24 | Select event.Key 25 | Case Key.Escape 26 | instance.Terminate() 'exit with escape 27 | End Select 28 | End Select 29 | End Method 30 | 31 | End Class 32 | 33 | Function Main() 34 | instance = New AppInstance 35 | 36 | New MyWindow 37 | 38 | App.Run() 39 | 40 | End Function 41 | -------------------------------------------------------------------------------- /Example - Closest turn Angle vs target angle.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | 'Our target angle 12 | Local target:Float=Pi+0.5 13 | 'Our angle 14 | Local angle:Float=TwoPi-0.2 15 | 16 | Print angledifference(target,angle) 17 | 18 | End method 19 | 20 | Method OnRender( canvas:Canvas ) Override 21 | App.RequestRender() ' Activate this method 22 | ' if key escape then quit 23 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 24 | End Method 25 | 26 | End Class 27 | 28 | ' Return a float value which is negative if the distance between the angle 29 | ' and target angle is shorter. Positive value if the right turn is shorter. 30 | ' Uses for homing missiles and turrets etc. 31 | ' returns - distance float 32 | Function angledifference:Float(target:Float,angle:Float) 33 | ' Our difference (Negative if left target angle is shorter or positive if right turn is closer) 34 | Local difference:Float = target - angle 35 | While (difference < -Pi) 36 | difference += TwoPi 37 | Wend 38 | While (difference > Pi) 39 | difference -= TwoPi 40 | Wend 41 | Return difference 42 | End Function 43 | 44 | Function Main() 45 | New AppInstance 46 | New MyWindow 47 | App.Run() 48 | End Function 49 | -------------------------------------------------------------------------------- /Example - Collision - Circle Rect overlap Function.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | ' circle x and y and radius 10 | Field cx:Int=100,cy:Int=100 11 | Field cr:Int=50 12 | 13 | Method New() 14 | Title="Circle Rectagle Collision Function" 15 | End Method 16 | 17 | Method OnRender( canvas:Canvas ) Override 18 | App.RequestRender() ' Activate this method 19 | ' 20 | ' Draw the things to the canvas. 21 | canvas.DrawCircle(cx,cy,cr) 22 | canvas.DrawRect(Mouse.X,Mouse.Y,50,50) 23 | ' 24 | ' Here we do the collision 25 | If circlerectcollide(cx,cy,cr,Mouse.X,Mouse.Y,50,50) 26 | canvas.DrawText("Collision",0,0) 27 | Else 'if no collision 28 | canvas.DrawText("No Collision",0,0) 29 | End If 30 | ' 31 | canvas.DrawText("Move mouse inside cirle...",100,Height-20) 32 | ' if key escape then quit 33 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 34 | End Method 35 | 36 | End Class 37 | 38 | Function Main() 39 | New AppInstance 40 | New MyWindow 41 | App.Run() 42 | End Function 43 | 44 | ' Here is the circle vs rectangle collide function 45 | ' input : 46 | ' circlex,circley,circleradius 47 | ' rectx,recty,rectwidth,rectheight 48 | ' Returns : True or False 49 | Function circlerectcollide:Bool(cx:Int,cy:Int,cr:Int, rx:Int,ry:Int,rw:Int,rh:Int) 50 | Local closestx:Float = Clamp(cx, rx, rx+rw) 51 | Local closesty:Float = Clamp(cy, ry, ry+rh) 52 | Local distancex :Float = cx - closestx 53 | Local distancey:Float = cy - closesty 54 | Local distancesquared:Float = (distancex * distancex) + (distancey * distancey) 55 | Return distancesquared < (cr * cr) 56 | End Function 57 | -------------------------------------------------------------------------------- /Example - Collision - Circles overlap.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field cx:Int=150,cy:Int=150 10 | Field cr:Int=50 11 | 12 | Method New() 13 | End Method 14 | 15 | Method OnRender( canvas:Canvas ) Override 16 | App.RequestRender() ' Activate this method 17 | ' 18 | ' Draw the things to the canvas. 19 | canvas.DrawCircle(Mouse.X,Mouse.Y,32) 20 | canvas.DrawCircle(cx,cy,cr) 21 | ' 22 | ' Here we do the collision 23 | If circleoverlap(Mouse.X,Mouse.Y,32,cx,cy,cr) 24 | canvas.DrawText("Collision",0,0) 25 | Else 'if no collision 26 | canvas.DrawText("No Collision",0,0) 27 | End If 28 | ' if key escape then quit 29 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 30 | End Method 31 | 32 | End Class 33 | 34 | Function Main() 35 | New AppInstance 36 | New MyWindow 37 | App.Run() 38 | End Function 39 | 40 | ' This is the circle circle overlap function 41 | ' x,y,radius vs x,y,radius 42 | ' returns true if collides 43 | ' 44 | Function circleoverlap:Bool(x1:Int,y1:Int,r1:Int,x2:Int,y2:Int,r2:Int) 45 | Local dx:Int = x1-x2 46 | Local dy:Int = y1-y2 47 | Local r:Int = r1+r2 48 | If dx*dx+dy*dy <= r*r Then Return True Else Return False 49 | End Function 50 | -------------------------------------------------------------------------------- /Example - Collision - Line Segment Circle.monkey2: -------------------------------------------------------------------------------- 1 | ' 2 | ' Line segment vs Circle collision 3 | ' 4 | #Import "" 5 | #Import "" 6 | 7 | Using std.. 8 | Using mojo.. 9 | 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End Method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | canvas.Clear(Color.Black) 19 | ' the mouse location for circle to line collision 20 | Local cx:Float=Mouse.X,cy:Float=Mouse.Y 21 | Local cr:Int=20 'circle radius 22 | 'draw the circle 23 | canvas.DrawCircle(cx,cy,cr) 24 | ' line coordinates 25 | Local l1:Int[]=New Int[](100,100,200,200) ' line x1,y1,x2,y2 26 | Local l2:Int[]=New Int[](250,100,290,300) 27 | ' draw the lines 28 | canvas.Color = Color.Grey 29 | canvas.DrawLine(l1[0],l1[1],l1[2],l1[3]) 'x1,y1,x2,y2 30 | canvas.DrawLine(l2[0],l2[1],l2[2],l2[3]) ',, 31 | 32 | canvas.Color = Color.White 33 | ' check the collisions 34 | If circlelinecollide(l1[0],l1[1],l1[2],l1[3],cx,cy,cr) 35 | canvas.DrawText("Collide line 1",0,20) 36 | Else 37 | canvas.DrawText("No Collide line 1",0,20) 38 | End If 39 | If circlelinecollide(l2[0],l2[1],l2[2],l2[3],cx,cy,cr) 40 | canvas.DrawText("Collide line 2",0,40) 41 | Else 42 | canvas.DrawText("No Collide line 2",0,40) 43 | End If 44 | 45 | canvas.DrawText("Move Mouse around to check collisions with lines.",0,0) 46 | 47 | ' if key escape then quit 48 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 49 | End Method 50 | 51 | End Class 52 | 53 | ' 54 | ' Line(segment) to Circle Collision 55 | ' 56 | Function circlelinecollide:Bool(sx1:Int, sy1:Int, sx2:Int, sy2:Int, cx:Int, cy:Int,cr:Float) 57 | Local xDelta:Float = sx2 - sx1 58 | Local yDelta:Float = sy2 - sy1 59 | Local px:Int,py:Int 60 | Local u:Float 61 | 62 | If ((xDelta = 0) And (yDelta = 0)) 63 | Print("Segment start equals segment end") 64 | 65 | End If 66 | 67 | u = ((cx - sx1) * xDelta + (cy - sy1) * yDelta) / (xDelta * xDelta + yDelta * yDelta) 68 | 69 | If (u < 0) 70 | px = sx1 71 | py = sy1 72 | Else If (u > 1) 73 | px = sx2 74 | py = sy2 75 | Else 76 | px = Int(Floor(sx1 + u * xDelta)) 77 | py = Int(Floor(sy1 + u * yDelta)) 78 | End If 79 | ' If the distance of the circle to the closest point in the line 80 | ' is less then the radius then there is a collision 81 | Local d:Int= Sqrt( Pow(px - cx,2) + Pow(py - cy,2)) 82 | If d<=(cr) Then Return True 83 | Return False 84 | End Function 85 | 86 | 87 | Function Main() 88 | New AppInstance 89 | New MyWindow 90 | App.Run() 91 | End Function 92 | -------------------------------------------------------------------------------- /Example - Collision - Lines segments intersect.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | ' The variable for a line on the screen 10 | Field x1:Int=100,y1:Int=100 11 | Field x2:Int=200,y2:Int=200 12 | 13 | Method New() 14 | End Method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' 19 | ' Draw some things to the screen 20 | canvas.DrawLine(x1,y1,x2,y2) 21 | canvas.DrawLine(Mouse.X,Mouse.Y,Mouse.X-50,Mouse.Y+50) 22 | ' 23 | ' Here we check the collision 24 | If lineintersect(x1,x1,x2,y2,Mouse.X,Mouse.Y,Mouse.X-50,Mouse.Y+50) 25 | canvas.DrawText("Collision..",0,0) 26 | Else 'if no collision - false 27 | canvas.DrawText("No Collision..",0,0) 28 | End If 29 | ' if key escape then quit 30 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 31 | End Method 32 | 33 | End Class 34 | 35 | Function Main() 36 | New AppInstance 37 | New MyWindow 38 | App.Run() 39 | End Function 40 | 41 | ' This is the line intersect/collision function /// BROKEN!! 42 | ' input : line 1 x1,x1,x2,y2 43 | ' line 2 x1,y1,x2,y2 44 | ' output : True or False 45 | Function lineintersect:Bool( x1:Float,y1:Float,x2:Float,y2:Float, 46 | u1:Float,v1:Float,u2:Float,v2:Float) 47 | Local b1:Float = (y2 - y1) / (x2 - x1) 48 | Local b2:Float = (v2 - v1) / (u2 - u1) 49 | Local a1:Float = y1 - b1 *x1 50 | Local a2:Float = v1 - b2 *u1 51 | Local xi:Float = - (a1-a2)/(b1-b2) 52 | Local yi:Float = a1+b1*xi 53 | If (x1 - xi)*(xi-x2)> -1 And (u1-xi)*(xi - u2)> 0 And (y1-yi)*(yi-y2)>-1 And (v1-yi)*(yi-v2)>-1 Return True 54 | Return false 55 | End Function 56 | 57 | 58 | 59 | '// Returns 1 if the lines intersect, otherwise 0. In addition, if the lines 60 | '// intersect the intersection point may be stored in the floats i_x and i_y. 61 | Function get_line_intersection:Bool(p0_x:Float, p0_y:Float, p1_x:Float, p1_y:Float, p2_x:Float,p2_y:Float, p3_x:Float, p3_y:Float) 62 | 63 | Local s1_x:Float, s1_y:Float, s2_x:Float, s2_y:Float 64 | s1_x = p1_x - p0_x 65 | s1_y = p1_y - p0_y 66 | s2_x = p3_x - p2_x 67 | s2_y = p3_y - p2_y 68 | 69 | Local s:Float, t:Float 70 | s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y) 71 | t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y) 72 | 73 | If (s >= 0 And s <= 1 And t >= 0 And t <= 1) Then Return True 74 | 75 | 76 | Return False '// No collision 77 | End Function 78 | -------------------------------------------------------------------------------- /Example - Collision - Point in Triangle.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field x1:Int=100,y1:Int=100 10 | Field x2:Int=200,y2:Int=200 11 | Field x3:Int=50,y3:Int=200 12 | 13 | Method New() 14 | Title="Point in Triangle Collision Function" 15 | End Method 16 | 17 | Method OnRender( canvas:Canvas ) Override 18 | App.RequestRender() ' Activate this method 19 | ' 20 | ' Draw the things to the canvas. 21 | canvas.DrawTriangle(x1,y1,x2,y2,x3,y3) 22 | '93 23 | ' Here we do the collision 24 | If pointintriangle2d(Mouse.X,Mouse.Y,x1,y1,x2,y2,x3,y3) 25 | canvas.DrawText("Collision",0,0) 26 | Else 'if no collision 27 | canvas.DrawText("No Collision",0,0) 28 | End If 29 | ' 30 | canvas.DrawText("Move mouse inside triangle...",100,Height-20) 31 | ' if key escape then quit 32 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 33 | End Method 34 | 35 | End Class 36 | 37 | Function Main() 38 | New AppInstance 39 | New MyWindow 40 | App.Run() 41 | End Function 42 | 43 | ' Here is the point in triangle collision function 44 | ' by RaGR (Ralph G. Roeske). from blitzbasic.com 45 | ' px,py = pixel x and y to test collision with 46 | ' x1,y1,x2,y2,x3,y3 = The triangle points 47 | Function pointintriangle2d:bool(px:Float,pz:Float, x1:Float,y1:Float,x2:Float,y2:Float,x3:Float,y3:Float) 48 | Local bc:Float,ca:Float,ab:Float,ap:Float,bp:Float,cp:Float,abc:Float 49 | bc = x2*y3 - y2*x3 50 | ca = x3*y1 - y3*x1 51 | ab = x1*y2 - y1*x2 52 | ap = x1*pz - y1*px 53 | bp = x2*pz - y2*px 54 | cp = x3*pz - y3*px 55 | abc = Sgn(bc + ca + ab) 56 | If (abc*(bc-bp+cp)>=0) And (abc*(ca-cp+ap)>=0) And (abc*(ab-ap+bp)>=0) Return True 57 | Return False 58 | End Function 59 | -------------------------------------------------------------------------------- /Example - Collision - Rectsoverlap function.monkey2: -------------------------------------------------------------------------------- 1 | 2 | #Import "" 3 | #Import "" 4 | 5 | Using std.. 6 | Using mojo.. 7 | 8 | 9 | Class MyWindow Extends Window 10 | 11 | Method New() 12 | End method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | ' 17 | canvas.Color = Color.White 18 | canvas.DrawRect(0,0,100,100) 19 | If rectsoverlap(Mouse.X,Mouse.Y,10,10,0,0,100,100) 20 | canvas.Color = Color.Black 21 | canvas.DrawRect(0,0,100,100) 22 | End if 23 | ' if key escape then quit 24 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 25 | End Method 26 | 27 | End Class 28 | 29 | Function rectsoverlap:Bool(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int) 30 | If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then Return False 31 | If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then Return False 32 | Return True 33 | End 34 | 35 | Function Main() 36 | New AppInstance 37 | New MyWindow 38 | App.Run() 39 | End Function 40 | -------------------------------------------------------------------------------- /Example - Convex Hull - Gift Wrapping.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | ' create our points 10 | Field numpoints:Int=20 11 | Field point:Vec2i[] 12 | ' our lines around the set of points 13 | Field wrap:Stack 14 | Method New() 15 | wrapit() 16 | 17 | End Method 18 | 19 | Method OnRender( canvas:Canvas ) Override 20 | App.RequestRender() ' Activate this method 21 | ' Draw the convex hull or gift wrap 22 | ' First the points 23 | canvas.Color=Color.White 24 | For Local i:Int=0 Until point.Length 25 | canvas.DrawRect(point[i].x,point[i].y,2,2) 26 | Next 27 | ' here the lines around the points 28 | For Local i:Int=1 Until wrap.Length 29 | canvas.DrawLine(wrap[i].x,wrap[i].y,wrap[i-1].x,wrap[i-1].y) 30 | Next 31 | canvas.DrawLine(wrap[0].x,wrap[0].y,wrap[wrap.Length-1].x,wrap[wrap.Length-1].y) 32 | ' 33 | ' if key escape then quit 34 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 35 | End Method 36 | 37 | ' 38 | ' This is the wrapping routine. It creates a set of points and then 39 | ' takes the left most point and then checks from the other points for 40 | ' the next points if there are no more points on the left side. Thus 41 | ' creating a set of lines around all the other points. 42 | ' 43 | Method wrapit() 44 | SeedRnd(Microsecs()) 45 | ' create our points 46 | point = New Vec2i[numpoints] 47 | For Local i:Int=0 Until numpoints 48 | point[i].x = Rnd(100,640-100) 49 | point[i].y = Rnd(100,480-100) 50 | Next 51 | ' create our stack for the wrap lines 52 | wrap = New Stack 53 | ' find our left most point 54 | Local lp:Vec2i = New Vec2i(1000,1000) 55 | For Local i:Int=0 Until numpoints 56 | If point[i].x wrap.Get(0)) 'when at the start point then end 75 | End Method 76 | 77 | End Class 78 | 79 | Function Main() 80 | New AppInstance 81 | New MyWindow 82 | App.Run() 83 | End Function 84 | 85 | 'Return if a point is left or center or right side of a line. 86 | 'line a to b and point left(<0) or center(0) or right(>0) is c 87 | Function orientation:Int(ax:Int,ay:Int,bx:Int,by:Int,cx:Int,cy:Int) 88 | Return Sgn((bx-ax)*(cy-ay)-(by-ay)*(cx-ax)) 89 | End Function 90 | -------------------------------------------------------------------------------- /Example - Creating Rects and Intersects.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | Class MyWindow Extends Window 11 | 12 | ' Create 2 rectangles 13 | Field rec1:Rect 14 | Field rec2:Rect 15 | 'Size of the rectangles 16 | Field rec1s:= new Vec2i(150,50) 17 | Field rec2s:= new Vec2i(100,100) 18 | 19 | Method New() 20 | ' put one rect here 21 | rec1.X = 150 22 | rec1.Y = 150 23 | 'Set the sizes of the rects 24 | rec1.Size = rec1s 25 | rec2.Size = rec2s 26 | End Method 27 | 28 | 29 | Method OnRender( canvas:Canvas ) Override 30 | ' if key escape then quit 31 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 32 | App.RequestRender() ' Loop with sync 33 | 34 | ' move rectangle with mouse 35 | rec2.X = Mouse.X 36 | rec2.Y = Mouse.Y 37 | 'update the width and height 38 | rec2.Size = rec2s 39 | 40 | canvas.Clear(Color.Black) 41 | canvas.Color = Color.White 42 | 43 | ' Draw the rects 44 | canvas.DrawRect(rec1.X,rec1.Y,rec1.Width,rec1.Height) 45 | canvas.Color = Color.Blue 46 | canvas.DrawRect(rec2.X,rec2.Y,rec2.Width,rec2.Height) 47 | 48 | ' Collision/Intersect with the two rects 49 | If rec1.Intersects(rec2) = true 50 | canvas.DrawText("Collision",320,0) 51 | End If 52 | 53 | End Method 54 | 55 | End Class 56 | 57 | Function Main() 58 | instance = New AppInstance 59 | 60 | New MyWindow 61 | 62 | App.Run() 63 | 64 | End Function 65 | -------------------------------------------------------------------------------- /Example - Desktop Width and Height.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | Class MyWindow Extends Window 10 | 11 | Method New() 12 | End method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | ' if key escape then quit 17 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 18 | canvas.DrawText("Canvas Width : "+canvas.Viewport.Width,0,0) 19 | canvas.DrawText("Canvas Height : "+canvas.Viewport.Height,0,15) 20 | Local mysize:Vec2i 21 | mysize = App.DesktopSize 22 | canvas.DrawText("Desktop Width : "+mysize.X,0,30) 23 | canvas.DrawText("Desktop Height : "+mysize.Y,0,45) 24 | End Method 25 | 26 | End Class 27 | 28 | Function Main() 29 | instance = New AppInstance 30 | 31 | New MyWindow 32 | 33 | App.Run() 34 | 35 | End Function 36 | -------------------------------------------------------------------------------- /Example - Different Framerate.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | Class MyWindow Extends Window 11 | 12 | Field timer:Timer 13 | 14 | Method New() 15 | timer = New Timer(10,OnUpdate) 16 | End Method 17 | 18 | Method OnUpdate() 19 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 20 | App.RequestRender() 21 | End Method 22 | 23 | Method OnRender( canvas:Canvas ) Override 24 | canvas.DrawText("FPS:"+App.FPS,0,0) 25 | End Method 26 | 27 | End Class 28 | 29 | Function Main() 30 | instance = New AppInstance 31 | 32 | New MyWindow 33 | 34 | App.Run() 35 | 36 | End Function 37 | -------------------------------------------------------------------------------- /Example - Draw to Image at mouse pos.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | Class MyWindow Extends Window 10 | 11 | Field image:Image 12 | 13 | Field icanvas:Canvas 14 | 15 | Method New() 16 | image=New Image( 256,256,TextureFlags.Mipmap|TextureFlags.Dynamic ) 17 | image.Handle=New Vec2f( 0,0 ) 18 | icanvas=New Canvas( image ) 19 | 20 | 21 | icanvas.Color=Color.White 22 | icanvas.DrawText( "This way up!",icanvas.Viewport.Width/2,0,.5,0 ) 23 | icanvas.Flush() 24 | 25 | End Method 26 | 27 | Method OnRender( canvas:Canvas ) Override 28 | 29 | App.RequestRender() 30 | 31 | canvas.DrawImage( image,App.MouseLocation.x,App.MouseLocation.y,0,.5,.5 ) 32 | 33 | canvas.DrawText( "Here!",0,0 ) 34 | 35 | End Method 36 | 37 | Method OnKeyEvent( event:KeyEvent ) Override 38 | 39 | Select event.Type 40 | Case EventType.KeyDown 41 | Select event.Key 42 | Case Key.Escape 43 | instance.Terminate() 44 | End Select 45 | End Select 46 | End Method 47 | 48 | End Class 49 | 50 | Function Main() 51 | instance = New AppInstance 52 | 53 | New MyWindow 54 | 55 | App.Run() 56 | 57 | End function 58 | -------------------------------------------------------------------------------- /Example - DrawQuad.monkey2: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Example - Drawpoly and Scissor and Image.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | 11 | Field image:Image 12 | Field icanvas:Canvas 13 | 14 | Method New() 15 | image=New Image(640,480,TextureFlags.Dynamic) 16 | icanvas=New Canvas(image) 17 | End method 18 | 19 | Method OnRender( canvas:Canvas ) Override 20 | App.RequestRender() ' Activate this method 21 | 22 | icanvas.Clear(Color.Black) 23 | icanvas.Scissor = New Recti(50,50,50+640-100,50+480-100) 24 | icanvas.Color = Color.White 25 | For Local i:=0.0 Until TwoPi Step TwoPi/10 26 | Local poly:=New Float[6] 27 | poly[0] = 320 28 | poly[1] = 240 29 | poly[2] = 320+Cos(i)*640 30 | poly[3] = 240+Sin(i)*480 31 | poly[4] = 320+Cos(i+TwoPi/20)*640 32 | poly[5] = 240+Sin(i+TwoPi/20)*480 33 | icanvas.DrawPoly(poly) 34 | Next 35 | 36 | icanvas.Flush() 37 | 38 | canvas.DrawImage(image,0,0) 39 | canvas.Color = Color.White 40 | canvas.DrawText("FPS:"+App.FPS,0,0) 41 | 42 | ' if key escape then quit 43 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 44 | End Method 45 | 46 | End Class 47 | 48 | Function Main() 49 | New AppInstance 50 | New MyWindow 51 | App.Run() 52 | End Function 53 | -------------------------------------------------------------------------------- /Example - Eachin Split.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | Title="Select Case Example" 12 | End Method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | Local out1:String="this is a test to see if it works." 17 | Local numspaces:Int=0 18 | For Local i:=Eachin out1.Split(" ") 19 | numspaces+=1 20 | Next 21 | canvas.DrawText("Counting the spaces resulted in : "+numspaces+" spaces.",0,50) 22 | ' if key escape then quit 23 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 24 | End Method 25 | 26 | End Class 27 | 28 | Function Main() 29 | New AppInstance 30 | New MyWindow 31 | App.Run() 32 | End Function 33 | -------------------------------------------------------------------------------- /Example - File Requester.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | 11 | Class MyWindow Extends Window 12 | 13 | Field filename:="Nothing" 14 | 15 | Method New() 16 | End Method 17 | 18 | 19 | Method OnRender( canvas:Canvas ) Override 20 | ' if key escape then quit 21 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 22 | If Keyboard.KeyReleased(Key.R) Then 23 | filename = RequestFile("Load the file") 24 | End if 25 | App.RequestRender() ' Loop with sync 26 | canvas.Clear(Color.Black) 27 | canvas.Color = Color.White 28 | canvas.DrawText("R - File Requester",0,0) 29 | canvas.DrawText(filename,0,15) 30 | End Method 31 | 32 | End Class 33 | 34 | Function Main() 35 | instance = New AppInstance 36 | 37 | New MyWindow 38 | 39 | App.Run() 40 | 41 | End Function 42 | -------------------------------------------------------------------------------- /Example - Generator - Bush fire map.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class bushfire 8 | Field map:Int[,] = New Int[1,1] 9 | Field mw:Int,mh:Int 10 | Field tw:Float,th:Float 11 | Field cols:Color[] = New Color[20] 12 | Method New(w:Float,h:Float,num:Int) 13 | Self.mw = 100 14 | Self.mh = 100 15 | Self.tw = w/mw 16 | Self.th = h/mh 17 | map = New Int[mw,mh] 18 | cols = New Color[num+1] 19 | For Local i:Int = 0 Until num 20 | map[Rnd(10,mw-10),Rnd(10,mh-10)] = i 21 | Local gs:Float=Rnd() 22 | cols[i] = New Color(gs,gs,gs) 23 | Next 24 | End Method 25 | Method burnmap(tile:Int,amount:Int) 26 | 27 | For Local i:=0 Until amount 28 | Local x1:Int=Rnd(1,mw-1) 29 | Local y1:Int=Rnd(1,mh-1) 30 | If map[x1,y1] = tile 31 | For Local y2:Int=-1 To 1 32 | For Local x2:Int=-1 To 1 33 | Local x3:Int=x1+x2 34 | Local y3:Int=y1+y2 35 | If Rnd()<.3 36 | If map[x3,y3] = 0 37 | map[x3,y3] = tile 38 | End If 39 | End If 40 | Next 41 | Next 42 | End If 43 | Next 44 | End Method 45 | Method draw(canvas:Canvas) 46 | canvas.Color=Color.Brown 47 | For Local y:Float=0 Until mh Step 1 48 | For Local x:Float=0 Until mw Step 1 49 | If map[x,y] > 0 50 | canvas.Color = cols[map[x,y]] 51 | canvas.DrawRect(x*tw,y*th,tw,th) 52 | End If 53 | Next 54 | Next 55 | End Method 56 | End Class 57 | 58 | Global mybushfire:bushfire 59 | 60 | Class MyWindow Extends Window 61 | Field num:Int=10 62 | field cnt:Int 63 | Method New() 64 | Title="Monkey 2 - Bushfire map generator." 65 | mybushfire = New bushfire(Width,Height,num) 66 | End Method 67 | 68 | Method OnRender( canvas:Canvas ) Override 69 | App.RequestRender() ' Activate this method 70 | canvas.Clear(Color.Black) 71 | cnt+=1 72 | If cnt>800 Or Mouse.ButtonReleased(MouseButton.Left) 73 | cnt=0 74 | num=Rnd(3,40) 75 | mybushfire = New bushfire(Width,Height,num) 76 | Endif 77 | For Local i:Int=0 Until num 78 | mybushfire.burnmap(i,Rnd(100,800)) 79 | Next 80 | mybushfire.draw(canvas) 81 | ' if key escape then quit 82 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 83 | End Method 84 | 85 | End Class 86 | 87 | 88 | Function Main() 89 | New AppInstance 90 | New MyWindow 91 | App.Run() 92 | End Function 93 | -------------------------------------------------------------------------------- /Example - Generator - Cartoon clouds.monkey2: -------------------------------------------------------------------------------- 1 | ' 2 | ' Draw Some cartoonish clouds using the drawoval and outline commands 3 | ' 4 | 5 | #Import "" 6 | #Import "" 7 | 8 | Using std.. 9 | Using mojo.. 10 | 11 | 12 | Class MyWindow Extends Window 13 | Class cloud 14 | Field px:Float,py:Float 15 | Field pw:Float,ph:Float 16 | Field mx:Float 17 | Field sw:Int 18 | Method New(x:Int,y:Int,w:Int,h:Int,screenwidth:Int) 19 | Self.px = x 20 | Self.py = y 21 | Self.pw = w 22 | Self.ph = h 23 | Self.sw = screenwidth 24 | mx = Rnd(.05,.25) 25 | End Method 26 | Method update() 27 | px += mx 28 | If px > sw+pw Then px=0-(pw*2) 29 | End Method 30 | ' This method draws a cloud/ 31 | ' at x,y with width w and height h 32 | Method draw(canvas:Canvas) 33 | 34 | ' Create a outline (draw border around oval) 35 | canvas.OutlineMode = OutlineMode.Solid 36 | canvas.OutlineColor = Color.Black 37 | canvas.OutlineWidth = 4 38 | canvas.Color = Color.White 39 | ' Draw 7 plumps (go around in a circle) 40 | For Local angle:Float=0 Until TwoPi Step TwoPi/7 41 | Local x2:Float=Cos(angle)*pw 42 | Local y2:Float=Sin(angle)*ph 43 | canvas.DrawOval(x2+px,y2+py,pw,ph) 44 | Next 45 | ' Turn off the outline mode 46 | canvas.OutlineMode = OutlineMode.None 47 | ' Draw a white oval to erase the center of the cloud 48 | canvas.DrawOval(px-pw/2,py-ph/2,pw+pw,ph+ph) 49 | End Method 50 | End Class 51 | ' Here we create a array of 10 clouds 52 | Field mycloud:cloud[] = New cloud[10] 53 | Method New() 54 | For Local i:Int=0 Until 10 55 | ' create our objects (class) inside our array 56 | ' x,y,w,h,our screen width(for scrolling) 57 | mycloud[i] = New cloud(Rnd(-Width*.5,Width),Rnd(Height),Rnd(30,150),Rnd(20,60),Width) 58 | Next 59 | End method 60 | 61 | Method OnRender( canvas:Canvas ) Override 62 | App.RequestRender() ' Activate this method 63 | ' Blue sky 64 | canvas.Clear(Color.Blue) 65 | ' Draw our clouds 66 | For Local i:=Eachin mycloud 67 | i.draw(canvas) 68 | i.update() 69 | Next 70 | ' if key escape then quit 71 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 72 | End Method 73 | End Class 74 | 75 | Function Main() 76 | New AppInstance 77 | New MyWindow 78 | App.Run() 79 | End Function 80 | -------------------------------------------------------------------------------- /Example - Generator - Maze Recursive backtracker.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class maze 8 | Field sw:Int,sh:Int 9 | Field mw:Int,mh:Int 10 | Field tw:Float,th:Float 11 | Field mazex:Stack = New Stack 12 | Field mazey:Stack = New Stack 13 | Field map:Int[,] = New Int[1,1] 14 | Field map2:Int[,,] = New Int[1,1,1] 15 | Enum wall 16 | up=1, 17 | right=2, 18 | down=3, 19 | left=4 20 | End Enum 21 | Method New(sw:Int,sh:Int,mw:Int,mh:Int) 22 | Self.sw = sw 23 | Self.sh = sh 24 | Self.mw = mw 25 | Self.mh = mh 26 | tw = sw/mw 27 | th = sh/mh 28 | map = New Int[mw,mh] 29 | map2 = New Int[mw,mh,4] 30 | makemaze() 31 | End Method 32 | Method makemaze() 33 | Local ax:Int[] = New Int[](0,1,0,-1) 34 | Local ay:Int[] = New Int[](-1,0,1,0) 35 | mazex.Push(Rnd(mw)) 36 | mazey.Push(Rnd(mh)) 37 | While mazex.Empty = False 38 | Local x:Int=mazex.Top 39 | Local y:Int=mazey.Top 40 | Local d:Int[] = New Int[4] 41 | Local deadend:Bool=True 42 | For Local i:=0 Until ax.Length 43 | Local x2:Int=x+ax[i] 44 | Local y2:Int=y+ay[i] 45 | If x2>=0 And x2=0 And y2 0 82 | Select i 83 | Case 0 84 | canvas.DrawLine(x2,y2,x2,y2-th) 85 | Case 1 86 | canvas.DrawLine(x2,y2,x2+tw,y2) 87 | Case 2 88 | canvas.DrawLine(x2,y2,x2,y2+th) 89 | Case 3 90 | canvas.DrawLine(x2,y2,x2-tw,y2) 91 | End Select 92 | End If 93 | Next 94 | Next 95 | Next 96 | End Method 97 | End Class 98 | 99 | Global mymaze:maze 100 | 101 | 102 | Class MyWindow Extends Window 103 | 104 | Method New() 105 | Title="Maze - Recursive backtracker." 106 | mymaze = New maze(Width,Height,10,10) 107 | End Method 108 | 109 | Method OnRender( canvas:Canvas ) Override 110 | App.RequestRender() ' Activate this method 111 | 112 | ' 113 | If Keyboard.KeyReleased(Key.Space) Or Mouse.ButtonReleased(MouseButton.Left) Then 114 | SeedRnd(Millisecs()) 115 | mymaze = New maze(Width,Height,10,10) 116 | End If 117 | ' 118 | mymaze.draw(canvas) 119 | ' 120 | canvas.Color = Color.Black 121 | canvas.DrawText("Press space for new maze",0,0) 122 | ' if key escape then quit 123 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 124 | End Method 125 | 126 | End Class 127 | 128 | Function Main() 129 | New AppInstance 130 | New MyWindow 131 | App.Run() 132 | End Function 133 | -------------------------------------------------------------------------------- /Example - Generator - Rogue Basin Conway maps.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | ' Based on code linked on Rogue Basin - Conways Game of Life 5 | 6 | Using std.. 7 | Using mojo.. 8 | 9 | Class MyWindow Extends Window 10 | 11 | Field neighbours:Int 12 | Field iterations:Int 13 | Field mapx:Int 14 | Field mapy:Int 15 | Field closecellprob:Int 16 | Field probexceeded:Bool 17 | Field map:Int[,] 18 | 19 | Method New() 20 | neighbours = 4 21 | iterations = 50000 22 | probexceeded = True 23 | mapx = 99 24 | mapy = 99 25 | closecellprob = 45 26 | go() 27 | End method 28 | 'build a map 29 | Method go() 30 | map = New Int[mapx,mapy] 31 | ' go through each cell and use the specified probability to determine if it's open 32 | For Local y:Int=0 Until map.GetSize(0) 33 | For Local x:Int=0 Until map.GetSize(1) 34 | If Rnd(100) < closecellprob 35 | map[x,y] = 1 36 | End If 37 | Next 38 | Next 39 | 'pick cell at random 40 | For Local i:Int=0 Until iterations 41 | Local rx:Int=Rnd(map.GetSize(0)) 42 | Local ry:Int=Rnd(map.GetSize(1)) 43 | If probexceeded=True 44 | If examineneighbours(rx,ry)>neighbours 45 | map[rx,ry] = 1 46 | Else 47 | map[rx,ry] = 0 48 | End If 49 | Else 50 | If examineneighbours(rx,ry)>neighbours 51 | map[rx,ry] = 0 52 | Else 53 | map[rx,ry] = 1 54 | End If 55 | End If 56 | Next 57 | End Method 58 | 'Count all the closed cells around the specified cell and return that number 59 | Method examineneighbours:Int(xval:Int,yval:Int) 60 | Local count:Int=0 61 | For Local x:Int=-1 To 1 62 | For Local y:Int=-1 To 1 63 | If checkcell(xval+x,yval+y) = True 64 | count+=1 65 | End If 66 | Next 67 | Next 68 | Return count 69 | End Method 70 | 'Check the examined cell is legal and closed 71 | Method checkcell:Bool(x:Int,y:Int) 72 | If (x>=0 And x=0 And y0 74 | Return True 75 | Else 76 | Return False 77 | End If 78 | Else 79 | Return False 80 | End If 81 | End Method 82 | 83 | Method OnRender( canvas:Canvas ) Override 84 | App.RequestRender() ' Activate this method 85 | 'Draw the map on the screen 86 | Local cw:Float=Float(Width)/Float(mapx) 87 | Local ch:Float=Float(Height)/Float(mapy) 88 | For Local y:Int=0 Until mapy 89 | For Local x:Int=0 Until mapx 90 | If map[x,y] = 1 91 | canvas.Color=Color.White 92 | Else 93 | canvas.Color=Color.Black 94 | End If 95 | canvas.DrawRect(x*cw,y*ch,cw,ch) 96 | Next 97 | Next 98 | 'If pressed space then create new map 99 | If Keyboard.KeyReleased(Key.Space) Then 100 | neighbours = 4 101 | iterations = 50000 102 | probexceeded = True 103 | If Rnd()<.5 Then 104 | probexceeded=False 105 | If Rnd() <.5 106 | neighbours=2 107 | End if 108 | End If 109 | closecellprob = Rnd(30,60)'45 110 | go() 111 | End If 112 | ' if key escape then quit 113 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 114 | canvas.Color = Color.Blue 115 | canvas.DrawRect(0,0,Width,15) 116 | canvas.Color = Color.Red 117 | canvas.DrawText("Press space to generate new map",0,0) 118 | End Method 119 | 120 | End Class 121 | 122 | Function Main() 123 | New AppInstance 124 | New MyWindow 125 | App.Run() 126 | End Function 127 | -------------------------------------------------------------------------------- /Example - Generator - World easy.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class world 8 | Field map:Int[,] = New Int[1,1] 9 | Field tw:Float,th:Float 10 | Field sw:Int,sh:Int 11 | Field mw:Int,mh:Int 12 | Method New(sw:Int,sh:Int,mw:Int,mh:Int) 13 | Self.mw = mw 14 | Self.mh = mh 15 | Self.sw = sw 16 | Self.sh = sh 17 | tw = Float(sw)/Float(mw) 18 | th = Float(sh)/Float(mh) 19 | map = New Int[mw,mh] 20 | makemap() 21 | End Method 22 | Method makemap() 23 | Local eloop:Bool=False 24 | While eloop=False 25 | Local x1:Int=Rnd(-10,mw) 26 | Local y1:Int=Rnd(-10,mh) 27 | Local w:Int=Rnd(1,12) 28 | Local h:Int=Rnd(1,10) 29 | For Local y2:=y1 To y1+h 30 | For Local x2:=x1 To x1+w 31 | If x2>=0 And x2=0 And y2 31 Then eloop=True 34 | End If 35 | Next 36 | Next 37 | Wend 38 | For Local y:=0 Until mh 39 | For Local x:=0 Until mw 40 | map[x,y] = (10.0/31)*map[x,y] 41 | Next 42 | Next 43 | End Method 44 | Method draw(canvas:Canvas) 45 | For Local y:float=0 Until mh Step 1 46 | For Local x:Float=0 Until mw Step 1 47 | Local t:Int=map[x,y] 48 | Select t 49 | Case 0 50 | canvas.Color = New Color(0,0,.5) 51 | Case 2 52 | canvas.Color = New Color(0,0,.6) 53 | Case 3 54 | canvas.Color = New Color(0,0,.7) 55 | Case 4 56 | canvas.Color = New Color(.1,.1,.7) 57 | Case 5 58 | canvas.Color = New Color(1,.8,0) 59 | Case 6 60 | canvas.Color = New Color(.2,1,.1) 61 | Case 7 62 | canvas.Color = New Color(0,.5,0) 63 | Case 8 64 | canvas.Color = New Color(0,.2,0) 65 | Case 9 66 | canvas.Color = New Color(.9,.8,.9) 67 | Case 10 68 | canvas.Color = New Color(.9,.9,.9) 69 | End Select 70 | canvas.DrawRect(x*tw,y*th,tw,th) 71 | Next 72 | Next 73 | End Method 74 | End Class 75 | 76 | Global myworld:world 77 | 78 | Class MyWindow Extends Window 79 | Field cnt:Int 80 | Method New() 81 | Title="Monkey 2 - World map Generator" 82 | myworld = New world(Width,Height,100,100) 83 | End Method 84 | 85 | Method OnRender( canvas:Canvas ) Override 86 | App.RequestRender() ' Activate this method 87 | cnt+=1 88 | If cnt>200 89 | cnt=0 90 | Local s:Int=Rnd(32,256) 91 | myworld = New world(Width,Height,s,s) 92 | End If 93 | myworld.draw(canvas) 94 | ' if key escape then quit 95 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 96 | End Method 97 | 98 | End Class 99 | 100 | Function Main() 101 | New AppInstance 102 | New MyWindow 103 | App.Run() 104 | End Function 105 | -------------------------------------------------------------------------------- /Example - Image Width and Height.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class MyWindow Extends Window 8 | 9 | Field can:Canvas 10 | Field im:Image 11 | 12 | Method New() 13 | im = New Image(640,480) 14 | can = New Canvas(im) 15 | can.Clear(Color.Black) 16 | can.Flush() 17 | End Method 18 | 19 | Method OnRender( canvas:Canvas ) Override 20 | App.RequestRender() ' Activate this method 21 | ' if key escape then quit 22 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 23 | canvas.DrawText("Image Width:"+im.Width,0,0) 24 | canvas.DrawText("Image Height:"+im.Height,0,30) 25 | End Method 26 | 27 | End Class 28 | 29 | Function Main() 30 | New AppInstance 31 | 32 | New MyWindow 33 | 34 | App.Run() 35 | 36 | End Function 37 | -------------------------------------------------------------------------------- /Example - Increase variable in OnRender.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | Class MyWindow Extends Window 10 | 11 | Field t:Int=0 12 | 13 | Method New() 14 | End Method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() 18 | t+=1 19 | canvas.DrawText(t,0,0) 20 | End Method 21 | 22 | Method OnKeyEvent( event:KeyEvent ) Override 23 | Select event.Type 24 | Case EventType.KeyDown 25 | Select event.Key 26 | Case Key.Escape 27 | instance.Terminate() 28 | End Select 29 | End Select 30 | End Method 31 | 32 | End Class 33 | 34 | Function Main() 35 | instance = New AppInstance 36 | 37 | New MyWindow 38 | 39 | App.Run() 40 | 41 | End Function 42 | -------------------------------------------------------------------------------- /Example - Keyboard KeyDown.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End Method 15 | 16 | 17 | Method OnRender( canvas:Canvas ) Override 18 | App.RequestRender() ' Loop with sync 19 | Local kd:=False 20 | If Keyboard.KeyDown(Key.Space) Then kd=true 21 | canvas.Clear(Color.Black) 22 | canvas.Color = Color.White 23 | If kd = True Then 24 | canvas.DrawText("Space is pressed",0,0) 25 | Else 26 | canvas.DrawText("Space is not pressed",0,0) 27 | End If 28 | End Method 29 | 30 | Method OnKeyEvent( event:KeyEvent ) Override 31 | Select event.Type 32 | Case EventType.KeyDown 33 | Select event.Key 34 | Case Key.Escape 35 | instance.Terminate() 'exit with escape 36 | End Select 37 | End Select 38 | End Method 39 | 40 | End Class 41 | 42 | Function Main() 43 | instance = New AppInstance 44 | 45 | New MyWindow 46 | 47 | App.Run() 48 | 49 | End Function 50 | -------------------------------------------------------------------------------- /Example - Metaballs.monkey2: -------------------------------------------------------------------------------- 1 | 'Thanks to the coding train for showing how it's done 2 | 3 | #Import "" 4 | #Import "" 5 | 6 | Using std.. 7 | Using mojo.. 8 | 9 | Class metaball 10 | Class blob 11 | Field x:Float,y:Float 12 | Field r:Float 13 | Field incx:Float,incy:Float 14 | Field screenwidth:Int,screenheight:Int 15 | Method New(x:Int,y:Int,sw:Int,sh:Int) 16 | Self.x = x 17 | Self.y = y 18 | r = Rnd(5,14) 19 | incx = Rnd(-2,2) 20 | incy = Rnd(-2,2) 21 | Self.screenwidth = sw 22 | Self.screenheight = sh 23 | End Method 24 | Method update() 25 | x += incx 26 | y += incy 27 | If x<0 Or x>screenwidth Then incx = -incx 28 | If y<0 Or y>screenheight Then incy = -incy 29 | End Method 30 | End Class 31 | Field image:Image 32 | Field pixels:Pixmap 33 | Field width:Int,height:Int 34 | Field ball:blob[] = New blob[4] 35 | Method New(w:Int,h:Int) 36 | width = w 37 | height = h 38 | pixels = New Pixmap( width,height ) 39 | image = New Image(width,height) 40 | For Local i:Int=0 Until 4 41 | ball[i] = New blob(Rnd(width),Rnd(height),width,height) 42 | Next 43 | End Method 44 | Method update() 45 | For Local i:=Eachin ball 46 | i.update() 47 | Next 48 | For Local y:Int=0 Until height 49 | For Local x:Int=0 Until width 50 | Local index:Int = x+y*width 51 | Local sum:Float = 0 52 | For Local b:=Eachin ball 53 | Local d:Float = distance(x,y,b.x,b.y) 54 | sum += b.r / d 55 | Next 56 | If sum<0 Then sum=0 57 | If sum>1 Then sum=1 58 | If sum<.5 Then sum = 0 Else sum=1 59 | pixels.SetPixel(x,y,New Color(sum,sum,sum)) 60 | Next 61 | Next 62 | image.Texture.PastePixmap(pixels,0,0) 63 | End Method 64 | Method draw(canvas:Canvas) 65 | canvas.Color = Color.White 66 | canvas.DrawImage(image,0,0,0,(1.0/width)*640.0,(1.0/height)*480.0) 67 | End Method 68 | Function distance:Float(x1:Int,y1:Int,x2:Int,y2:Int) 69 | Return Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) 70 | End Function 71 | End Class 72 | 73 | 74 | Class MyWindow Extends Window 75 | Field mymetaball:metaball 76 | Field time:Int=Millisecs()+3000 77 | Method New() 78 | mymetaball = New metaball(200,200) 79 | End method 80 | 81 | Method OnRender( canvas:Canvas ) Override 82 | App.RequestRender() ' Activate this method 83 | ' 84 | If Mouse.ButtonReleased(MouseButton.Left) Or Millisecs()>time 85 | time = Millisecs() + 3000 86 | mymetaball = New metaball(200,200) 87 | End If 88 | mymetaball.update() 89 | mymetaball.draw(canvas) 90 | ' if key escape then quit 91 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 92 | End Method 93 | 94 | End Class 95 | 96 | Function Main() 97 | New AppInstance 98 | New MyWindow 99 | App.Run() 100 | End Function 101 | -------------------------------------------------------------------------------- /Example - Microsecs() and Millisecs().monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | Class MyWindow Extends Window 10 | 11 | Method New() 12 | End method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Loop with sync 16 | canvas.Clear(Color.Black) 17 | canvas.Color = Color.White 18 | canvas.DrawText("Milliseconds from start of program - ",0,0) 19 | canvas.DrawText(Millisecs(),320,0) 20 | canvas.DrawText("Microseconds from start of program - ",0,15) 21 | canvas.DrawText(Microsecs(),320,15) 22 | End Method 23 | 24 | Method OnKeyEvent( event:KeyEvent ) Override 25 | Select event.Type 26 | Case EventType.KeyDown 27 | Select event.Key 28 | Case Key.Escape 29 | instance.Terminate() 'exit with escape 30 | End Select 31 | End Select 32 | End Method 33 | 34 | End Class 35 | 36 | Function Main() 37 | instance = New AppInstance 38 | 39 | New MyWindow 40 | 41 | App.Run() 42 | 43 | End Function 44 | -------------------------------------------------------------------------------- /Example - Mouse X and Mouse Y.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End Method 15 | 16 | 17 | Method OnRender( canvas:Canvas ) Override 18 | App.RequestRender() ' Loop with sync 19 | canvas.Clear(Color.Black) 20 | canvas.Color = Color.White 21 | canvas.DrawText("Mouse.X:"+Mouse.X,0,0) 22 | canvas.DrawText("Mouse.Y:"+Mouse.Y,0,15) 23 | End Method 24 | 25 | Method OnKeyEvent( event:KeyEvent ) Override 26 | Select event.Type 27 | Case EventType.KeyDown 28 | Select event.Key 29 | Case Key.Escape 30 | instance.Terminate() 'exit with escape 31 | End Select 32 | End Select 33 | End Method 34 | 35 | End Class 36 | 37 | Function Main() 38 | instance = New AppInstance 39 | 40 | New MyWindow 41 | 42 | App.Run() 43 | 44 | End Function 45 | -------------------------------------------------------------------------------- /Example - MulitDimensional Arrays.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | 11 | Class MyWindow Extends Window 12 | 13 | field map:= new int[10,10] 14 | 15 | Method New() 16 | Local cnt:=0 17 | For Local y:=0 Until 10 18 | For Local x:=0 Until 10 19 | map[x,y]=cnt 20 | cnt+=1 21 | Next 22 | Next 23 | End Method 24 | 25 | 26 | Method OnRender( canvas:Canvas ) Override 27 | App.RequestRender() ' Loop with sync 28 | canvas.Clear(Color.Black) 29 | canvas.Color = Color.White 30 | For Local y:=0 Until 10 31 | For Local x:=0 Until 10 32 | canvas.DrawText(map[x,y],x*32,y*20) 33 | Next 34 | Next 35 | End Method 36 | 37 | Method OnKeyEvent( event:KeyEvent ) Override 38 | Select event.Type 39 | Case EventType.KeyDown 40 | Select event.Key 41 | Case Key.Escape 42 | instance.Terminate() 'exit with escape 43 | End Select 44 | End Select 45 | End Method 46 | 47 | End Class 48 | 49 | Function Main() 50 | instance = New AppInstance 51 | 52 | New MyWindow 53 | 54 | App.Run() 55 | 56 | End Function 57 | -------------------------------------------------------------------------------- /Example - Notify requester.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | Class MyWindow Extends Window 10 | 11 | Method New() 12 | End method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | ' if key escape then quit 17 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 18 | If Keyboard.KeyReleased(Key.Space) Then Notify("Message","Hello") 19 | canvas.DrawText("Press Space for Notify Message",0,0) 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | instance = New AppInstance 26 | 27 | New MyWindow 28 | 29 | App.Run() 30 | 31 | End Function 32 | -------------------------------------------------------------------------------- /Example - Orientation - point left or right vs line.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field point1:Vec2i,point2:Vec2i 10 | Field point3:Vec2i 11 | Field time:Int 12 | Method New() 13 | newline() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | time+=1 19 | 'draw the line 20 | canvas.Color = Color.White 21 | canvas.DrawLine(point1,point2) 22 | canvas.DrawText("p1",point1.x,point1.y) 23 | canvas.DrawText("p2",point2.x,point2.y) 24 | canvas.Color = Color.Red 25 | canvas.DrawCircle(point3.x,point3.y,20) 26 | canvas.Color = Color.Yellow 27 | Local side:Int = orientation(point1.x,point1.y,point2.x,point2.y,point3.x,point3.y) 28 | Local t:String="point is on the line." 29 | If side<0 Then t="Point is on the left side of the line." 30 | If side>0 Then t="Point is on the right side of the line." 31 | canvas.DrawText(t,0,0) 32 | If Keyboard.KeyReleased(Key.Space) Or time>120 Then newline() ; time=0 33 | ' if key escape then quit 34 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 35 | 36 | End Method 37 | 38 | Method newline() 39 | point1 = New Vec2i(Rnd(50,590),Rnd(50,430)) 40 | Local x:Int=Rnd(40,90),y:Int=Rnd(40,90) 41 | If Rnd()<.5 Then x=-x 42 | If Rnd()<.5 Then y=-y 43 | point2 = New Vec2i(point1.x+x,point1.y+y) 44 | point3 = New Vec2i(Rnd(640),Rnd(480)) 45 | End Method 46 | 47 | End Class 48 | ' 49 | ' This is the orientation function. It returns -1 if the point is left of the inputted line. 50 | ' 0 if on the same and 1 if on the right of the line. 51 | ' 52 | Function orientation:Int(ax:Int,ay:Int,bx:Int,by:Int,cx:Int,cy:Int) 53 | Return Sgn((bx-ax)*(cy-ay)-(by-ay)*(cx-ax)) 54 | End Function 55 | 56 | Function Main() 57 | New AppInstance 58 | New MyWindow 59 | App.Run() 60 | End Function 61 | -------------------------------------------------------------------------------- /Example - Pi and Cos and Sin.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | Class MyWindow Extends Window 10 | 11 | Field angle:=0.0 12 | 13 | Method New() 14 | End Method 15 | 16 | 17 | Method OnRender( canvas:Canvas ) Override 18 | App.RequestRender() ' Loop with sync 19 | angle+=0.05 20 | If angle>Pi*2 Then angle=0 21 | canvas.Clear(Color.Black) 22 | canvas.Color = Color.White 23 | canvas.DrawText(angle,0,0) 24 | canvas.DrawText("2xPi is full circle",0,15) 25 | canvas.DrawLine(320,200,320+Cos(angle)*20,200+Sin(angle)*20) 26 | End Method 27 | 28 | Method OnKeyEvent( event:KeyEvent ) Override 29 | Select event.Type 30 | Case EventType.KeyDown 31 | Select event.Key 32 | Case Key.Escape 33 | instance.Terminate() 'exit with escape 34 | End Select 35 | End Select 36 | End Method 37 | 38 | End Class 39 | 40 | Function Main() 41 | instance = New AppInstance 42 | 43 | New MyWindow 44 | 45 | App.Run() 46 | 47 | End Function 48 | -------------------------------------------------------------------------------- /Example - Pixmap and Clear and Image.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Field pixmap:=New Pixmap( 640,480 ) 11 | Field image:=New Image( 640,480,TextureFlags.Dynamic ) 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | 19 | pixmap.Clear(New Color(Rnd(),Rnd(),Rnd(),1)) 20 | 21 | image.Texture.PastePixmap( pixmap,0,0 ) 22 | canvas.DrawImage( image,0,0 ) 23 | 24 | canvas.Color = Color.White 25 | canvas.DrawText("FPS:"+App.FPS,0,0) 26 | 27 | ' if key escape then quit 28 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 29 | End Method 30 | 31 | End Class 32 | 33 | Function Main() 34 | New AppInstance 35 | New MyWindow 36 | App.Run() 37 | End Function 38 | -------------------------------------------------------------------------------- /Example - Pixmap and SetPixel.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Field pixmap:=New Pixmap( 640,480 ) 11 | Field image:=New Image( 640,480,TextureFlags.Dynamic ) 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | 19 | pixmap.Clear(New Color(Rnd(),Rnd(),Rnd(),1)) 20 | For Local y:=0 Until 100 21 | For Local x:=0 Until 100 22 | pixmap.SetPixel(x,y,Color.Black) 23 | Next 24 | Next 25 | 26 | image.Texture.PastePixmap( pixmap,0,0 ) 27 | canvas.DrawImage( image,0,0 ) 28 | 29 | canvas.Color = Color.White 30 | canvas.DrawText("FPS:"+App.FPS,0,0) 31 | 32 | ' if key escape then quit 33 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 34 | End Method 35 | 36 | End Class 37 | 38 | Function Main() 39 | New AppInstance 40 | New MyWindow 41 | App.Run() 42 | End Function 43 | -------------------------------------------------------------------------------- /Example - Player swinging on a rope.monkey2: -------------------------------------------------------------------------------- 1 | ' 2 | ' Thanks to Daniel SHiffman (book - nature of code) 3 | ' 4 | ' This example might be usable in platformer games (rope and swing) 5 | ' 6 | 7 | #Import "" 8 | #Import "" 9 | 10 | Using std.. 11 | Using mojo.. 12 | 13 | 14 | Class MyWindow Extends Window 15 | 16 | ' player x and y and width and height 17 | Field px:Float=100,py:Float=200 18 | Field pw:Int=32,ph:Int=32 19 | 20 | 'our anchor x and y 21 | Field ax:Int=320,ay:Int=0 22 | 23 | Field angle:Float 'our angle between anchor and player 24 | Field dist:Float 'distance between anchor and player 25 | Field acc:Float,vel:Float 'acceleration and velocity 26 | Field gravity:Float=0.4 ' 27 | Field damping:Float=0.995 'slowdown 28 | Field r:Float 'distance between anchor and player 29 | Method New() 30 | ' Get the angle betweem the player and the anchor 31 | angle = getangle(px,py,ax,ay) 32 | ' Get the distgance between the player and anchor 33 | r = distance(px,py,ax,ay) 34 | End method 35 | 36 | Method OnRender( canvas:Canvas ) Override 37 | App.RequestRender() ' Activate this method 38 | 39 | ' swing harder 40 | If Keyboard.KeyReleased(Key.Right) Then vel+=.005 41 | If Keyboard.KeyReleased(Key.Left) Then vel-=.005 42 | ' CLimb up or down.. 43 | If Keyboard.KeyDown(Key.Up) Then r-=2 44 | If Keyboard.KeyDown(Key.Down) Then r+=2 45 | 46 | ' Do the swing math 47 | acc=(-1*gravity/r)*Sin(angle) 48 | vel+=acc 49 | angle+=vel 50 | vel*=damping 51 | 52 | ' update our position 53 | px = r*Sin(angle) 54 | py = r*Cos(angle) 55 | px+=ax 56 | py+=ay 57 | 58 | ' Draw our player 59 | canvas.DrawRect(px,py,pw,ph) 60 | canvas.DrawLine(px+pw/2,py,ax,ay) 61 | 62 | canvas.DrawText("Press cursor left and right to apply force..",0,0) 63 | canvas.DrawText("Hold cursor Up Down to climb or Lower yourself..",0,20) 64 | 65 | ' if key escape then quit 66 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 67 | End Method 68 | 69 | End Class 70 | 71 | ' Return the angle from - to in float 72 | Function getangle:Float(x1:Int,y1:Int,x2:Int,y2:Int) 73 | Return ATan2(y2-y1, x2-x1) 74 | End Function 75 | 76 | ' Manhattan Distance (less precise) 77 | Function distance:Float(x1:Float,y1:Float,x2:Float,y2:Float) 78 | Return Abs(x2-x1)+Abs(y2-y1) 79 | End Function 80 | 81 | Function Main() 82 | New AppInstance 83 | New MyWindow 84 | App.Run() 85 | End Function 86 | -------------------------------------------------------------------------------- /Example - Point vs Polygon Collision.monkey2: -------------------------------------------------------------------------------- 1 | 2 | #Import "" 3 | #Import "" 4 | 5 | Using std.. 6 | Using mojo.. 7 | 8 | 9 | Class MyWindow Extends Window 10 | ' The variable for a line on the screen 11 | 12 | Field polygonx:Int[],polygony:Int[] 13 | 14 | Method New() 15 | polygonx = New Int[4] 16 | polygony = New Int[4] 17 | polygonx[0] = 50 18 | polygony[0] = 50 19 | polygonx[1] = 150 20 | polygony[1] = 100 21 | polygonx[2] = 150 22 | polygony[2] = 200 23 | polygonx[3] = 100 24 | polygony[3] = 100 25 | End Method 26 | 27 | 28 | Method OnRender( canvas:Canvas ) Override 29 | App.RequestRender() ' Activate this method 30 | ' 31 | 32 | ' 33 | drawpoly(canvas) 34 | If pointpolycollide(Mouse.X,Mouse.Y) =True Then 35 | canvas.Color = Color.White 36 | canvas.DrawText("Collision",0,0) 37 | End If 38 | 39 | canvas.Color = Color.White 40 | canvas.DrawText("Move mouse inside and outside of polygon",320,0) 41 | ' if key escape then quit 42 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 43 | End Method 44 | Method drawpoly(canvas:Canvas) 45 | canvas.Color = Color.White 46 | For Local i:Int=1 Until polygonx.GetSize(0) 47 | canvas.DrawLine(polygonx[i-1],polygony[i-1],polygonx[i],polygony[i]) 48 | Next 49 | canvas.DrawLine(polygonx[0],polygony[0],polygonx[polygonx.GetSize(0)-1],polygony[polygony.GetSize(0)-1]) 50 | End Method 51 | Method pointpolycollide:Bool(pointx:Int,pointy:Int) 52 | Local collisioncount:Int=0 53 | Local sx:Int=Rnd(640),sy:Int=Rnd(480) 54 | If Rnd()<.5 Then sx-=1000 Else sx+=1000 55 | If Rnd()<.5 Then sy-=1000 Else sy+=1000 56 | For Local i:Int=1 Until polygonx.GetSize(0) 57 | If get_line_intersection(polygonx[i-1],polygony[i-1],polygonx[i],polygony[i],pointx,pointy,sx,sy) Then collisioncount+=1 58 | Next 59 | If get_line_intersection(polygonx[0],polygony[0],polygonx[polygonx.GetSize(0)-1],polygony[polygony.GetSize(0)-1],pointx,pointy,sx,sy) Then collisioncount+=1 60 | ' 61 | If (collisioncount Mod 2) > 0 Then Return True 62 | 63 | ' 64 | Return False 65 | End Method 66 | End Class 67 | 68 | Function Main() 69 | New AppInstance 70 | New MyWindow 71 | App.Run() 72 | End Function 73 | 74 | 75 | '// Returns 1 if the lines intersect, otherwise 0. Andre la moth. 76 | Function get_line_intersection:Bool(p0_x:Float, p0_y:Float, p1_x:Float, p1_y:Float, p2_x:Float,p2_y:Float, p3_x:Float, p3_y:Float) 77 | 78 | Local s1_x:Float, s1_y:Float, s2_x:Float, s2_y:Float 79 | s1_x = p1_x - p0_x 80 | s1_y = p1_y - p0_y 81 | s2_x = p3_x - p2_x 82 | s2_y = p3_y - p2_y 83 | 84 | Local s:Float, t:Float 85 | s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y) 86 | t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y) 87 | 88 | If (s >= 0 And s <= 1 And t >= 0 And t <= 1) Then Return True 89 | 90 | 91 | Return False '// No collision 92 | End Function 93 | -------------------------------------------------------------------------------- /Example - Read and Write ClipboardText.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | 11 | Class MyWindow Extends Window 12 | 13 | Field mydata:="Nothing" 14 | 15 | Method New() 16 | End Method 17 | 18 | Method OnRender( canvas:Canvas ) Override 19 | ' if key escape then quit 20 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 21 | ' Put the clipboardtext into the string variable 22 | If Keyboard.KeyReleased(Key.Space) Then 23 | mydata = instance.ClipboardText 24 | End If 25 | 'Put string into the clipboard text 26 | If Keyboard.KeyReleased(Key.P) Then 27 | instance.ClipboardText = mydata 28 | End If 29 | 30 | App.RequestRender() ' Loop with sync 31 | canvas.Clear(Color.Black) 32 | canvas.Color = Color.White 33 | canvas.DrawText("Space to Paste Current Clipboardtext..",0,0) 34 | canvas.DrawText("P to copy data into the clipboardtext..",0,15) 35 | canvas.DrawText(mydata,0,30) 36 | End Method 37 | 38 | End Class 39 | 40 | Function Main() 41 | instance = New AppInstance 42 | 43 | New MyWindow 44 | 45 | App.Run() 46 | 47 | End Function 48 | -------------------------------------------------------------------------------- /Example - Recursion Fractal.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field r:Float=0 10 | Method New() 11 | 12 | End method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | ' 17 | r+=.1 18 | If r>=TwoPi Then r=0 19 | canvas.Translate(Width/2,Height/2) 20 | canvas.Rotate(r) 21 | canvas.OutlineMode = OutlineMode.Solid 22 | canvas.OutlineWidth = 2 23 | canvas.OutlineColor = Color.Black 24 | drawcirc(canvas,0,0,300) 25 | canvas.Translate(-Width/2,-Height/2) 26 | ' if key escape then quit 27 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 28 | End Method 29 | Method drawcirc(canvas:Canvas,x:Int,y:Int,r:Int) 30 | If r<2 Then Return 31 | canvas.DrawCircle(x,y,r) 32 | drawcirc(canvas,x+r*0.5,y,r*0.5) 33 | drawcirc(canvas,x-r*0.5,y,r*0.5) 34 | drawcirc(canvas,x,y+r*0.5,r*0.5) 35 | drawcirc(canvas,x,y-r*0.5,r*0.5) 36 | End Method 37 | 38 | End Class 39 | 40 | Function Main() 41 | New AppInstance 42 | New MyWindow 43 | App.Run() 44 | End Function 45 | -------------------------------------------------------------------------------- /Example - Replace.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | 9 | Class map 10 | Field map:String[,] = New String[10,40] 11 | Method New() 12 | For Local i:=0 Until 110*40 13 | Local ns:String 14 | For Local ii:=0 Until 10 15 | ns+=String.FromChar(Rnd(255)) 16 | Next 17 | map[Rnd(0,10),Rnd(0,40)] = ns 18 | Next 19 | End Method 20 | Method draw(canvas:Canvas) 21 | canvas.Color = Color.White 22 | For Local y:=0 Until 40 23 | For Local x:=0 Until 10 24 | canvas.DrawText(map[x,y],x*64,y*16) 25 | Next 26 | Next 27 | End Method 28 | End Class 29 | 30 | Global mymap:map 31 | 32 | Class MyWindow Extends Window 33 | 34 | Method New() 35 | mymap = New map() 36 | 37 | ' 38 | End Method 39 | 40 | Method OnRender( canvas:Canvas ) Override 41 | App.RequestRender() ' Activate this method 42 | ' if key escape then quit 43 | mymap.draw(canvas) 44 | For Local y:=0 until 40 45 | For Local x:=0 Until 10 46 | Local a:String=mymap.map[x,y] 47 | a=mymap.map[x,y] 48 | a = a.Replace(String.FromChar(Rnd(255)),"") 49 | mymap.map[x,y]=a 50 | Next 51 | Next 52 | 53 | Local cnt:=0 54 | For Local y:=0 until 40 55 | For Local x:=0 Until 10 56 | If mymap.map[x,y] <> "" Then cnt+=1 57 | Next 58 | Next 59 | If cnt<40*3 Then 60 | mymap = New map() 61 | End If 62 | ' 63 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 64 | End Method 65 | 66 | End Class 67 | 68 | Function Main() 69 | New AppInstance 70 | New MyWindow 71 | App.Run() 72 | End Function 73 | -------------------------------------------------------------------------------- /Example - Rnd and Image.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | 10 | Class MyWindow Extends Window 11 | 12 | Field can:Canvas 13 | Field im:Image 14 | 15 | Method New() 16 | im = New Image(640,480) 17 | can = New Canvas(im) 18 | can.Clear(Color.Black) 19 | End Method 20 | 21 | Method OnRender( canvas:Canvas ) Override 22 | App.RequestRender() ' Activate this method 23 | ' if key escape then quit 24 | If Keyboard.KeyReleased(Key.Escape) Then instance.Terminate() 25 | 26 | ' canvas.Clear(Color.Black) 27 | can.Color = Color.White 28 | can.DrawText(Int(Rnd(0,10)),Rnd(0,640),Rnd(20,480)) 29 | can.Flush() 30 | canvas.DrawImage(im,0,0) 31 | canvas.Color = Color.Yellow 32 | canvas.DrawText("Int(Rnd(0,10))",0,0) 33 | End Method 34 | 35 | End Class 36 | 37 | Function Main() 38 | instance = New AppInstance 39 | 40 | New MyWindow 41 | 42 | App.Run() 43 | 44 | End Function 45 | -------------------------------------------------------------------------------- /Example - Rpg - Battle Screen.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class battleui 8 | Field Width:Int,Height:Int 9 | Field phealth:Int 10 | Field pattack:Int 11 | Field pdefence:Int 12 | Field npchealth:Int 13 | Field npcattack:Int 14 | Field npcdefence:Int 15 | Field battlestate:Int 16 | Field uititle:String = "Battle Screen" 17 | Field timer:Int 18 | Field cursory:Int 19 | Enum state 20 | intro=1, 21 | fighting=2, 22 | victory=3, 23 | selattack=4 24 | End Enum 25 | Enum loot 26 | healthpotion=1, 27 | diamond=2 28 | End Enum 29 | Method New(width:Int,height:Int) 30 | Width = width 31 | Height = height 32 | timer = 120 33 | battlestate = state.selattack 34 | npchealth = Rnd(4,12) 35 | npcattack = Rnd(1,10) 36 | npcdefence = Rnd(1,10) 37 | phealth = Rnd(20,100) 38 | pattack = Rnd(10,50) 39 | pdefence = Rnd(1,50) 40 | End Method 41 | Method update() 42 | Select battlestate 43 | Case state.selattack 44 | If Keyboard.KeyReleased(Key.Down) Then cursory+=1 45 | If Keyboard.KeyReleased(Key.Up) Then cursory-=1 46 | cursory = Clamp(cursory,0,2) 47 | End Select 48 | End Method 49 | Method draw(canvas:Canvas) 50 | canvas.Clear(Color.Black) 51 | canvas.Color=Color.White 52 | canvas.Scale(2,2) 53 | canvas.DrawText(uititle,0,0) 54 | ' draw the player stats 55 | canvas.DrawText("Player",32,50) 56 | canvas.DrawText("Health : "+phealth,32,70) 57 | canvas.DrawText("Attack : "+pattack,32,90) 58 | canvas.DrawText("Defence : "+pdefence,32,110) 59 | ' draw the enemy stats 60 | canvas.DrawText("Enemy",196,50) 61 | canvas.DrawText("Health : "+npchealth,196,70) 62 | canvas.DrawText("Attack : "+npcattack,196,90) 63 | canvas.DrawText("Defence : "+npcdefence,196,110) 64 | 65 | ' draw the user interface is needed 66 | Select battlestate 67 | Case state.selattack 68 | ' selection 69 | Local s:String[] = New String[]("Attack","Run Away","Use Health Potion") 70 | s[cursory] = "->"+s[cursory]+"<-" 71 | canvas.DrawText(s[0],Width/4 ,Height/2-40,.5,.5) 72 | canvas.DrawText(s[1],Width/4 ,Height/2-25,.5,.5) 73 | canvas.DrawText(s[2],Width/4,Height/2-10,.5,.5) 74 | ' 75 | End Select 76 | End Method 77 | End Class 78 | 79 | Global mybattleui:battleui 80 | 81 | Class MyWindow Extends Window 82 | 83 | Method New() 84 | mybattleui = New battleui(Width,Height) 85 | End Method 86 | 87 | Method OnRender( canvas:Canvas ) Override 88 | App.RequestRender() ' Activate this method 89 | mybattleui.update() 90 | mybattleui.draw(canvas) 91 | ' if key escape then quit 92 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 93 | End Method 94 | 95 | End Class 96 | 97 | Function Main() 98 | New AppInstance 99 | New MyWindow 100 | App.Run() 101 | End Function 102 | -------------------------------------------------------------------------------- /Example - Save png screenshot of window.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' Press space to make screenshot..... 16 | If Keyboard.KeyReleased(Key.Space) 17 | Local filename:String=DesktopDir()+"screenshot.png" 18 | Local screenshot:Pixmap = canvas.CopyPixmap(New Recti(0, 0, Width, Height)) 19 | SavePixmap(screenshot, filename) 20 | Print "Screenshot saved as : "+filename 21 | End If 22 | ' if key escape then quit 23 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 24 | End Method 25 | 26 | End Class 27 | 28 | Function Main() 29 | New AppInstance 30 | New MyWindow 31 | App.Run() 32 | End Function 33 | -------------------------------------------------------------------------------- /Example - Smooth values or terrain.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field numdots:Int=150 10 | Field dots:Int[] 11 | Method New() 12 | dots = New Int[numdots] 13 | fillrandom(0,100) 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | canvas.Clear(Color.Black) 19 | drawdots(canvas) 20 | If Keyboard.KeyReleased(Key.Space) Then smootdots() 21 | If Keyboard.KeyReleased(Key.Enter) Then fillrandom(0,100) 22 | canvas.Color = Color.White 23 | canvas.DrawText("Press Space to smooth press Enter to randomize",100,320) 24 | ' if key escape then quit 25 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 26 | End Method 27 | 28 | Method fillrandom(low:Int,high:Int) 29 | For Local i:Int=0 Until dots.Length 30 | dots[i] = Rnd(low,high) 31 | Next 32 | End Method 33 | 34 | Method smootdots() 35 | For Local i:Int=1 Until dots.Length 36 | Local a:Int=dots[i-1] 37 | Local b:Int=dots[i] 38 | If a" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class star 8 | Field s:Vec2f 9 | Field m:Vec2f 10 | Method New() 11 | s.X = Rnd(0,640) 12 | s.Y = Rnd(0,480) 13 | m.X = Rnd(-1,-0.1) 14 | End Method 15 | Method update() 16 | s+=m 17 | If s.X < 0 Then s.X = 640 18 | End Method 19 | Method draw(canvas:Canvas) 20 | canvas.Color = Color.White 21 | canvas.DrawPoint(s.X,s.Y) 22 | End Method 23 | End Class 24 | 25 | Global mylist:List = New List 26 | 27 | Class MyWindow Extends Window 28 | 29 | Method New() 30 | For Local i:=0 To 100 31 | mylist.AddLast(New star()) 32 | Next 33 | End Method 34 | 35 | Method OnRender( canvas:Canvas ) Override 36 | App.RequestRender() ' Activate this method 37 | ' if key escape then quit 38 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 39 | ' 40 | canvas.Clear(Color.Black) 41 | For Local i:=Eachin mylist 42 | i.update() 43 | i.draw(canvas) 44 | Next 45 | End Method 46 | 47 | End Class 48 | 49 | Function Main() 50 | New AppInstance 51 | New MyWindow() 52 | App.Run() 53 | End Function 54 | -------------------------------------------------------------------------------- /Example - Testing BSP.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | ' Testing/learning bsp 5 | 6 | Using std.. 7 | Using mojo.. 8 | 9 | Class MyWindow Extends Window 10 | 11 | Class zone 12 | Field id:Int 13 | Field parent:Int 14 | Field child:Int 15 | Field layer:Int 16 | Field x:Int,y:Int,w:Int,h:Int 17 | End Class 18 | Field z:zone[] 19 | 20 | Method New() 21 | go() 22 | End method 23 | 24 | Method go() 25 | Local d:Int=Rnd(4,20) 26 | z = New zone[300] 27 | z[1] = New zone() 28 | z[1].x = 0 29 | z[1].y = 0 30 | z[1].w = Width 31 | z[1].h = Height 32 | z[1].parent = -1 33 | z[1].layer = 1 34 | 'SeedRnd(6) 35 | 36 | For Local l:Int=1 Until d 37 | For Local i:Int=0 Until z.Length 38 | If z[i] = Null Then Continue 39 | If z[i].layer = l 40 | newchildren(i,Rnd(2),l) 41 | Endif 42 | Next 43 | 44 | Next 45 | 46 | End Method 47 | 48 | Method newchildren(parent:Int,hor:Int=True,layer:Int) 49 | Local a:Int,b:Int 50 | For Local i:Int=1 Until z.Length 51 | If z[i] = Null Then 52 | a = i 53 | For Local ii:Int=i+1 Until z.Length 54 | If z[ii] = Null Then 55 | b = ii 56 | i = z.Length 57 | Exit 58 | End If 59 | Next 60 | End If 61 | Next 62 | 63 | z[a] = New zone() 64 | z[b] = New zone() 65 | If hor = 0 66 | Local l:Int=Rnd(5,z[parent].w) 67 | z[a].x = z[parent].x 68 | z[a].y = z[parent].y 69 | z[a].w = l 70 | z[a].h = z[parent].h 71 | z[a].parent = parent 72 | z[a].layer = layer+1 73 | z[b].x = z[parent].x+l 74 | z[b].y = z[parent].y 75 | z[b].w = z[parent].w-l 76 | z[b].h = z[parent].h 77 | z[b].parent = parent 78 | z[b].layer = layer+1 79 | 80 | Else 81 | Local h:Int=Rnd(5,z[parent].h) 82 | z[a].x = z[parent].x 83 | z[a].y = z[parent].y 84 | z[a].w = z[parent].w 85 | z[a].h = h 86 | z[a].parent = parent 87 | z[a].layer = layer+1 88 | z[b].x = z[parent].x 89 | z[b].y = z[parent].y+h 90 | z[b].w = z[parent].w 91 | z[b].h = z[parent].h-h 92 | z[b].parent = parent 93 | z[b].layer = layer+1 94 | End If 95 | End Method 96 | 97 | Method OnRender( canvas:Canvas ) Override 98 | App.RequestRender() ' Activate this method 99 | ' if key escape then quit 100 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 101 | 102 | For Local i:Int=1 Until z.Length 103 | If z[i] = Null Then Continue 104 | canvas.Color = New Color(Rnd(),0,0,.5) 105 | canvas.DrawRect(z[i].x,z[i].y,z[i].w,z[i].h) 106 | 107 | Next 108 | 109 | canvas.Color = Color.Blue 110 | canvas.DrawRect(0,0,Width,15) 111 | canvas.Color = Color.Red 112 | canvas.DrawText("Press space to generate new map",0,0) 113 | If Keyboard.KeyReleased(Key.Space) Then go() 114 | 115 | End Method 116 | 117 | End Class 118 | 119 | Function Main() 120 | New AppInstance 121 | New MyWindow 122 | App.Run() 123 | End Function 124 | -------------------------------------------------------------------------------- /Example - Tilemap Data.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class MyWindow Extends Window 8 | 9 | Field m:= New String[3] 10 | 11 | Method New() 12 | m[0] = "000" 13 | m[1] = "010" 14 | m[2] = "000" 15 | End Method 16 | 17 | Method OnRender( canvas:Canvas ) Override 18 | App.RequestRender() ' Activate this method 19 | ' 20 | canvas.Clear(Color.White) 21 | For Local y:=0 To 2 22 | For Local x:=0 To 2 23 | canvas.Color = Color.Black 24 | canvas.DrawRect(x*32+100,y*32+100,32,32) 25 | If m[y][x] = 48 Then 26 | canvas.Color = Color.Green 27 | canvas.DrawText("X",x*32+100,y*32+100) 28 | End If 29 | If m[y][x] = 49 Then 30 | canvas.Color = Color.Blue 31 | canvas.DrawText("X",x*32+100,y*32+100) 32 | End If 33 | Next 34 | Next 35 | ' if key escape then quit 36 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 37 | End Method 38 | 39 | End Class 40 | 41 | Function Main() 42 | New AppInstance 43 | New MyWindow 44 | App.Run() 45 | End Function 46 | -------------------------------------------------------------------------------- /Example - Timers.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | Class MyWindow Extends Window 10 | 11 | Field timer:Timer 12 | Field timer2:Timer 13 | Field cnt:=0 14 | field cnt2:=0 15 | 16 | Method New() 17 | ' start running a timer that calls 18 | ' a function 19 | timer = New Timer( 60,OnUpdate ) '60 every 1000 millisecs() 20 | timer2 = New Timer(1,subroutine) 21 | End Method 22 | 23 | Method subroutine() 24 | cnt2+=1 25 | End Method 26 | 27 | Method OnUpdate() 28 | cnt+=1 29 | End Method 30 | 31 | Method OnRender( canvas:Canvas ) Override 32 | App.RequestRender() ' Loop with sync 33 | canvas.Clear(Color.Black) 34 | canvas.Color = Color.White 35 | canvas.DrawText("Timer 60 ticks every second",0,0) 36 | canvas.DrawText(cnt,320,0) 37 | canvas.DrawText("Timer 1 tick every second",0,15) 38 | canvas.DrawText(cnt2,320,15) 39 | End Method 40 | 41 | Method OnKeyEvent( event:KeyEvent ) Override 42 | Select event.Type 43 | Case EventType.KeyDown 44 | Select event.Key 45 | Case Key.Escape 46 | instance.Terminate() 'exit with escape 47 | End Select 48 | End Select 49 | End Method 50 | 51 | End Class 52 | 53 | Function Main() 54 | instance = New AppInstance 55 | 56 | New MyWindow 57 | 58 | App.Run() 59 | 60 | End Function 61 | -------------------------------------------------------------------------------- /Example - Touch Released X and Y.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' 16 | If Touch.FingerReleased(0) Then 17 | Print "Finger x and y : "+ Touch.FingerX(0)+","+Touch.FingerY(0) 18 | Print "Finger x and y : "+ Touch.FingerX(0)/640+","+Touch.FingerY(0)/480 19 | End If 20 | ' if key escape then quit 21 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 22 | End Method 23 | 24 | End Class 25 | 26 | Function Main() 27 | New AppInstance 28 | New MyWindow 29 | App.Run() 30 | End Function 31 | -------------------------------------------------------------------------------- /Example - Vector - Blob fixed speed to mouse.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field blob:= New Vec2f(50,50) 10 | Field velocity:=New Vec2f(0,-2) 11 | Field acc:=New Vec2f() 12 | Method New() 13 | 14 | End Method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | 19 | ' Create a vector that holds the desired velocity 20 | Local desired:= New Vec2f() 21 | ' Get the angle between blob and mouse and multiply it by 0.1 22 | desired = (blob-Mouse.Location)*0.1 23 | desired.Normalize() 24 | ' Substract the desired from blob to move blob 25 | ' towards the mouse 26 | 27 | 28 | Local steer:= New Vec2f() 29 | steer= desired-velocity 30 | 31 | ' set the movement speed to 1 32 | steer = vectorlimit(steer,1) 33 | 34 | blob-=steer 35 | 'draw the blob 36 | canvas.DrawCircle(blob.x,blob.y,10) 37 | 38 | ' if key escape then quit 39 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 40 | End Method 41 | 42 | End Class 43 | 44 | Function vectorlimit:Vec2f(myvec:Vec2f,max:float) 45 | Local out:= New Vec2f() 46 | out = myvec 47 | Local lengthSquared:Float = out.x * out.x + out.y * out.y 48 | 49 | if( ( lengthSquared > max * max ) and ( lengthSquared > 0 ) ) 50 | Local ratio:Float = max / Sqrt( lengthSquared ) 51 | out.x *= ratio 52 | out.y *= ratio 53 | End If 54 | Return out 55 | End Function 56 | 57 | Function Main() 58 | New AppInstance 59 | New MyWindow 60 | App.Run() 61 | End Function 62 | -------------------------------------------------------------------------------- /Example - Vector - Blob moves to mouse.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field blob:= New Vec2f(50,50) 10 | Method New() 11 | 12 | End Method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Activate this method 16 | 17 | ' Create a vector that holds the desired velocity 18 | Local desired:= New Vec2f() 19 | ' Get the angle between blob and mouse and multiply it by 0.1 20 | desired = (blob-Mouse.Location)*0.1 21 | 22 | ' Substract the desired from blob to move blob 23 | ' towards the mouse 24 | blob-=desired 25 | 26 | 'draw the blob 27 | canvas.DrawCircle(blob.x,blob.y,10) 28 | 29 | ' if key escape then quit 30 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 31 | End Method 32 | 33 | End Class 34 | 35 | Function Main() 36 | New AppInstance 37 | New MyWindow 38 | App.Run() 39 | End Function 40 | -------------------------------------------------------------------------------- /Example - Vector Distance.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | 12 | 13 | End method 14 | 15 | Method OnRender( canvas:Canvas ) Override 16 | App.RequestRender() ' Activate this method 17 | 18 | ' These lines below get the distance between 2 vectors! 19 | Local dist:Int=New Vec2i(10,10).Distance(New Vec2i(Mouse.X,Mouse.Y)) 20 | Local distf:Float=New Vec2f(10,10).Distance(New Vec2f(Mouse.X,Mouse.Y)) 21 | 22 | canvas.DrawText(dist,0,0) 23 | canvas.DrawText(distf,0,20) 24 | 25 | ' if key escape then quit 26 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 27 | End Method 28 | 29 | End Class 30 | 31 | Function Main() 32 | New AppInstance 33 | New MyWindow 34 | App.Run() 35 | End Function 36 | -------------------------------------------------------------------------------- /Example - Vectory acc to mouse.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Field point1:Vec2f 11 | Field point2:Vec2f 12 | Field dir:Vec2f,acc:Vec2f,velocity:Vec2f 13 | Method New() 14 | point1 = New Vec2f(100,100) 15 | point2 = New Vec2f(320,240) 16 | dir = New Vec2f() 17 | acc = New Vec2f() 18 | velocity = New Vec2f() 19 | End method 20 | 21 | Method OnRender( canvas:Canvas ) Override 22 | App.RequestRender() ' Activate this method 23 | 24 | ' Get the mouse position in poitn 2 25 | point2.x = Mouse.X 26 | point2.Y = Mouse.Y 27 | 28 | ' get point2 into dir 29 | dir = point2 30 | 'substract position of the point1 from the dir 31 | dir-= point1 32 | ' normalize the dir 33 | dir.Normalize() 34 | ' multi ply the dir 35 | dir*=.05 36 | ' copy the dir into the acceleration vector 37 | acc=dir 38 | 39 | 40 | ' Set our velocity 41 | velocity+=acc 42 | ' Limit our velocity 43 | velocity.x = Clamp(velocity.x,-4.0,4.0) 44 | velocity.y = Clamp(velocity.y,-4.0,4.0) 45 | ' Move our point1 46 | point1+=velocity 47 | 48 | 49 | canvas.DrawText("yeh",point1.x,point1.y) 50 | canvas.DrawText("Here",point2.x,point2.y) 51 | 52 | ' if key escape then quit 53 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 54 | End Method 55 | 56 | End Class 57 | 58 | Function Main() 59 | New AppInstance 60 | New MyWindow 61 | App.Run() 62 | End Function 63 | -------------------------------------------------------------------------------- /Example - Zooming in canvas center.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field zoom:Float=1 10 | Method New() 11 | End Method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' if key escape then quit 16 | canvas.Translate(Width/2-(Width/2*zoom),Height/2-(Height/2*zoom)) 17 | canvas.Scale(New Vec2f(zoom)) 18 | canvas.DrawText("Top Left....",0,0) 19 | canvas.DrawText("Center....",Width/2,Height/2) 20 | If Keyboard.KeyReleased(Key.Left) Then zoom-=.1 21 | If Keyboard.KeyReleased(Key.Right) Then zoom+=.1 22 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 23 | End Method 24 | 25 | End Class 26 | 27 | Function Main() 28 | New AppInstance 29 | New MyWindow 30 | App.Run() 31 | End Function 32 | -------------------------------------------------------------------------------- /Example - brownian map test 2.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Field range:Int=6 11 | Field len:Int=300 12 | 13 | Class node 14 | Field x:Int 15 | Field y:Int 16 | Method New(x:Int,y:Int) 17 | Self.x = x 18 | Self.y = y 19 | End Method 20 | End Class 21 | 22 | Field brownian:Stack 23 | 24 | Method New() 25 | make() 26 | End method 27 | 28 | Method make() 29 | brownian = New Stack 30 | brownian.Push(New node(100,100)) 31 | For Local i:Int=0 Until len 32 | Local nx:Int=brownian.Get(i).x 33 | Local ny:Int=brownian.Get(i).y 34 | SeedRnd(Microsecs()) 35 | nx += Rnd(-range,range) 36 | ny += Rnd(-range,range) 37 | nx = Clamp(nx,0,200) 38 | ny = Clamp(ny,0,200) 39 | brownian.Push(New node(nx,ny)) 40 | Next 41 | 'make the coordinates gridlike 42 | For Local i:Int=0 Until brownian.Length 43 | Local x:Int=brownian.Get(i).x 44 | Local y:Int=brownian.Get(i).y 45 | brownian.Get(i).x = (x/10)*10 46 | brownian.Get(i).y = (y/10)*10 47 | Next 48 | End Method 49 | 50 | Method OnRender( canvas:Canvas ) Override 51 | App.RequestRender() ' Activate this method 52 | canvas.Clear(Color.Black) 53 | canvas.Color = Color.White 54 | For Local i:Int=0 Until brownian.Length-1 55 | Local x1:Int=brownian.Get(i).x 56 | Local y1:Int=brownian.Get(i).y 57 | Local x2:Int=brownian.Get(i+1).x 58 | Local y2:Int=brownian.Get(i+1).y 59 | x1*=2 60 | y1*=2 61 | x2*=2 62 | y2*=2 63 | SeedRnd(100) 64 | Local l:Float=4 65 | For Local y:Int=Rnd(-l,-1) To Rnd(1,l) 66 | For Local x:Int=Rnd(-l,-1) To Rnd(1,l) 67 | canvas.DrawLine(x1+x+100,y1+y+100,x2+x+100,y2+y+100) 68 | l+=Rnd(1) 69 | If l>10 Then l=4 70 | Next 71 | Next 72 | Next 73 | ' If key pace then new map 74 | If Keyboard.KeyReleased(Key.Space) Then make() 75 | ' if key escape then quit 76 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 77 | End Method 78 | 79 | End Class 80 | 81 | Function Main() 82 | New AppInstance 83 | New MyWindow 84 | App.Run() 85 | End Function 86 | -------------------------------------------------------------------------------- /Example - brownian map test 3.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Field range:Int=6 11 | Field len:Int=500 12 | 13 | Class node 14 | Field x:Int 15 | Field y:Int 16 | Method New(x:Int,y:Int) 17 | Self.x = x 18 | Self.y = y 19 | End Method 20 | End Class 21 | 22 | Field brownian:Stack 23 | 24 | Field my_x:Int[,] = New Int[1000,2] 25 | 26 | Method New() 27 | make() 28 | End method 29 | 30 | Method make() 31 | brownian = New Stack 32 | brownian.Push(New node(100,100)) 33 | For Local i:Int=0 Until len 34 | Local nx:Int=brownian.Get(i).x 35 | Local ny:Int=brownian.Get(i).y 36 | SeedRnd(Microsecs()) 37 | nx += Rnd(-range,range) 38 | ny += Rnd(-range,range) 39 | nx = Clamp(nx,0,200) 40 | ny = Clamp(ny,0,200) 41 | brownian.Push(New node(nx,ny)) 42 | Next 43 | 44 | my_x = New Int[1000,2] 45 | 'get the leftest x on y 46 | For Local i:Int=0 Until brownian.Length 47 | Local x:Int=brownian.Get(i).x 48 | Local y:Int=brownian.Get(i).y 49 | 'Print x+","+y 50 | If my_x[y,0] = 0 Then my_x[y,0] = x ; Continue 51 | If xmy_x[y,1] Then my_x[y,1] = x 59 | Next 60 | 'if left and right is same then make wider 61 | For Local i:Int=0 Until 1000 62 | If my_x[i,0] >0 63 | If my_x[i,0] = my_x[i,1] Then my_x[i,1]+=Rnd(20) 64 | End If 65 | Next 66 | 67 | ' handle between y values 68 | 'find topmost y 69 | Local t:Int=0 70 | For t = 0 Until 1000 71 | If my_x[t,0] > 0 Then Exit 72 | Next 73 | For Local i:Int=t Until 999 74 | Local a:Int=my_x[i,0] 75 | Local b:Int=my_x[i,1] 76 | Local ly:Int=i 77 | i+=1 78 | Local con:Bool=False 79 | For Local ii:Int=i Until 1000 80 | If my_x[ii,0] > 0 Then 81 | con=True 82 | End If 83 | Next 84 | If con=False Then Exit 85 | While my_x[i,0] = 0 86 | If i>=999 Then Exit 87 | my_x[i,0] = a 88 | my_x[i,1] = b 89 | i+=1 90 | Wend 91 | Next 92 | 93 | End Method 94 | 95 | Method OnRender( canvas:Canvas ) Override 96 | App.RequestRender() ' Activate this method 97 | canvas.Clear(Color.Black) 98 | canvas.Color = Color.White 99 | For Local i:Int=0 Until 240 100 | If my_x[i,0] = 0 Then Continue 101 | If my_x[i,1] = 0 Then Continue 102 | 103 | canvas.DrawRect(my_x[i,0]*2+50,i*2,my_x[i,1]-my_x[i,0]*2,20) 104 | Next 105 | ' ' If key pace then new map 106 | If Keyboard.KeyReleased(Key.Space) Then make() 107 | ' if key escape then quit 108 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 109 | End Method 110 | 111 | End Class 112 | 113 | Function Main() 114 | New AppInstance 115 | New MyWindow 116 | App.Run() 117 | End Function 118 | -------------------------------------------------------------------------------- /Example - canvas Width and Height.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global instance:AppInstance 8 | 9 | Class MyWindow Extends Window 10 | 11 | Method New() 12 | End method 13 | 14 | Method OnRender( canvas:Canvas ) Override 15 | App.RequestRender() ' Loop with sync 16 | canvas.Clear(Color.Black) 17 | canvas.Color = Color.White 18 | canvas.DrawText("Width of the canvas - ",0,0) 19 | canvas.DrawText(canvas.Viewport.Width,320,0) 20 | canvas.DrawText("Height of the canvas - ",0,15) 21 | canvas.DrawText(canvas.Viewport.Height,320,15) 22 | End Method 23 | 24 | Method OnKeyEvent( event:KeyEvent ) Override 25 | Select event.Type 26 | Case EventType.KeyDown 27 | Select event.Key 28 | Case Key.Escape 29 | instance.Terminate() 'exit with escape 30 | End Select 31 | End Select 32 | End Method 33 | 34 | End Class 35 | 36 | Function Main() 37 | instance = New AppInstance 38 | 39 | New MyWindow 40 | 41 | App.Run() 42 | 43 | End Function 44 | -------------------------------------------------------------------------------- /Example - texteffect stack class erase.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class texteffect 8 | Field x:Int,y:Int 9 | Field l:Int 10 | Field text:String 11 | Field type:String="begin" 12 | Field cnt:Int 13 | Field time:Float 14 | Field delay:Float=60 15 | Field timeend:Int=60 16 | Field deleteme:Bool=False 17 | Method New(text:String,x:Int,y:Int) 18 | Self.x = x/2 19 | Self.y = y/2 20 | Self.text = text 21 | l = text.Length 22 | End Method 23 | Method update() 24 | time+=1 25 | If time>delay Then 26 | time=0 27 | If type = "begin" 28 | cnt+=1 29 | while text[cnt-1] = 32 30 | If cnt>=l Then exit 31 | cnt+=1 32 | Wend 33 | If cnt>=l Then type = "finished" 34 | End If 35 | End If 36 | If type = "finished" 37 | timeend-=1 38 | If timeend=0 Then deleteme=true 39 | End If 40 | End Method 41 | 42 | Method draw(canvas:Canvas) 43 | canvas.PushMatrix() 44 | canvas.Scale(2,2) 45 | canvas.Color = New Color(0.7,0.7,0.7) 46 | Select type 47 | Case "begin" 48 | For Local x2:=0 Until cnt 49 | 50 | If x2=cnt-1 Then 51 | Local s:Float=(0.3/delay)*time 52 | canvas.Color = New Color(1-s,1-s,1-s) 53 | End If 54 | 55 | canvas.DrawText(text.Mid(x2,1),x+(x2*14),y) 56 | 57 | Next 58 | Case "finished" 59 | For Local x2:Int=0 Until l 60 | canvas.DrawText(text.Mid(x2,1),x+(x2*14),y) 61 | Next 62 | End Select 63 | canvas.PopMatrix() 64 | End Method 65 | end Class 66 | 67 | Global mytexteffect:Stack = New Stack 68 | 69 | 70 | 71 | Class MyWindow Extends Window 72 | Field t:String[] = New String[]( "This is a test.", 73 | "Monkey 2 is great!", 74 | "Programming", 75 | "Coding Coding..") 76 | Method New() 77 | Title = "Tinted Gradient background Example.." 78 | 79 | mytexteffect.Push(New texteffect(t[Rnd(0,t.Length)],Rnd(50,Width-100),Rnd(20,Height-40))) 80 | End Method 81 | 82 | Method OnRender( canvas:Canvas ) Override 83 | App.RequestRender() ' Activate this method 84 | ' 85 | canvas.Clear(Color.Black) 86 | ' 87 | If Millisecs() Mod 15 = 1 Then' 88 | If Rnd()<.5 89 | mytexteffect.Push(New texteffect(t[Rnd(0,t.Length)],Rnd(-50,Width+50),Rnd(0,Height))) 90 | End If 91 | End If 92 | ' 93 | For Local i:=Eachin mytexteffect 94 | i.update() 95 | i.draw(canvas) 96 | Next 97 | ' remove from stack 98 | Local it:= mytexteffect.All() 99 | While Not it.AtEnd 100 | Local item:=it.Current 101 | If item.deleteme it.Erase() Else it.Bump() 102 | Wend 103 | ' if key escape then quit 104 | canvas.Color = Color.White 105 | canvas.DrawText("Press escape to end..",0,0) 106 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 107 | End Method 108 | 109 | End Class 110 | 111 | Function Main() 112 | New AppInstance 113 | New MyWindow 114 | App.Run() 115 | End Function 116 | -------------------------------------------------------------------------------- /Example - turret turn left or right.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | Field angle:Float,targetangle:Float 10 | Field turretxy:Vec2i 11 | Field turretw:Int=20,turreth:Int=20 12 | Method New() 13 | turretxy.x = 320 14 | turretxy.y = 240 15 | End method 16 | 17 | Method OnRender( canvas:Canvas ) Override 18 | App.RequestRender() ' Activate this method 19 | 20 | ' Get the target angle (from >> To) 21 | targetangle = getangle(turretxy.x,turretxy.y,Mouse.X,Mouse.Y) 22 | 23 | ' 24 | ' Find the shortest turn towards target angle (stackoverflow) 25 | 26 | ' Make the angle and target angle suitable for comapisment 27 | If (targetangle >= (angle + Pi)) 28 | angle += TwoPi 29 | ElseIf (targetangle < (angle - Pi)) 30 | targetangle += TwoPi 31 | End If 32 | ' .05 is the turn speed 33 | Local directiondiff:Float = targetangle - angle 34 | 35 | If (directiondiff < -.05) 36 | directiondiff = -.05 37 | End If 38 | If (directiondiff > .05) 39 | directiondiff = .05 40 | End If 41 | angle+=directiondiff 42 | 43 | ' draw the turret 44 | canvas.Color = Color.Blue 45 | canvas.DrawOval(turretxy.x,turretxy.y,turretw,turreth) 46 | canvas.Color = Color.White 47 | ' turret center x and y 48 | Local tcx:Int=turretxy.x+turretw/2 49 | Local tcy:Int=turretxy.y+turreth/2 50 | ' target angle 51 | Local dx:Float,dy:Float 52 | dx = tcx+Cos(targetangle)*20 53 | dy = tcy+Sin(targetangle)*20 54 | 55 | ' get the turret barrel angle 56 | Local tx:Float,ty:Float 57 | tx = tcx+Cos(angle)*20 58 | ty = tcy+Sin(angle)*20 59 | canvas.LineWidth = 5 60 | canvas.DrawLine(tcx,tcy,tx,ty) 61 | canvas.LineWidth = 2 62 | canvas.DrawLine(tcx,tcy,dx,dy) 63 | 64 | ' if key escape then quit 65 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 66 | End Method 67 | Function getangle:Float(x1:Int,y1:Int,x2:Int,y2:Int) 68 | Return ATan2(y2-y1, x2-x1) 69 | End Function 70 | 71 | End Class 72 | 73 | Function Main() 74 | New AppInstance 75 | New MyWindow 76 | App.Run() 77 | End Function 78 | -------------------------------------------------------------------------------- /Generator - Spaceships or Items Generator.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class item 8 | Field can:Canvas 9 | Field image:Image 10 | Field rot:Double 11 | Field rotspd:Double 12 | Method New() 13 | rot=Rnd(TwoPi) 14 | rotspd=Rnd(-0.1,0.1) 15 | image = New Image(48,48) 16 | can = New Canvas(image) 17 | image.Handle=New Vec2f( .5,.5 ) 18 | can.Color = Color.None 19 | can.BlendMode=BlendMode.Opaque 20 | can.DrawRect(0,0,48,48) 21 | makeimage() 22 | End Method 23 | Method makeimage() 24 | Local w:Int=Rnd(4,20) 25 | Local h:Int=Rnd(4,20) 26 | For Local i:=0 Until 10 27 | Local a:Float=Rnd() 28 | can.Color = New Color(a,a,a) 29 | If Rnd() <0.1 Then 30 | Local a:int=Rnd()*4 31 | Select a 32 | Case 0 33 | can.Color = New Color(Rnd(.2,1),0,0) 34 | Case 1 35 | can.Color = New Color(0,Rnd(.2,1),0) 36 | Case 2 37 | can.Color = New Color(0,0,Rnd(.2,1)) 38 | Case 3 39 | can.Color = New Color(Rnd(.2,1),Rnd(.2,1),0) 40 | End Select 41 | End If 42 | can.DrawTriangle( New Vec2f(24+Rnd(-w,w),24+Rnd(-h,h)), 43 | New Vec2f(24+Rnd(-w,w),24+Rnd(-h,h)), 44 | New Vec2f(24+Rnd(-w,w),24+Rnd(-h,h))) 45 | Next 46 | can.Flush() 47 | End Method 48 | End Class 49 | 50 | Global myitem:List = New List 51 | 52 | Class MyWindow Extends Window 53 | 54 | Method New() 55 | Super.New("Graphics Generator - Spaceships or Items",800,600) 56 | For Local y:=0 To 10 57 | For Local x:=0 To 10 58 | myitem.AddLast(New item()) 59 | Next 60 | Next 61 | ClearColor = Color.Black 62 | End Method 63 | 64 | Method OnRender( canvas:Canvas ) Override 65 | App.RequestRender() ' Activate this method 66 | ' if key escape then quit 67 | If Keyboard.KeyReleased(Key.Space) 68 | myitem.Clear() 69 | For Local y:=0 To 10 70 | For Local x:=0 To 10 71 | myitem.AddLast(New item()) 72 | Next 73 | Next 74 | end if 75 | Local x:Int=0 76 | Local y:Int=0 77 | For Local i:=Eachin myitem 78 | canvas.DrawImage(i.image,x*48+96,y*48+32,i.rot) 79 | i.rot+=i.rotspd 80 | If i.rot<0 Then i.rot=TwoPi 81 | If i.rot>TwoPi Then i.rot=0 82 | x+=1 83 | If x>10 Then y+=1;x=0 84 | Next 85 | canvas.Color = Color.White 86 | canvas.DrawText("Press Space to generate new items - press escape to end",0,560) 87 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 88 | End Method 89 | 90 | End Class 91 | 92 | Function Main() 93 | New AppInstance 94 | New MyWindow 95 | App.Run() 96 | End Function 97 | -------------------------------------------------------------------------------- /Mojox - Example - Button.monkey2: -------------------------------------------------------------------------------- 1 | ' I took this from the monkey 2 forum. Keeping it here so I can access it quickly. 2 | ' btw - I found a number of mojox examples in the test folder in the mojox folder 3 | 'in the monkey installation 4 | 5 | #import "" 6 | #import "" 7 | #import "" 8 | 9 | Using std.. 10 | Using mojo.. 11 | Using mojox.. 12 | 13 | Class MyWindow Extends Window 14 | 15 | Method New() 16 | Super.New( "Button Demo",640,480,WindowFlags.Resizable ) 17 | 18 | Local button:=New PushButton( "Click ME!" ) 19 | button.Layout="float" 'normal size 20 | button.Gravity=New Vec2f( 0,0 ) 'top-left 21 | button.MaxSize=New Vec2i( 100,0 ) 22 | 23 | button.Clicked=Lambda() 24 | Alert( "Well done sir!" ) 25 | End 26 | 27 | ContentView=button 28 | End 29 | 30 | End 31 | 32 | Function Main() 33 | 34 | New AppInstance 35 | 36 | New MyWindow 37 | 38 | App.Run() 39 | End 40 | -------------------------------------------------------------------------------- /Template Minimum and keys.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | Class MyWindow Extends Window 9 | 10 | Method New() 11 | End method 12 | 13 | Method OnRender( canvas:Canvas ) Override 14 | App.RequestRender() ' Activate this method 15 | ' if key escape then quit 16 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 17 | End Method 18 | 19 | End Class 20 | 21 | Function Main() 22 | New AppInstance 23 | New MyWindow 24 | App.Run() 25 | End Function 26 | -------------------------------------------------------------------------------- /generators/BrushAdditiveClouds.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | 9 | Class MyWindow Extends Window 10 | Field im:Image 11 | Field can:Canvas 12 | Field map:Float[,] 13 | 14 | Method New() 15 | SeedRnd(Microsecs()) 16 | makenew() 17 | 18 | End method 19 | Method makenew() 20 | im = New Image(640,480) 21 | can = New Canvas(im) 22 | map = New Float[im.Width,im.Height] 23 | 24 | ' Here we move a paintbrush(additive) in a array and then make 25 | ' a image out of it. 26 | Local amount:Int=Rnd(500,1000) 27 | For Local i:Int=0 To amount 28 | movebrush(can,Rnd(0,640),Rnd(0,480)) 29 | Next 30 | 31 | ' turn the array into a image. 32 | For Local y:Int=0 Until im.Height 33 | For Local x:Int=0 Until im.Width 34 | Local c:Color = New Color(map[x,y],map[x,y],map[x,y]) 35 | can.Color = c 36 | can.DrawRect(x,y,1,1) 37 | Next 38 | Next 39 | can.Flush() 40 | End Method 41 | Method OnRender( canvas:Canvas ) Override 42 | App.RequestRender() ' Activate this method 43 | canvas.DrawImage(im,0,0) 44 | ' create new image 45 | If Keyboard.KeyReleased(Key.Space) Then makenew() 46 | ' if key escape then quit 47 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 48 | End Method 49 | Method movebrush(canvas:Canvas,dx:Int,dy:Int) 50 | Local a:Float = Rnd(TwoPi) ' angle of brush 51 | Local am:Float= Rnd(-.2,.2) ' angle of brush modifier 52 | Local x:Float = dx 53 | Local y:Float = dy 54 | Local len:Int=Rnd(50,200) ' length of line to draw 55 | Local rad:Int=Rnd(2,10) ' radius of brush 56 | Local radmod:Int=Rnd(-2,2) ' grow or shrink brush 57 | For Local i:Int=0 Until len 58 | x += Cos(a) 59 | y += Sin(a) 60 | a+=am 61 | If Rnd()<.1 Then am = Rnd(-.2,.2) ' change brush angle 62 | If Rnd()<.02 Then ' every now and then change size of brush 63 | rad+=radmod 64 | If rad<2 Then radmod=-radmod 65 | If rad>20 Then radmod=-radmod 66 | End If 67 | ' draw the brush 68 | For Local y2:Int=-rad To rad 69 | For Local x2:Int=-rad To rad 70 | Local x3:Int=x+x2 71 | Local y3:Int=y+y2 72 | Local t:Float = 1.0-(1.0 / Float(rad*2) * Float(distance(x3,y3,x,y))) 73 | If x3<0 Or y3<0 Or x3>=im.Width Or y3>=im.Height Then Continue 74 | 'Print t 75 | map[x3,y3] += Abs(t)/30 76 | If map[x3,y3] > 1.0 Then map[x3,y3] = 1.0 77 | 'canvas.Color = Color.White.Blend(Color.Black,t) 78 | 'canvas.DrawRect(x3,y3,1,1) 79 | Next 80 | Next 81 | Next 82 | 83 | End Method 84 | ' Manhattan Distance (less precise) 85 | Function distance:Float(x1:Float,y1:Float,x2:Float,y2:Float) 86 | Return Abs(x2-x1)+Abs(y2-y1) 87 | End Function 88 | End Class 89 | 90 | Function Main() 91 | New AppInstance 92 | New MyWindow 93 | App.Run() 94 | End Function 95 | -------------------------------------------------------------------------------- /generators/Brushadditiveclouds2.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | 8 | 9 | Class MyWindow Extends Window 10 | Field im:Image 11 | Field can:Canvas 12 | Field map:Float[,] 13 | 14 | Method New() 15 | SeedRnd(Microsecs()) 16 | makenew() 17 | 18 | End method 19 | Method makenew() 20 | Local met:Int=Rnd(2) 21 | im = New Image(640,480) 22 | can = New Canvas(im) 23 | map = New Float[im.Width,im.Height] 24 | 25 | ' Here we move a paintbrush(additive) in a array and then make 26 | ' a image out of it. 27 | Local amount:Int=Rnd(100,700) 28 | For Local i:Int=0 To amount 29 | movebrush(can,Rnd(0,640),Rnd(0,480)) 30 | Next 31 | 32 | ' turn the array into a image. 33 | For Local y:Int=0 Until im.Height 34 | For Local x:Int=0 Until im.Width 35 | Local c:Color = New Color(map[x,y],map[x,y],map[x,y]) 36 | can.Color = c 37 | If met = 1 38 | If map[x,y] < 0.4 Then can.Color = Color.Blue Else can.Color=Color.Green 39 | End If 40 | can.DrawRect(x,y,1,1) 41 | Next 42 | Next 43 | can.Flush() 44 | End Method 45 | Method OnRender( canvas:Canvas ) Override 46 | App.RequestRender() ' Activate this method 47 | canvas.DrawImage(im,0,0) 48 | ' create new image 49 | If Keyboard.KeyReleased(Key.Space) Then makenew() 50 | ' if key escape then quit 51 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 52 | End Method 53 | Method movebrush(canvas:Canvas,dx:Int,dy:Int) 54 | Local a:Float = Rnd(TwoPi) ' angle of brush 55 | Local am:Float= Rnd(-.2,.2) ' angle of brush modifier 56 | Local x:Float = dx 57 | Local y:Float = dy 58 | Local len:Int=Rnd(150,360) ' length of line to draw 59 | Local rad:Int=Rnd(2,10) ' radius of brush 60 | Local radmod:Int=Rnd(-2,2) ' grow or shrink brush 61 | Local inversion:Bool=True 62 | If Rnd()<.5 Then inversion=False 63 | For Local i:Int=0 Until len 64 | x += Cos(a) 65 | y += Sin(a) 66 | a+=am 67 | If Rnd()<.1 Then am = Rnd(-.2,.2) ' change brush angle 68 | If Rnd()<.02 Then ' every now and then change size of brush 69 | rad+=radmod 70 | If rad<2 Then radmod=-radmod 71 | If rad>20 Then radmod=-radmod 72 | End If 73 | If Rnd()<.01 74 | If inversion = True Then inversion = False Else inversion = True 75 | End If 76 | ' draw the brush 77 | For Local y2:Int=-rad To rad 78 | For Local x2:Int=-rad To rad 79 | Local x3:Int=x+x2 80 | Local y3:Int=y+y2 81 | Local t:Float = 1.0-(1.0 / Float(rad*2) * Float(distance(x3,y3,x,y))) 82 | If x3<0 Or y3<0 Or x3>=im.Width Or y3>=im.Height Then Continue 83 | 'Print t 84 | If inversion Then 85 | map[x3,y3] += Abs(t)/30 86 | Else 87 | map[x3,y3] -= Abs(t)/30 88 | End If 89 | 90 | If map[x3,y3] > 1.0 Then map[x3,y3] = 1.0 91 | If map[x3,y3] < 0 Then map[x3,y3] = 0 92 | 'canvas.Color = Color.White.Blend(Color.Black,t) 93 | 'canvas.DrawRect(x3,y3,1,1) 94 | Next 95 | Next 96 | Next 97 | 98 | End Method 99 | ' Manhattan Distance (less precise) 100 | Function distance:Float(x1:Float,y1:Float,x2:Float,y2:Float) 101 | Return Abs(x2-x1)+Abs(y2-y1) 102 | End Function 103 | End Class 104 | 105 | Function Main() 106 | New AppInstance 107 | New MyWindow 108 | App.Run() 109 | End Function 110 | -------------------------------------------------------------------------------- /generators/asteroidmapgen.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global screenwidth:Int,screenheight:Int 8 | 9 | Class astroidmapgen 10 | Field tw:Float,th:Float 'tile width and height 11 | Field map:Int[,] 12 | Field w:Int,h:Int ' map width and height 13 | Field posx:Float,posy:Float 14 | Method New(w:Int,h:Int) 15 | Self.w = w 16 | Self.h = h 17 | Self.tw = Float(screenwidth) / Float(w) 18 | Self.th = Float(screenheight) / Float(h) 19 | map = New Int[w,h] 20 | createmap() 21 | End Method 22 | Method createmap() 23 | Print w+"."+h 24 | ' we start the map from the center with a random angle to move to 25 | posx=w/2 26 | posy=h/2 27 | 28 | For Local i:Int=0 To w*h/500 29 | ' here we tunnel out area 30 | shootstone(posx,posy,Rnd(TwoPi)) 31 | Next 32 | End Method 33 | Method shootstone(x:Float,y:Float,angle:Float) 34 | Local shot:Bool=False 35 | 36 | Local depth:Int=Rnd(w/10,w/3) 37 | Local z:Float=3.0/Float(depth) 38 | map[x,y]=1 39 | For Local i:Int=0 Until depth 40 | x+=Cos(angle) 41 | y+=Sin(angle) 42 | 'reset position to center if to close to edge 43 | If x<5 Or y<5 Or x>w-5 Or y>h-5 Then 44 | posx = w/2 45 | posy = h/2 46 | Return 47 | End If 48 | 'bounce 49 | If Rnd()<.1 And i>depth/2 Then angle+=Rnd() 50 | ' here we brush the current position 51 | Local d:Float=Abs(3.0-(z*Float(i))) 52 | For Local y1:Int=y-d To y+d+1 53 | For Local x1:Int=x-d To x+d+1 54 | If x1<3 Or y1<3 Or x1>=w-3 Or y1>=h-3 Then Continue 55 | map[x1,y1] = 1 56 | Next 57 | Next 58 | 59 | Next 60 | 'store our current location 61 | posx = x 62 | posy = y 63 | End Method 64 | Method draw(canvas:Canvas) 65 | For Local y:Int=0 Until h 66 | For Local x:Int=0 Until w 67 | Local dx:Int=x*tw 68 | Local dy:Int=y*th 69 | If map[x,y] > 0 70 | canvas.Color = Color.White 71 | canvas.DrawRect(dx,dy,tw+1,th+1) 72 | End If 73 | Next 74 | Next 75 | 76 | End Method 77 | End Class 78 | 79 | Class MyWindow Extends Window 80 | Field mymap:astroidmapgen 81 | Method New() 82 | SeedRnd(Microsecs()) 83 | screenwidth = Width 84 | screenheight = Height 85 | mymap = New astroidmapgen(100,100) 86 | End method 87 | 88 | Method OnRender( canvas:Canvas ) Override 89 | App.RequestRender() ' Activate this method 90 | mymap.draw(canvas) 91 | ' if key escape then quit 92 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 93 | If Keyboard.KeyReleased(Key.Space) Then mymap = New astroidmapgen(Rnd(50,200),Rnd(50,200)) 94 | End Method 95 | 96 | End Class 97 | 98 | Function getangle:float(x1:Int,y1:Int,x2:Int,y2:Int) 99 | Return ATan2(y2-y1, x2-x1) 100 | End Function 101 | 102 | Function Main() 103 | New AppInstance 104 | New MyWindow 105 | App.Run() 106 | End Function 107 | -------------------------------------------------------------------------------- /generators/grow shapes - Shaded edges.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | 5 | ' 6 | ' Create rocks or shapes with shaded edges. 7 | ' This is done by taking the angle from the center of the shape to 8 | ' the edge and turning this into a light or dark color. 9 | ' 10 | 11 | Using std.. 12 | Using mojo.. 13 | 14 | 15 | Class MyWindow Extends Window 16 | Field im:Image[,] 17 | Field can:Canvas[,] 18 | Method New() 19 | SeedRnd(Microsecs()) 20 | im = New Image[10,10] 21 | can = New Canvas[10,10] 22 | For Local y:Int=0 Until 7 23 | For Local x:Int=0 Until 9 24 | createblob(x,y,Rnd(Pi)) 25 | Next 26 | Next 27 | End method 28 | 29 | Method OnRender( canvas:Canvas ) Override 30 | App.RequestRender() ' Activate this method 31 | Local scale:Float=2 32 | If Keyboard.KeyDown(Key.LeftShift) Then scale=1 33 | For Local y:Int=0 Until 7 34 | For Local x:Int=0 Until 9 35 | canvas.DrawImage(im[x,y],x*64,y*64,0,scale,scale) 36 | Next 37 | Next 38 | canvas.DrawImage(im[0,0],320-32*4,240-32*4,0,4,4) 39 | ' if key escape then quit 40 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 41 | End Method 42 | Method createblob(a:Int,b:Int,angle:Float) 43 | im[a,b] = New Image(64,64) 44 | im[a,b].Handle = New Vec2f(0,0) 45 | can[a,b] = New Canvas(im[a,b]) 46 | can[a,b].Clear(New Color(0,0,0,0)) 47 | ' Create our shape by growing it 48 | Local tmp:Int[,] = New Int[im[a,b].Width,im[a,b].Height] 49 | tmp[im[a,b].Width/2,im[a,b].Height/2] = 1 50 | For Local i:Int=0 Until (im[a,b].Width*im[a,b].Height)*14 51 | Local x:Int=Rnd(1,im[a,b].Width-2) 52 | Local y:Int=Rnd(1,im[a,b].Height-2) 53 | If tmp[x,y] = 1 Then tmp[x+Rnd(-1,2),y+Rnd(-1,2)] = 1 54 | Next 55 | ' Create light and dark edges 56 | For Local y:Int=1 Until im[a,b].Height-1 57 | For Local x:Int=1 Until im[a,b].Width-1 58 | If tmp[x,y] = 0 'if no shape value 59 | ' loop around the no shape value 60 | For Local y2:Int=y-1 To y+1 61 | For Local x2:Int=x-1 To x+1 62 | ' if here is a shape value 63 | If tmp[x2,y2] = 1 64 | ' edge color is based on angle (2 or 3) 65 | Local cangle:Float=getangle(x,y,im[a,b].Width/2,im[a,b].Height/2) 66 | If cangle>-angle And cangle<-angle+Pi 67 | tmp[x2,y2] = 2 68 | Else 69 | tmp[x2,y2] = 3 70 | End If 71 | End If 72 | Next 73 | Next 74 | End If 75 | Next 76 | Next 77 | ' Turn the shape into a image 78 | For Local y:Int=0 Until im[a,b].Height 79 | For Local x:Int=0 Until im[a,b].Width 80 | If tmp[x,y] = 0 Then Continue 81 | Select tmp[x,y] 82 | Case 1 83 | can[a,b].Color = Color.Grey 84 | Case 2 85 | can[a,b].Color = Color.LightGrey 86 | Case 3 87 | can[a,b].Color = Color.DarkGrey 88 | End Select 89 | 'add some noise 90 | If Rnd()<.5 And tmp[x,y]<>2 91 | can[a,b].Color = can[a,b].Color.Blend(Color.White,Rnd(.2)) 92 | Else 93 | can[a,b].Color = can[a,b].Color.Blend(Color.Black,Rnd(.2)) 94 | End If 95 | can[a,b].DrawRect(x,y,1,1) 96 | Next 97 | Next 98 | can[a,b].Flush() 99 | End Method 100 | Function getangle:float(x1:Int,y1:Int,x2:Int,y2:Int) 101 | Return ATan2(y2-y1, x2-x1) 102 | End Function 103 | End Class 104 | 105 | Function Main() 106 | New AppInstance 107 | New MyWindow 108 | App.Run() 109 | End Function 110 | -------------------------------------------------------------------------------- /generators/growvisualiser.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | 5 | ' 6 | ' 7 | ' 8 | ' 9 | 10 | 11 | 12 | Using std.. 13 | Using mojo.. 14 | 15 | Global w:Int=40 16 | Global h:Int=40 17 | 18 | Class MyWindow Extends Window 19 | Field ex1:Int[,] 20 | Field ex2:Int[,] 21 | Field ex1pos:Vec2i 22 | Field ex2pos:Vec2i 23 | Field tw:Int,th:Int 24 | Method New() 25 | tw = 5 26 | th = 5 27 | ex1pos.X = 50 28 | ex1pos.Y = 50 29 | ex2pos.X = 370 30 | ex2pos.Y = 50 31 | reset() 32 | End method 33 | 34 | Method OnRender( canvas:Canvas ) Override 35 | App.RequestRender() ' Activate this method 36 | ' 37 | If Keyboard.KeyDown(Key.Key1) 38 | For Local i:Int=0 Until 50 39 | ex1 = grow(ex1) 40 | ex2 = grow(ex2) 41 | Next 42 | End If 43 | 44 | editgrow() 45 | ' 46 | draw(canvas,ex1,ex1pos.X,ex1pos.Y) 47 | draw(canvas,ex2,ex2pos.X,ex2pos.Y) 48 | 49 | canvas.Color = Color.White 50 | canvas.DrawText("Press and hold '1' - to grow arrays.",0,0) 51 | canvas.DrawText("Press 'c' - to clear arrays.",0,10) 52 | canvas.DrawText("Use Mouse and Left/Right Button to draw in area.",0,300) 53 | ' if key escape then quit 54 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 55 | If Keyboard.KeyReleased(Key.C) Then reset() 56 | End Method 57 | Method editgrow() 58 | Local c:Int = -1 59 | If Mouse.ButtonDown(MouseButton.Left) Then c=1 60 | If Mouse.ButtonDown(MouseButton.Right) Then c=0 61 | If c=-1 Then Return 62 | 63 | If Mouse.X > ex1pos.X And Mouse.Y > ex1pos.Y 64 | Local x:Int=(Mouse.X - ex1pos.X) / tw 65 | Local y:Int=(Mouse.Y - ex1pos.Y) / th 66 | If x ex2pos.X And Mouse.Y > ex2pos.Y 71 | Local x:Int=(Mouse.X - ex2pos.X) / tw 72 | Local y:Int=(Mouse.Y - ex2pos.Y) / th 73 | If x=w-1 Or y>=h-1) 89 | x = Rnd(w) 90 | y = Rnd(h) 91 | If x<1 Or y<1 Or x>=w-1 Or y>=h-1 Then Continue 92 | If ar[x,y] = 1 93 | ret[x+Rnd(-1,2),y+Rnd(-1,2)] = 1 94 | End If 95 | Wend 96 | Return ret 97 | End Method 98 | Method draw(canvas:Canvas,ar:Int[,],x:Int,y:Int) 99 | canvas.Color = Color.White 100 | canvas.DrawRect(x-1,y-1,ar.GetSize(0)*tw+2,ar.GetSize(1)*th+2) 101 | For Local y2:Int = 0 Until ar.GetSize(1) 102 | For Local x2:Int = 0 Until ar.GetSize(0) 103 | Select ar[x2,y2] 104 | Case 0 105 | canvas.Color = Color.Black 106 | Case 1 107 | canvas.Color = Color.White 108 | End select 109 | canvas.DrawRect(x+x2*tw,y+y2*th,tw,th) 110 | Next 111 | Next 112 | End Method 113 | Method reset() 114 | ex1 = New Int[w,h] 115 | ex2 = New Int[w,h] 116 | End Method 117 | End Class 118 | 119 | Function Main() 120 | New AppInstance 121 | New MyWindow 122 | App.Run() 123 | End Function 124 | -------------------------------------------------------------------------------- /generators/texturestonebarklike.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Global screenwidth:Int,screenheight:Int 8 | 9 | ' 10 | ' This effect creates a grey stone like texture 11 | ' 12 | Class effect 13 | Field w:Int,h:Int ' map array width and height 14 | Field tw:Float,th:Float 'tile width and height 15 | Field map:Float[,] 'our map that holds the texture (greyscale) 16 | Method New(w:Int,h:Int) 17 | Self.w = w 18 | Self.h = h 19 | Self.tw = float(screenwidth)/float(w) 20 | Self.th = Float(screenheight)/Float(h) 21 | ' Here we create our texture array 22 | map = New Float[w,h] 23 | ' Here we call the brush stroking method that creates the texture. 24 | ' we call it 20000 times in this setting. It takes a random position 25 | ' on the map and a random direction. 26 | For Local i:Int=0 Until w+h*20 27 | shootproj(Rnd(w),Rnd(h),Rnd(TwoPi)) 28 | Next 29 | 30 | End Method 31 | ' This is the painters brush. It takes a position 32 | ' and angle and strokes the brush for a certain distance. 33 | ' While it moves it increases the color value under the tip. 34 | Method shootproj(x:Float,y:Float,angle:Float) 35 | For Local i:Int=0 Until w+h 36 | x+=Cos(angle)/Rnd(1,10) 37 | y+=Sin(angle)/Rnd(1,10) 38 | If x<0 Or y<0 Or x>=w Or y>=h Then Exit 39 | map[x,y]+=1 40 | If map[x,y]>255 Then map[x,y] = 255 41 | If map[x,y]<0 Then map[x,y] = 0 42 | Next 43 | End Method 44 | ' This draws the texture 45 | Method draw(canvas:Canvas) 46 | For Local y:Int=0 Until h 47 | For Local x:Int=0 Until w 48 | Local dx:Int=x*tw 49 | Local dy:Int=y*th 50 | Local a:Float=(1.0/255.0)*Float(map[x,y]) 51 | canvas.Color = New Color(a,a,a) 52 | canvas.DrawRect(dx,dy,tw+1,th+1) 53 | Next 54 | Next 55 | End Method 56 | End Class 57 | 58 | Class MyWindow Extends Window 59 | 60 | Field myeffect:effect 61 | Method New() 62 | SeedRnd(Microsecs()) 63 | screenwidth = Width 64 | screenheight = Height 65 | myeffect = New effect(640,480) 66 | End method 67 | 68 | Method OnRender( canvas:Canvas ) Override 69 | App.RequestRender() ' Activate this method 70 | myeffect.draw(canvas) 71 | ' if key escape then quit 72 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 73 | End Method 74 | 75 | End Class 76 | 77 | Function Main() 78 | New AppInstance 79 | New MyWindow 80 | App.Run() 81 | End Function 82 | -------------------------------------------------------------------------------- /rpgbook/cave.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class cave 8 | 9 | End Class 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | -------------------------------------------------------------------------------- /rpgbook/charactercreation.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class charactercreation 8 | 9 | End Class 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | -------------------------------------------------------------------------------- /rpgbook/classselection.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class classselection 8 | 9 | End Class 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | -------------------------------------------------------------------------------- /rpgbook/combat.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class combat 8 | 9 | End Class 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | -------------------------------------------------------------------------------- /rpgbook/cutscenes.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class cutscenes 8 | 9 | End Class 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | -------------------------------------------------------------------------------- /rpgbook/dialogtree.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class dialogtree 8 | 9 | End Class 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | -------------------------------------------------------------------------------- /rpgbook/effects.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | ' 8 | ' effects for the game 9 | ' 10 | 11 | Class effects 12 | 13 | End Class 14 | 15 | Class MyWindow Extends Window 16 | 17 | Method New() 18 | End method 19 | 20 | Method OnRender( canvas:Canvas ) Override 21 | App.RequestRender() ' Activate this method 22 | ' if key escape then quit 23 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 24 | End Method 25 | 26 | End Class 27 | 28 | Function Main() 29 | New AppInstance 30 | New MyWindow 31 | App.Run() 32 | End Function 33 | -------------------------------------------------------------------------------- /rpgbook/hud.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | ' 8 | ' inventory and heads up displays 9 | ' update health and logic 10 | ' 11 | ' 12 | 13 | Class hud 14 | 15 | End Class 16 | 17 | Class MyWindow Extends Window 18 | 19 | Method New() 20 | End method 21 | 22 | Method OnRender( canvas:Canvas ) Override 23 | App.RequestRender() ' Activate this method 24 | ' if key escape then quit 25 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 26 | End Method 27 | 28 | End Class 29 | 30 | Function Main() 31 | New AppInstance 32 | New MyWindow 33 | App.Run() 34 | End Function 35 | -------------------------------------------------------------------------------- /rpgbook/inventory.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class inventory 8 | 9 | End Class 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | -------------------------------------------------------------------------------- /rpgbook/monsterleveling.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class monsterleveling 8 | 9 | End Class 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | -------------------------------------------------------------------------------- /rpgbook/overworld.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class overworld 8 | 'map layers 9 | 'dirt, grass, stone etc 10 | Field background:Int[,] 11 | 'buildings, mountains, trees, villages 12 | Field ground:Int[,] 13 | 'rocks, weeds, flowers, skulls etc 14 | Field decoration:Int[,] 15 | 'shapes where player can and can not move to 16 | Field collision:Int[,] 17 | 'player and non player character spawns 18 | Field spawn:Int[,] 19 | 'trigger events (move from one map to another) 20 | Field portal:Int[,] 21 | 22 | End Class 23 | 24 | Class MyWindow Extends Window 25 | 26 | Method New() 27 | End method 28 | 29 | Method OnRender( canvas:Canvas ) Override 30 | App.RequestRender() ' Activate this method 31 | ' if key escape then quit 32 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 33 | End Method 34 | 35 | End Class 36 | 37 | Function Main() 38 | New AppInstance 39 | New MyWindow 40 | App.Run() 41 | End Function 42 | -------------------------------------------------------------------------------- /rpgbook/playerclass.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class player 8 | 'weapon useage and attack damage 9 | Field strength:Int 10 | 'magic useage and spell damage bonusses 11 | Field intelligence:Int 12 | 'how much damage can the character take before dying 13 | Field hitpoints:Int 14 | 'how much magic damage can character inflict before running out 15 | Field magicpoints:Int 16 | 'every level you need a certain amount of points to level up 17 | Field experiencepoints:Int 18 | 19 | End Class 20 | 21 | Class MyWindow Extends Window 22 | 23 | Method New() 24 | End method 25 | 26 | Method OnRender( canvas:Canvas ) Override 27 | App.RequestRender() ' Activate this method 28 | ' if key escape then quit 29 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 30 | End Method 31 | 32 | End Class 33 | 34 | Function Main() 35 | New AppInstance 36 | New MyWindow 37 | App.Run() 38 | End Function 39 | -------------------------------------------------------------------------------- /rpgbook/quests.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class quests 8 | 9 | End Class 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | -------------------------------------------------------------------------------- /rpgbook/shop.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class shop 8 | 9 | End Class 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | -------------------------------------------------------------------------------- /rpgbook/shopinterface.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | ' 8 | ' view select purchase sell items 9 | ' 10 | 11 | Class shopinterface 12 | 13 | End Class 14 | 15 | Class MyWindow Extends Window 16 | 17 | Method New() 18 | End method 19 | 20 | Method OnRender( canvas:Canvas ) Override 21 | App.RequestRender() ' Activate this method 22 | ' if key escape then quit 23 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 24 | End Method 25 | 26 | End Class 27 | 28 | Function Main() 29 | New AppInstance 30 | New MyWindow 31 | App.Run() 32 | End Function 33 | -------------------------------------------------------------------------------- /rpgbook/skilltree.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class skilltree 8 | 9 | End Class 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | -------------------------------------------------------------------------------- /rpgbook/speechwindow.monkey2: -------------------------------------------------------------------------------- 1 | #Import "" 2 | #Import "" 3 | 4 | Using std.. 5 | Using mojo.. 6 | 7 | Class speechwindow 8 | 9 | End Class 10 | 11 | Class MyWindow Extends Window 12 | 13 | Method New() 14 | End method 15 | 16 | Method OnRender( canvas:Canvas ) Override 17 | App.RequestRender() ' Activate this method 18 | ' if key escape then quit 19 | If Keyboard.KeyReleased(Key.Escape) Then App.Terminate() 20 | End Method 21 | 22 | End Class 23 | 24 | Function Main() 25 | New AppInstance 26 | New MyWindow 27 | App.Run() 28 | End Function 29 | --------------------------------------------------------------------------------