News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Double the Fun: How to Return Multiple Values from a Function in PlayBASIC!

Started by kevin, August 22, 2024, 04:56:46 AM

Previous topic - Next topic

kevin

Double the Fun: How to Return Multiple Values from a Function in PlayBASIC!


If you're new to coding games in PlayBASIC, you'll be excited to learn about a cool feature that allows you to return more than one value from a function. This might sound a bit advanced, but it's super useful and not too hard to understand once you see it in action!

Imagine you're drawing something on the screen, like a spiral. To do this, you need to keep track of where you are and where you want to go next. Normally, when you ask a function to give you a new position, it might only give you back one value. But in PlayBASIC, you can return multiple values at once, which makes things a lot easier.

In our example code, we have a function called `Polar_Step` that helps us calculate the next position in our spiral. This function takes in the current position, an angle, and a distance, then gives us back two new values: the X and Y coordinates of the next point. These two values are returned together, so we can use them right away to draw the next line in our spiral.

This ability to return multiple values is super handy when you're working on more complex projects. It lets you bundle up information neatly and keep your code clean and easy to manage.

So, next time you're coding in PlayBASIC, remember that you can make your functions even more powerful by returning more than one value! It's a small trick that can make a big difference as you build your games.

PlayBASIC Code: [Select]
    //  

x#= GetScreenWidth()/2
Y#= GetScreenHeight()/2


for lp = 0 to 100

// Compute to the next position
X2#,Y2#=Polar_Step(X#,Y#,lp*10,lp*0.25)

// draw line from the last cordinate to the next cordinate
line x#,y#,x2#,y2#

// set this new cordinate to the last cord
x#=x2#
y#=y2#

next


Sync
waitkey


Function Polar_Step(X#,Y#,Angle#,Distance#)
NewX#=X#+cos(Angle#)*Distance#
NewY#=Y#+sin(Angle#)*Distance#
EndFunction NewX#,NewY#






Here's a line-by-line summary of the provided PlayBASIC code:

x#= GetScreenWidth()/2

- Sets `x#` to the horizontal center of the screen by dividing the screen width by 2.

Y#= GetScreenHeight()/2

- Sets `y#` to the vertical center of the screen by dividing the screen height by 2.

for lp = 0 to 100

- Starts a loop that will run 101 times (from 0 to 100).

    // Compute to the next position
    X2#,Y2#=Polar_Step(X#,Y#,lp*10,lp*0.25)

- Calls the `Polar_Step` function to calculate the next position on the spiral. The angle is `lp * 10`, and the distance is `lp * 0.25`. The function returns two values, `X2#` and `Y2#`, representing the new position.

    // draw  line from the last coordinate to the next coordinate
    line x#,y#,x2#,y2#

- Draws a line from the current position (`x#, y#`) to the newly calculated position (`x2#, y2#`).

    // set this new coordinate to the last coordinate
    x#=x2#
    y#=y2#

- Updates the current position (`x#, y#`) to the new position (`x2#, y2#`).

next

- Ends the loop and goes back to the start for the next iteration.

```plaintext
Sync
```
- Refreshes the screen, displaying the drawn spiral.

waitkey

- Waits for the user to press a key before ending the program.

Function Polar_Step(X#,Y#,Angle#,Distance#)


- Defines the `Polar_Step` function that calculates a new position based on an angle and distance from a given point (`X#, Y#`).

    NewX#=X#+cos(Angle#)*Distance#

- Calculates the new X coordinate (`NewX#`) using the cosine of the angle and the distance.

    NewY#=Y#+sin(Angle#)*Distance#

- Calculates the new Y coordinate (`NewY#`) using the sine of the angle and the distance.

EndFunction NewX#,NewY#

- Ends the `Polar_Step` function and returns the new coordinates (`NewX#, NewY#`).