Page 1 of 1

Random bad guy face generator

Posted: Tue Jan 13, 2026 6:03 pm
by minimy
This is a simple code what generate an image with a random cartoon bad guy face and random name.
I did this a few time ago, for the game project but now i change the way to make it, and will be shared.
Is a draft with not too much clean code, but work fine.
You can modify the code for your own purpose.
I hope can be useful for some of you.

Image

Code: Select all

Procedure.s generadorNombre(lmax.b=8,lmin.b=3, names.b=1)
  Protected.s finalname, name, ch
  Protected.b largo
  Protected.b vocal, old, p
  For p=1 To names
    name= "":old=0:RandomSeed( Random(999999) )
    largo= Random(lmax,lmin):vocal=old
;     Debug largo
    Repeat
      Repeat
        vocal= 0
        RandomSeed( Random(999999) )
        ch= Chr(Random(90,65))
        If ch="A" Or ch="E" Or ch="I" Or ch="O" Or ch="U"
          vocal= 1
        EndIf
      Until vocal+old > 0
      name+ ch
      old= vocal
    Until Len(name) >= largo
    If names<2
      finalname= Left(name,1)+LCase(Mid(name,2,Len(name)-1))
    Else
      finalname +" "+ Left(name,1)+LCase(Mid(name,2,Len(name)-1))
    EndIf
    finalname= Trim(finalname)
  Next p
  ProcedureReturn finalname
EndProcedure
Procedure   genFace(w,h,font, name.s="")
  Protected.d rw= w, rh=h
  Protected.i img=  CreateImage(#PB_Any,400,400):w=400:h=400
  Protected.b ran
  Protected.l fondo= $333333
  Protected.l pelo= RGB( Random(200)+45,Random(100)+120, Random(130)+20)
  Protected.l camisa= RGB( Random(180)+55,Random(180)+55, Random(180)+55)
;   Protected.s name
  Protected.l camisa2= RGB( Red(camisa)-35,Green(camisa)-35, Blue(camisa)-35)
  StartDrawing(ImageOutput(img))
  Box(0,0,OutputWidth(),OutputHeight(),fondo)
  
  Protected.l cp,cb,ca
  Protected.i cw,ch,cx,cy,r, x,y
  Protected.d td,of,mw,mh,mx,my
  Protected.d bw,bh,bx,by
  Protected.i es,ed,ex1,ex2,ey1,l,l2,a
  Protected.i i
  Protected.d nw,nh,nx,ny
  Protected.d nes, gro,col
  Protected.b cir
  Protected.s txt
  Protected.b man= Random(2)
  Debug man
  cp= RGB( Random(100)+145,Random(50)+120, Random(30)+70)
  cb= RGB( Red(cp)-30,Green(cp)-30,Blue(cp)-30)
  ca= RGB( Red(cp)-60,Green(cp)-60,Blue(cp)-60)
  
  ;camisa
  Ellipse(OutputWidth()/2,OutputHeight(),OutputWidth()* Random(1.3,1.0), OutputHeight()/2, camisa2)
  Ellipse(OutputWidth()*0.35,OutputHeight()+20,OutputWidth()* Random(1.3,1.0), OutputHeight()/2, camisa)
  
  ;cabeza coordenadas
  cw= Random(w*0.6,w*0.4)
  ch= Random(h*0.75,h*0.5)
  cx= (OutputWidth()-cw) / 2
  cy= (OutputHeight()-ch) / 2
  r=  Random(ch*0.4,ch*0.2)
  ;camisa decoración
  If Random(10)>4
    Box(cx,h/2,cw,h/2,camisa2)
  EndIf
;   If Random(10)>4
;     DrawingFont(FontID(font))
;     a=Random(5,1)
;     txt= " "
;     For p=1 To a
;       txt= Chr(Random(90,65))
;     Next p
;     txt+ "-"+RSet(Str(Random(9999)),4,"0")+" "
;     DrawText(w*0.7+Random(50),h*0.65+Random(50),txt,$ffffff,camisa2)
;   EndIf
  
  ;orejas
  nw= (cw*0.1) * Random(2,1)
  nh= (ch*0.1) * Random(3,1.5)
  RoundBox(cx-(nw/2),h*0.35,nw,nh,r,r,cb)
  RoundBox(cx+cw-(nw/2),h*0.35,nw,nh,r,r,cp)
  
  ;pelo
  If Random(10)>4
  DrawingMode(#PB_2DDrawing_Default)     
  py= Random(cy+(ch/2), cy+(ch/8))
  For y=cy To py Step 5
  For x=cx To cx+cw Step 5
    Ellipse(x,y+Random(30),Random(20,10),Random(30,10),pelo)
    Ellipse(x,y+30+Random(30),Random(20,10),Random(30,10),pelo)
  Next x
  Next y
  EndIf

  ;cabeza
  DrawingMode(#PB_2DDrawing_Gradient)     
    BackColor(cp)
    FrontColor(cb)
    LinearGradient(0, cy, w, cy+ch)    
    RoundBox(cx,cy,cw,ch,r,r);,cp)
  ;brazos
  If Random(10)>5  
    nw= w/4  
    If Random(10)>5  
      RoundBox(-nw/2,h*0.7,nw,h,r,r,cp)
      RoundBox(w-(nw/2),h*0.7,nw,h,r,r,cb)
    Else
      Box(-nw/2,h*0.7,nw,h,cp)
      Box(w-(nw/2),h*0.7,nw,h,cb)
    EndIf
  EndIf
    
  ;mandibula
  DrawingMode(#PB_2DDrawing_Gradient)     
  td= cy+ch  
  of= Random(h*0.1,h*0.05)
  mw= Random(w*0.4,w*0.3)
  mh= Random(h*0.3,h*0.2)
  mx= (OutputWidth()-mw) / 2
  my= td-mh+5
  r=  Random(ch*0.1,ch*0.05)
    BackColor(cp)
    FrontColor(ca)
    LinearGradient(0, my, w, my+mh)    
  DrawingMode(#PB_2DDrawing_AlphaBlend)     
  RoundBox(mx+4,my+4,mw,mh,r,r,$77000000)
  DrawingMode(#PB_2DDrawing_Gradient)     
  RoundBox(mx,my,mw,mh,r,r,cb)
  
  ;boca
  DrawingMode(#PB_2DDrawing_Default)     
  bw= Random(mw*0.9,mw*0.4)
  bh= Random(mh*0.9,mh*0.2)
  If Random(100)>50
    bx= OutputWidth()/2-(bw/2)
    by= my+((mh-bh)/2)+Random(bh/2)
    RoundBox(bx,by,bw,bh/2,r,r,$ffffff)
    LineXY(bx,by+(bh/4),bx+bw,by+(bh/4),0)
    bx+r:by+(bh/4)
  Else
;     If Random(100)>50
;       LineXY(OutputWidth()/2-(bw/2),my+((mh-bh)/2)+Random(bh/2),OutputWidth()/2-(bw/2)+bw,my+((mh-bh)/2)+bh/2,10)
;     Else
    bx= OutputWidth()/2-(bw/2)
    by= my+((mh-bh)/2)+Random(bh/2)
      LineXY(bx,by,OutputWidth()/2-(bw/2)+bw,my+((mh-bh)/2)+Random(bh/2),10)
;     EndIf
  EndIf
    
  
  ;ojos posicion
  es= Random(w*0.15,w*0.05)  *0.7
  ed= Random(cw*0.4,cw*0.2) + es/2
    ex1= (OutputWidth()/2)-ed+(es/2)
    ex2= (OutputWidth()/2)+ed-(es/2)
    ey1= cy+(Random(cw*0.2,cw*0.1)*1.5)
    ;mas pelo
    If Random(10)>4
    l= cw/2
    l2= ey1-cy + Random(50,40) 
    For a=-90 To 90
      x= (w/2) + Sin(Radian(a)) * l
      y= ey1 - Cos(Radian(a)) * l2
      Ellipse(x,y,Random(20,10),Random(40,20),pelo)
    Next a
    EndIf
    ;ojos
    Ellipse(ex1,ey1, es,es*0.8,$ffffff)
    Ellipse(ex2,ey1, es,es*0.8,$ffffff)
    ce= RGB( Random(220),Random(220),Random(220))
    ;el
    Circle(ex1, ey1, es*0.5,ce)
    Circle(ex1, ey1, es*0.25,0)
    ;er
    Circle((OutputWidth()/2)+ed-(es/2), ey1, es*0.5,ce)
    Circle((OutputWidth()/2)+ed-(es/2), ey1, es*0.25,0)
    
  ;parpados
  DrawingMode(#PB_2DDrawing_Gradient)     
  If Random(10)>4  
    Ellipse(ex1,ey1-(es*0.5), es,es*0.5,$ffffff)
  EndIf
  If Random(10)>4  
    Ellipse(ex2,ey1-(es*0.5), es,es*0.5,$ffffff)
  EndIf
  
  ;brillo y sombra
    DrawingMode(#PB_2DDrawing_Default)  
;     For y= cy To cy+ch
    For y= 0 To h-1
    i=10
;     For x= cx To cx+cw*0.125
    For x= 0 To cx+cw*0.125
      p= Point(x,y)
      If p<>fondo And p<>pelo And p<>camisa And p<>camisa2 And p<>0 And p<>$ffffff
        Plot(x,y, RGB(Red(p)+i,Green(p)+i,Blue(p)+i) )
      EndIf
    Next x
     i=-10
;      For x= cx+cw*0.85 To cx+cw
     For x= cx+cw*0.85 To w-1
        p= Point(x,y)
        If p<>fondo And p<>pelo And p<>camisa And p<>camisa2 And p<>0 And p<>$ffffff
          Plot(x,y, RGB(Red(p)+i,Green(p)+i,Blue(p)+i) )
        EndIf
      Next x
    Next y
    
  ;bigote
  If Random(10)>4 And  man=1
    l= mw/2
    l2= (mh/2) + Random(20,10) 
    For a= -90 To 90
      x= mx+(mw/2) + Sin(Radian(a)) * l
      y= my+(mh/2) - Cos(Radian(a)) * l2
      Ellipse(x,y,Random(20,10),Random(40,20),pelo)
    Next a
  EndIf
    
  ;nariz
  nw= Random(mw*0.4,mw*0.2)
  nh= Random(mh*0.9,mh*0.2)
  nx= OutputWidth()/2-(nw/2)
  ny= ey1+Random(es)
  DrawingMode(#PB_2DDrawing_AlphaBlend)     
  RoundBox(nx+4,ny+4,nw,nh,r,r,$99000000)
  DrawingMode(#PB_2DDrawing_Gradient)     
  RoundBox(nx,ny,nw,nh,r,r,cb)
  
  ;pelo
  DrawingMode(#PB_2DDrawing_Default)     
  If Random(10)>4
  For x=cx To cx+cw Step 5
    Ellipse(x,cy,Random(20,10),Random(30,10),pelo)
  Next x
  EndIf
    
  ;barba
  If Random(10)>4 And  man=1
    l= cw/2
    l2= (cy+ch)-(h/2) + Random(50,40) 
    For a= 90 To 270
      x= (w/2) + Sin(Radian(a)) * l
      y= (h/2) - Cos(Radian(a)) * l2
      Ellipse(x,y,Random(20,10),Random(40,20),pelo)
    Next a
  EndIf
  
    ;brillo y sombra
    DrawingMode(#PB_2DDrawing_Default)  
;     For y= 0 To cy+ch
    For y= 0 To h-1
    i=10
;     For x= cx To cx+cw*0.125
    For x= 0 To cx+cw*0.125
      p= Point(x,y)
      If p=pelo
        Plot(x,y, RGB(Red(p)+i,Green(p)+i,Blue(p)+i) )
      EndIf
    Next x
     i=-10
;      For x= cx+cw*0.85 To cx+cw
     For x= cx+cw*0.85 To w-1
        p= Point(x,y)
        If p=pelo
          Plot(x,y, RGB(Red(p)+i,Green(p)+i,Blue(p)+i) )
        EndIf
      Next x
    Next y
    
    For y= h/2 To h-1
    i=15
    For x= 0 To w*0.10
      p= Point(x,y)
      If p=camisa Or p=camisa2
        Plot(x,y, RGB(Red(p)+i,Green(p)+i,Blue(p)+i) )
      EndIf
    Next x
     i=-15
     For x= w-(w*0.10) To w-1
        p= Point(x,y)
        If p=camisa Or p=camisa2
          Plot(x,y, RGB(Red(p)+i,Green(p)+i,Blue(p)+i) )
        EndIf
      Next x
    Next y
    
;     LineXY(OutputWidth()/2,0,OutputWidth()/2,OutputHeight(),$aaaaaa)
;     LineXY(0,OutputHeight()/2,OutputWidth(),OutputHeight()/2,$aaaaaa)
;     DrawingFont(FontID(font))
;     DrawingMode(#PB_2DDrawing_Transparent)
;     name.s= generadorNombre(6,6,2)
;     DrawText(0,h-TextHeight("Kg"),name,0)
;     DrawText(1,h-TextHeight("Kg")-1,name,$ffffff)
  StopDrawing()
  
  ;gafas
;   If Random(1000)>500
  nes= es + Random(es * 0.35) + es * 0.1
  gro= Random(new*0.2) + nes * 0.2
  col= Random($ffffff)
    StartVectorDrawing(ImageVectorOutput(img))
    VectorSourceColor(RGBA(Red(col), Green(col), Blue(col), 190))
    If Random(10)>4:cir=1:EndIf
    If cir
      If Random(10)>5 : AddPathCircle(ex1,ey1,nes) :g+1: FillPath(#PB_Path_Default) : EndIf
      If Random(10)>5 : AddPathCircle(ex2,ey1,nes) :g+2: FillPath(#PB_Path_Default) : EndIf
    Else
      If Random(10)>5 : AddPathBox(ex1-nes,ey1,nes*2,nes*1.25) :g+1: FillPath(#PB_Path_Default) : EndIf
      If Random(10)>5 : AddPathBox(ex2-nes,ey1,nes*2,nes*1.25) :g+2: FillPath(#PB_Path_Default) : EndIf
    EndIf  
    VectorSourceColor(RGBA(Red(col), Green(col), Blue(col), 255))
    If cir
      Select g
        Case 1
          AddPathCircle(ex1,ey1,nes) : StrokePath(gro)
          MovePathCursor(ex1-nes,ey1) : AddPathLine(cx,ey1) : StrokePath(gro)
        Case 2
          AddPathCircle(ex2,ey1,nes) : StrokePath(gro)
          MovePathCursor(ex2+nes,ey1) : AddPathLine(cx+cw,ey1) : StrokePath(gro)
        Case 3
          AddPathCircle(ex1,ey1,nes) : StrokePath(gro)
          AddPathCircle(ex2,ey1,nes) : StrokePath(gro)
          MovePathCursor(ex1-nes,ey1) : AddPathLine(cx,ey1) : StrokePath(gro)
          MovePathCursor(ex2+nes,ey1) : AddPathLine(cx+cw,ey1) : StrokePath(gro)
          MovePathCursor(ex1+nes,ey1) : AddPathLine(ex2-nes,ey1) : StrokePath(gro)
      EndSelect
    Else
      Select g
        Case 1
          AddPathBox(ex1-nes,ey1,nes*2,nes*1.25) : StrokePath(gro)
          MovePathCursor(ex1-nes,ey1) : AddPathLine(cx,ey1) : StrokePath(gro)
        Case 2
          AddPathBox(ex2-nes,ey1,nes*2,nes*1.25) : StrokePath(gro)
          MovePathCursor(ex2+nes,ey1) : AddPathLine(cx+cw,ey1) : StrokePath(gro)
        Case 3
          AddPathBox(ex1-nes,ey1,nes*2,nes*1.25) : StrokePath(gro)
          AddPathBox(ex2-nes,ey1,nes*2,nes*1.25) : StrokePath(gro)
          MovePathCursor(ex1-nes,ey1) : AddPathLine(cx,ey1) : StrokePath(gro)
          MovePathCursor(ex2+nes,ey1) : AddPathLine(cx+cw,ey1) : StrokePath(gro)
          MovePathCursor(ex1+nes,ey1) : AddPathLine(ex2-nes,ey1) : StrokePath(gro)
      EndSelect
    EndIf
    
    ;cigarro
    If Random(10)>4
      col= $eeeeee
      VectorSourceColor(RGBA(Red(col), Green(col), Blue(col), 255))
      MovePathCursor(bx,by)
      AddPathLine(bx-30,by+30)
      StrokePath(10)
      col= $0000ff
      VectorSourceColor(RGBA(Red(col), Green(col), Blue(col), 255))
      MovePathCursor(bx-30,by+30)
      AddPathLine(bx-40,by+40)
      StrokePath(10)
      col= $777777
      VectorSourceColor(RGBA(Red(col), Green(col), Blue(col), 255))
      MovePathCursor(bx-40,by+40)
      AddPathLine(bx-50,by+50)
      StrokePath(10)
      
      VectorSourceColor($33ffffff)
      x= bx-50: r=5
      For p= by To 0 Step -15
        AddPathCircle(x,p,r)
        FillPath(#PB_Path_Default)
        x- (Random(15)-5)
        r+ Random(3,1)
      Next p
    EndIf
    
    If name
      AddPathBox(0,h-60,w,60)
      VectorSourceColor($88000000)
      FillPath(#PB_Path_Default)
      
;       name.s= generadorNombre(6,6,2)
      VectorFont(FontID(font),50)
      MovePathCursor((w-VectorTextWidth(name))/2, h-VectorTextHeight("Kg"))
        AddPathText(name)
        VectorSourceColor($ffffffff)
        FillPath(#PB_Path_Default)
;       MovePathCursor(5, h-VectorTextHeight("Kg"))
;         AddPathText(name)
;         VectorSourceColor($ff000000)
;         StrokePath(2)
      EndIf
    StopVectorDrawing()
;   EndIf
    
    ;fx luz y sombra
    If Random(10)>7
      StartDrawing(ImageOutput(img))
      DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Gradient)      
        BackColor($ff000000)
        GradientColor(0.4, $ee000000)
        GradientColor(0.7, $77000000)
        FrontColor($00000000)
        LinearGradient(0, 0, 0, OutputHeight())    
        Box(0,0,OutputWidth(),OutputHeight())
      StopDrawing()
    EndIf
    
  ResizeImage(img,rw,rh,#PB_Image_Smooth)  
  ProcedureReturn img
EndProcedure


;---- INICIO
w=400:h=w
fnt1= LoadFont(#PB_Any,"Impact",30,#PB_Font_HighQuality)
win= OpenWindow(#PB_Any,0,0,1000,500,"Generator",#PB_Window_ScreenCentered | #PB_Window_SystemMenu)
SetWindowColor(win,$111111)
; ButtonGadget(1,0,0,200,30,"Genera careto v2")
ButtonGadget(2,200,0,200,30,"Genera careto")
CanvasGadget(0,0,40,w,h)
  img= genFace(w,h,fnt1, generadorNombre(6,6,2))
  StartDrawing(CanvasOutput(0))
    DrawAlphaImage(ImageID(img),0,0)
  StopDrawing()

Repeat
  event= WindowEvent()
  Select event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
        Case 2
          If IsImage(img):FreeImage(img):EndIf
          img= genFace(w,h,fnt1, generadorNombre(6,4,2))
;           img= genFace2(w,h,fnt1, generadorNombre(6,4,2))
          StartDrawing(CanvasOutput(0))
            Box(0,0,OutputWidth(),OutputHeight(),$ffffff)
            DrawImage(ImageID(img),0,0)
;             DrawAlphaImage(ImageID(img),0,0)
          StopDrawing()
          FreeImage(img)
      EndSelect
          
    Case #PB_Event_CloseWindow
      End
  EndSelect
  Delay(1)
ForEver

Re: Random bad guy face generator

Posted: Tue Jan 13, 2026 6:09 pm
by miso
Haha, thats actually very cool. :)

Re: Random bad guy face generator

Posted: Tue Jan 13, 2026 6:13 pm
by minimy
Thanks miso! Bad guys from PB :lol:

Re: Random bad guy face generator

Posted: Tue Jan 13, 2026 9:20 pm
by Andre
Cool and funny :D (and impressive, how much new stuff you show us regularly)

Re: Random bad guy face generator

Posted: Tue Jan 13, 2026 9:29 pm
by zikitrake
:lol: :lol: :lol: QuƩ grande!
( :lol: :lol: :lol: That's great!) :)

Re: Random bad guy face generator

Posted: Tue Jan 13, 2026 9:41 pm
by Fangbeast
Some of those faces could be me in the morning without my decaf!!