PLAYBASIC SOURCE CODES


Merged Sorted Arrays

Merged Sorted Arrays

This function takes two two sorted Integer (Numeric) and merged them into a new array.

PlayBASIC Code:
Size=100

Dim Test1(Size)
Dim Test2(Size)

; fill both arrays with some random integers between 0 and 1000
for lp =0 to Size
Test1(lp)=rnd(1000)
Test2(lp)=rnd(1000)
next

; Place a couple of value at the end of our arrays to check we're
; getting all the values in the merged output
test1(Size) = 5000
test2(Size) = 9999


; Sort the Arrays 
SortArray Test1(),0,Size
SortArray Test2(),0,Size


Dim Result(0)


Result()=MergeSortedArrays(Test1(), Test2())

Sync
waitkey




Function MergeSortedArrays(a(), b())

 ; dim our arrat C() to hold both merged arrays
  A_Size =getarrayelements(a())
 B_Size =getarrayelements(b())



 ; output array total size
  C_Size = A_Size+B_Size+1
  Dim c(C_Size)

  ; Initialize variables to track the current position in each array
  i = 0
  j = 0
  for LP=0 to C_Size
   ; Check if the value in array A is
   ; smaller than the value in array B

   if a(i) < b(j) 
       ; Add the value from array A to the merged array
      c(lp) = a(i)
      i++ ; Move to the next element in array A

; Check if we're out of values in array A
if i>A_Size
; If so, we copy the rest of B() to C()
For CopyLP = j to B_Size
LP++
c(LP)=b(CopyLP)
next

  endif
  
    Else 
    
    ; Otherwise, the value in array B is smaller
      ; Add the value from array B to the merged array
      c(lp) = b(j)
      j++ ; Move to the next element in array B

; Check if we're out of values in array B
if j>B_Size

; if so, we can copy the rest of A() to C()
For CopyLP = i to A_Size
LP++
c(lp)=a(CopyLP)
next
endif

    Endif

  next lp
  ; Return the merged array
EndFunction c() 
COMMANDS USED: DIM | RND | SORTARRAY | SYNC | WAITKEY | GETARRAYELEMENTS |




Learn To Code How it Works:

The function called MergeSortedArrays takes two sorted arrays, a and b, as input and returns a new array containing the merged and sorted values from both input arrays.

The function starts by declaring a new array, c, to hold the merged values of the two input arrays. It then initializes three variables, i, j, and lp, which will be used to track the current position in each of the input arrays and to loop through the elements of the output array.


The function then enters a FOR loop which will iterate lp from 0 to the size of the output array. Inside the loop, the function compares the current element of array a to the current element of array b. If the element from array a is smaller, it is added to the merged array and the i variable is incremented to move to the next element in array a. If the element from array b is smaller, it is added to the merged array and the j variable is incremented to move to the next element in array b.


If the end of either array a or b is reached, the rest of the values in the other array are added to the output array in a separate FOR loop. Finally, the function returns the merged array c.

Lines Intersect

    This code is a function that determines if two lines intersect and returns the intersection point as the coordinates (x,y). The function takes in the starting and ending x and y coordinates for each line as arguments. The function first calculates the slope and length of each line. It then uses these values to determine if the lines intersect and, if they do, calculates the intersection point. The function returns a boolean value indicating whether the lines intersect, as well as the x and y coordinates of the intersection point. The function is called in a loop that clears the screen, draws the two lines with the mouse coordinates as one of the endpoints, and prints the intersection point (if the lines intersect).

PlayBASIC Code:
Do
// Clear the screen
Cls

// Set line1StartX, line1StartY, line1EndX, and line1EndY to variables
line1StartX=100
line1StartY=100
line1EndX = mousex()
line1EndY = mousey()

// Set line2StartX, line2StartY, line2EndX, and line2EndY to variables
line2StartX=20
line2StartY=200
line2EndX=1000
line2EndY=700

// Draw line1 in black
linec line1StartX, line1StartY, line1EndX, line1EndY,-1

// Draw line2 in green
linec line2StartX, line2StartY, line2EndX, line2EndY,$00ff00

// Check if lines intersect
State,x#,y#=Lines_Intersect(line1StartX, line1StartY, line1EndX, line1EndY,_
            line2StartX, line2StartY, line2EndX, line2EndY)

// Print intersect state
print State

// If lines intersect, draw a red circle at intersect point
if State
Circlec X#,y#,10,true,$ff0000
endif

// Display the screen

sync
loop spacekey()

end


 
Function Lines_Intersect(line1StartX, line1StartY, line1EndX, line1EndY, line2StartX, line2StartY, line2EndX, line2EndY)

// Initialize variables
intersects = False
intersectX = 0
intersectY = 0
s1_x# = line1EndX - line1StartX
s1_y# = line1EndY - line1StartY
s2_x# = line2EndX - line2StartX
s2_y# = line2EndY - line2StartY

// Calculate s and t
s# = (-s1_y# * (line1StartX - line2StartX) + s1_x# * (line1StartY - line2StartY)) / (-s2_x# * s1_y# + s1_x# * s2_y#)
t# = ( s2_x# * (line1StartY - line2StartY) - s2_y# * (line1StartX - line2StartX)) / (-s2_x# * s1_y# + s1_x# * s2_y#)

// Check if s and t are within valid range
if s# >= 0 and s# <= 1 and t# >= 0 and t# <= 1
// Set intersects to true and calculate intersectX and intersectY
    intersects = True
    intersectX# = line1StartX + (t# * s1_x#)
    intersectY# = line1StartY + (t# * s1_y#)
EndIf

// Return intersects, intersectX, and intersectY

EndFunction intersects, intersectX#, intersectY#
COMMANDS USED: CLS | MOUSEX | MOUSEY | LINEC | PRINT | CIRCLEC | SYNC | SPACEKEY | AND |

Hal Teach me to code - Morphing Shapes

     This tiny source code snippet uses PlayBASIC's shapes to draw batches of line fragments. The fragments are all connected in a circle were each vertex is waving and rotating. On their own the effect is really simple; but when you overlay (rotate and scale) a bunch of them on top of each other it creates a sort of button effect similar to the robotic voice in the classic science function movie 2001 A Space Odyssey. Excuse my poor impressions of Hal in this video.. It's just for laughs


Video:


3d Fire Lines - PlayBASIC Edition

     I originally wrote this demo 20 years ago in #DarkBASIC so it's fitting today to bring to #PlayBASIC in full glory

     The code is basically the same; visually i've added alpha addition to the lines and a globe to the line head with an alpha multiply pass to fade previous pixel date away.

     I did tweak the line logic also so that rather than randomly jump between directions it'll interpolate between them. Giving a more curved motion the lines..

     In this edition we've added smoother motion of the fire lines with alpha addition and rendering a circle to show the head of the line as well as a pass of alpha multiply.


Video:



Play-BASIC- 3D Fire Lines - 2022-04-16.jpg

Destructible Aliens From Space

(Destructible) Aliens From Space - (2021-03-31 )

Destructible Aliens From Space is Space Invaders Styled game with destructible player , aliens and bunkers.. Destroy them pixel by pixel.



Aliens-From-Space-Pic0001.png