DSiWare Forum

Topic: Petit Computer Tutorials

Showing 1 to 20 of 95

AuthorMessage
Avatar

Shadow1364

1. Posted:

Ok, I've been going to the Petit Computer threads and noticed that many people don't know how to program anything. I made this to have people post tutorials for different functions in easy-to-follow messages. To keep it easy to browse I ask of you to not post questions about Petit Computer itself. This is only meant to be an easy reference guide. Thank you.

Edited on by theblackdragon

FC: 1934-1622-0902

AuthorMessage
Avatar

petitprogrammer

2. Posted:

To load sprites and make them move use this coding. PS.CLS clears the screen of text so it's good to use at the start of programs.PPS.First you need to make sprites in PRG:CHU0. And set it to sprite mode also you'll notice the numbers 0-7 on the bottom where i put 0 just change it depending on what one you use and filename is what you sved it as.NOTE USING FALSE WHEN LOADING A PROGRAM WILL STOP IT FROM POSTING THE OK MESSAGE AT THE BEGINNING ON THE TOUCH SCREEN.
CLS
LOAD"SPU0:FILENAME",FALSE
SPSET 0,LOCATION,0,0,0,0,16,16
PRINT "Use the d-pad to move.
@LOOP
IF BTRIG()==1 GOTO @UP
IF BTRIG()==2 GOTO @DOWN
IF BTRIG()==4 GOTO @LEFT
IF BTRIG()==8 GOTO @RIGHT
GOTO @LOOP
@UP
SPREAD(0)X,Y
SPOFS 0,X,Y-16,60
GOTO @LOOP
@DOWN
SPREAD(0),X,Y
SPOFS 0,X,Y+16,60
GOTO @LOOP
@LEFT
SPREAD(0)X,Y
SPOFS 0,X-16,Y,60
GOTO @LOOP
@RIGHT
SPREAD(0)X,Y
SPOFS 0,X+16,Y,60
GOTO @LOOP

Edited on by petitprogrammer

petitprogrammer

AuthorMessage
Avatar

Shadow1364

3. Posted:

@petitprogramer Thanx for starting us off! Now for the basics.
PRINT "HELLO" will display the word within the quotations -(hello)- on the screen
WAIT 120 will tell it to wait for 2 seconds. 60 is the equivalent of 1 second, and so on.

FC: 1934-1622-0902

AuthorMessage
Avatar

ramstrong

4. Posted:

Just a note that BGMPLAY takes a number, not a string. So, if you do something like BGMPLAY MENU right on top of your program without defining the content of MENU variable, it is equivalent to BGMPLAY 0. There are a lot of preset music that is very convenient to use. Look under Help Menu 10 - Preset Music. You have 30 songs to choose from.

Petit Computer Journal
Old site http://ramstrong.blogspot.com

Let's just call a spade, a spade.

3DS Friend Code: 1091-7596-4855

AuthorMessage
Avatar

ramstrong

5. Posted:

DATA statements are nice. You can have a lot of data in compact form. I always use label and RESTORE command along with READ, like this:
RESTORE @MYDATA:FOR I=0 to 2:READ A$[I]:NEXT
...
END :' End of program

@MYDATA
DATA "HELLO","WORLD","FRIEND"

Also, you can have more than one, selected by RESTORE keyword. For example, you can RESTORE @MYDATA2 to select that particular data. This is useful because you can have @LEVEL1, @LEVEL2, @LEVEL3 and so on. Use RESTORE LEVEL$ to do so, after assigning appropriate entry to LEVEL$
@LEVEL1
DATA for level1
@LEVEL2
DATA for level2
@LEVEL3
DATA for level3

That being said, I'd just sooner assign the data to string variables directly. DATA keyword is specific to BASIC. You won't find it in any other languages.

Petit Computer Journal
Old site http://ramstrong.blogspot.com

Let's just call a spade, a spade.

3DS Friend Code: 1091-7596-4855

AuthorMessage
Avatar

Morpheel

6. Posted:

I guess I'll repost all my tutorially stuff here:

When you use PRINT (or ?) you can use either , or ; to separate normal text from variables.
, will print the variable after a "tab" while ; will print it right after the text.

: is used to separate different functions on the same line, like PRINT "HI": PRINT "HI AGAIN".

Oh, and to generate saves for your games you will most likely need to use str$(), val() and the "search and replace" functions ejamer mentioned.
To save the state of your game you need to fill the MEM$ variable which you can then save using SAVE"MEM:name of the file" and load using LOAD"MEM:name of the file".
All you need to do is save all the important variables values to that file at the moment of saving and then download them back on the variables when you want to load.
I think I read that every mem file is limited to 255 bytes (1 character = 1 byte) so you need to squeeze all you can out from those bytes.

CHRED lets you edit CHR files, these are the ones that you can use as sprites AND background tiles.
The CHR files you create with this can be loaded to a game to replace one of the following default settings:
BGU0-3 (background tiles)
SPU0-7(sprites)
BGF0(text characters)
Example, the comand LOAD "SPU1:mysprites" will replace the default SPU1 page of sprites (the one with the warrior and the witch) with those on the CHR file named "mysprites".
It doesn't matter where in the program you create your sprites, once you save them as a CHR flie, you can use them for any of these purposes.

Note: saving will only save the page of sprites shown on the upper screen, if you edited more than one page you need to save each one manually before leaving the program.

SCRED allows you to create SCR files, SCR files are actually just "premade" backgrounds you can load directly to your game and use them without the need to set every single tile manually by code, backgrounds can use two layers, layer 0 and Layer 1. You need to be very careful with these, L0 is actually the top-most layer and L1 is under it.
The SCRED program start asking if you want to load a character, saying yes brings up the oportunity to load the CHR files you created with CHRED.
To save your background just press X to bring up the menu and then A to pen the File Menu. If you type just S it will only save the layer you're working with right now, type SW to save both (the program will save 2 files, one for layer 0 and another for layer 1)
To use them on your game you need to load both files using LOAD "SCR0:background0" and then LOAD "SCR1:background1"

GRPED lets you create and edit GRP files, which are pretty much just drawings you can load to your game to use instead of backgrounds or something idk. GRP files can be loaded to 1 of 4 layers by using LOAD "GRP(0-3):grpfile".

I don't really fully understand the other one though.

Yeah, that's because CHRED auto-arranges anything you load as background tiles (BGU) in 8x8 squares, so anything you draw in sprite mode (SPU, which by default is 16x16) and load as background tiles (BGU) looks different.

This

QW
ER

Becomes this

QWER

I'm glad I could help.

All the graphical "layers" (but the console) have 2 "pages".
Page 0 is the top screen
Page 1 is the bottom screen.

For example
GPAGE 0
GFILL 0,0,255,191,COLOR
GPAGE 1
GFILL 0,0,128,191,COLOR

Will fill the top screen with a solid color (stored in the COLOR variable) AND fill half of the touch screen with the same color.

Other graphical layers have their own PAGE commands

So, in a way you can only work with a screen at a time, but it's so fast you won't notice it.

Peidorrento wrote:

Does anyone knows how to save and load a single variable in a file? I couldn't figure out googling it... I found that it has something to do with a "mem" file, but I don't understand how to manipulate it.

You need to use the variable MEM$.
As you can see it is an alphanumeric variable so you'll need to use the command STR$(Variable) to save numbers on it.
MEM$ can contain only 255 characters (or was it 256?) so you need to think of a good way to use that space.
After you fill MEM$ with the contents you want to save just use SAVE "MEM:name", that will save all the contents of MEM$ on the MEM file named "name".

Example:
MEM$=STR$(highscore)
SAVE "MEM:HSCORES"

To load it back, you need to use LOAD "MEM:name", that will replace the local MEM$ variable with the contents on the MEM file named "name".
Then you can start reading MEM$, since it is actually a string of characters, if you want to read numbers you'll need to use VAL(string).

Example:
LOAD "MEM:HSCORES"
highscore=VAL(MEM$)

If you have more than one value stored on MEM$, then you'll have to use MID$ or something similar to search through MEM$.
And of course you can save and load more than one MEM file for the same program, but there is only one MEM$ variable.

Well you see, these one of the few things that are actually explained quite nicely on the manual, but I'll try to explain the logic behind sprites.

You can set up to about 100 sprites at the same time on the screen, though you could see them more as "objects" than sprites.
To "initiate" a Sprite (or object) you use SPSET (look the manual for the parameters), the one thing you need to remember is the "Control Number" you give to that sprite (which can be any number from 0 to 99 I think), using that control number you can then use other comands like SPOFS (which allows you to move the sprite around), SPSCALE, SPANGLE, etc.
The same control number is used with the colition detection commands (which are also explained in the manual), so you can check if the sprite 0 is colliding with the sprite 1 or any other sprite and tell the game what to do.
You can reinitialize sprites as much as you want so you can reuse Control Numbers, just remember, only 100 at a time on the screen

I'd recommend you to get a notebook and write all the SP-related commands.

This language is very old school so you won't find any easy solution to make "walls" from the background, you could use invisible sprites or something completely different, you need to think of various ways to solve that, just look at it from different angles until you find the solution that best suits your project, then you'll even be able to reuse that solution in other games!

Backgrounds are limited to 512*512 pixels (that's a little more than 4 full DS screens) and you can scroll through these pixels with easy to use commands, just keep in mind this: Even if you scroll through the background, sprites will stay in the same position until you move them by code, and.... If you want bigger enviroments, you'll need to find a way to update the background by code.

It may sound a little overwhelming, but it is not as hard as it seems... Playing some old games or reading about how they work will help you.
For example: Metroid has actually a set number of pre-made rooms or "room styles" that repeat themselves around the game world with different palletes to create the different areas of planet Zebes (several other NES games work in the same way), by using the DATA and READ commands, that would be actually quite easy to do!

ON-GOSUB/GOTO

It works in this way (writing by memory, I could have something wrong):
You have a numeric variable X and you use:
ON X GOSUB @sub,GOSUB @sub2, GOSUB @sub3

What it really does:

If X==0 THEN GOSUB @sub
If X==1 THEN GOSUB @sub2
If X==2 THEN GOSUB @sub3

It is useful for things like menus.

names can only be 8 characters long.

Eh let's see...
You have
@MAP1
DATA "111111"
DATA "100001"
DATA "102201"
DATA "102201"
DATA "100001"
DATA "111111"

And you want to use that data to draw a 6*6 room using some background tiles.

LABEL$="@MAP1" 'set the label where the DATA is

@DROOM 'a subroutine to draw a 6*6 room using DATA
RESTORE LABEL$ 'set the DATA pointer to that label
FOR Y=0 to 5 'start Y for
READ L$ 'read a line of DATA
FOR X=0 to 5 'start X for
IF MID$(L$,X,1)=="0" THEN BGPUT 0,X,Y,45,8,0,0 'draw background tile 45 if the Xth data of the Yth DATA line is 0
IF MID$(L$,X,1)=="1" THEN BGPUT 0,X,Y,54,8,0,0 'draw background tile 54 if the Xth data of the Yth DATA line is 1
IF MID$(L$,X,1)=="2" THEN BGPUT 0,X,Y,65,8,0,0 'draw background tile 65 if the Xth data of the Yth DATA line is 2
NEXT X:NEXT Y 'Move on
RETURN 'Return

That way you can write yourself multiple DATA structures with number variations to draw different rooms using the same code.
Or something like that.

If you want to make Game and Watch games. I just thought this up:
You could use GRP background with certain higlighted areas (think: Game and Watch character's "shadows") and use GPAINT to make them black or a color similar to the background depending on what should be on screen.

I may need to try it myself now.

Dim is used to set arrays.

Example:

DIM MYARRAY(X)
Creates a numeric array named MYARRAY with space for X numeric values.

These values can be assignated and used by writing MYARRAY(I), where I is any numer from 0 to X-1. Example:

DIM MYARRAY(3)
MYARRAY(0)=5
MYARRAY(1)=2
MYARRAY(2)=MYARRAY(0)+MYARRAY(1)
PRINT MYARRAY(2)

(That will print 7)

You can also create alphanumeric arrays to hold strings. Also, arrays in petit computer can have up to 2 dimensions.
(example)
DIM AR$(X,Y)
Creates an alphanumeric array with space for X*Y string lines.

GOSUB is pretty important, it allows you to go somewhere else on the code without terminating what you're doing at that moment. Example

You have:

PRINT "This goes first"
GOSUB @DOSOMETHING
PRINT "This will be printed 3rd"
GOTO @DOOTHERTHING
PRINT "This won't be printed"

@DOSOMETHING
PRINT "This will be printed 2nd"
RETURN

@DOOTHERTHING
PRINT "This will be printed last"
END

See the difference?
When you use GOSUB you need to end the code of the subroutine with RETURN. When RETURN is reached, the program will go back to where it was before using GOSUB.
When you use GOTO, the program will continue on with whatever is on the new routine, never turning back.

About SPREAD, it works differently from normal commands in that it needs variables to store the info it returns.
The most basic form of the command would be like this:

SPREAD (<control number>), X, Y

That means that you must indicate the control number of the sprite you want to check in the () and then put two variables, one for storing the current X coordinate of that sprite and another for the Y coordinate of it.

It can be expanded (optionally) to include more data:

SPREAD (<cn>),X,Y,Angle,Scale,Character

Only X and Y are required, and you can expand it in that order.
In other words, if you want to obtain the current character data (the image being displayed to represent the sprite), you MUST also ask for X, Y, the angle AND the scale, designating a variable for each (or make them share the same variable, since it will be overwritten anyway). But if you only want to know the angle, there's no need to ask for the scale and character

Edited on by Morpheel

Oops, no username pun.

Oh look! A Morphloggery.
Oh! eShop Gurus.

3DS Friend Code: 0173-1330-0080 | Nintendo Network ID: Abgarok

AuthorMessage
Avatar

ramstrong

7. Posted:

SPRITE TUTORIAL

There is some confusion in how to use sprites in Petit Computer. I hope this text will make the matter a bit clearer. In Petit Computer, sprites can be thought of as independent objects, with its own layer and behavior. This means the graphic,BG,Console display will never be corrupted by sprites.

To use sprites, you must first define their existence. Just as you cannot use Arrays without DIM, you cannot use sprites without SPSET. This is all the command that you need to display sprites.

To animate sprites, you can use SPOFS, and SPCHR.
SPOFS: SPrite OFfSet is the way to position your sprites on screen. There is some confusion as to whether the sprite offset is relative value. It is not relative. It uses absolute measurements. You simply specify the x,y coordinate and the sprite will go there.

SPCHR: SPrite CHaRacter is the image (character number) that you want the sprite to be. For example: Boy is 64. Witch is 96. By changing this value, you can animate the sprite.

Those two commands are the only ones you need to use the sprites. But Petit Computer does provide you with convenient animation capabilities.

Think of the sprites as independent objects. When you tell the sprite to go somewhere via SPOFS, you can specify the time it takes to get there (in number of frames). If you want a sprite to go somewhere in 3 seconds, you can specify it using the time variable. Example: Move sprite 1 to location 15,60 in 180 frames: SPOFS 1,15,60,180. The sprite will do just that, unless there is another command that overrides it.

Think of it as "Set it and forget it" type of mechanism. You can do other things without manually adjusting the sprites. Another convenient feature is SPANIM
SPANIM: SPrite ANIMation lets you cycle animation automatically without your intervention. If you want to animate the walking cycle of the witch, you can do this: SPANIM 1,4,15 where 1 is the sprite number, 4 is the number of frames, and 15 is the number of frames each animation lasts.

Here's an example:
CLS:CLEAR

'BOY ANIMATION - AUTOMATIC
SPSET 2,64,0,0,0,0,16,16
SPANIM 2,4,15
SPOFS 2,0,0:MOVE TO 0,0
SPOFS 2,172,0,660:MOVE TO 172,0 IN 660 FRAMES

'WITCH ANIMATION - MANUAL
SPSET 1,96,0,0,0,0,16,16
FOR J=0 TO 10
FOR I=0 TO 3
VSYNC 15
SPCHR 1,96+I
SPOFS 1,J*16+I*4,16
NEXT
NEXT

'STOP BOY ANIMATION
SPANIM 2,1,0

You can tell the difference between the two easily, if you stop the program midway. The witch animation stops, while the boy animation keeps going.

Here's how you can use the automatic animation for your program. Notice the VSYNC 20. That means to cycle every 20 frames. If you use WAIT 20, the animation timing will be off.

@LOOP
B=BUTTON(0)
IF B AND 1 THEN D$="U"
IF B AND 2 THEN D$="D"
IF B AND 4 THEN D$="L"
IF B AND 8 THEN D$="R"

LOCATE 0,0:?D$
IF D$=="R" THEN A=64:SX=(SX+1)%16
IF D$=="D" THEN A=68:SY=(SY+1)%12
IF D$=="L" THEN A=72:SX=(SX+16-1)%16
IF D$=="U" THEN A=76:SY=(SY+12-1)%12

VSYNC 20:GOSUB @ANIM
GOTO @LOOP

@ANIM
SPCHAR 2,A
SPANIM 2,4,5
SPOFS 2,SX*16,SY*16,20
RETURN

And there you have it: Sprites that moves by DPad yet animates smoothly!

Edited on by ramstrong

Petit Computer Journal
Old site http://ramstrong.blogspot.com

Let's just call a spade, a spade.

3DS Friend Code: 1091-7596-4855

AuthorMessage
Avatar

ramstrong

8. Posted:

Duplicate definition (N, DIM) error?
You cannot redefine variables. You will get this error if you DIM a variable more than once, say, by running the program more than once. You can set up a special @INIT section like this:

'Program start
IF VARSET==FALSE THEN GOSUB @INIT
?"Program begins..."
END

@INIT
?"Initializing variables"
DIM A$[10]
VARSET==TRUE
RETURN

Or you can use CLEAR to clear off all variables in memory.

Petit Computer Journal
Old site http://ramstrong.blogspot.com

Let's just call a spade, a spade.

3DS Friend Code: 1091-7596-4855

AuthorMessage
Avatar

ramstrong

9. Posted:

How to make your own music with BGM. Repost from Post #596

GrabSomeEyes wrote:

Cocobanana wrote:

Its going good.

I dont know how to use MML though:/

cough cough http://www.gamefaqs.com/boards/663843-petit-computer/63524945 cough
AH http://www.gamefaqs.com/boards/663843-petit-computer/63484250 CHOO!
Well then! Excuse me.

Edit:

neoxid500 wrote:

http://d.hatena.ne.jp/eidaht/20120630/1341016869

This was originally on the QR Code sharing thread but people are being CATZ... and keep asking about MMLS. Use this program to make 'em much easier and implement them in your games.

NOW STOP ASKING XD

While I love that program, it doesn't have nearly as many sounds as you get with just regular programming. I just use it to mess around with like Mario Paint. Any actual music making I'd recommend learning how to create in the editor. Plus, the satisfaction of hearing something you worked hard for.

I will add this program, which is straight from Help #44:
Once you have the MML string, put it into DATA. Unlike normal DATA statement, you don't need to READ it. Just need to Refer to it, similar in a way to refer to LABEL for GOTO/GOSUB. Make sure the DATA statements ends with DATA 0.

Some MML strings are too long, so split them into sections, let's say "PART A", "PART B", "PART C". We'll store the song into song#128. User defined song can be numbered 128 to 255.

In your main code, do this:
BGMSETD 128, @MMLDATA <== LABEL FOR MUSIC, CAN BE ANY LABEL
BGMPLAY 128 <== PLAYS THE MUSIC

Somewhere down in the DATA section:
@MMLDATA
DATA "PART A"
DATA "PART B"
DATA "PART C"
DATA 0 <== VERY IMPORTANT.

And that's it. Now find some MML strings out there and do it!

Petit Computer Journal
Old site http://ramstrong.blogspot.com

Let's just call a spade, a spade.

3DS Friend Code: 1091-7596-4855

AuthorMessage
AvatarAdmin

theblackdragon

10. Posted:

@Sprite: our general Petit Computer thread is here if you'd like to ask that question. This thread is only for users to post their completed tutorials, I'm afraid. If you post your question to the proper thread, I'm sure someone will be nice enough to help you out. :3

BEST THREAD EVER
future of NL >:3
[16:43] James: I should learn these site rules more clearly
[16:44] LztheBlehBird: James doesn't know the rules? For shame!!!
[16:44] Vintage: We have rules?
[16:44] Reala: don't expose the staff to sunlight, don't get them wet and don't feed them after midnight

3DS Friend Code: 3136-6802-7042 | Nintendo Network ID: gentlemen_cat | Twitter: theblackdragon

AuthorMessage
Avatar

Linput

11. Posted:

Hi i need help using and making stored data like this:

DATA"HELLO"

basically so i can just use the data to store text for l8r

Linput-3DS friend code: 4725-8494-2702
Pokemon White 2 pal-pad code: 1979 9213 55

I'm currently looking for some peeps to make SALLY a website, facebook, and email. If anyone does this u will be mentioned as a producer in the SALLY OS credits! :)

AuthorMessage
Avatar

Retro_on_theGo

12. Posted:

UUUGHGHH!!!!!! All the terms I don't understand and odd pairing of words! It huuuuurtss. I wish I had the time to make sense of this all and carefully read the posts here multiple times. Guess I'll have to wait until december to get serious with Petit Computer. D:

"When freaky aliens give you lemons, make freaky alien lemonade!" -Hades 3DS FC: 5026-4421-7593
Black 2 FC: 0605 4087 9952 My Backloggery
My Colors! 3D Gallery

AuthorMessage
Avatar

petitgamer

13. Posted:

Retro_on_theGo wrote:

UUUGHGHH!!!!!! All the terms I don't understand and odd pairing of words! It huuuuurtss. I wish I had the time to make sense of this all and carefully read the posts here multiple times. Guess I'll have to wait until december to get serious with Petit Computer. D:

I know right?

3ds FC is 1934-1168-4523
fyi lucas is better than ness in any way shape or form.That is a coldhard fact.

AuthorMessage
Avatar

smartman789

14. Posted:

I have an idea.
Why don't you make a tutorial program and give the qr code for it?
I bet morphtroid could do this. :)

smartman789

AuthorMessage
Avatar

Discostew

15. Posted:

Made a tutorial about large backgrounds + scrolling about a month ago. Guess I might as well add a link to it here. Tutorial comes with complete source + QR code.

http://lazerlight.x10.mx/tut-large-bg-scroll/

"What fun is there in making sense?"
Lazer Light Studios - Home of the MM2 PTC project

Pokemon 3DS Friend Safari - Ice - Spheal, Sneasel, Lapras

3DS Friend Code: 4425-1477-0127 | Nintendo Network ID: Discostew

AuthorMessage
Avatar

smartman789

16. Posted:

Discostew wrote:

Made a tutorial about large backgrounds + scrolling about a month ago. Guess I might as well add a link to it here. Tutorial comes with complete source + QR code.

http://lazerlight.x10.mx/tut-large-bg-scroll/

sorry, but my "parental controls" for the internet resticted that website.
could you display the qr code on this forum?

smartman789

AuthorMessage
Avatar

Discostew

17. Posted:

smartman wrote:

Discostew wrote:

Made a tutorial about large backgrounds + scrolling about a month ago. Guess I might as well add a link to it here. Tutorial comes with complete source + QR code.

http://lazerlight.x10.mx/tut-large-bg-scroll/

sorry, but my "parental controls" for the internet resticted that website.
could you display the qr code on this forum?

Untitled

Unfortunately, the "tutorial" portion is quite lengthy, and I don't think it would be appropriate to post it all here.

"What fun is there in making sense?"
Lazer Light Studios - Home of the MM2 PTC project

Pokemon 3DS Friend Safari - Ice - Spheal, Sneasel, Lapras

3DS Friend Code: 4425-1477-0127 | Nintendo Network ID: Discostew

AuthorMessage
Avatar

KAHN

18. Posted:

Discostew wrote:

smartman wrote:

Discostew wrote:

Made a tutorial about large backgrounds + scrolling about a month ago. Guess I might as well add a link to it here. Tutorial comes with complete source + QR code.

http://lazerlight.x10.mx/tut-large-bg-scroll/

sorry, but my "parental controls" for the internet resticted that website.
could you display the qr code on this forum?

Untitled

Unfortunately, the "tutorial" portion is quite lengthy, and I don't think it would be appropriate to post it all here.

look at all the lengthy posts on this thread. HAVE NO SHAME.
post it... for reals... yo..

KAHN

3DS Friend Code: 1032-1301-2772 | Nintendo Network ID: Milkman12

AuthorMessage
Avatar

Javo118

19. Posted:

I found this on another website (I dont read the terms, so i dunno if i can post the website XD, i will read them, i promise).

Alternate Loops
How Alternate Loops Work
Although these alternate uses of FOR~NEXT might not be easy to understand at first glance, remember that TRUE and FALSE values returned by any conditional statement are actually integer values 1 and 0. TRUE is always equal to 1, while FALSE is always equal to 0.
While Loop
In some cases you may want to enter and then repeat a section of code as long as a specific condition is true.
Syntax:
FOR I=1 TO (while condition)
I=0
...
NEXT I
Note that the upper limit for the loop (ie: condition) is re-evaluated for each iteration. Setting I=0 inside the loop and then incrementing (+1) that value with the NEXT statement ensures that I is always equal to 1 when the FOR statement is evaluated. In this case, the loop continues running as long as the while condition is TRUE (1) and ends as soon as the specified condition returns a value of FALSE (0).
Example: Print the value of X while X is less than 10.
X=0
FOR I=1 TO (X<10)
I=0
PRINT "Value of X is: ";X
X=X+1
NEXT I
Do~While Loop
Although similar to the While loop discussed above, a Do~While loop differs by ensuring that the loop body is always executed at least once and continues to execute as long as the (while condition) remains true.
Syntax:
FOR I=0 TO 1
...
I=!(while condition)
NEXT I
Note that you must negate the while condition in this case. Using the logical NOT operator (!) changes a TRUE (1) result to FALSE (0) and vice versa, so that the value of I is always the opposite of the while condition. In this case, the only way to exit the loop is for the while condition to return FALSE (0), which sets the value of I to TRUE (1). When incremented at the end of the loop, I becomes 2 and the loop ends.
Example: Print the value of X, and continue printing that value while X is less than 10.
X=0
FOR I=0 TO 1
PRINT "Value of X is: ";X
X=X+1
I=!(X<10)
NEXT I
IF~ENDIF and Nested IF Blocks
Petit Computer limits the commands you can use after an IF statement to a single line. When larger blocks of code need to be executed after an if statement, the most obvious solutions are to:
string together several commands on the same line, separating them with colons (:)
use a GOSUB command and create a subroutine with the code you want to execute
make creative use of GOTO statements and multiple labels
One other option you can use is a FOR~NEXT block of code.
Syntax:
FOR I=1 TO (if condition)
...
NEXT
In this code, if the condition is FALSE (0) then the value of I is already larger and the loop body is skipped. If the condition is TRUE (1) then the values are equal and the loop body will execute exactly once - after which the value of I is increased to 2, larger than both TRUE and FALSE values, ensuring the loop can never run twice.
An added benefit of creating blocks of code this way is that it's easy to nest multiple IF statements within the FOR~NEXT body.
Example:
INPUT "Enter your score and username";myScore,myName$
FOR I=1 TO (highScore<myScore)
PRINT " High Score Achieved! Way To Go ";myName$
IF highScoreName$!="" AND highScoreName$!=myName$ THEN PRINT " You Beat ";highScoreName;"!"
highScore=myScore : highScoreName$=myName$
NEXT
PRINT "Thanks for playing!"
This sample program can be run multiple times to see the different possible results, as high scores and names are carried over until memory is cleared using the CLEAR command.
BREAK (Exit a FOR Loop)
When written as follows, you can exit the FOR loop without using the GOTO command.Syntax:
FOR I=0 TO value
IF (break condition) THEN I=value : NEXT : FOR J=1 to 0
...
NEXT
The key here is to understand the three steps in the break process. When you want to break out of the FOR~NEXT loop, the following things must happen:
First the value of I is incremented to be greater than or equal to the value checked in the initial FOR loop.
Then a NEXT command is used, so that I is incremented and the FOR condition is checked again.
Finally, the program tries to continue with the statement after the NEXT command , where a second "dummy" loop (FOR J=1 TO 0) is declared that is designed to automatically jump to code underneath the following NEXT command.
Note that you can re-use the variable I in the second FOR statement, but using a different variable will preserve the value of I when the break occurs - often desirable behavior if you want to know how many times the initial loop was run.
Important Note: Use caution when using this BREAK syntax if specifying variables with the NEXT commands or nesting FOR~NEXT commands.

3DS FC: 2921-9808-6856

AuthorMessage
Avatar

KAHN

20. Posted:

this site should help. it's a site this guy is running. he's translating the japanese version into english, so it should be a good relevant read.
http://yakra13.site88.net/smiletuts.htm

Edited on by KAHN

KAHN

3DS Friend Code: 1032-1301-2772 | Nintendo Network ID: Milkman12