UnderwareDESIGN

General => Competitions => CleverCoders => Topic started by: kevin on December 22, 2012, 03:16:05 PM

Title: Challenge #26 - Christmas / New Years Scene generation
Post by: kevin on December 22, 2012, 03:16:05 PM

(http://www.underwaredesign.com/PlayBasicSig.png) (http://www.playbasic.com)


Challenge #26 - Christmas / New Years Scene generation


   It's well and truly that time of season again, so in keeping with the season let's make a programming challenge out of it.  This time you're challenged to create a Christmas or News Years scene purely out of code.    So the trick here,  is you're going to have to procedural generate the graphics in the scene, you can't use any media.  

  The scene you create can be either animated or static, no difference.   The subject matter can be pretty much anything, a  Christmas scene might show a Tree with lights, Santa, Presents,  Snow Man / Snow scene .....  Where a New years scene is probably going to be a fireworks show or something.. But think outside the box and see what you come up with.


    As always we'll award   Extra Brownie points to the most creative submissions .



Submission

 * Submissions can be written in any version of PlayBASIC (http://www.playbasic.com)
 
 * Submission can only be accepted in Source Code form only.

 * No external media is permitted for this challenge.

 * Zip up your Submissions and please try to keep it smaller than 500K !

 * Authors automatically give consent for their submission(s) to distributed and used for promotional purposes via UnderwareDesign.com, PlayBasic.com code tank.

 * Authors can submit as many times as they like.

 * To make a submission, zip up your projects and either post it in this thread, or create your own thread in either the Source Codes or Show Case forums and post a link bellow.



Dead Line

    Anytime before the next ice age



Prizes

    * The best submissions will be periodically highlighted through the PlayBasic IDE news service/News Letters/ FaceBook (http://www.facebook.com/pages/PlayBasic/127905400584274),  Twitter (http://www.twitter.com/PlayBasic) or the PlayBASIC youtube channel (http://www.youtube.com/playbasic).




Return to Challenge Index (http://www.underwaredesign.com/forums/index.php?topic=3225.0)

Title: Re: Challenge #26 - Christmas / New Years Scene generation
Post by: Sigtrygg on December 26, 2012, 08:24:48 AM
Merry christmas, all together!

Here is my programm for the competition.
A small programm for creating snowflakes.
Have fun to change parameters for flakes.

Sigtrygg

[pbcode]
; PROJECT : SnowFlake
; AUTHOR  : Sigtrygg
; CREATED : 12/26/2012
; EDITED  : 12/26/2012
; ---------------------------------------------------------------------

//==============================================================================
//============================== SNOWFLAKE-PROGRAMM=============================
//==============================================================================

// Have fun to make experiments with changing parameters!!!



r=300; Radius
x=100; start x, center of image
y=300; start y, center of image


Do

CreateImage 1,200,600,2;size of image = 200x600
RenderToImage 1

Ink RGB (Rnd(255),Rnd(255),Rnd(255))
Line x,y,x,y-r
t=RndRange (1,100);distance of first feather from center
distance=RndRange(1,50);distance between feathers

For u= t To 300 Step distance
   Ink RGB (Rnd(255),Rnd(255),Rnd(255))
   i=RndRange (5,80);Lenght of feather
   Line x,y-u,x+1.333*i,y-u-i;make lines in angles of 60 degree
   Line x,y-u,x-1.333*i,y-u-i
Next u

RenderToScreen
CreateSprite 1
SpriteImage 1,1
SpriteHandle 1,-100,-300
SpriteDrawMode 1,2

For t=1 To 5
   CopySprite 1,t+1
   TurnSprite t+1,t*60
   PositionSprite t,GetScreenWidth()/2,GetScreenHeight()/2
Next t

DrawAllSprites

Sync

;waitkey
Wait 1000

Cls
 
Loop
[/pbcode]
Title: Re: Challenge #26 - Christmas / New Years Scene generation
Post by: kevin on December 28, 2012, 07:19:29 PM
  This function draws a chunky pine Christmas tree without decorations.  


[pbcode]
   
   Draw_Xmas_Tree(400,300,200,300)   

   Sync
   WaitKey
          

Function Draw_Xmas_Tree(xpos,ypos,width,Height)   
   
      ; 90 % of it's height is branches
      Top      =Ypos-Height
      Bottom   =top+(Height*0.90)

      ; scaler between width and height
      WidthScaler#=Width/float(Height)

      BranchSize#=2.3

      x2=xpos
      y2=ypos+top

      c2=$20a800   
      c1=rgbfade(c2,80)   

      
      lp#=top
      while lp#<=Bottom
      
            Dist#            =lp#-top
            BranchWidth#   =Dist#*WidthScaler#

            x1=xpos
            y1=ypos+Dist#

            ; draw the left and right fragments
            gouraudtri x2-BranchWidth#,y1,c2,x1,y1,c1,x2,y2, c2   
            gouraudtri x2+BranchWidth#,y1,c2,x1,y1,c1,x2,y2, c2
            
            lp#+=BranchSize#
            BranchSize#+=1.5
            x2=x1
            y2=y1               

      endwhile

      ; draw trunk
      y2=Ypos+Height

      Size#=width*0.05
      x1=xpos-size#
      x2=xpos+size#

      c1=$915700
      c2=$714700
      shadebox x1,y1,x2,y2,c1,c2,c2,c1
      

EndFunction


[/pbcode]


  This version draws something of a perspective forest of pine trees

[pbcode]


   setfps 60

   global ProjectionX#=200
   global ProjectionY#=200

   MaxDepth      =2000
   Trees         =150
   
   Type tTree
         x#
         y#
         Width#
         Height#
         DEpth#
   
   EndType

   Dim tree(trees) as tTree

   depth#=MaxDepth
   depthstep#=float(MaxDepth)/Trees
   For lp =1 to Trees
      inittree(lp,depth#)
      depth#-=Depthstep#
   next
   


   Cam=NewCamera()

   cameracls cam,off

SceneCacheSize 1024*1024

speed#=4

Do

   cls


   capturetoscene
   clsscene
      For lp =1 to Trees
         Tree(lp).depth-=Speed#
            
         if Tree(lp).depth<50
            inittree(lp,maxdepth)
         endif

         capturedepth Tree(lp).depth

         DrawTree(lp)


      next
   drawcamera cam

   sync

loop


      



Function  DrawTree(lp)

      depth#=Tree(lp).depth
      width#=Tree(lp).Width/2
      Height#=Tree(lp).Height

      x1#=Tree(lp).x-Width#
      x2#=Tree(lp).x+Width#

      y1#=Tree(lp).y-Height#
      y2#=Tree(lp).y

      
      ; convert 3d space to 2d space

      Screenx1#   =(ProjectionX#*X1#)/depth#      
      ScreenY1#   =(ProjectionY#*y1#)/depth#      
      Screenx2#   =(ProjectionX#*X2#)/depth#      
      ScreenY2#   =(ProjectionY#*y2#)/depth#      

      ; center in screen
      ScreenX1#+=400
      ScreenX2#+=400

      ScreenY1#+=300
      ScreenY2#+=300
      
      
      x=(SCreenX2#+ScreenX1#)/2
      y=(SCreenY2#)      
      W=(SCreenX2#-ScreenX1#)
      H=(SCreenY2#-ScreenY1#)
      
      Draw_Xmas_Tree(x,y,w,H)   
      


EndFunction


Psub InitTree(lp,depth#)

      Width=rndrange(100,250)
      Height=rndrange(400,600)

      Tree(lp).x      =rndrange(-2000,2000)
      Tree(lp).y      =0
      Tree(lp).Width   =Width
      Tree(lp).Height=Height
      
      Tree(lp).depth=depth#

EndPsub
          

Function Draw_Xmas_Tree(xpos,ypos,width,Height)   
   
   
      ; 90 % of it's height is branches
      Top      =Ypos-Height
      Bottom#   =top+(Height*0.90)

      ; scaler between width and height
      WidthScaler#=Width/float(Height)

      BranchCount   =15

      BranchSize#=(Height*0.80)/BranchCount

      x2=xpos
      y2=ypos+top

      c2=$20a800   
      c1=rgbfade(c2,80)   

      
      lp#=top
      for Branch=1 to BranchCount      
            Dist#            =lp#-top
            BranchWidth#   =Dist#*WidthScaler#

            x1=xpos
            y1=ypos+Dist#

            ; draw the left and right fragments
            gouraudtri x2-BranchWidth#,y1,c2,x1,y1,c1,x2,y2, c2   
            gouraudtri x2+BranchWidth#,y1,c2,x1,y1,c1,x2,y2, c2
            
            lp#+=BranchSize#
            BranchSize#+=0.15
            x2=x1
            y2=y1               
      next
      
      ; draw trunk
      y2=Ypos+Height

      Size#=width*0.05
      x1=xpos-size#
      x2=xpos+size#

      c1=$915700
      c2=$714700
      shadebox x1,y1,x2,y2,c1,c2,c2,c1

EndFunction

[/pbcode]