Page 1 of 1

Lines madness reloaded!

Posted: Tue Apr 20, 2010 2:40 pm
by Raybarg
Long time ago I posted this code in General forum under other "line madness" application I wrote one lonely night.

I dug it up and changed the tiny bits to make it work with 4.41 and played around with it. It's simple but it works and my opinion is that even though the user "interface" is as simple as it can get, it works logically and fluidly.

During my many years of professional programming career I've come to learn that "never scrap working code". So my thoughts were like, what if I take this small piece and make it into a tiny casual game where player has to use limited number of lines/junctions and then bunch of "balls" are dropped into the contruction and they should all end up going trough a loop placed somewhere on the screen :)

Extended ideas were like "have few different types of lines, accelerator, deccelerators, rubber(bouncy ones) etc". And also instead of wackly acting "snow" pixels, implement basic 2D physics for the balls.

Yes, maybe! :)

Meanwhile, have fun with this little program.
Screenshot: http://www.netphobia.net/jmp/linemadness.png

Code: Select all

EnableExplicit

#RAD = 0.0175
#VEC_SCR_WIDTH = 800
#VEC_SCR_HEIGHT = 600
#VEC_SCR_DEPTH = 16
#VEC_INDICATOR_SIZE = 15

#VEC_SNOWFLAKES = 2000

Structure tVertex
  x.l
  y.l
EndStructure

Structure tSnowFlake
  x.f
  y.f
EndStructure

Declare.f findangle(x1.f,y1.f,x2.f,y2.f)
Declare.l Vectorz_Main()
Declare.l Vertex_Add( List Vertex.tVertex(), lX.l, lY.l )
Declare.l Vertex_Display( List Vertex.tVertex(), bS.b )

Vectorz_Main()


Procedure.l Vectorz_Main()
  Protected MX.l, MY.l, AddReleased.b = #True, bShowDebug.b = #False
  Protected hovered.l
  Protected *selected.tVertex, *previous.tVertex, *near1.tVertex, *near2.tVertex, lListPositionForNearest.l
  Protected bFound.b, bMovingVertex.b = #False
  Protected *temp.tVertex
  Protected distance.f, angle.f, tempsnow.f
  Protected lCheckX.l, lCheckY.l
  Protected lMouseDistance.l, lPreviousDistance.l, lAddSnow.l
  
  NewList Vertex.tVertex()
  NewList Snow.tSnowFlake()

  Vertex_Add( Vertex.tVertex(), 100, 100 )
  Vertex_Add( Vertex.tVertex(), 100, 200 )
  Vertex_Add( Vertex.tVertex(), 200, 200 )
  Vertex_Add( Vertex.tVertex(), 200, 100 )

  *selected = @Vertex()
  
  If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0 
    MessageRequester("Error", "Can't open DirectX 7 Or later", 0)
  Else
    If OpenScreen(#VEC_SCR_WIDTH, #VEC_SCR_HEIGHT, #VEC_SCR_DEPTH, "Pixel Madness by Raybarg")
      Repeat
        FlipBuffers()
        ClearScreen(RGB(0,0,0))
        ExamineKeyboard()
        ExamineMouse()
        
        MX = MouseX()
        MY = MouseY()
        
        If KeyboardPushed(#PB_Key_Add)
          If AddReleased = #True
            Vertex_Add( Vertex.tVertex(), MX, MY )
            AddReleased = #False
          EndIf
        Else
          AddReleased = #True
        EndIf
        
        bMovingVertex = #False
        If MouseButton( #PB_MouseButton_Left ) And bFound = #True
          *selected\x = MX
          *selected\y = MY
          bMovingVertex = #True
        ElseIf MouseButton( #PB_MouseButton_Left ) And lListPositionForNearest >= 0
          SelectElement( Vertex(), lListPositionForNearest )
          InsertElement( Vertex() )
          Vertex()\x = MX
          Vertex()\y = MY 
        EndIf
        
        If MouseButton( #PB_MouseButton_Right )
          bShowDebug = #True
        Else
          bShowDebug = #False
        EndIf
        
        StartDrawing( ScreenOutput() )
        Vertex_Display( Vertex.tVertex(), bShowDebug )
        
        bFound=#False
        *previous = #Null
        *near1 = #Null
        lListPositionForNearest = -1
        
        ForEach Vertex()
          If  MX >= Vertex()\x - (#VEC_INDICATOR_SIZE/2) And  MX <= Vertex()\x + (#VEC_INDICATOR_SIZE/2) And MY >= Vertex()\y - (#VEC_INDICATOR_SIZE/2) And MY <= Vertex()\y + (#VEC_INDICATOR_SIZE/2)
            FrontColor( RGB( 200,0,0))
            DrawingMode( #PB_2DDrawing_Outlined )
            Box( Vertex()\x - (#VEC_INDICATOR_SIZE/2-1), Vertex()\y - (#VEC_INDICATOR_SIZE/2-1), #VEC_INDICATOR_SIZE-1, #VEC_INDICATOR_SIZE-1 )
            bFound = #True
            If bMovingVertex = #False
              *selected.tVertex = @Vertex.tVertex()
            EndIf
            Break
          EndIf
        Next
        ForEach Vertex()
          If *previous = #Null 
            *previous = @Vertex()
            *near1 = @Vertex()
            *near2 = @Vertex()
            lPreviousDistance = 9999
          Else
            distance = Sqr( Pow(Vertex()\x-*previous\x,2) + Pow(Vertex()\y-*previous\y,2) )
            angle = findangle(Vertex()\x, Vertex()\y, *previous\x, *previous\y)
            lCheckX = *previous\x+Cos(angle *#RAD)*(distance/2)
            lCheckY = *previous\y+Sin(angle *#RAD)*(distance/2)
            lMouseDistance = Sqr( Pow(lCheckX-MX,2) + Pow(lCheckY-MY,2) )
            If lMouseDistance < distance/2 And lMouseDistance < lPreviousDistance
              *near1 = *previous
              *near2 = @Vertex()
              lListPositionForNearest = ListIndex( Vertex() )
              lPreviousDistance = lMouseDistance
            EndIf
            *previous = @Vertex()
          EndIf
        Next
        If lListPositionForNearest >= 0 And bFound = #False
          LineXY( *near1\x, *near1\y, *near2\x, *near2\y, RGB($FF,$CC,00) )
        EndIf
        
        
        
        If lAddSnow < #VEC_SNOWFLAKES
          AddElement( Snow() )
          Snow()\x = Random( #VEC_SCR_WIDTH/2 ) + #VEC_SCR_WIDTH/4
          Snow()\y = 1
          lAddSnow = lAddSnow +1
        EndIf
        
        FrontColor( RGB( 255,255,255))
        ForEach Snow()
          tempsnow = Snow()\x + Random(100/50)-1
          
          If Point(tempsnow, Snow()\y+1) = 0 And Point(Snow()\x,Snow()\y+1) = 0
            Snow()\y = Snow()\y + 1
            Snow()\x = tempsnow
          ElseIf Point(Snow()\x+1,Snow()\y) = 0 And Point(Snow()\x+1,Snow()\y+1) = 0 And Point(Snow()\x+1,Snow()\y-1) = 0
            Snow()\y = Snow()\y -1
            Snow()\x = Snow()\x +1
          ElseIf Point(Snow()\x-1,Snow()\y) = 0 And Point(Snow()\x-1,Snow()\y+1) = 0 And Point(Snow()\x-1,Snow()\y-1) = 0
            Snow()\y = Snow()\y -1
            Snow()\x = Snow()\x -1
          EndIf
          
          Plot( Snow()\x, Snow()\y )
          If Snow()\y >= #VEC_SCR_HEIGHT-5
            Snow()\x = Random( #VEC_SCR_WIDTH/2 ) + #VEC_SCR_WIDTH/4
            Snow()\y = 1
          EndIf
        Next
        
        
        LineXY( MX - 6, MY, MX+6, MY, RGB($FF,$CC,00) )
        LineXY( MX, MY - 6, MX, MY+6, RGB($FF,$CC,00) )
        
        StopDrawing()
        
      Until KeyboardPushed(#PB_Key_Escape)
      
    Else
      MessageRequester("Error", "Could not open fullscreen mode", 0)
    EndIf
  EndIf
  
  ProcedureReturn #Null
EndProcedure

Procedure.l Vertex_Add( List Vertex.tVertex(), lX.l, lY.l )
  AddElement( Vertex() )
  Vertex()\x = lX
  Vertex()\y = lY
   
  ProcedureReturn #Null
EndProcedure

Procedure.l Vertex_Display( List Vertex.tVertex(), bS.b )
  Protected lPX.l, lPY.l, distance.f, angle.f
  
  DrawingMode( #PB_2DDrawing_Outlined )
  ResetList( Vertex() )
  While NextElement( Vertex() )
    FrontColor( RGB( 128,128,128))
    Box( Vertex()\x - (#VEC_INDICATOR_SIZE/2), Vertex()\y - (#VEC_INDICATOR_SIZE/2), #VEC_INDICATOR_SIZE+1, #VEC_INDICATOR_SIZE+1 )
    If lPX <> 0
      LineXY( Vertex()\x, Vertex()\y, lPX, lPY )
      
      If bS = #True
        distance = Sqr( Pow(Vertex()\x-lPX,2) + Pow(Vertex()\y-lPY,2) )
        angle = findangle(Vertex()\x, Vertex()\y, lPX, lPY)
        FrontColor( RGB( $80,00,00))
        Circle( lPX+Cos(angle *#RAD)*(distance/2), lPY+Sin(angle *#RAD)*(distance/2), distance/2 )
      EndIf
    EndIf
    lPX = Vertex()\x
    lPY = Vertex()\y 
  Wend
  
  ProcedureReturn #Null
EndProcedure

Procedure.f findangle( x1.f, y1.f, x2.f, y2.f )
  Protected a.f, b.f, c.f, angle.f
  a.f = x1 - x2 
  b.f = y2 - y1 
  c.f = Sqr( a * a + b * b ) 
  angle.f = ACos( a / c ) * 57.29577 
  If y1 < y2 
    angle = 360.0 - angle
  EndIf 
  ProcedureReturn angle.f
EndProcedure

Re: Lines madness reloaded!

Posted: Tue Apr 20, 2010 3:30 pm
by Foz
:mrgreen: Love It! :mrgreen:

Re: Lines madness reloaded!

Posted: Wed May 19, 2010 11:39 pm
by PureLeo
Very cool XD

Re: Lines madness reloaded!

Posted: Thu May 20, 2010 6:53 am
by Pureabc
Very Nice!!