Author Topic: COLOR BASIC Code Examples  (Read 17905 times)

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
COLOR BASIC Code Examples
« on: July 29, 2012, 10:36:03 PM »
I thought I'd start a thread for example programs for Propeller Color Basic.

You can type these in yourself, or just copy/paste them to text files and put them on your microSD card.
(Be sure and name the files with a .BAS extension, as a new feature will take advantage of this soon.)

SIMPLE DRAWING PROGRAM
(Uses Wii Classic Controller: DPAD TO DRAW, A BUTTON TO CHANGE COLOR)
Code: [Select]
10 COLOR 63,0
20 CLS
30 C=22
40 X=50
50 Y=50
51 REM ** START OF DRAWING LOOP **
60 IF JOY = 64 THEN X=X+1
70 IF JOY = 256 THEN X=X-1
80 IF JOY = 32 THEN Y=Y+1
90 IF JOY = 128 THEN Y=Y-1
100 IF JOY = 2048 THEN C=C+1
110 PLOT X,Y,C
120 GOTO 51
« Last Edit: August 31, 2014, 11:14:05 AM by Oldbitcollector »

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
Re: Code Examples
« Reply #1 on: July 29, 2012, 10:36:44 PM »
FLASH AN LED
Insert LED into P12 and V3.3 or Gnd (This example works either way)
Code: [Select]
10 FOR X = 1 TO 10
20 OUTA[12]=0
30 PAUSE 100
40 OUTA[12]=1
50 PAUSE 100
60 NEXT X

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
Re: Code Examples
« Reply #2 on: July 29, 2012, 10:37:24 PM »
DISPLAY ALL COLORS AND CORRESPONDING NUMBERS
Code: [Select]
10 COLOR 1,0
20 CLS
30 FOR X = 0 TO 63
40 COLOR X,0
50 PRINT X;
60 PRINT " ";
70 NEXT X
80 PRINT " "

For those with 256 color boards:
Code: [Select]
1 REDEFINE 33,255,255,255,255,255,255,255,255
10 COLOR 1,0
20 CLS
30 C=0
40 FOR X = 1 TO 16
50 FOR Y = 1 TO 16
60 COLOR C,0
70 C=C+1
80 PRINT "!";
90 NEXT Y
100 PRINT " ";
110 NEXT X
[code]
« Last Edit: January 01, 2013, 02:21:13 PM by Oldbitcollector »

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
Re: Code Examples
« Reply #3 on: July 29, 2012, 10:38:03 PM »
There are 112 re-definable characters, sometimes it's handy to be able to review them all.
DISPLAY AVAILABLE CHARACTERS AND ASCII VALUES
Code: [Select]
10 COLOR 63,0
20 FOR X = 16 TO 127
30 COLOR 14,0
50 DISPLAY(X);
60 COLOR 63,0
70 PRINT X;
80 PRINT " ":
90 NEXT X
100 PRINT " "

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
Re: Code Examples
« Reply #4 on: July 29, 2012, 10:38:48 PM »
Fun with re-definable characters

Note, Redefined characters themselves will display backwards from the direction you put them in.
Code: [Select]


Here's an example of how to redefine the 8X8 character A  (ASCII 65).

########
       #
       #
       #      Let's make a shape that looks like the one on the left.
       #
       #
       #
########

11111111 = decimal 255
00000001 = decimal 1
00000001 = decimal 1
00000001 = decimal 1
00000001 = decimal 1
00000001 = decimal 1
00000001 = decimal 1
11111111 = decimal 255


The command would be:

REDEFINE 65,255,1,1,1,1,1,1,255

The displayed character will look like this...

########
#
#
#
#
#
#
########

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
Re: Code Examples
« Reply #5 on: July 29, 2012, 10:39:26 PM »
More fun with re-definable characters

DISPLAY A GRAPHIC "HEART"
Code: [Select]

10 REDEFINE 16,62,127,255,255,255,255,255,255
20 REDEFINE 17,63,127,255,243,249,249,251,255
30 REDEFINE 18,0,128,192,192,192,192,192,192
40 REDEFINE 19,127,63,31,15,7,3,1,0
50 REDEFINE 20,255,255,254,252,248,224,192,0
60 REDEFINE 21,128,0,0,0,0,0,0,0
100 DISPLAY (18)
110 DISPLAY (17)
120 DISPLAY (16)
125 PRINT " "
130 DISPLAY (21)
140 DISPLAY (20)
160 DISPLAY (19)
170 PRINT " "

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
Re: Code Examples
« Reply #6 on: July 29, 2012, 10:40:13 PM »
Playing with sounds.

You probably need a firmware upgrade to use the PLUCK command. (Added 6/16/12)

MUSICAL KEYS
Use top two rows to play notes, A&Z toggle ATTACK, D&C toggle DECAY, F&V toggle filter
Code: [Select]

10 z=15
11 f=2
12 d=17
20 ATTACK z
21 FILTER f
22 DECAY d
100 a=INKEY
120 IF a="q" THEN PLUCK 60
130 IF a="2" THEN PLUCK 61
140 IF a="w" THEN PLUCK 62
150 IF a="3" THEN PLUCK 63
160 IF a="e" THEN PLUCK 64
170 IF a="r" THEN PLUCK 65
180 IF a="5" THEN PLUCK 66
190 IF a="t" THEN PLUCK 67
200 IF a="6" THEN PLUCK 68
210 IF a="y" THEN PLUCK 69
220 IF a="7" THEN PLUCK 70
230 IF a="u" THEN PLUCK 71
240 IF a="i" THEN PLUCK 72
250 IF a="9" THEN PLUCK 73
300 IF a="a" THEN z=z+1
305 IF a="z" THEN z=z-1
310 IF a="f" THEN f=f+1
320 IF a="v" THEN f=f-1
330 IF a="d" THEN d=d+1
340 IF a="c" THEN d=d-1
520 GOTO 20

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
Re: Code Examples
« Reply #7 on: July 29, 2012, 10:41:05 PM »
Serial Communication {up to 115200 baud}

You probably need to upgrade your firmware to use this example. (Added 6/17/12)

COMMANDS:
SERIAL {RECEIVE},{TRANSMIT},{MODE},{BAUD}
TX {data to transmit}
{received data} = RX

SIMPLE SERIAL TERMINAL
{Communicate between the Pocket Mini Computer and a connected PC.}
Code: [Select]
10 SERIAL 31,30,0,115200
20 A=INKEY
30 IF A > 0 THEN DISPLAY A  : REM HALF DUPLEX DISPLAY
40 IF A > 0 THEN TX A
50 B=RX
60 IF B > 0 THEN DISPLAY B
70 GOTO 20

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
Re: Code Examples
« Reply #8 on: July 29, 2012, 10:41:42 PM »
DISPLAY A TEXT FILE TO SCREEN

Displaying a text file from the SD card. Works will all versions.
Code: [Select]
10 OPEN "textfile.txt",R
20 A=FILE
30 IF A > 12 THEN DISPLAY A;
40 IF A > -1 THEN GOTO 20
50 PRINT " "

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
Re: Code Examples
« Reply #9 on: July 29, 2012, 10:42:21 PM »
DISPLAY RAW MEMORY BYTES

Want to see the guts that powers your BASIC?
Here's a program that reveals the bytecode that makes up both BASIC and your program at the end.
The bytecode itself looks like gibberish, but you'll see ASCII text here and there.
(If you wait long enough.)
Code: [Select]
10 X=0
20 X=X+1
30 A=BYTE[X]
40 IF A > 12 THEN DISPLAY A
50 GOTO 20

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
Re: Code Examples
« Reply #10 on: July 29, 2012, 10:43:15 PM »
PICTURE VIEWER (OF SORTS)

Here's some code which will read .ppm images (can be created by GIMP).
For best results, limit the number of colors. Maximum width is 60. Create in RAW mode.
Code: [Select]
10 COLOR 63,0
20 CLS
30 X=0
40 Y=0
50 D=0
60 S=60 : REM SET TO WIDTH OF IMAGE
70 OPEN "{image.ppm}", R
80 A=FILE
90 B=FILE
100 C=FILE
110 IF A=10 THEN D=D+1
120 IF B=10 THEN D=D+1
130 IF C=10 THEN D=D+1
140 IF D < 4 THEN GOTO 80
150 PLOT X,Y,A
160 X=X+1
170 IF X>S-1 THEN Y+Y+1 : X=0
180 IF A = -1 THEN END
190 GOTO 80

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
Re: Code Examples
« Reply #11 on: July 29, 2012, 10:43:53 PM »
RANDOM LINES DEMO - Requires version 2.2h or greater.

Draw random colored lines on the screen
This demo can also demonstrate the box routine. Replace LINE with BOX.
Code: [Select]
10 COLOR 63,0
20 CLS
30 X=X+1
40 A=RND(100)
50 B=RND(100)
60 C=RND(100)
70 D=RND(100)
80 E=RND(63)
90 LINE A,B,C,D,E
100 IF X>50 THEN CLS
110 IF X>60 THEN LET X=0
120 GOTO 30

Nurbit

  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: BASIC Code Examples
« Reply #12 on: August 20, 2012, 01:13:49 PM »
Here's something I knocked up
It's a times table tester.

For those who don't know, I'm writing something similar in spin to help learn the language. I thought I'd start porting it over to basic while I was at it :)

Here is the code so far........

Code: [Select]
10 PRINT "Which table would you like?"
20 INPUT x
30 y = RND (12)
40 PRINT "what is",x;
50 PRINT " times ";y
60 INPUT z
70 IF z=x*y THEN GOTO 100
80 IF z<>x*y THEN GOTO 200
100 PRINT "correct"
199 END
200 PRINT "wrong"
« Last Edit: August 20, 2012, 01:15:36 PM by Nurbit »

Oldbitcollector

  • Holy Smoke!
  • Administrator
  • Hero Member
  • *****
  • Posts: 2048
  • Projects, Not Platforms
    • View Profile
    • Propellerpowered
Re: BASIC Code Examples
« Reply #13 on: August 24, 2012, 01:02:53 AM »
Using SRAM extended memory as $tringspace for fetching and storing user data.

Code: [Select]
10 FOR x= 1 TO 200
11 POKE x,0
12 NEXT x
15 CLS
20 PRINT "Please enter your name:";
25 x=1 : GOSUB 100
30 PRINT " " : PRINT " "
40 PRINT "How do you feel? :";
50 x=50 : GOSUB 100
60 PRINT " ": PRINT " ": PRINT "Hello ";
70 c=1: GOSUB 300
80 PRINT " " : PRINT " ":PRINT "I'm pleased you are ";
90 c=50:GOSUB 300
95 PRINT " "
99 END
100 REM *******************
110 REM ** Input Routine **
120 REM *******************
130 a=INKEY
140 IF a= 200 THEN DISPLAY 8: DISPLAY 32: DISPLAY 8
150 IF a= 200 THEN x=x-1
160 IF a= 200 THEN LET a=0
170 IF a= 13 THEN RETURN
180 IF a > 0 THEN POKE x,a
190 IF a > 0 THEN LET x=x+1
200 IF a > 0 THEN DISPLAY a
210 GOTO 130
300 REM ********************
310 REM ** Display Memory **
320 REM ********************
330 a=PEEK (c)
340 IF a > 0 THEN DISPLAY PEEK (c)
350 IF a =0 THEN RETURN
360 LET c=c+1
370 GOTO 330

Roguelantern

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Re: BASIC Code Examples
« Reply #14 on: November 18, 2012, 12:53:16 PM »
The classic HAMURABI game

Hopefully I took out most of the bugs. Rows 1000 ... 1170 were not present in the original version, but I left them there as an example of how to set up debug info in BASIC. Just "GOSUB 1000" from any row you want, and you will see the values of all the variables in the game.

Code: [Select]

10 CLS : PRINT "HAMURABI": PRINT " "
20 PRINT "CREATIVE COMPUTING MORRISTOWN,"
21 PRINT "NEW JERSEY"
30 PRINT " ":PRINT " ": PRINT " ":
80 PRINT "TRY YOUR HAND AT GOVERNING ANCIENT"
81 PRINT "SUMERIA FOR A TEN YEAR TERM OF OFFICE."
90 PRINT " "
95 G=0:R=0
100 Z=0:P=95:S=2800:H=3000:E=H-S
110 Y=3:A=H/Y:I=5:Q=1
210 D=0
215 PRINT " ":PRINT " ": Z=Z+1
216 PRINT "HAMURABI, I BEG TO REPORT TO YOU,"
217 PRINT "IN THE YEAR ";Z;", ";D;" PEOPLE"
218 PRINT "STARVED, ";I;" CAME TO THE CITY,"
219 P=P+I
225 IF Q>0 THEN GOTO 230
226 P=P/2
227 PRINT "A HORRIBLE PLAGUE STRUCK!"
228 PRINT "HALF THE PEOPLE DIED!"
230 PRINT "POPULATION IS NOW ";P
232 PRINT "THE CITY NOW OWNS ";A;" ACRES."
235 PRINT "YOU HARVESTED ";Y;" BUSHELS PER ACRE."
250 PRINT "THE RATS ATE ";E;" BUSHELS."
260 PRINT "YOU NOW HAVE ";S;" BUSHELS IN STORE.":PRINT " "
270 IF Z=11 THEN GOTO 860
310 C=1+RND (10):Y=C+17
312 PRINT "LAND IS TRADING AT ";Y;" BUSHELS"
313 PRINT "PER ACRE."
320 PRINT "HOW MANY ACRES DO YOU WISH TO BUY"
321 INPUT Q:IF Q<0 THEN GOTO 850
322 IF Y*Q <= S THEN GOTO 330
323 GOSUB 710
324 GOTO 320
330 IF Q=0 THEN GOTO 340
331 A=A+Q: S=S-Y*Q: C=0
334 GOTO 400
340 PRINT "HOW MANY ACRES DO YOU WISH TO SELL"
341 INPUT Q: IF Q<0 THEN GOTO 850
342 IF Q<A THEN GOTO 350
343 GOSUB 720
344 GOTO 340
350 A=A-Q: S=S+Y*Q: C=0
400 PRINT " "
410 PRINT "HOW MANY BUSHELS DO YOU WISH TO FEED"
411 PRINT "YOUR PEOPLE": INPUT Q
412 IF Q<0 THEN GOTO 850
418 REM * TRYING TO USE MORE GRAIN THAN IN SILOS
420 IF Q<=S THEN GOTO 430
421 GOSUB 710
422 GOTO 410
430 S=S-Q: C=1: PRINT " "
440 PRINT "HOW MANY ACRES DO YOU WISH TO PLANT"
441 PRINT "WITH SEED ": INPUT D
442 IF D=0 THEN GOTO 511
443 IF D<0 THEN GOTO 450
444 REM * PLANTING MORE ACRES THAN YOU OWN
445 IF D<=A THEN GOTO 450
446 GOSUB 720
447 GOTO 440
449 REM * ENOUGH GRAIN FOR SEED
450 IF (D/2)<=S THEN GOTO 455
452 GOSUB 710
453 GOTO 440
454 REM * ENOUGH PEOPLE TO TEND THE CROPS
455 IF D<=10*P THEN GOTO 510
460 PRINT "BUT YOU HAVE ONLY ";P;" PEOPLE TO"
461 PRINT "TO TEND THE FIELDS! NOW THEN,"
470 GOTO 440
510 S=S-D/2
511 GOSUB 800
512 REM * A BOUNTIFUL HARVEST
515 Y=C: H=D*Y: E=0
521 GOSUB 800
522 IF C//2=0 THEN GOTO 530: REM ZERO REMAINDER
523 REM * RATS ARE RUNNING WILD
525 E=S/C
530 S=S-E+H
531 GOSUB 800
532 REM * LET'S HAVE SOME BABIES
533 I=C*(20*A+S)/P/100+1
539 REM * HOW MANY PEOPLE HAD FULL TUMMIES
540 C=Q/20
541 REM * HORRORS, A 15% CHANCE OF PLAGUE
542 Q=7-RND (8): REM Q LESS THAN ZERO IS PLAGUE
550 IF P<C THEN GOTO 210
551 REM * STARVE ENOUGH FOR IMPEACHMENT
552 D=P-C: IF D>P/2 THEN GOTO 560
553 G=((Z-1)*R+D*100/P)Z
555 P=C: G=G+D: GOTO 215
560 PRINT " "
561 PRINT "YOU STARVED ";D;" PEOPLE IN ONE YEAR!"
565 PRINT "DUE TO THIS MISMANAGEMENT YOU HAVE"
566 PRINT "NOT ONLY BEEN IMPEACHED AND THROWN"
567 PRINT "OUT OF THE OFFICE BUT YOU HAVE"
568 PRINT "ALSO BEEN DECRALED NATIONAL FINK!"
569 GOTO 990
710 PRINT "HAMURABI, THINK AGAIN. YOU HAVE ONLY"
711 PRINT S;" BUSHELS OF GRAIN. NOW THEN,"
712 RETURN
720 PRINT "HAMURABI, THINK AGAIN. YOU HAVE ONLY"
721 PRINT A;" ACRES. NOW THEN,"
730 RETURN
800 C=1+RND (5)
801 RETURN
850 PRINT " ": PRINT " "
851 PRINT "HAMURABI, I CANNOT DO WHAT YOU WISH."
855 PRINT "GET YOURSELF ANOTHER STEWARD!"
857 GOTO 990
860 PRINT "IN YOUR 10-YEAR TERM OF OFFICE,"
861 PRINT R;" PERCENT OF THE POPULATION STARVED"
862 PRINT "PER YEAR ON AVERAGE, I.E. A TOTAL OF"
865 PRINT G;" PEOPLE DIED!": L=A/P
870 PRINT "YOU STARTED WITH 10 ACRES PER PERSON"
871 PRINT "AND ENDED WITH ";L;" ACRES PER"
875 PRINT "PERSON.": PRINT " "
880 IF R>10 THEN GOTO 940
885 IF L<9 THEN GOTO 940
895 IF R>3 THEN GOTO 960
896 IF L<10 THEN GOTO 960
900 PRINT "A FANTASTIC PERFORMANCE! CHARLEMAGNE"
901 PRINT "DISREALI, AND JEFFERSON COMBINED"
902 PRINT "COULD NOT HAVE DONE BETTER!"
905 GOTO 990
940 PRINT "YOUR HEAVY-HANDED PERFORMANCE SMACKS"
941 PRINT "OF NERO AND IVAN IV. THE PEOPLE"
942 PRINT "(REMAINING) FIND YOU AN UNPLEASANT"
943 PRINT "RULER, AND, FRANKLY, HATE YOUR GUTS!"
950 GOTO 990
960 PRINT "YOUR PERFORMANCE COULD HAVE BEEN"
961 PRINT "SOMEWHAT BETTER, BUT REALLY WASN'T"
962 PRINT "TOO BAD AT ALL. "
963 PRINT 1+RND (P);" PEOPLE WOULD DEARLY LIKE"
964 PRINT "TO SEE YOU ASSASINATED BUT WE ALL"
965 PRINT "HAVE OUR TRIVIAL PROBLEMS."
990 PRINT " "
991 FOR N=1 TO 10
992 PRINT "*";
993 NEXT N
995 PRINT " SO LONG FOR NOW.": PRINT " "
999 END
1000 PRINT "***DEBUG***": REM * DEBUG SECTION
1010 PRINT "TOTAL PEOPLE DIED ";G
1020 PRINT "PERCENT POPULATION STARVED ";R
1030 PRINT "CURRENT YEAR IS ";Z
1040 PRINT "POPULATION IS ";P
1050 PRINT "GRAIN IN STORAGE ";S
1060 PRINT "HARVEST WAS ";H
1070 PRINT "RATS ATE ";E
1080 PRINT "HARVEST YIELD, PRICE PER ACRE ";Y
1090 PRINT "ACRES ";A
1100 PRINT "PEOPLE CAME TO CITY ";I
1110 PRINT "CURRENT INPUT OR RND VALUE ";Q
1120 PRINT "PEOPLE STARVED OR ACRES PLANTED ";D
1130 PRINT "RND VALUE FROM 1 TO 10 ";C
1140 PRINT "ACRES PER PERSON ";L
1150 PRINT "RND VALUE FROM 1 TO 5 OR FULL"
1151 PRINT "TUMMIES ";C
1160 PRINT "***DEBUG***": REM END OF DEBUG
1170 RETURN

« Last Edit: November 21, 2012, 02:27:45 PM by Roguelantern »