Can you add something to make my 11 LOCs more awesome?

Everything else that doesn't fall into one of the other PB categories.
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Can you add something to make my 11 LOCs more awesome?

Post by firace »

So I have this tiny program that just makes a grid scattered with nicely colored squares.
Can you add some more code (preferably just a few lines) to make it do something cool,
funny, useful, and/or beautiful?

Would love to see what you guys come up with!


Image

Code: Select all

InitSprite() :  W=540 : H=450  : C = $82BF62 : Cellsize = 30

OpenWindow(0, 35, 35, W+1, H+40, "Awesome Pixels", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,W+1,H+40,1,0,0) 

StartDrawing(ScreenOutput())

For i = 0 To H Step 30 :  Line(0, i, W+1, 1, $313100)  :  Next
For j = 0 To W Step 30 :  Line(j, 0, 1, H+1, $313100)  :  Next

For a = 0 To W-Cellsize Step 30 : For b = 0 To H-Cellsize Step 30 
 If Not Random(4) :   Box(a, b, Cellsize-1, Cellsize-1, C*1.3+(a+b)*3)  : EndIf
Next : Next    

StopDrawing() : FlipBuffers()

Repeat : Until WaitWindowEvent() = 13116
Last edited by firace on Tue Oct 13, 2015 8:46 pm, edited 5 times in total.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Can you make something awesome from my 11-line program?

Post by Keya »

Its great we can get that grid established with just a few lines of Purebasic :)

it looks a bit like the basis of Conway's Game Of Life where cells are born, remain alive, or die, depending on the cells around them:
Image
* Any live cell with fewer than two live neighbours dies, as if caused by under-population.
* Any live cell with two or three live neighbours lives on to the next.
* Any live cell with more than three live neighbours dies, as if by over-population.
* Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Game Of Life in Purebasic:
http://www.purebasic.fr/english/viewtop ... =12&t=9081
http://purebasic.info/phpBB3ex/viewtopi ... =12&t=1367
Last edited by Keya on Sun Oct 11, 2015 11:37 pm, edited 2 times in total.
Amilcar Matos
User
User
Posts: 43
Joined: Thu Nov 27, 2014 3:10 pm
Location: San Juan, Puerto Rico

Re: Can you make something awesome from my 11-line program?

Post by Amilcar Matos »

Code: Select all

;{- Program header
;==Code Header Comment==============================
;        Name/title: Awsome Pixels.pb
;   Executable name: Awsome Pixels.exe
;           Version: 1.0
;            Author: Firace
;     Collaborators: Amílcar Matos Pérez
; Release date/hour: 11/Oct/2015
;  Operating system: Windows  [X]GUI
;  Compiler version: PureBasic 5.30 (x86)
;         Copyright: (C)2015 AMP All rights reserved.
;           License: 
;         Libraries: 
;       Explanation: 
; ==================================================
;.......10........20........30........40........50........60........70........80
;}

; What can i do? Lets see;
;  1. Put colon separated statements each in its own line.
;  2. Opened space on each code line.
;  3. Completed some statements - for example Next statements.
;  4. Beautify the code with PB editor commands - indentation, etc.
;  5. Added EnableExplicit.
;  6. Made explicit the variables types.
;  7. Improve the variable naming.
;  8. Added code comments.
;  9. Added four window flags.
; 10. Added the program header. 
; Conclussion: Made your code more robust and bug resistant.
;              Hopefully reading and understanding the code will be easier
;              and faster for everyone.

EnableExplicit

;{ Global Variables
Global C.l                             ;  1   repetitions
Global Cellsize.l                      ;  1   repetitions
Global CellCoordX.l                    ;  1   repetitions
Global CellCoordY.l                    ;  1   repetitions
Global RowLines.l                      ;  1   repetitions
Global ColumnLines.l                   ;  1   repetitions
Global MaxRowLines.l                   ;  1   repetitions
Global MaxColLines.l                   ;  0   repetitions
Global LineColor.l                     ;  2   repetitions
Global CellRandomColor.l               ;  1   repetitions
Global Flags.l                         ;  1   repetitions
;}

InitSprite() 
;{ Initial values assignment
MaxColLines = 540 
MaxRowLines = 450  
          C = $82BF62 
   Cellsize = 30
  LineColor = $313100
  ;}
  
Flags = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered
Flags = Flags | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget
OpenWindow(0, 35, 35, MaxColLines + 1, MaxRowLines + 40, "Awesome Pixels", Flags)  
OpenWindowedScreen(WindowID(0), 0, 0, MaxColLines + 1, MaxRowLines + 40, 1, 0, 0)  

StartDrawing(ScreenOutput())

;{ Draw the grid row lines.
For RowLines = 0 To MaxRowLines Step 30 
  Line(0, RowLines, MaxColLines + 1, 1, LineColor)  
Next RowLines
;}

;{ Draw the grid column lines.
For ColumnLines = 0 To MaxColLines Step 30 
  Line(ColumnLines, 0, 1, MaxRowLines + 1, LineColor)  
Next ColumnLines
;}

;{ Fill the cell with random color
For CellCoordX = 0 To MaxColLines - Cellsize Step 30  
  For CellCoordY = 0 To MaxRowLines - Cellsize Step 30 
    If Not Random(4) 
      CellRandomColor = C * 1.3 + (CellCoordX + CellCoordY) * 3
      Box(CellCoordX, CellCoordY, Cellsize - 1, Cellsize - 1, CellRandomColor)  
    EndIf
  Next CellCoordY
Next CellCoordX  
;}

StopDrawing() 
FlipBuffers()

Repeat 
Until WaitWindowEvent() = 13116

End
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Can you make something awesome from my 11-line program?

Post by firace »

Thanks for both replies!

@Keya: I hadn't thought about the Conway's Game of Life! Thank you for the links to PB implementations.

@Amilcar Matos: Thanks for putting more structure into it. However, I meant this topic to be about compact code, just for fun - a bit in the PurePunch spirit, but with no strict rules - just be creative :)
I will clarify that in the initial post.

A quick example of a clock-like variant (though it's quite unpolished and incomplete) with just a few more lines:

Code: Select all

InitSprite() :  W=750 : H=540  : C = $824F12 : Cellsize = 30

OpenWindow(0, 35, 35, W+1, H, "E7", #PB_Window_SizeGadget|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,W+1,H,1,0,0)  : AddWindowTimer(0, 1, 990)

CreateSprite(33, W+200,W+200, #PB_Sprite_AlphaBlending)

Repeat 
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow :   Break
    Case #PB_Event_Timer : 
          
      x = 0 :  Repeat : x+1
        ClearScreen(0)           
        RotateSprite(33,9,#PB_Relative) : DisplayTransparentSprite(33,-100,-100,134) 
        FlipBuffers()
      Until x = 8
      
      StartDrawing(SpriteOutput(33))
      
      For a = 0 To W+200-Cellsize Step 30 : For b = 0 To W+200-Cellsize Step 30 
          If Not Random(4) :   Box(a, b, Cellsize-1, Cellsize-1, C+(a+b)*3)  : DrawRotatedText(a+3,b+3,RSet(Str(Second(Date())),2,"0"),0) : EndIf
      Next : Next    
      
      StopDrawing() :  DisplayTransparentSprite(33,-100,-100,134) : TransparentSpriteColor(33,0) 
      
  EndSelect
ForEver
I know, I'm not the greatest coder around, but I just like to try / throw around simple ideas :)
Amilcar Matos
User
User
Posts: 43
Joined: Thu Nov 27, 2014 3:10 pm
Location: San Juan, Puerto Rico

Re: Can you add something to make my 11 LOCs more awesome?

Post by Amilcar Matos »

Code: Select all

;{- Program header
;==Code Header Comment==============================
;        Name/title: E7.pb
;   Executable name: E7.exe
;           Version: 1.0
;            Author: Firace
;     Collaborators: Amílcar Matos Pérez
;           Release: 14/Oct/2015
;  Operating system: Windows  [X]GUI
;  Compiler version: PureBasic 5.31 (x86)
;         Copyright: 
;           License: 
;         Libraries: 
;            Forum : http://www.purebasic.fr/english/viewtopic.php?f=7&t=63685
;       Explanation: A vertigo variant of the Awesome Pixels program.
; ==================================================
;.......10........20........30........40........50........60........70........80
;}
EnableExplicit

Enumeration   ;{
  #MainWindow
EndEnumeration  ;}

;{ Global Variables
Global InnerWidth.l 
Global InnerHeigth.l 
Global Cellsize.l
Global Flags.l
Global TurnCounter.l
Global MaxTurns.l
Global UserValues.l
Global Swirl.l
Global Rotation.l
Global BoxContent.l
Global ShowRowLines.l
Global ShowColLines.l
;}

;{ Declare procedures
Declare.l VertigoBox()
Declare.l AwesomePixels()  
Declare.l PlayingInstructions()
;}

;{ Initial values assignment
;{ User Control Panel (enter user play values here).
  UserValues  = #True   ;<- change to #True to play with the numbers. Change to #False for default values.
  InnerWidth  = 540     ; suggestion 540 
  InnerHeigth = 450     ; suggestion 450 
  Cellsize    =  30     ; suggestion  60  
  TurnCounter =   0
  MaxTurns    =   6
  Swirl       = #False
  Rotation    = #True
  BoxContent  = 188     ;<- write the ascii number of the desired character.
  ShowRowLines = #True
  ShowColLines = #True
;}

;{ Default values, write changes above.
If UserValues = #False
  InnerWidth  = 750      
  InnerHeigth = 540      
  Cellsize    =  30       
  TurnCounter =   0
  MaxTurns    =   6
  Swirl       = #False
  Rotation    = #True
  BoxContent  = 0
  ShowRowLines = #True
  ShowColLines = #True
EndIf
;}
;}

InitSprite() 

Flags = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered
Flags = Flags | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget
OpenWindow(#MainWindow, 35, 35, InnerWidth + 1, InnerHeigth, "E7", Flags)  
OpenWindowedScreen(WindowID(#MainWindow), 0, 0, InnerWidth + 1, InnerHeigth, 1, 0, 0) 
AddWindowTimer(#MainWindow, 1, 990)  
PlayingInstructions()
CreateSprite(33, InnerWidth + 200, InnerWidth + 200, #PB_Sprite_AlphaBlending)  


Repeat  ;{ endless loop
  
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow 
      Break
    Case #PB_Event_Timer 

      If TurnCounter < (MaxTurns/2) ; half of the turns to one procedure then the other.
        VertigoBox()
        TurnCounter = TurnCounter + 1
      Else
        ClearScreen(0)
        AwesomePixels()
        TurnCounter = TurnCounter + 1
        If TurnCounter > MaxTurns  
          TurnCounter = 0           ; reset the turn counter to zero.
        EndIf        
      EndIf
      
  EndSelect
  
ForEver  ;}

Procedure.l VertigoBox()
  ;{- Procedure explanation
  ; To spin boxes in the screen.
  ;}
  
  ;{- Protected variables
  Protected BoxRandomColor.l   ; 1   repetitions
  Protected BoxTxt$            ; 1   repetitions
  Protected CoordX.l           ; 1   repetitions
  Protected CoordY.l           ; 1   repetitions
  Protected x.l                ; 2   repetitions
  Protected ColorNb.l
  ;}
  
  ColorNb = $824F12  
  SetWindowTitle(#MainWindow, "E7") 

  ;{ Display and Rotate blocks
  x = 0 
  Repeat 
    x = x + 1 
    If Swirl = #False
      ClearScreen(0)
    EndIf
    If Rotation = #True
      RotateSprite(33, 9, #PB_Relative)  
    EndIf
    
    DisplayTransparentSprite(33, - 100, - 100, 134)  
    FlipBuffers()
  Until x = 8
  ;}
  
  StartDrawing(SpriteOutput(33))
  
  ;{ draw screen content
  For CoordX = 0 To InnerWidth + 200 - Cellsize Step 30  
    For CoordY = 0 To InnerWidth + 200 - Cellsize Step 30 
      If Not Random(4) 
        ; only enters when Random(4) is zero.          
        BoxRandomColor = ColorNb + (CoordX + CoordY) * 3
        Box(CoordX, CoordY, Cellsize - 1, Cellsize - 1, BoxRandomColor)  
        
        If BoxContent = #False
          BoxTxt$ = RSet(Str(Second(Date())), 2, "0") ; runs from 01 to 00
        Else
          BoxTxt$ = Chr(BoxContent)  
        EndIf
        
        DrawRotatedText(CoordX + 3, CoordY + 3, BoxTxt$, 0)  
      EndIf
    Next CoordY
  Next CoordX  
  ;}
  
  StopDrawing() 
  DisplayTransparentSprite(33, - 100, - 100, 134)  
  TransparentSpriteColor(33, 0)  
   
  ProcedureReturn #True
EndProcedure     ;} VertigoBox

Procedure.l AwesomePixels()
  ;{- Procedure explanation
  ; To draw a grid with colored cells.
  ;}
  
  ;{- Protected variables
  Protected CellRandomColor.l   ; 1   repetitions
  Protected CellCoordX.l        ; 1   repetitions
  Protected CellCoordY.l        ; 1   repetitions
  Protected ColumnLines.l       ; 1   repetitions
  Protected RowLines.l          ; 0   repetitions
  Protected MaxRowLines.l
  Protected MaxColLines.l
  Protected LineColor.l
  Protected ColorNb.l
                                ;}
  
  MaxColLines = 540 
  MaxRowLines = 450  
      ColorNb = $82BF62 
    LineColor = $313100
    
  SetWindowTitle(#MainWindow, "Awesome Pixels") 
    
  StartDrawing(ScreenOutput())
  
  ;{ Draw the grid row lines.
  If ShowRowLines < > #False
    For RowLines = 0 To MaxRowLines Step 30 
      Line(0, RowLines, MaxColLines + 1, 1, LineColor)  
    Next RowLines
  EndIf
  ;}
  
  ;{ Draw the grid column lines.
  If ShowColLines < > #False
    For ColumnLines = 0 To MaxColLines Step 30 
      Line(ColumnLines, 0, 1, MaxRowLines + 1, LineColor)  
    Next ColumnLines
  EndIf
  ;}
  
  ;{ Fill the cell with random color
  For CellCoordX = 0 To MaxColLines - Cellsize Step 30  
    For CellCoordY = 0 To MaxRowLines - Cellsize Step 30 
      If Not Random(4) 
        ; only enters when Random(4) is zero.
        CellRandomColor = ColorNb * 1.3 + (CellCoordX + CellCoordY) * 3
        Box(CellCoordX, CellCoordY, Cellsize - 1, Cellsize - 1, CellRandomColor)  
      EndIf
    Next CellCoordY
  Next CellCoordX  
  ;}
  
  StopDrawing() 
  FlipBuffers()
  
  ProcedureReturn #True
EndProcedure     ; AwesomePixels

Procedure.l PlayingInstructions()
  
  Protected Text$
  
  Text$ = "Bienvenu!" + #LF$
  Text$ = Text$ + "The main window is resizable. Grab it then stretch it and see the pixels fly."
  MessageRequester("Games", Text$)
  
  ProcedureReturn #True
EndProcedure
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Can you add something to make my 11 LOCs more awesome?

Post by firace »

Thanks Amilcar :)

And another take at a clock-like thingy... Sorry for the lazy coding style, I'm really knackered right now

EDIT: added a bit more chaos in the mix :)

Image

Code: Select all

InitSprite() :  W=750 : H=540  : C = $324F72 : Cellsize = 50 : LoadFont(99, "Arial", 16)

OpenWindow(0, 35, 35, W+1, H, "E8", #PB_Window_SizeGadget|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,W+1,H,1,0,0)  : AddWindowTimer(0, 1, 1000)

CreateSprite(33, W+200,W+200, #PB_Sprite_AlphaBlending) 
CreateSprite(34, W+200,W+200, #PB_Sprite_AlphaBlending)   

Repeat 
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow :   Break
    Case #PB_Event_Timer :
      
      StartDrawing(SpriteOutput(33)) : Box(0,0,W*2,H*2,0) : DrawingFont(FontID(99))
        sec$ = RSet(Str(Second(Date())),2,"0")
        For a = 0 To W+200-Cellsize Step 50 : For b = 0 To W+200-Cellsize Step 50 
            If Not Random(Val(sec$)/10+1) :   Box(a, b, Cellsize-1, Cellsize-1, C+(a+b)*6)  : DrawRotatedText(a+3,b+3,Sec$,0) : EndIf
        Next : Next    
      StopDrawing()  
      CopySprite(33,34) 
      
      x = 0 :  Repeat : x+1
        ClearScreen(0) 
        RotateSprite(33, 9,#PB_Relative) : DisplayTransparentSprite(33,-100,-100,134) 
        RotateSprite(34,-9,#PB_Relative) : DisplayTransparentSprite(34,-100,-100,134) 
        FlipBuffers()
      Until x = 8
      
  EndSelect
ForEver
Post Reply