Information Technology - Computer Programming - Source Code - Homebrew - Open Source - Software - Hardware - 8 bit - 16 bit - 32 bit - 64 bit - x86 - x64 - DOS - Windows - Linux - Arduino - Embedded - Development - Retro - Vintage - Math - Science - History - Hobby - Beginners - Professionals - Experiment - Research - Study - Fun - Games

Roll a Die

Share your Net Basic creations here.
Post Reply
User avatar
Brian
Posts: 15
Joined: Mon Dec 11, 2023 2:19 pm

Roll a Die

Post by Brian »

A traditional die is a cube with each of its six faces marked with a different number of dots from one to six. When rolled the die comes to rest showing a random integer from one to six on its upper surface. This program simulates the roll of a die every time you click on the run button.

Code: Select all

1 REM Roll a Die
2 HTML
4 LET a = 1 + INT(RND * 6)
6 LET x = 20
8 LET y = 20
10 LET rx = 40
12 LET ry = 40
14 LET width = 200
16 LET height =200
18 PRINT "<svg width = '240' height = '240' style = 'background-color:lightskyblue;'>"
20 PRINT "<rect x = '"; x; "' y = '"; y; "' rx = '"; rx; "' ry = '"; ry; "' width = '"; width; "' height = '"; height; "' fill = 'red' />"
22 LET cx = 60
24 LET r = 20
26 PRINT "<text x = '"; x; "' y = '"; y; "' font-size = '5' fill = '#1E90FF'>LENS 2024</text>"
28 IF a = 1 THEN GOTO 100
30 IF a = 2 THEN GOTO 200
32 IF a = 3 THEN GOTO 300
34 IF a = 4 THEN GOTO 400
36 IF a = 5 THEN GOTO 500
38 IF a = 6 THEN GOTO 600
40 IF a < 1 OR a > 6 THEN GOTO 4
100 LET cx = 120
102 LET cy = 120
104 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
106 GOTO 624
200 LET cy = 180
202 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
204 LET cx = 180
206 LET cy = 60
208 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
210 GOTO 624
300 LET cy = 180
302 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
304 LET cx = 120
306 LET cy = 120
308 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
310 LET cx = 180
312 LET cy = 60
314 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
316 GOTO 624
400 LET cy = 60
402 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
404 LET cy = 180
406 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
408 LET cx = 180
410 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
412 LET cy = 60
414 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
416 GOTO 624
500 LET cy = 60
502 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
504 LET cy = 180
506 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
508 LET cx = 180
510 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
512 LET cy = 60
514 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
516 LET cx = 120
518 LET cy = 120
520 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
522 GOTO 624
600 LET cy = 60
602 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
604 LET cy = 120
606 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
608 LET cy = 180
610 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
612 LET cx = 180
614 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
616 LET cy = 120
618 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
620 LET cy = 60
622 PRINT "<circle cx = '"; cx; "' cy = '"; cy; "' r = '"; r; "' fill = 'white' />"
624 PRINT "</svg>"
626 END


























Attachments
DIE.png
DIE.png (6.11 KiB) Viewed 3007 times
admin
Site Admin
Posts: 109
Joined: Wed Feb 22, 2023 6:51 am

Re: Roll a Die

Post by admin »

Good example of how to use Net Basic with SVG.

Just wondering have you tried the program encoded URL feature yet? It allows you to store your whole program in a url as encoded text in a GET variable.

Basically when you press run, you get two links. One HTTPS and one HTTP for older browsers. The links contain your program. This allows them to be ran as web apps. The page will display the text output box with the html breakout area below.

When you run the dice sim, try clicking the program encoded url and observe the url. Copy, paste, and share it to run anywhere such as email, social media, forums, etc. Your whole program is stored in the URL which brings up an unusual feature. The URL may contain other variables than the encoded source code. Think of these as command line parameters, or makeshift input when combined with input forms. Use the GET command in Net Basic to read their contents.

This way you may put the URL with your screenshot and source listing so people have the option to just click to run. Especially for a dice program that's useful because then you just need to F5 for a new roll and on a clean page ready for board gaming.
User avatar
Brian
Posts: 15
Joined: Mon Dec 11, 2023 2:19 pm

Re: Roll a Die

Post by Brian »

Hello Gemino,

I had tried the encoded URL feature briefly but until your detailed explanation I did not realise how versatile and useful this feature could be. I will experiment with the possibilities of this feature for future programs and look deeper into the workings of the GET command.
admin
Site Admin
Posts: 109
Joined: Wed Feb 22, 2023 6:51 am

Re: Roll a Die

Post by admin »

Looking forward to see what you do. Let me know if you have any questions. I'll always do my best to provide support. This is all journey in progress. I really have no clue what I am doing half the time (or more).
Post Reply