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.
Github link
Import mojo Class theimage Field image:Image Field pixels:Int[] Field width:Int Field height:Int Method New() Self.width = DeviceWidth() Self.height = DeviceHeight() pixels = New Int[width*height] image = CreateImage(width,height) makeimage image.WritePixels(pixels, 0, 0, width, height, 0) End Method Method makeimage() ' here we make the ' noisy rainbow Local x:Int Local ty:Int Local cnt:Int Local sc:Int 'dheight with num of cols For Local i:Int = 0 Until width*height Local col:Int=sc If sc<15 If Rnd((16-ty)+4) < 2 col+=1 End If End If pixels[i] = argb( spread.r[col], spread.g[col], spread.b[col]) x+=1 If x>width x=0 cnt+=1 ty+=1 If ty>height/16 ty=0 End If If cnt>height/16 cnt=0 sc+=1 End If End If Next For Local ii=0 Until 20 For Local i:Int = 0 Until width*(height-1) Local r1:Int=getred(pixels[i]) Local g1:Int=getgreen(pixels[i]) Local b1:Int=getblue(pixels[i]) Local r2:Int=getred(pixels[i+1]) Local g2:Int=getgreen(pixels[i+1]) Local b2:Int=getblue(pixels[i+1]) Local r3:Int=getred(pixels[i+width]) Local g3:Int=getgreen(pixels[i+width]) Local b3:Int=getblue(pixels[i+width]) Local ra:Int=(r1+r2+r3)/3 Local ga:Int=(g1+g2+g3)/3 Local ba:Int=(b1+b2+b3)/3 pixels[i] = argb(ra,ga,ba) Next next End Method End Class Class colorspread16 Field r:Float[16] Field g:Float[16] Field b:Float[16] Method New( r1:Int,g1:Int,b1:Int, r2:Int,g2:Int,b2:Int) Local sr:Float Local sg:Float Local sb:Float If r1>r2 sr = (r1-r2)/16 For Local i:Float=0 Until 16 Step 1 r[i] = r1-(i*sr) Next Else sr = (r2-r1)/16 For Local i:Float=0 Until 16 Step 1 r[i] = r1+i*sr Next End If If g1>g2 sg = (g1-g2)/16 For Local i:Float=0 Until 16 Step 1 g[i] = g1-(i*sg) Next Else sg = (g2-g1)/16 For Local i:Float=0 Until 16 Step 1 g[i] = g1+i*sg Next End If If b1>b2 sb = (b1-b2)/16 For Local i:Float=0 Until 16 Step 1 b[i] = b1-(i*sb) Next Else sb = (b2-b1)/16 For Local i:Float=0 Until 16 Step 1 b[i] = b1+i*sb Next End If End Method End Class Global spread:colorspread16 Global myimage:theimage Class MyGame Extends App Field cnt:Int Field r:Int[2] Field g:Int[2] Field b:Int[2] Method OnCreate() SetUpdateRate(60) Seed = GetDate[5] For Local i=0 To 1 r[i] = Rnd(0,255) g[i] = Rnd(0,255) b[i] = Rnd(0,255) Next spread = New colorspread16( r[0],g[0],b[0], r[1],g[1],b[1]) myimage = New theimage() End Method Method OnUpdate() cnt+=1 If cnt>200 cnt=0 For Local i=0 To 1 r[i] = Rnd(0,255) g[i] = Rnd(0,255) b[i] = Rnd(0,255) Next spread = New colorspread16( r[0],g[0],b[0], r[1],g[1],b[1]) myimage = New theimage() End If End Method Method OnRender() Cls 0,0,0 SetColor 255,255,255 DrawImage myimage.image,0,0 ' Draw the text SetColor 255,255,255 DrawText "Monkey-X - Making Noisy Smoothed "+ " Rainbows - Example",0,0 End Method End Class Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255) Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b End Function Function getred:Int(rgba:Int) Return((rgba Shr 16) & $FF) End Function Function getgreen:Int(rgba:Int) Return((rgba Shr 8) & $FF) End Function Function getblue:Int(rgba:Int) Return(rgba & $FF) End Function Function getalpha:Int(rgba:Int) Return ((rgba Shr 24) & $FF) End Function Function Main() New MyGame() End Function
github page
Import mojo Class theimage Field image:Image Field pixels:Int[] Field width:Int Field height:Int Method New() Self.width = DeviceWidth() Self.height = DeviceHeight() pixels = New Int[width*height] image = CreateImage(width,height) makeimage image.WritePixels(pixels, 0, 0, width, height, 0) End Method Method makeimage() ' here we make the ' noisy rainbow Local x:Int Local ty:Int Local cnt:Int Local sc:Int 'dheight with num of cols For Local i:Int = 0 Until width*height Local col:Int=sc If sc<15 If Rnd((16-ty)+4) < 2 col+=1 End If End If pixels[i] = argb( spread.r[col], spread.g[col], spread.b[col]) x+=1 If x>width x=0 cnt+=1 ty+=1 If ty>height/16 ty=0 End If If cnt>height/16 cnt=0 sc+=1 End If End If Next End Method End Class Class colorspread16 Field r:Float[16] Field g:Float[16] Field b:Float[16] Method New( r1:Int,g1:Int,b1:Int, r2:Int,g2:Int,b2:Int) Local sr:Float Local sg:Float Local sb:Float If r1>r2 sr = (r1-r2)/16 For Local i:Float=0 Until 16 Step 1 r[i] = r1-(i*sr) Next Else sr = (r2-r1)/16 For Local i:Float=0 Until 16 Step 1 r[i] = r1+i*sr Next End If If g1>g2 sg = (g1-g2)/16 For Local i:Float=0 Until 16 Step 1 g[i] = g1-(i*sg) Next Else sg = (g2-g1)/16 For Local i:Float=0 Until 16 Step 1 g[i] = g1+i*sg Next End If If b1>b2 sb = (b1-b2)/16 For Local i:Float=0 Until 16 Step 1 b[i] = b1-(i*sb) Next Else sb = (b2-b1)/16 For Local i:Float=0 Until 16 Step 1 b[i] = b1+i*sb Next End If End Method End Class Global spread:colorspread16 Global myimage:theimage Class MyGame Extends App Field cnt:Int Field r:Int[2] Field g:Int[2] Field b:Int[2] Method OnCreate() SetUpdateRate(60) Seed = GetDate[5] For Local i=0 To 1 r[i] = Rnd(0,255) g[i] = Rnd(0,255) b[i] = Rnd(0,255) Next spread = New colorspread16( r[0],g[0],b[0], r[1],g[1],b[1]) myimage = New theimage() End Method Method OnUpdate() cnt+=1 If cnt>200 cnt=0 For Local i=0 To 1 r[i] = Rnd(0,255) g[i] = Rnd(0,255) b[i] = Rnd(0,255) Next spread = New colorspread16( r[0],g[0],b[0], r[1],g[1],b[1]) myimage = New theimage() End If End Method Method OnRender() Cls 0,0,0 SetColor 255,255,255 DrawImage myimage.image,0,0 ' Draw the text SetColor 255,255,255 DrawText "Monkey-X - Making images from "+ "16 colors rainbow palette -"+ " (Noisy Rainbow) - Example",0,0 End Method End Class Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255) Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b End Function Function Main() New MyGame() End Function
Github page
Import mojo Class theimage Field image:Image Field pixels:Int[] Field width:Int Field height:Int Method New() Self.width = DeviceWidth() Self.height = DeviceHeight() pixels = New Int[width*height] image = CreateImage(width,height) makeimage image.WritePixels(pixels, 0, 0, width, height, 0) End Method Method makeimage() For Local i:Int = 0 Until width*height Local col:Int=Rnd(0,16) pixels[i] = argb( spread.r[col], spread.g[col], spread.b[col]) Next End Method End Class Class colorspread16 Field r:Float[16] Field g:Float[16] Field b:Float[16] Method New( r1:Int,g1:Int,b1:Int, r2:Int,g2:Int,b2:Int) Local sr:Float Local sg:Float Local sb:Float If r1>r2 sr = (r1-r2)/16 For Local i:Float=0 Until 16 Step 1 r[i] = r1-(i*sr) Next Else sr = (r2-r1)/16 For Local i:Float=0 Until 16 Step 1 r[i] = r1+i*sr Next End If If g1>g2 sg = (g1-g2)/16 For Local i:Float=0 Until 16 Step 1 g[i] = g1-(i*sg) Next Else sg = (g2-g1)/16 For Local i:Float=0 Until 16 Step 1 g[i] = g1+i*sg Next End If If b1>b2 sb = (b1-b2)/16 For Local i:Float=0 Until 16 Step 1 b[i] = b1-(i*sb) Next Else sb = (b2-b1)/16 For Local i:Float=0 Until 16 Step 1 b[i] = b1+i*sb Next End If End Method End Class Global spread:colorspread16 Global myimage:theimage Class MyGame Extends App Field cnt:Int Field r:Int[2] Field g:Int[2] Field b:Int[2] Method OnCreate() SetUpdateRate(60) Seed = GetDate[5] For Local i=0 To 1 r[i] = Rnd(0,255) g[i] = Rnd(0,255) b[i] = Rnd(0,255) Next spread = New colorspread16( r[0],g[0],b[0], r[1],g[1],b[1]) myimage = New theimage() End Method Method OnUpdate() cnt+=1 If cnt>30 cnt=0 For Local i=0 To 1 r[i] = Rnd(0,255) g[i] = Rnd(0,255) b[i] = Rnd(0,255) Next spread = New colorspread16( r[0],g[0],b[0], r[1],g[1],b[1]) myimage = New theimage() End If End Method Method OnRender() Cls 0,0,0 SetColor 255,255,255 DrawImage myimage.image,0,0 ' Draw the text SetColor 255,255,255 DrawText "Monkey-X - Making images from "+ "16 colors rainbow palette -"+ " Example",0,0 DrawText "R1:"+r[0]+ " G1:"+g[0]+ " B1:"+b[0],0,20 DrawText "R2:"+r[1]+ " G2:"+g[1]+ " B2:"+b[1],200,20 End Method End Class Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255) Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b End Function Function Main() New MyGame() End Function
Import mojo Class colorspread16 Field r:Float[16] Field g:Float[16] Field b:Float[16] Method New( r1:Int,g1:Int,b1:Int, r2:Int,g2:Int,b2:Int) Local sr:Float Local sg:Float Local sb:Float If r1>r2 sr = (r1-r2)/16 For Local i:Float=0 Until 16 Step 1 r[i] = r1-(i*sr) Next Else sr = (r2-r1)/16 For Local i:Float=0 Until 16 Step 1 r[i] = r1+i*sr Next End If If g1>g2 sg = (g1-g2)/16 For Local i:Float=0 Until 16 Step 1 g[i] = g1-(i*sg) Next Else sg = (g2-g1)/16 For Local i:Float=0 Until 16 Step 1 g[i] = g1+i*sg Next End If If b1>b2 sb = (b1-b2)/16 For Local i:Float=0 Until 16 Step 1 b[i] = b1-(i*sb) Next Else sb = (b2-b1)/16 For Local i:Float=0 Until 16 Step 1 b[i] = b1+i*sb Next End If End Method End Class Global spread:colorspread16 Class MyGame Extends App Field cnt:Int Field r:Int[2] Field g:Int[2] Field b:Int[2] Method OnCreate() SetUpdateRate(60) Seed = GetDate[5] For Local i=0 To 1 r[i] = Rnd(0,255) g[i] = Rnd(0,255) b[i] = Rnd(0,255) Next spread = New colorspread16( r[0],g[0],b[0], r[1],g[1],b[1]) End Method Method OnUpdate() cnt+=1 If cnt>200 cnt=0 For Local i=0 To 1 r[i] = Rnd(0,255) g[i] = Rnd(0,255) b[i] = Rnd(0,255) Next spread = New colorspread16( r[0],g[0],b[0], r[1],g[1],b[1]) End If End Method Method OnRender() Cls 0,0,0 SetColor 255,255,255 ' Draw the background For Local i=0 Until 16 SetColor spread.r[i], spread.g[i], spread.b[i] DrawRect i*(DeviceWidth/16),0, (DeviceWidth/16),DeviceHeight Next ' Draw the gui part Local c:Int=0 For Local y=0 Until 8 For Local x=0 To 1 SetColor 0,0,0 DrawRect x*34+100, y*34+100, 34,34 SetColor spread.r[c], spread.g[c], spread.b[c] DrawRect x*34+101, y*34+101, 32,32 c+=1 Next Next ' Draw the text SetColor 255,255,255 DrawText "Monkey-X - Spreading 2 colors "+ "out into 16 colors - Example",0,0 DrawText "R1:"+r[0]+ " G1:"+g[0]+ " B1:"+b[0],100,50 DrawText "R2:"+r[1]+ " G2:"+g[1]+ " B2:"+b[1],100,70 End Method End Class Function Main() New MyGame() End Function
Github link,
Import mojo Class game Field px:Int,py:Int Field pw:Int,ph:Int Field mapwidth:Int Field mapheight:Int Field tilewidth:Int Field tileheight:Int Field map:Int[][] Method New(width:Int,height:Int) mapwidth = width mapheight = height tilewidth = DeviceWidth()/mapwidth tileheight = DeviceHeight()/mapheight map = New Int[mapwidth][] For Local i=0 Until mapwidth map[i] = New Int[mapheight] Next For Local i=0 Until 10 Local x:Int=Rnd(0,mapwidth) Local y:Int=Rnd(0,mapheight) map[x][y] = 1 Next pw = 16 ph = 24 Local exitloop:Bool=False While exitloop=False Local x:Int=Rnd(0,mapwidth) Local y:Int=Rnd(0,mapheight) If map[x][y] <> 1 px = x*tilewidth py = y*tileheight exitloop = True End If Wend End Method Method playerupdate() If KeyDown(KEY_LEFT) px-=1 End If If KeyDown(KEY_RIGHT) px+=1 End If If KeyDown(KEY_UP) py-=1 End If If KeyDown(KEY_DOWN) py+=1 End If px = Clamp(px,0,DeviceWidth()-pw) py = Clamp(py,ph,DeviceHeight()-ph) End Method Method draw() For Local y=0 Until mapheight For Local x=0 Until mapwidth Local dx:Int=x*tilewidth Local dy:Int=y*tileheight If map[x][y] = 1 SetColor 100,100,0 DrawRect dx,dy,tilewidth,tileheight drawtree dx+16,dy-20 End If Next ' Here we find if we should draw the player ' as to have the trees be correct with ' his location on the screen. If rectsoverlap( px,py-32,pw,ph, 0, y*tileheight, DeviceWidth(), tileheight) = True ' here we draw the player SetColor 255,255,0 DrawRect px,py,pw,ph End If Next End Method Method drawtree(x:Int,y:Int) SetColor 0,200,0 DrawRect x-32,y-32,64,64 SetColor 200,100,0 DrawRect x-10,y+32,20,10 End Method End Class Global mygame:game Class MyGame Extends App Method OnCreate() SetUpdateRate(60) Seed = GetDate[5] mygame = New game(20,15) End Method Method OnUpdate() mygame.playerupdate End Method Method OnRender() Cls 0,0,0 mygame.draw SetColor 255,255,255 DrawText "Monkey-X - 2d Player "+ "behind And infront of"+ " trees - code example", 0,10 DrawText "Press Cursor + left-"+ "right-up-down",0, DeviceHeight()-20 End Method End Class Function Main() New MyGame() End Function Function rectsoverlap:Bool(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int) If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then Return False If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then Return False Return True End Function
Different targets seem to have different performance. Paste in monkey and see.
Import mojo Class theimage Field x:Int Field y:Int Field image:Image Field pixels:Int[] Field width:Int Field height:Int Method New( x:Int,y:Int, width:Int, height:Int) pixels = New Int[width*height] image = CreateImage(width,height) Self.x = x Self.y = y Self.width = width Self.height = height makeimage image.WritePixels(pixels, 0, 0, width, height, 0) End Method Method makeimage() For Local i:Int = 0 Until width*height Local col:Int=Rnd(255) pixels[i] = argb(col,col,col) Next End Method Method draw() DrawImage image,x,y End Method End Class Global im1:theimage Class MyGame Extends App Method OnCreate:Int() SetUpdateRate(60) Seed = GetDate[5] im1 = New theimage(10,20,620,400) End Method Method OnUpdate() im1 = New theimage(10,20,620,400) End Method Method OnRender:Int() Cls im1.draw SetColor 255,255,255 DrawText "Monkey-X - Pixel arrays"+ " speedtest ",0,0 DrawText "620*400 image with random "+ "grey pixels on it being "+ "opened in a loop at "+ "60 frames per second.",0,420 End Method End Class Function Main:Int() New MyGame() End Function Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255) Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b End Function Function getred:Int(rgba:Int) Return((rgba Shr 16) & $FF) End Function Function getgreen:Int(rgba:Int) Return((rgba Shr 8) & $FF) End Function Function getblue:Int(rgba:Int) Return(rgba & $FF) End Function Function getalpha:Int(rgba:Int) Return ((rgba Shr 24) & $FF) End Function
Click here for the article
Import mojo Class theimage Field x:Int Field y:Int Field image:Image Field pixels:Int[] Field width:Int Field height:Int Method New( x:Int,y:Int, width:Int, height:Int) pixels = New Int[width*height] image = CreateImage(width,height) Self.x = x Self.y = y Self.width = width Self.height = height makeimage image.WritePixels(pixels, 0, 0, width, height, 0) End Method Method makeimage() Seed = 10 For Local i:Int = 0 Until width*height Local col:Int=Rnd(255) pixels[i] = argb(col,col,col) Next End Method Method sineplasmaimage(set:Int) Local x:Int,y:Int For Local i:Int = 0 Until width*height If x>width-1 x=0 y+=1 End If Local c:Float c = 127+63.5*Sin(x*set)+ 63.5*Sin(y*set) pixels[i] = argb(c,c,c) x+=1 Next image.WritePixels(pixels, 0, 0, width, height, 0) End Method Method draw() DrawImage image,x,y End Method End Class Global im1:theimage Global im2:theimage Class MyGame Extends App Field cnt:Int Field passim:Int Method OnCreate:Int() SetUpdateRate(60) Seed = GetDate[5] im1 = New theimage(10,20,200,200) im2 = New theimage(330,20,200,200) End Method Method OnUpdate() cnt+=1 If cnt>10 im2.sineplasmaimage(passim) cnt=0 passim+=1 End If If passim > 20 im2 = New theimage(330,20,200,200) passim = 0 End If End Method Method OnRender:Int() Cls im1.draw im2.draw SetColor 255,255,255 DrawText "Monkey-X - SinePlasma(Hugi) on"+ " Random Greyscale Image (pixels).",0,0 DrawText "modifier : "+ passim+ " of 20",10,400 End Method End Class Function Main:Int() New MyGame() End Function Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255) Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b End Function Function getred:Int(rgba:Int) Return((rgba Shr 16) & $FF) End Function Function getgreen:Int(rgba:Int) Return((rgba Shr 8) & $FF) End Function Function getblue:Int(rgba:Int) Return(rgba & $FF) End Function Function getalpha:Int(rgba:Int) Return ((rgba Shr 24) & $FF) End Function
Link to original Hugi article
Import mojo Class theimage Field x:Int Field y:Int Field image:Image Field pixels:Int[] Field width:Int Field height:Int Method New( x:Int,y:Int, width:Int, height:Int) pixels = New Int[width*height] image = CreateImage(width,height) Self.x = x Self.y = y Self.width = width Self.height = height makeimage image.WritePixels(pixels, 0, 0, width, height, 0) End Method Method makeimage() Seed = 10 For Local i:Int = 0 Until width*height Local col:Int=Rnd(255) pixels[i] = argb(col,col,col) Next End Method Method smoothimage() For Local i:Int = 0 Until width*height Local rpix:Int=i+1 Local bpix:Int=i+width Local cpix:Int=i ' read right, bottom and current If bpix<width*height And rpix<width*height Local r1:Int = getred(pixels[cpix]) Local g1:Int = getgreen(pixels[cpix]) Local b1:Int = getblue(pixels[cpix]) Local r2:Int = getred(pixels[rpix]) Local g2:Int = getgreen(pixels[rpix]) Local b2:Int = getblue(pixels[rpix]) Local r3:Int = getred(pixels[bpix]) Local g3:Int = getgreen(pixels[bpix]) Local b3:Int = getblue(pixels[bpix]) Local nr:Int,ng:Int,nb:Int 'avarage nr = (r1+r2+r3)/3 ng = (g1+g2+g3)/3 nb = (b1+b2+b3)/3 pixels[i] = argb(nr,ng,nb) End If Next image.WritePixels(pixels, 0, 0, width, height, 0) End Method Method draw() DrawImage image,x,y End Method End Class Global im1:theimage Global im2:theimage Class MyGame Extends App Field cnt:Int Field passim:Int Method OnCreate:Int() SetUpdateRate(60) Seed = GetDate[5] im1 = New theimage(10,20,200,200) im2 = New theimage(330,20,200,200) End Method Method OnUpdate() cnt+=1 If cnt>10 im2.smoothimage cnt=0 passim+=1 End If If passim > 80 im2 = New theimage(330,20,200,200) passim = 0 End If End Method Method OnRender:Int() Cls im1.draw im2.draw SetColor 255,255,255 DrawText "Monkey-X - Smooth(Hugi) on"+ " Random Greyscale Image (pixels).",0,0 DrawText "Pass : "+ passim+ " of 80",10,400 End Method End Class Function Main:Int() New MyGame() End Function Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255) Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b End Function Function getred:Int(rgba:Int) Return((rgba Shr 16) & $FF) End Function Function getgreen:Int(rgba:Int) Return((rgba Shr 8) & $FF) End Function Function getblue:Int(rgba:Int) Return(rgba & $FF) End Function Function getalpha:Int(rgba:Int) Return ((rgba Shr 24) & $FF) End Function
Import mojo Class theimage Field x:Int Field y:Int Field image:Image Field pixels:Int[] Field width:Int Field height:Int Method New( x:Int,y:Int, width:Int, height:Int) pixels = New Int[width*height] image = CreateImage(width,height) Self.x = x Self.y = y Self.width = width Self.height = height makeimage image.WritePixels(pixels, 0, 0, width, height, 0) End Method Method makeimage() Seed = 10 For Local i:Int = 0 Until width*height Local col:Int=Rnd(255) pixels[i] = argb(Rnd(255),Rnd(255),Rnd(255)) Next End Method Method effectimage() For Local i:Int = 0 Until width*height Local r:Int = getred(pixels[i]) Local g:Int = getgreen(pixels[i]) Local b:Int = getblue(pixels[i]) r=r*r/256 g=g*g/256 b=b*b/256 r = Clamp(r,0,255) g = Clamp(g,0,255) b = Clamp(b,0,255) pixels[i] = argb(r,g,b) Next image.WritePixels(pixels, 0, 0, width, height, 0) End Method Method draw() DrawImage image,x,y End Method End Class Global im1:theimage Global im2:theimage Class MyGame Extends App Field cnt:Int Field resetim:Int Method OnCreate:Int() SetUpdateRate(60) im1 = New theimage(10,20,200,200) im2 = New theimage(330,20,200,200) ' im2.effectimage() End Method Method OnUpdate() cnt+=1 resetim+=1 If cnt>10 Then im2.effectimage() cnt=0 End If If resetim > 100 im2 = New theimage(330,20,200,200) resetim=0 End If End Method Method OnRender:Int() Cls im1.draw im2.draw SetColor 255,255,255 DrawText "Monkey-X - Effect(Hugi) on Image (pixels).",0,0 End Method End Class Function Main:Int() New MyGame() End Function Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255) Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b End Function Function getred:Int(rgba:Int) Return((rgba Shr 16) & $FF) End Function Function getgreen:Int(rgba:Int) Return((rgba Shr 8) & $FF) End Function Function getblue:Int(rgba:Int) Return(rgba & $FF) End Function Function getalpha:Int(rgba:Int) Return ((rgba Shr 24) & $FF) End Function
Tinting an image is adding the color r,g,b to the original r,g,b value.
Import mojo Class theimage Field x:Int Field y:Int Field image:Image Field pixels:Int[] Field width:Int Field height:Int Method New( x:Int,y:Int, width:Int, height:Int) pixels = New Int[width*height] image = CreateImage(width,height) Self.x = x Self.y = y Self.width = width Self.height = height makeimage image.WritePixels(pixels, 0, 0, width, height, 0) End Method Method makeimage() Seed = 10 For Local i:Int = 0 Until width*height Local col:Int=Rnd(255) pixels[i] = argb(col,col,col) Next End Method Method tintimage(r:Int,g:Int,b:Int) makeimage For Local i:Int = 0 Until width*height Local r2:Int = getred(pixels[i]) Local g2:Int = getgreen(pixels[i]) Local b2:Int = getblue(pixels[i]) r2+=r g2+=g b2+=b r2 = Clamp(r2,0,255) g2 = Clamp(g2,0,255) b2 = Clamp(b2,0,255) pixels[i] = argb(r2,g2,b2) Next image.WritePixels(pixels, 0, 0, width, height, 0) End Method Method draw() DrawImage image,x,y End Method End Class Global im1:theimage Global im2:theimage Class MyGame Extends App Field tintred:Int = 100 Field tintgreen:Int = 0 Field tintblue:Int = 0 Method OnCreate:Int() SetUpdateRate(60) im1 = New theimage(10,20,200,200) im2 = New theimage(330,20,200,200) ' ' Here we tint the image ' im2.tintimage(tintred,tintgreen,tintblue) End Method Method OnUpdate() If Rnd(320)<2 DebugLog Millisecs() Seed = Millisecs() tintred = Rnd(0,255) tintgreen = Rnd(0,255) tintblue = Rnd(0,255) im2.tintimage(tintred,tintgreen,tintblue) End If End Method Method OnRender:Int() Cls im1.draw im2.draw SetColor 255,255,255 DrawText "Monkey-X - Tinting a image (pixels).",0,0 DrawText "Red: "+tintred+ " Green: "+tintgreen+ " BLue: "+tintblue, 10,300 SetColor tintred,tintgreen,tintblue DrawRect 400,300,50,50 End Method End Class Function Main:Int() New MyGame() End Function Function argb:Int(r:Int, g:Int, b:Int ,alpha:Int=255) Return (alpha Shl 24) | (r Shl 16) | (g Shl 8) | b End Function Function getred:Int(rgba:Int) Return((rgba Shr 16) & $FF) End Function Function getgreen:Int(rgba:Int) Return((rgba Shr 8) & $FF) End Function Function getblue:Int(rgba:Int) Return(rgba & $FF) End Function Function getalpha:Int(rgba:Int) Return ((rgba Shr 24) & $FF) End Function
Import mojo Class themap Field map:Int[][] Field mapwidth:Int Field mapheight:Int Field tilewidth:Float Field tileheight:Float Method New( mapwidth:Int, mapheight:Int) tilewidth = DeviceWidth()/Float(mapwidth) tileheight = DeviceHeight()/Float(mapheight) Self.mapwidth = mapwidth Self.mapheight = mapheight map = New Int[mapwidth][] For Local i = 0 Until mapwidth map[i] = New Int[mapheight] Next makemap End Method Method makemap() Local x:Int=mapwidth/2 Local y:Int=mapheight/2 Local dx:Int[]=[0,1,0,-1] Local dy:Int[]=[-1,0,1,0] For Local i=0 To mapwidth*mapheight Local m : Int = map[x][y]+16 If m>255 Then m=255 map[x][y] = m Local d:Int=Rnd(0,4) x+=dx[d] y+=dy[d] If x<1 Or y<1 Or x>mapwidth-3 Or y>mapheight-3 Then x=mapwidth/2 y=mapheight/2 End If Next End Method Method draw() For Local y=0 Until mapheight For Local x=0 Until mapwidth Local col:Int=map[x][y] If col > 0 SetColor col,0,0 DrawRect x*tilewidth, y*tileheight, tilewidth+1, tileheight+1 end if Next Next End Method End Class Global mymap:themap Class MyGame Extends App Method OnCreate() SetUpdateRate(60) Local date := GetDate() Seed = date[5] mymap = New themap(320,256) End Method Method OnUpdate() If KeyHit(KEY_SPACE) = True mymap = New themap(320,356) End If End Method Method OnRender() Cls 0,0,0 mymap.draw SetColor 255,255,255 DrawText "Monkey-X - Red Clouds Example.", 0,0 DrawText "Press Space to Render new",0,20 End Method End Class Function Main() New MyGame() End Function
Import mojo Class waitanim Field angle:Int Field x:Int,y:Int Field size:Int Method New(x:Int,y:Int,size:Int) Self.x = x Self.y = y Self.size = size End Method Method update() angle+=4 If angle>359 Then angle=0 End Method Method draw() SetColor 150,150,150 For Local i=0 To 360 DrawOval x+(Cos(i)*size), y+(Sin(i)*size),4,4 Next SetColor 220,220,220 For Local i=0 To 48 Local da:Int=angle+i If da>359 Then da = da-359 DrawOval x+(Cos(da)*size), y+(Sin(da)*size),6,6 Next End Method End Class Global mywait:waitanim Class MyGame Extends App Method OnCreate() SetUpdateRate(60) mywait = New waitanim( DeviceWidth/2, DeviceHeight/2, 24) End Method Method OnUpdate() mywait.update End Method Method OnRender() Cls 0,0,0 SetColor 255,255,255 DrawText "Wait/Busy Animation "+ "thing - code example.", 0,0 mywait.draw End Method End Class Function Main() New MyGame() End function
What the code does is tunnel through the map and sometimes store a coord from the path.
This location then after a amount of steps gets a room placed on top. The blurring can be
1, 2 or 3. 1 having no edges, just lines and rooms. and 3 going around to each cell and
placing a lower value if possible. This creates the look of caverns.
Import mojo Class themap Field x:Int Field y:Int Field mapwidth:Int Field mapheight:Int Field tilewidth:Float Field tileheight:Float Field map:Int[][] 'this holds the offsets the we 'use to tunnel. 0=upwards;1=right ect 'use like : x+=dx[0] Field dx:Int[]=[0,1,0,-1] Field dy:Int[]=[-1,0,1,0] Field blur:Int Method New( mapwidth:Int, mapheight:Int, blur:Int) Self.mapwidth = mapwidth Self.mapheight = mapheight Self.blur = blur tilewidth = DeviceWidth()/mapwidth tileheight = DeviceHeight()/mapheight x = mapwidth/2 y = mapheight/2 map = New Int[mapwidth][] For Local i = 0 Until mapwidth map[i] = New Int[mapheight] Next tunnel() mapblur() End Method Method mapblur() For Local i=0 To mapwidth*mapheight Local x1:Int=Rnd(2,mapwidth-4) Local y1:Int=Rnd(2,mapheight-4) If map[x1][y1] > 0 For Local y2=y1-1 To y1+1 For Local x2=x1-1 To x1+1 If map[x2][y2] = 0 map[x2][y2] = map[x1][y1] / 2 End If Next Next End If Next End Method Method tunnel() ' direction of tunneling Local dir:Int=1 'make room countdown Local mrt:Int=10 ' location on the path ' where next room is to be made Local rx:Int,ry:Int For Local i=0 Until ((mapwidth*mapheight)/10) map[x][y] = blur If Rnd(0,5) < 2 mrt-=1 dir = newdir(dir) If mrt = 0 If rx>3 And ry>3 If rx<mapwidth-3 And ry<mapheight-3 For Local y1 = ry-3 To ry+3 For Local x1 = rx-3 To rx+3 map[x1][y1] = blur Next Next End If End If mrt=10 End If If Rnd(0,20)<2 Or mrt=0 rx = x ry = y mrt = 10 End If End If If x>3 And x<mapwidth-3 And y>3 And y<mapheight-3 Then x+=dx[dir] y+=dy[dir] Else x = mapwidth/2 y = mapheight/2 End If Next End Method Method newdir:Int(dir:Int) Local exitloop:Bool=False Local cnt:Int=0 While exitloop=False Local d:Int=Rnd(0,4) cnt+=1 If cnt>500 Then Return 0 If d<>dir ' is nothing ahead taken If map[x+dx[d]][y+dy[d]] = 0 If map[x+(dx[d]*2)][y+(dy[d]*2)] = 0 ' do not go in opposite direction Select d Case 0 If dir<>2 Then Return d End If Case 1 If dir<>3 Then Return d End If Case 2 If dir<>0 Then Return d End If Case 3 If dir<>1 Then Return d End If End Select End If End If End If Wend End Method Method draw() For Local y=0 Until mapheight For Local x=0 Until mapwidth If map[x][y] > 0 Local d:Int=distance( mapwidth/2, mapheight/2, x, y) Local col:Int=0+(1500/(d+1)) SetColor col,col,col DrawRect x*tilewidth, y*tileheight, tilewidth-1, tileheight-1 End If Next Next End Method Method distance:Int(x1:Int,y1:Int,x2:Int,y2:Int) Return Abs(x2-x1)+Abs(y2-y1) End Method End Class Global mymap:themap Class MyGame Extends App Field cnt:Int Field mw:Int Field mh:Int Field bl:Int Method OnCreate() SetUpdateRate(60) Local date := GetDate() Seed = date[5] mymap = New themap(Rnd(32,150),Rnd(32,150),Rnd(1,10)) End Method Method OnUpdate() cnt+=1 If cnt>200 mw = Rnd(32,150) mh = Rnd(32,150) 'the blurring(edging) bl = Rnd(1,4) mymap = New themap(mw,mh,bl) cnt=0 End If End Method Method OnRender() Cls 0,0,0 mymap.draw SetColor 255,255,255 DrawText "Mapw:"+mw+ " Maph:"+mh+" Blur:"+ bl,0,20 DrawText "Map gen - Tunneling and "+ "path room placement and "+ "blurring.",0,0 End Method End Class Function Main() New MyGame() End Function
I read through a article and it mentioned prefabs and tunneling. I made this
to see if I could do anything new. Not very special or so. It basically tunnels in a
random direction and sometimes stores a position on the path where it after a number of
loops places a room.
Import mojo Class themap Field x:Int Field y:Int Field mapwidth:Int Field mapheight:Int Field tilewidth:Float Field tileheight:Float Field map:Int[][] 'this holds the offsets the we 'use to tunnel. 0=upwards;1=right ect 'use like : x+=dx[0] Field dx:Int[]=[0,1,0,-1] Field dy:Int[]=[-1,0,1,0] Method New( mapwidth:Int, mapheight:Int) Self.mapwidth = mapwidth Self.mapheight = mapheight tilewidth = DeviceWidth()/mapwidth tileheight = DeviceHeight()/mapheight x = mapwidth/2 y = mapheight/2 map = New Int[mapwidth][] For Local i = 0 Until mapwidth map[i] = New Int[mapheight] Next tunnel() End Method Method tunnel() ' direction of tunneling Local dir:Int=1 'make room countdown Local mrt:Int=10 ' location on the path ' where next room is to be made Local rx:Int,ry:Int For Local i=0 Until ((mapwidth*mapheight)/10) map[x][y]=1 If Rnd(0,5) < 2 mrt-=1 dir = newdir(dir) If mrt = 0 If rx>3 And ry>3 If rx<mapwidth-3 And ry<mapheight-3 For Local y1 = ry-3 To ry+3 For Local x1 = rx-3 To rx+3 map[x1][y1]=1 Next Next End If End If mrt=10 End If If Rnd(0,20)<2 Or mrt=0 rx = x ry = y mrt = 10 End If End If If x>3 And x<mapwidth-3 And y>3 And y<mapheight-3 Then x+=dx[dir] y+=dy[dir] Else x = mapwidth/2 y = mapheight/2 End If Next End Method Method newdir:Int(dir:Int) Local exitloop:Bool=False Local cnt:Int=0 While exitloop=False Local d:Int=Rnd(0,4) cnt+=1 If cnt>500 Then Return 0 If d<>dir ' is nothing ahead taken If map[x+dx[d]][y+dy[d]] = 0 If map[x+(dx[d]*2)][y+(dy[d]*2)] = 0 ' do not go in opposite direction Select d Case 0 If dir<>2 Then Return d End If Case 1 If dir<>3 Then Return d End If Case 2 If dir<>0 Then Return d End If Case 3 If dir<>1 Then Return d End If End Select End If End If End If Wend End Method Method draw() For Local y=0 Until mapheight For Local x=0 Until mapwidth If map[x][y] > 0 Local d:Int=distance( mapwidth/2, mapheight/2, x, y) Local col:Int=0+(1500/(d+1)) SetColor col,col,col DrawRect x*tilewidth, y*tileheight, tilewidth-1, tileheight-1 End If Next Next End Method Method distance:Int(x1:Int,y1:Int,x2:Int,y2:Int) Return Abs(x2-x1)+Abs(y2-y1) End Method End Class Global mymap:themap Class MyGame Extends App Field cnt:Int Method OnCreate() SetUpdateRate(60) Local date := GetDate() Seed = date[5] mymap = New themap(Rnd(32,150),Rnd(32,150)) End Method Method OnUpdate() cnt+=1 If cnt>100 mymap = New themap(Rnd(32,150),Rnd(32,150)) cnt=0 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
Import mojo Class themap Field mapwidth:Int Field mapheight:Int Field tilewidth:Float Field tileheight:Float Field map:Int[][] Field cmode:Int=Rnd(1,4) Method New( mapwidth:Int, mapheight:Int) Self.mapwidth = mapwidth Self.mapheight = mapheight tilewidth = Float(DeviceWidth())/Float(mapwidth) tileheight = Float(DeviceHeight())/Float(mapheight) map = New Int[mapwidth][] For Local i = 0 Until mapwidth map[i] = New Int[mapheight] Next map[mapwidth/2][mapheight/2] = 255 For Local i=0 To (mapwidth*mapheight)/4 placeblock Next mapblur End Method Method mapblur() For Local i=0 To mapwidth*mapheight Local x1:Int=Rnd(2,mapwidth-4) Local y1:Int=Rnd(2,mapheight-4) If map[x1][y1] > 0 For Local y2=y1-1 To y1+1 For Local x2=x1-1 To x1+1 If map[x2][y2] = 0 map[x2][y2] = map[x1][y1] / 2 End If Next Next End If Next End Method Method placeblock() Local x:Int=Rnd(0,mapwidth) Local y:Int=Rnd(0,mapheight) Local fitsx:Int=Rnd(3,12) Local fitsy:Int=Rnd(3,12) If Rnd(0,10) < 2 Then If Rnd(1,3) = 1 fitsx = Rnd(6,22) fitsy = 3 Else fitsy = Rnd(6,22) fitsx = 3 End If End If If mapfit(x,y,fitsx,fitsy) = True fitmap(x,y,fitsx,fitsy) End If End Method Method fitmap(x:Int,y:Int,w:Int,h:Int) For Local y2 = y Until y+h For Local x2 = x Until x+w map[x2][y2] = 255 Next Next End Method Method mapfit:Bool(x:Int,y:Int,w:Int,h:Int) If x+w > mapwidth-3 Then Return False If y+h > mapheight-3 Then Return False If x<3 Then Return False If y<3 Then Return False For Local y2 = y Until y+h For Local x2 = x Until x+w If map[x2][y2] > 0 Then Return False Next Next For Local y2 = y+1 Until y+h-1 If map[x-1][y2] > 0 Then Return True If map[x+w][y2] > 0 Then Return True Next For Local x2 = x+1 Until x+w-1 If map[x2][y-1] > 0 Then Return True If map[x2][y+h] > 0 Then Return True Next End Method Method draw() For Local y:Float=0 Until mapheight Step 1 For Local x:Float=0 Until mapwidth Step 1 If map[x][y] > 0 Local d:Int = distance(x,y,320,240) d = d / 2.5 SetColor map[x][y],d,0 DrawRect x*tilewidth, y*tileheight, tilewidth+1, tileheight+1 End If Next Next End Method End Class Global mymap:themap Class MyGame Extends App Field refresh:Int Method OnCreate() SetUpdateRate(60) mymap = New themap(100,100) End Method Method OnUpdate() refresh+=1 If refresh>120 mymap = New themap(Rnd(100,200),Rnd(100,200)) refresh=0 Endif End Method Method OnRender() Cls 0,0,0 SetColor 255,255,255 mymap.draw End Method End Class Function Main() New MyGame() End Function Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int) Return Abs(x2-x1)+Abs(y2-y1) End Function
Ported from a example by AdamStrange from a thread on the Blitzmax forum.
Link
Import mojo Class themap Field mapwidth:Int Field mapheight:Int Field tilewidth:Float Field tileheight:Float Field map:Float[][] Method New( mapwidth:Int, mapheight:Int, numpoints:Int) Self.mapwidth = mapwidth Self.mapheight = mapheight tilewidth = DeviceWidth()/Float(mapwidth) tileheight = DeviceHeight()/Float(mapheight) map = New Float[mapwidth][] For Local i = 0 Until mapwidth map[i] = New Float[mapheight] Next addheightmappoints(numpoints) For Local i=0 Until numpoints expandheightmap next End Method Method expandheightmap() For Local n=0 To 100000 Local x1:Int=Rnd(1,mapwidth-1) Local y1:Int=Rnd(1,mapheight-1) If map[x1][y1] > 0 For Local y2=y1-1 To y1+1 For Local x2=x1-1 To x1+1 If map[x1][y1] > map[x2][y2] map[x2][y2] = (map[x1][y1]+ map[x2][y2])*Rnd(0.49,0.5) End If Next Next End If Next End Method Method addheightmappoints(count:Int) For Local i=0 Until count Local x:Int=Rnd( mapwidth/2-(mapwidth/3), mapwidth/2+(mapwidth/3)) Local y:Int=Rnd( mapheight/2-(mapheight/3), mapheight/2+(mapheight/3)) map[x][y] = Rnd(64,200) Next End Method Method drawmap() For Local y:Float=0 Until mapheight Step 1 For Local x:Float=0 Until mapwidth Step 1 SetColor map[x][y],0,0 DrawRect x*tilewidth, y*tileheight, tilewidth+1, tileheight+1 Next Next End Method Method clearheightmap() End Method End Class Global mymap:themap Class MyGame Extends App Field nm:Int Method OnCreate() SetUpdateRate(60) Local date := GetDate() Seed = date[5] mymap = New themap( Rnd(50,150), Rnd(50,150), Rnd(2,20)) End Method Method OnUpdate() nm+=1 If nm>100 mymap = New themap( Rnd(50,150), Rnd(50,150), Rnd(2,20)) nm=0 End If End Method Method OnRender() Cls 0,0,0 mymap.drawmap SetColor 255,255,255 DrawText "MonkeyX - Heightmap/"+ "texture/image generator", 0,0 End Method End Class Function Main() New MyGame() End Function