News:

Function Finder  Find all the functions within source code files

Main Menu

Learn To Code: Code For beginners

Started by kevin, April 18, 2022, 09:04:18 PM

Previous topic - Next topic

kevin

   Note:  These examples are in no particular order.    They're just small bits of demo to help show certain Ideas.  




 
Learn To Code Question:


         How do I  Render Code For Beginners message down screen, each time starting from a new character within the message



   Answer:


 
         In this example we're going to display a message to the screen.   Each time we draw the message; we'll be starting from the different character within the message.  

  For example:


Code For Beginners
ode For Beginners C
de For Beginners Co

etc etc


  This will help new programmers get used to working with STRING$ mainly.  But there's a few other tricky functions hidden away in this code too.  





PlayBASIC Code: [Select]
   Message$="Coding For Beginners "

For Row=0 to Len(Message$)-1

s$=""
For ChrLP =0 to Len(Message$)-1
// Compute the character from the message we should grab this loop
ChrINDEX = 1 +( Mod(ChrLP+Row,Len(MEssage$)))
// Add that Character to the output message in s$
s$=s$+mid$(Message$,ChrINDEX)
next

// Display the output message to the screen
print s$

next

// Display the screen and wait for a key press before ending
Sync
waitkey
waitnokey






      Note: Cut'N'Paste this code into a PlayBASIC - Then press F5 to run...


     Example output bellow

kevin

#1

Learn To Code Question:


         How do i find the smallest value within an integer array ?



   Answer:


        You can sort the array;  but it's not necessary.

        So here's the basic logic..

      Step 1

            Grab the first element.
           This becomes your starting smallest number.  


      Step 2


         - Then loop through remaining elements.
         - Compare each element to the current smallest value.
            - If this element is smaller; then assign
              this value to the current smallest
              and continue loop.

      Step #3

           Display Smallest Value





PlayBASIC Code: [Select]
; PROJECT : Project1
; AUTHOR : LearnToCode Learn To Code In PlayBASIC
; CREATED : 5/05/2022
; ---------------------------------------------------------------------

/*

--------------------------------------------------------------------------------------
Question:
--------------------------------------------------------------------------------------

How do i find the smallest value within an integer array ?


--------------------------------------------------------------------------------------
Anwser:
--------------------------------------------------------------------------------------

You can sort the array; but it's not necessary.

So here's the basic logic..

Step 1
======

Grab the first element.
This becomes your starting smallest number.


Step 2
======


- Then loop through remaining elements.
- Compare each element to the current smallest value.
- If this element is smaller; then assign
this value to the current smallest
and continue loop.

Step #3

Display Smallest Value




--------------------------------------------------------------------------------------

*/



// --------------------------------------------------------------------------------------
// Example: Find Smallest Value in array - How to Find the smallest number within an array
// --------------------------------------------------------------------------------------

// Set up an array values so we have something to search

Size = 100

// Declare an integer array called NUmbers()
Dim Numbers(Size)

// Loop through each element and write a random number
// between 1000 and 10000 into it
For lp =0 to 100
Numbers(lp) = rndrange(1000,10000)
next


// -------------------------------------------------------
// Find the Smallest number in array
// -------------------------------------------------------

// Step 1
// Grab the first element in the array, in this example
// that's the element at index 0 within the array.
// This variable will hold what the smallest number
// currently is
SmallestNUMBER=Numbers(0)

// Step 2
// Loop through the remain elements within the
// Numbers() array
For lp = 1 to Size

// Compare if this the value at Number(lp)
// is SMALLER then our SmallestNUMBER variable.
if Numbers(lp) < SmallestNUMBER

// If it is; then this becomes our current smallest
SmallestNUMBER = Numbers(lp)
endif
next

// At the end of the For / Next Loop. SmallestNUMBER will be the
// the smallest within the Numbers() array


// Display the SmallestNUMBER variable to the screen
Print SmallestNUMBER

// Show the screen to the user and Wait before exiting
Sync
waitkey






kevin

#2

Learn To Code Question:


         
         Nested For / NEXT loops -  What's going on here ?



   Answer:


     at glance the inner loop is counting from 1 to the whatever the outer loop currently is.


     So  FOR  I=1 to 10 loop.  I will be initialized to equal 1  when entering the for  next loop block.



      When execution hits the inner loop.   The inner counter is set to it's starting position of 1  ( For J=1 to I )


       Since the inner loop is using the outter loops current loop counter of I.  Then whatever I is;  will be the range of the inner J for loop.  


      This means the first time the inner runs I will equal 1, so the J loop  will only run once.

      If we could substitute the I variable values at run time; the inner loop would look like this on that first loop.

      For J=1 to 1



       now since the I  loop range is from 1 through all the numbers up to 10.     The I loop will keep running whatever is inside it.  Which in turn will run the inner J for next loop.





PlayBASIC Code: [Select]
   // --------------------------------------------------
// Learn to code: Nesting FOR / NEXT LOOPS
// --------------------------------------------------

// Set up a FOR / NEXT loop that will repeat 10 times
for I =1 to 10

// Set up inner FOR / NEXT loop that will repeat
// for the 1 UP to and Including whatever that
// value of I currently is
for J =1 to I

// Draw a row of circles across screen
// so we've something to see
Circle 100+(J*32),100+(I*32),16,true

next J

next I


// Show the graphics screen to the user
// and wait for key press before ending
Sync
waitkey



kevin

#3

Learn To Code Question:


         
         Given a string, return a string where for every char in the original, there are two chars.

         Input:  PlayBASIC

         Output:  PPllaayyBBAASSIICC
 



   Answer:


        To do this we loop through all the characters in the source string; each loop we grab each character and add it to the output string$ twice.


PlayBASIC Code: [Select]
   print DoubleChar("PlayBASIC")

Sync
waitkey


Function DoubleChar(String$)

for lp=1 to Len(String$)
// read one character from the original string
ThisCHR$=mid$(String$,lp,1)

// Add it to the output twice
result$+=(ThisCHR$+ThisCHR$)
next

EndFunction Result$






kevin




PlayBASIC Code: [Select]
   print LeadingZeros$(1,1)
print LeadingZeros$(1,2)
print LeadingZeros$(1,3)
print LeadingZeros$(1,4)
print LeadingZeros$(1,5)
print LeadingZeros$(1,6)

sync
waitkey




Function LeadingZeros$(Number,Digits)
if digits>0
Result$=Make$("0",Digits)
Result$+=Str$(Number)
Result$=Right$(Result$,Digits)
endif
Endfunction Result$





kevin




   These functions are used to check if a character in a string is in uppercase or lowercase. They can be useful when working with text and wanting to ensure that it is formatted correctly


PlayBASIC Code: [Select]
   TestString$="Hello World"


// Scan String for upper case letters
for lp=1 to Len(TestString$)
// grab a character from the sourcestring
s$=mid$(TestString$,lp,1)
print s$+" IsUpper ="+str$(IsUpper(s$))
next


print ""

// Scan String for lower case letters
for lp=1 to Len(TestString$)
// grab a character from the sourcestring
s$=mid$(TestString$,lp,1)
print s$+" IsLower ="+str$(IsLower(s$))
next





sync
waitkey







Function IsUpper(ThisCHR$)
local ThisCHR = Mid(ThisCHR$,1)
Result = range(ThisCHR,asc("A"),asc("Z"))
EndFunction Result

Function IsLower(ThisCHR$)
local ThisCHR = Mid(ThisCHR$,1)
Result = range(ThisCHR,asc("a"),asc("z"))
EndFunction Result




kevin

#6

 
Learn To Code Question:


           How would I read all the pixels on screen and add a random RGB colour to each one ?



   Answer:


 This code will loop through all the pixels in the screen, read the current pixel color, add a random RGB color to it, and then draw the new pixel color to the screen using FastDot. By using FastPoint and FastDot, we can make the process of reading and writing pixels faster than using their regular counterparts (Point and DotC). However, it's important to note that FastPoint and FastDot can only be used on surfaces that have been locked using LockBuffer, and should not be used if the surface has been captured using CaptureToScene or CaptureToWorld.

PlayBASIC Code: [Select]
; Open A Screen of 640x by 480y, 32bit in Windowed mode
OpenScreen 640,480,32,1

; Lock the screen buffer
LockBuffer

; Read a pixel from the surface to ensure it's locked correctly
temp = Point(0, 0)

; Loop through all the pixels in the screen
For ylp = 0 To GetScreenHeight() - 1
For xlp = 0 To GetScreenWidth() - 1
; Read the current pixel color
currentColor = FastPoint(xlp, ylp)
; Add a random RGB color to the current pixel color
newColor = currentColor + RndRGB()
; Draw the new pixel color to the screen
FastDot xlp, ylp, newColor
Next
Next

; Unlock the screen buffer
UnlockBuffer

; Show the screen
Sync

; Wait for a key press
WaitKey