Import mojo
Class cloud
Field px:Float,py:Float
Field pw:Int,ph:Int
Field mx:Float
Field sw:Int
Method New(x:Int,y:Int,w:Int,h:Int,sw:Int)
px = x
py = y
pw = w
ph = h
Self.sw = sw
mx = Rnd(0.05,0.2)
End Method
Method update()
px += mx
If px > sw+pw Then px = 0-(pw*2)
End Method
' This method draws a cloud/
' at x,y with width w and height h
Method draw()
' Draw 7 plumps (go around in a circle)
For Local angle:Int=0 Until 360 Step 360/7
Local x2:Float=Cos(angle)*pw
Local y2:Float=Sin(angle)*ph
SetColor 0,0,0
DrawOval(x2+px,y2+py,pw,ph)
SetColor 255,255,255
DrawOval(x2+px+4,y2+py+4,pw-8,ph-8)
Next
' Draw a white oval to erase the center of the cloud
SetColor 255,255,255
DrawOval(px-pw/2,py-ph/2,pw+pw,ph+ph)
End Method
End Class
Class MyGame Extends App
Field mycloud:List<cloud> = New List<cloud>
Method OnCreate()
SetUpdateRate(60)
Seed = GetDate[4]*GetDate[5]
For Local i:Int = 0 Until 15
mycloud.AddLast(New cloud(Rnd(-DeviceWidth*.5,DeviceWidth),Rnd(DeviceHeight),Rnd(30,130),Rnd(20,50),DeviceWidth))
Next
End Method
Method OnUpdate()
For Local i:=Eachin mycloud
i.update()
Next
End Method
Method OnRender()
Cls 0,0,255
For Local i:=Eachin mycloud
i.draw()
Next
End Method
End Class
Function Main()
New MyGame()
End Function
Artificial intelligence/templates/examples/rts/rpg/strategy ect. in MonkeyX/CerberusX language. You can download the free version of MonkeyX from itch.io or Cerberus-x.com The Flash applets will stop working in around 2020.
Sunday, November 12, 2017
Monkey-X - Generator - Cartoon Clouds - code example
Monkey-X - Generator - 10 Print Chr - code example
Import mojo
Class MyGame Extends App
Field x:Int,y:Int
Field spacing:Int=10
Field map:Int[][]
Method OnCreate()
SetUpdateRate(60)
map = New Int[100][]
For Local i:Int=0 Until 100
map[i] = New Int[100]
Next
makemaze()
End Method
Method OnUpdate()
If KeyHit(KEY_SPACE) Or MouseHit(MOUSE_LEFT)
makemaze()
End If
End Method
Method OnRender()
Cls 0,0,0
drawmaze()
DrawText "Press Space or Mouse for new maze",0,0
End Method
Method drawmaze()
For Local y:Int=0 Until 100
For Local x:Int=0 Until 100
Select map[x][y]
Case 0
DrawLine x*10,y*10,x*10+10,y*10+10
Case 1
DrawLine x*10+10,y*10,x*10,y*10+10
End Select
Next
Next
End Method
Method makemaze()
For Local y:Int=0 Until 100
For Local x:Int=0 Until 100
If Rnd(1)<.5 Then map[x][y] = 1 Else map[x][y] = 0
Next
Next
End Method
End Class
Function Main()
New MyGame()
End Function
Monkey-X - Beginners - Class in array - code example
Import mojo
'
' This is our enemy class
'
Class enemy
Field px:Int,py:Int
Method New(x:Int,y:Int)
Self.px = x
Self.py = y
End Method
Method move(x:Int,y:Int)
px += x
py += y
End Method
End Class
Class MyGame Extends App
' How many enemies are there
Field numenemies:Int=10
' Set up the array using the enemy class
Field myenemy:enemy[]
Method OnCreate()
' Create the enemies in the array
myenemy = New enemy[numenemies]
For Local i:Int=0 Until numenemies
myenemy[i] = New enemy(Rnd(DeviceWidth()),Rnd(DeviceHeight))
Next
End Method
Method OnUpdate()
End Method
Method OnRender()
Cls 0,0,0
SetColor 255,255,255
' Loop through all array containers (numenemies)
' and draw them.
For Local i:=Eachin myenemy
DrawRect i.px,i.py,32,32
Next
' We can acces arrays directly and call/modify anything inside it.
myenemy[0].move(5,0)
If myenemy[0].px > DeviceWidth Then myenemy[0].px = -10
End Method
End Class
Function Main()
New MyGame()
End Function
Monkey-X - Lerp - Linear Interpolation - patrolling - code example
Import mojo
Class MyGame Extends App
Field enemyx:Int,enemyy:Int
Field startx:Int=100,starty:Int=100
Field destx:Int=320,desty:Int=230
Field percentage:Float=0 ' how far in the path are we
Field stp:Float=0.01 ' how fast do we move
Method OnCreate()
SetUpdateRate(60) ' Fps
enemyx = startx
enemyy = starty
End Method
Method OnUpdate()
' Get our new x and y position
enemyx = lerp(percentage,startx,destx)
enemyy = lerp(percentage,starty,desty)
' Set the new position
percentage+=stp
' Keep inside the value of 0.0 and 1.0
If percentage<=0 Or percentage>=1 Then stp=-stp
End Method
Method OnRender()
Cls 0,0,0
SetColor 255,255,255
' Draw the enemy sprite
DrawRect enemyx,enemyy,32,32
'
DrawText "Lerp(Linear Interpolation) Patrolling - example",0,0
End Method
' Percentage 0 to 1 returns number between a and b
Function lerp:Int(t:Float , a:Float, b:Float)
Return a + t * (b - a)
End Function
End Class
Function Main()
New MyGame()
End Function
Friday, November 10, 2017
Monkey-X - Generator - Simple 2d Pixelart tree - code example
Import mojo
Class tree
Field px:Int,py:Int
Field pw:Float,ph:Float
Field mapone:Int[][]
Field treecolor1:Int=Rnd(230,255)
Field treecolor1r:Int=Rnd(50,200)
Field treecolor2:Int=Rnd(190,220)
Field treecolor3:Int=Rnd(130,180)
Field treecolor4:Int=Rnd(70,120)
Field basecolor1:Int=150
Field basecolor2:Int=190
Field basecolor3:Int=220
Method New(x:Int,y:Int,w:Int,h:Int)
If Rnd(3)<2 Then treecolor1r = 0
px = x
py = y
pw = w
ph = h
mapone = New Int[w][]
For Local i:Int=0 Until w
mapone[i] = New Int[h]
Next
maketree()
End Method
Method maketree()
Local mx:Float=0.05
Local my:Float=.1
Local y:Float=1
Local x:Float=pw/2+1
Local base:Float=0
Local bounce:Float=.1
Local col:Int
Local num:Float=2
Local stap:Float=Rnd(0.001,0.005)
Local stap2:Float=Rnd(0.01,0.2)
Local stap3:Float=Rnd(0.5,1.5)
' Place two black pixels at the top of the tree
mapone[x-1][0] = 1
mapone[x-2][0] = 1
' create the tree
While (y+5)<=(ph-(ph/20))
y+=my
x+=mx
' stay inside the image
If x>=pw Then x=pw-2
If x<=0 Then x=0
' change color of the tree depending
' on the current y location
If y<ph/1.4 Then col = treecolor4
If y<ph/1.6 Then col = treecolor3
If y<ph/1.9 Then col = treecolor2
If y<ph/4 Then col = treecolor1
' fill the current line
filltoleft(x,y,pw-x,col)
' black pixel to the left and right
mapone[x][y] = 1
mapone[pw-x][y] = 1
' next step in the tree shape
mx-=stap
If y<ph/1.45 Then
If mx<0
If x < ((pw/2)+num) Then mx=bounce ; bounce+=stap2 ; num+=stap3
End If
Else
If mx<0
If x<((pw/2)+num) Then bounce=.1 ; mx=bounce ; num-=stap3
Endif
End If
Wend
' Make sure the bottom of the tree is also drawn
filltoleft(x,y,pw-x,1)
' Make the tree trunk
maketreebase()
End Method
'
' Fill from x to tox on y line using col(or)
' We go from right to left and fill the line with
' a number. (tree inside color)
'
Method filltoleft(x:Int,y:Int,tox:Int,col:Int)
Local ls:Int=(pw/2)
Local len1:Int=(x-ls)/2
Local len2:Int=(x-ls)/1.7
For Local x2:Int=x To tox Step -1
mapone[x2][y] = col
If col = treecolor2 Then
If Rnd(4) < 1 And distance(x2,0,tox,0) < len1 And y<ph/2 Then mapone[x2][y] = treecolor1
If Rnd(4) < 1 And distance(x2,0,x,0)< len1 And y<ph/2 Then mapone[x2][y] = treecolor1
If Rnd(2)<1.3 And distance(x2,0,ls-len1,0) < 2 And y<ph/2 Then mapone[x2][y] = treecolor1
If Rnd(2)<1.3 And distance(x2,0,ls+len1,0) < 2 And y<ph/2 Then mapone[x2][y] = treecolor1
End If
If col=treecolor1
If Rnd(2) < 1 And y>5 And distance(x2,0,ls,0) < 3 Then mapone[x2][y] = treecolor2
End If
If col=treecolor3
If Rnd(2)<1.3 And distance(x2,0,ls,0) < len2 And y<ph/1.8 Then mapone[x2][y] = treecolor2
End If
If col=treecolor4
If Rnd(2)<1.3 And distance(x2,0,ls,0) < len2 And y<ph/1.45 Then mapone[x2][y] = treecolor3
End If
Next
End Method
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
Method maketreebase()
' treebase
For Local y:Int=ph-(ph/5) Until ph
For Local x:Int=(pw/2)-(pw/8) Until (pw/2)+(pw/8)
If x<0 Or y<0 Or x>=pw Or y>= ph Then Continue
mapone[x][y] = basecolor1
If x=(pw/2)-(pw/8) Then mapone[x][y] = 1
If x=(pw/2)+(pw/8)-1 Then mapone[x][y]=1
If y=ph-1 Then mapone[x][y]=1
Next
Next
For Local y:Int=ph-(ph/5) Until ph-(ph/5)
For Local x:Int=(pw/2)-(pw/8) Until (pw/2)+(pw/8)
If x<0 Or y<0 Or x>=pw Or y>= ph Then Continue
mapone[x][y] = 1
Next
Next
' tree base center lighting
For Local y:Int=ph-(ph/7) Until ph-1
For Local x:Int=(pw/2)-(pw/30) Until (pw/2)+(pw/30)
If x<0 Or y<0 Or x>=pw Or y>= ph Then Continue
mapone[x][y] = basecolor3
Next
Next
For Local y:Int=ph-(ph/7) Until ph-1
For Local x:Int=(pw/2) Until (pw/2)+(pw/30)
If x<0 Or y<0 Or x>=pw Or y>= ph Then Continue
mapone[x][y] = basecolor2
Next
Next
'Remove two black pixels from the bottom of the treebase
mapone[(pw/2)-(pw/8)][ph-1] = 0
mapone[(pw/2)+(pw/8)-1][ph-1] = 0
End Method
Method draw()
For Local y:Int=0 Until ph
For Local x:Int=0 Until pw
If mapone[x][y] = 0 Then Continue
Select mapone[x][y]
Case 1
SetColor 0,0,0
Case treecolor1
SetColor treecolor1r/2,treecolor1,0
Case treecolor2
SetColor treecolor1r/1.5,treecolor2,0
Case treecolor3
SetColor treecolor1r/1.2,treecolor3,0
Case treecolor4
SetColor treecolor1r,treecolor4,0
Case basecolor1
SetColor basecolor1,basecolor1/2,0
Case basecolor2
SetColor basecolor2,basecolor2/2,0
Case basecolor3
SetColor basecolor3,basecolor3/2,0
End Select
DrawRect px+(x*1),py+(y*1),1,1
Next
Next
End Method
End Class
Class MyGame Extends App
Field mytree:List<tree>
Field time:Int=Millisecs()
Field hw:Int=48,hh:Int=64
Method OnCreate()
Seed = GetDate[4]*GetDate[5]
SetUpdateRate(1)
maketrees()
End Method
Method OnUpdate()
If KeyHit(KEY_SPACE) Or Millisecs() > time
time=Millisecs()+2000
maketrees
End If
End Method
Method OnRender()
Cls 0,0,0
SetColor 50,125,235
DrawRect 0,0,DeviceWidth,150+hh
SetColor 50,155,255
DrawRect 0,100,DeviceWidth,(150+hh)-100
SetColor 5,250,5
DrawRect 0,150+hh,DeviceWidth,DeviceHeight-(150+hh)
SetColor 125,250,125
DrawRect 0,150+hh,DeviceWidth,2
For Local i:=Eachin mytree
i.draw()
Next
End Method
Method maketrees()
mytree = New List<tree>
For Local x:Int=0 Until DeviceWidth Step 64
If Rnd(3)<2 Then mytree.AddLast(New tree(x,150,48,64))
Next
Local sy:Int=0
For Local i:Int=0 Until 35
mytree.AddLast(New tree(Rnd(DeviceWidth),150+sy,48,64))
sy+=8
Next
End Method
End Class
Function Main()
New MyGame()
End Function
Thursday, November 9, 2017
Monkey-X - Generator - 2d Houses (town) - code example
Import mojo
Class building
Field px:Int,py:Int
Field totalwidth:Int
Field blockhouse:Int=1
Field blockdoor:Int=2
' For collision (enter/flee in home/shop)
Field doorx:Int,doory:Int
Field doorwidth:Int,doorheight:Int
Field blocksmallwindow:Int=3
Field blockwidewindow:Int=4
Field blockcrateleft:Int=5
Field blockcrateright:Int=6
Field blockiceboxleft:Int=7
Field blockiceboxright:Int=7
Field blocktoiletleft:Int=8
Field blocktoiletright:Int=9
Field blockfrontcrate:Int=10
Field blockrooftop:Int=11
Field blockchimney:Int=12
Field blockshopsign:Int=13
Field houselayer:Int[]=New Int[3] 'base blocks
Field rooftoplayer:Int[]=New Int[3]
Field chimneylayer:Int[]=New Int[3]
Field doorlayer:Int[] = New Int[3]
Field windowlayer:Int[] = New Int[3] '011010'
Field housesidelayer:Int[] = New Int[2]
Field shopsignlayer:Int[] = New Int[3]
Field frontlayer:Int[] = New Int[6]
Method New(x:Int,y:Int,w:Int,isshop:Bool)
px = x
py = y
totalwidth = w
makehouse(w,isshop)
End Method
Method makehouse(w:Int,isshop:Bool)
' Make the base house blocks
For Local i:Int=0 Until w
houselayer[i] = blockhouse
Next
' Create the items at the side of the houses
If Rnd(10)<5
' add to which side(s)
Local sides:String="left"
If Rnd(10)<3 Then sides="right"
If Rnd(10)<3 Then sides="both"
' Add items to side(s)
If sides="left" Or sides="both" Then
housesidelayer[0] = blocktoiletleft
If Rnd(10)<3 Then
housesidelayer[0] = blockcrateleft
End If
If Rnd(10)<3 Then
housesidelayer[0] = blockiceboxleft
End If
Endif
If sides="right" Or sides="both" Then
housesidelayer[1] = blocktoiletright
If Rnd(10)<3 Then
housesidelayer[1] = blockcrateright
End If
If Rnd(10)<3 Then
housesidelayer[1] = blockiceboxright
End If
Endif
End If
'Create the crates at the front of the house
For Local i:Int= 0 Until (w*2)-2
If Rnd(10)<2 Then
frontlayer[i] = blockfrontcrate
End If
Next
'Create windows
Select w
Case 2
windowlayer[0] = blocksmallwindow
Case 3
windowlayer[0] = blockwidewindow
End Select
' create door
Select w
Case 1
doorlayer[0] = blockdoor
If isshop Then shopsignlayer[0] = blockshopsign
Case 2
doorlayer[1] = blockdoor
If isshop Then shopsignlayer[1] = blockshopsign
Case 3
doorlayer[2] = blockdoor
If isshop Then shopsignlayer[2] = blockshopsign
End Select
' rooftop
Select w
Case 1
rooftoplayer[0] = blockrooftop
Case 2
rooftoplayer[0] = blockrooftop
rooftoplayer[1] = blockrooftop
Case 3
rooftoplayer[0] = blockrooftop
rooftoplayer[1] = blockrooftop
rooftoplayer[2] = blockrooftop
End Select
' chimney
Select w
Case 1
chimneylayer[0] = blockchimney
Case 2
chimneylayer[Rnd(0,2)] = blockchimney
Case 3
chimneylayer[Rnd(0,3)] = blockchimney
End Select
End Method
Method draw(w:Int,h:Int)
Local bw:Int=w
Local bh:Int=h
' Draw the house blocks
For Local i:Int=0 Until 3
If houselayer[i] = blockhouse Then
SetColor 150,140,150
DrawRect px+(i*bw),py,bw+1,bh
SetColor 200,200,200
DrawRect px+(i*bw),py,bw,bh
'SetColor 230,230,230
'shadow top
SetColor 60,60,60
DrawRect px+(i*bw),py,bw,bh/15
'shadow bottom
SetColor 150,150,150
DrawRect px+(i*bw),py+bh/1.1,bw,bh/8
'highlight left
If i=0
SetColor 220,220,220
DrawRect px+(i*bw),py,1,bh/3
Endif
End If
Next
' Draw the rooftop
For Local i:Int=0 Until 3
If rooftoplayer[i] = blockrooftop Then
'SetColor 200,100,100
SetColor 170,70,60
DrawRect px+(i*bw),py-(bh/1.5),bw,bh-(bh/3)
' Bottom shade
SetColor 130,50,30
'SetColor 200,100,100
DrawRect px+(i*bw),py-(bh/8),bw,bh/8
' top shade
SetColor 190,90,80
DrawRect px+(i*bw),py-(bh/1.5),bw,1
' top shade
If i=0
'horizontal
SetColor 220,120,110
DrawRect px+(i*bw),py-(bh/1.5),bw/2,1
'vertical
SetColor 200,100,100
DrawRect px+(i*bw),py-(bh/1.5),1,bh/3
End If
End If
Next
' Draw the chimney
For Local i:Int=0 Until 3
If chimneylayer[i] = blockchimney Then
SetColor 100,100,100
DrawRect px+(i*bw)+(bw/4),py-(bh/1.2),bw/2.5,bh/4
'chimney highlight
SetColor 140,130,120
DrawRect px+(i*bw)+(bw/4),py-(bh/1.2),bw/6,1
End If
Next
'Draw the windows
For Local i:Int=0 Until 3
If windowlayer[i] = blocksmallwindow
SetColor 0,100,200
DrawRect px+(i*bw)+(bw/3),py+(bh/5),bw-(bw/3),bh-(bh/2.5)
' light bottom
SetColor 0,115,210
DrawRect px+(i*bw)+(bw/3),py+(bh/2),bw-(bw/3),(bh/3.3)
' dark bottom
SetColor 180,125,20
DrawRect px+(i*bw)+(bw/3),py+(bh*.7),bw-(bw/3),(bh/8.3)
End If
If windowlayer[i] = blockwidewindow
SetColor 0,100,200
DrawRect px+(i*bw)+(bw/3),py+(bh/5),(bw*2)-(bw/3),bh-(bh/2.5)
'light bottom
SetColor 0,115,210
DrawRect px+(i*bw)+(bw/3),py+(bh/2),(bw*2)-(bw/3),(bh/3.3)
'dark bottom
SetColor 180,125,20
DrawRect px+(i*bw)+(bw/3),py+(bh*.7),(bw*2)-(bw/3),(bh/8.3)
End If
Next
' Draw the door
For Local i:Int=0 Until 3
If doorlayer[i] = blockdoor
SetColor 100,50,50
If shopsignlayer[i] = blockshopsign
SetColor 250,200,50
End If
DrawRect px+(i*bw)+(bw/5),py+(bh/5),bw-(bw/2),bh-(bh/4)
' doorknob
SetColor 200,210,210
DrawRect px+(i*bw)+(bw/2),py+(bh/1.7),(bw/9),(bh/9)
'numberplate
SetColor 200,250,250
DrawRect px+(i*bw)+(bw/1.3),py+(bh/3),(bw/9),(bh/9)
SetColor 10,50,50
DrawRect px+(i*bw)+(bw/1.25),py+(bh/2.8),(bw/18),(bh/14)
End If
Next
' Draw the sides
If housesidelayer[0] = blocktoiletleft Then drawtoilet(px,py,bw,bh,"left")
If housesidelayer[1] = blocktoiletright Then drawtoilet(px,py,bw,bh,"right")
If housesidelayer[0] = blockcrateleft Then drawsidecrate(px,py,bw,bh,"left")
If housesidelayer[1] = blockcrateright Then drawsidecrate(px,py,bw,bh,"right")
If housesidelayer[0] = blockiceboxleft Then drawsideicebox(px,py,bw,bh,"left")
If housesidelayer[1] = blockiceboxright Then drawsideicebox(px,py,bw,bh,"right")
'Draw the crates at the front of the house
For Local i:Int=0 Until (totalwidth*2)-2
If frontlayer[i] = blockfrontcrate
SetColor 100,50,50
DrawRect px+((bw/2)*i),py+bh/1.2,bw/4,bh/6.4
End If
Next
'Draw the shop sign
For Local i:Int=0 Until totalwidth
If shopsignlayer[i] = blockshopsign
SetColor 255,40,30
Local x:Int=px+(bw*i)-bw/8
Local y:Int=py-bh/5
DrawRect x,y,bw*1.2,bh/3
SetColor 255,255,255
'DrawText "Shop X",x+5,y+5
DrawRect x+bh/10,y+bh/12,4,4
End If
Next
End Method
Method drawtoilet(x:Int,y:Int,w:Int,h:Int,side:String)
If side = "left"
Local ltx:Float=x-w/2
Local lty:Float=y+(h/4)
Local rtx:Float=ltx+(w/2)
Local rty:Float=lty
Local lbx:Float=ltx
Local lby:Float=lty+(h-h/4)
Local rbx:Float=ltx+(w/2)
Local rby:Float=lby
Local toil:Float[8]
toil[0] = ltx
toil[1] = lty-(h/6)
toil[2] = rtx
toil[3] = rty
toil[4] = rbx
toil[5] = rby
toil[6] = lbx
toil[7] = lby
SetColor 150,50,50
DrawPoly(toil)
'DrawRect x-w/2,y+10,w/2,h-10
Elseif side="right"
Local ltx:Float=(x)+(totalwidth*w)
Local lty:Float=y+(h/4)
Local rtx:Float=ltx+(w/2)
Local rty:Float=lty
Local rbx:Float=ltx+(w/2)
Local rby:Float=rty+(h-h/4)
Local lbx:Float=ltx
Local lby:Float=rby
SetColor 150,50,50
Local box:Float[8]
box[0] = ltx
box[1] = lty
box[2] = rtx
box[3] = rty-(h/6)
box[4] = rbx
box[5] = rby
box[6] = lbx
box[7] = lby
DrawPoly(box)
' SetColor 100,50,50
' DrawRect (x)+totalwidth*w,y+10,w/2,h-10
End If
End Method
Method drawsidecrate(x:Int,y:Int,w:Int,h:Int,side:String)
If side = "left"
' pipe
SetColor 120,120,120
DrawRect x-w/8,y,w/8,h
'barrel
SetColor 100,50,50
DrawRect x-w/3,y+(h/1.5),w/3,h-(h/1.5)
Elseif side="right"
'pipe
SetColor 120,120,120
DrawRect (x)+totalwidth*w,y,w/8,h
'barrel
SetColor 100,50,50
DrawRect (x)+totalwidth*w,y+(h/1.5),w/3,h-(h/1.5)
End If
End Method
Method drawsideicebox(x:Int,y:Int,w:Int,h:Int,side:String)
If side = "left"
Local ltx:Float=x-w/2
Local lty:Float=y+(h/1.5)
Local rtx:Float=ltx+(w/2)
Local rty:Float=lty
Local lbx:Float=ltx
Local lby:Float=lty+(h-h/1.5)
Local rbx:Float=ltx+(w/2)
Local rby:Float=lby
SetColor 200,200,200
Local box:Float[8]
box[0] = ltx
box[1] = lty+(h/6)
box[2] = rtx
box[3] = rty
box[4] = rbx
box[5] = rby
box[6] = lbx
box[7] = lby
'DrawRect ltx,lty,w/2,h-(h/1.5)
DrawPoly(box)
Elseif side="right"
Local ltx:Float=(x)+(totalwidth*w)
Local lty:Float=y+(h/1.5)
Local rtx:Float=ltx+(w/2)
Local rty:Float=lty
Local rbx:Float=ltx+(w/2)
Local rby:Float=rty+(h-h/1.5)
Local lbx:Float=ltx
Local lby:Float=rby
SetColor 200,200,200
Local box:Float[8]
box[0] = ltx
box[1] = lty
box[2] = rtx
box[3] = rty+h/6
box[4] = rbx
box[5] = rby
box[6] = lbx
box[7] = lby
DrawPoly(box)
'DrawRect (x)+totalwidth*w,y+(h/1.5),w/2,h-(h/1.5)
End If
End Method
End Class
Class MyGame Extends App
Field mybuilding:List<building>
Field time:Int=Millisecs()
Field hw:Int=48,hh:Int=64
Method OnCreate()
Seed = GetDate[4]*GetDate[5]
SetUpdateRate(2)
makehouses()
End Method
Method OnUpdate()
If KeyHit(KEY_SPACE) Or Millisecs() > time
time=Millisecs()+2000
makehouses
End If
End Method
Method OnRender()
Cls 0,0,0
SetColor 50,155,255
DrawRect 0,0,DeviceWidth,150+hh
SetColor 50,125,235
DrawRect 0,100,DeviceWidth,(150+hh)-100
SetColor 5,250,5
DrawRect 0,150+hh,DeviceWidth,DeviceHeight-(150+hh)
SetColor 125,250,125
DrawRect 0,150+hh,DeviceWidth,2
For Local i:=Eachin mybuilding
i.draw(hw,hh)
Next
End Method
Method makehouses()
mybuilding = New List<building>
hw = Rnd(20,50)
hh = hw
Local st:Int=hw*4
Local x:Int=0
While x<DeviceWidth
Local z1:Bool
If Rnd(5)<1 Then z1 = True
Local w:Int = Rnd(1,4)
st = hw*(w+2)
' mybuilding = New building(x,150,Rnd(1,4),z1)
mybuilding.AddLast(New building(x,150,w,z1))
x+=st
Wend
End Method
End Class
Function Main()
New MyGame()
End Function
Saturday, November 4, 2017
Monkey-X - Generator - 2D Fantasy maps (zelda ish) - code example
' Based somewhat on http://www.squidi.net/three/entry.php?id=164
Import mojo
Class map
Field sw:Int,sh:Int
Field tw:Float,th:Float
Field mw:Int,mh:Int
Field map:Int[][]
Field grassmap:Int[][]
Field bridgemap:Int[][]
Field tileroad:Int=999
Field numzones:Int
Method New(sw:Int,sh:Int,mw:Int,mh:Int,numzones:Int)
Self.numzones = numzones
Self.sw = sw
Self.sh = sh
Self.mw = mw
Self.mh = mh
Self.tw = Float(sw) / Float(mw)
Self.th = Float(sh) / Float(mh)
map = New Int[mw][]
grassmap = New Int[mw][]
bridgemap = New Int[mw][]
For Local i:Int=0 Until mw
map[i] = New Int[mh]
grassmap[i] = New Int[mh]
bridgemap[i] = New Int[mh]
Next
createmap()
End Method
Method createmap()
'Create a number of zones (area's)
For Local zone:Int=0 Until numzones
map[Rnd(mw)][Rnd(mh)] = zone+1
Next
'Grow the zones
Local cangrow:Bool=True
Local mx:Int[]=[-1,0,1,0]
Local my:Int[]=[-1,0,0,1]
While cangrow=True
Local x:Int=Rnd(mw)
Local y:Int=Rnd(mh)
If map[x][y] > 0
Local tile:Int=map[x][y]
For Local i:Int=0 Until mx.Length
Local x2:Int=mx[i]+x
Local y2:Int=my[i]+y
If x2<0 Or y2<0 Or x2>=mw Or y2>=mh Then Continue
If Rnd(10)<2 And map[x2][y2] = 0
map[x2][y2] = tile
End If
Next
End If
' Every now and then check if every spot is taken
If Rnd(mw)<mw/10
cangrow = False
For Local y2:Int=0 Until mh
For Local x2:Int=0 Until mw
If map[x2][y2] = 0 Then cangrow = True;Exit
Next
Next
End If
Wend
' Create the roads
For Local y:Int=0 Until mh
For Local x:Int=0 Until mw
Local t:Int=map[x][y]
If x+1 < mw And map[x+1][y] <> t And grassmap[x+1][y] = 0 Then grassmap[x][y] = 1
If y+1 < mh And map[x][y+1] <> t And grassmap[x][y+1] = 0 Then grassmap[x][y] = 1
If x+1 < mw And y+1 < mh And map[x+1][y+1] <> t And grassmap[x+1][y+1] = 0 Then grassmap[x][y] = 1
Next
Next
'create the bridges
For Local y:Int=0 Until mh
For Local x:Int=0 Until mw
If grassmap[x][y] <> 1 Then Continue
Local cnt:Int=0
For Local y2:Int=y-1 To y+1
For Local x2:Int=x-1 To x+1
If x2<0 Or y2<0 Or x2>=mw Or y2>=mh Then Continue
If map[x2][y2] >= numzones/3 Then cnt+=1
Next
Next
If cnt=0 Then bridgemap[x][y] = 1
Next
Next
End Method
Method draw()
' map pass 1
For Local y:Int=0 Until mh
For Local x:Int=0 Until mw
If map[x][y] = 0 Then Continue
Local x2:Int = x*tw
Local y2:Int = y*th
If map[x][y] < numzones/3 Or map[x][y] = 1 Then
' water
SetColor 44,140,200
DrawRect x2,y2,tw+1,th+1
SetColor 46,165,225
DrawOval x2+tw/3,y2+th/5,tw/4,th/2
SetColor 56,195,255
DrawOval x2+tw/3,y2+th/4,tw/5,th/7
Else
'grass
SetColor 160,200,105
DrawRect x2,y2,tw+1,th+1
'treebase dark
SetColor 10,45,0
DrawOval x2+tw/3,y2+th/2,tw/2,th/1.5
'treebase light
SetColor 200,55,0
DrawOval x2+tw/2.2,y2+th/2,tw/4,th/1.5
' tree top dark
SetColor 0,55,0
DrawOval x2+tw/8,y2+th/10,tw/1.1,th/1.3
' tree top
SetColor 0,190,0
DrawOval x2+tw/8+1,y2+th/10,tw/1.2-2,th/1.3
' highlight top
SetColor 200,255,200
DrawOval x2+tw/4+1,y2+th/5,tw/3,th/3
End If
Next
Next
'grass
For Local y:Int=0 Until mh
For Local x:Int=0 Until mw
If grassmap[x][y] = 0 Then Continue
Local x2:Int = x*tw
Local y2:Int = y*th
SetColor 80,230,20
DrawRect x2,y2,tw+1,th+1
Next
Next
'bridges
For Local y:Int=0 Until mh
For Local x:Int=0 Until mw
If bridgemap[x][y] = 0 Then Continue
Local x2:Int = x*tw
Local y2:Int = y*th
SetColor 150,120,15
DrawRect x2,y2,tw+1,th+1
'plank
SetColor 200,170,20
DrawRect x2+tw/10,y2,tw/3,th+1
DrawRect x2+tw/1.8,y2,tw/3,th+1
'plank shadow and light
SetColor 240,200,200
DrawRect x2+tw/10,y2,1,th+1
DrawRect x2+tw/1.8,y2,1,th+1
SetColor 0,0,0
DrawRect x2+tw/10+tw/3,y2,1,th+1
DrawRect x2+tw/1.8+tw/3,y2,1,th+1
Next
Next
' map pass 2 (shadow under trees/grass and highlight up water)
For Local y:Int=1 Until mh
For Local x:Int=0 Until mw
If map[x][y] = 0 Then Continue
Local x2:Int = x*tw
Local y2:Int = y*th
'shadow under trees
If grassmap[x][y] = 1 And grassmap[x][y-1] = 0 And map[x][y] > numzones/3
SetColor 40,160,30
DrawRect x2+tw/4,y2,tw-th/2,th/3
End If
'shadow under water
If grassmap[x][y] = 0 And grassmap[x][y-1] = 1 And map[x][y] < numzones/3
SetColor 0,0,0
DrawRect x2,y2,tw+1,th/8
End If
Next
Next
End Method
End Class
Class MyGame Extends App
Field refresh:Int
Field mymap:map
Field plumps
Method OnCreate()
SetUpdateRate(1)
Seed = GetDate[4] * GetDate[5]
Local s:Int=Rnd(30,100)
If s<40 Then plumps = s/1.5 Else plumps = s*2
mymap = New map(DeviceWidth,DeviceHeight,s,s,plumps)
refresh = Millisecs()+3000
End Method
Method OnUpdate()
If Millisecs() > refresh
Local s:Float=Rnd(30,100)
If Rnd(10)<8 Then s = Rnd(20,40)
If s<40 Then plumps = s/1.5 Else plumps = s*2
mymap = New map(DeviceWidth,DeviceHeight,s,s,plumps)
refresh = Millisecs()+3000
End If
End Method
Method OnRender()
Cls 0,0,0
SetColor 255,255,255
mymap.draw()
End Method
End Class
Function Main()
New MyGame()
End Function
Subscribe to:
Posts (Atom)