Post by David Drake on Feb 21, 2020 7:18:38 GMT -5
I created the code below for the Rosetta Code task: Galton box animation (rosettacode.org/wiki/Galton_box_animation). The concept of the Galton box is detailed at the top of the link. The task is "For the purpose of this task the box should have at least 5 pins on the bottom row. Your solution can use graphics or ASCII animation. Provide a sample of the output/display such as a screenshot. Your solution can have either one or more balls in flight at the same time. If multiple balls are in flight, ensure they don't interfere with each other. Your solution should allow users to specify the number of balls or it should run until full or a preset limit. Optionally, display the number of balls. "
I enjoyed this one because I created a less sophisticated version for a statistics class.
You will need to download these linked sprites: ball.bmp, pin.bmp
As always, before I post to Rosetta Code, I welcome the community's feedback and comments.
I enjoyed this one because I created a less sophisticated version for a statistics class.
You will need to download these linked sprites: ball.bmp, pin.bmp
As always, before I post to Rosetta Code, I welcome the community's feedback and comments.
nomainwin
WindowWidth=400
WindowHeight=450
UpperLeftX=1
UpperLeftY=1
textbox #1.tb, 10, 350,370,25
open "Galton Machine" for graphics_nf_nsb as #1
loadbmp "ball","ball.bmp"
loadbmp "pin","pin.bmp"
#1 "trapclose [q];down;fill black;flush"
#1 "getbmp bg 1 1 400 600"
#1 "background bg"
#1 "addsprite ball ball;spriteround ball"
for pinCount = 1 to 28
#1 "addsprite pin";pinCount;" pin;spriteround pin";pinCount
next pinCount
for y = 1 to 7
for x = 1 to y
pin=pin+1
xp=200-x*50+y*25
yp=y*35
#1 "spritexy pin";pin;" ";xp;" ";yp
#1 "drawsprites"
next x
next y
prompt "Number of cycles to run: ";cycleMax
[reset]
#1 "spritexy ball 174 1"
#1 "spritemovexy ball 0 5"
timer 1, [move]
wait
[move]
#1 "drawsprites"
#1 "spritecollides ball hits$"
if hits$<>"" then
direction = rnd(1)
if direction >0.4999999 then #1 "spritemovexy ball 1 0" else #1 "spritemovexy ball -1 0"
for a = 1 to 25:#1 "drawsprites":next a
#1 "spritemovexy ball 0 5"
end if
#1 "spritexy? ball x y"
if y > 280 then
timer 0
select case
case x<49
slot(1)=slot(1)+1
case x=49
slot(2)=slot(2)+1
case x=99
slot(3)=slot(3)+1
case x=149
slot(4)=slot(4)+1
case x=199
slot(5)=slot(5)+1
case x=249
slot(6)=slot(6)+1
case x=299
slot(7)=slot(7)+1
case x>299
slot(8)=slot(8)+1
end select
#1.tb, slot(1);" ";slot(2);" ";slot(3);" ";slot(4);" ";slot(5);" ";slot(6);" ";slot(7);" ";slot(8);" (";cycles+1;")"
for a = x to 380 step 2
#1 "spritexy ball ";a;" 280;drawsprites"
next a
for b = 300 to 1 step -2
#1 "spritexy ball 380 ";b;";drawsprites"
next b
for c = 380 to 174 step -2
#1 "spritexy ball ";c;" 1;drawsprites"
next c
cycles = cycles + 1
if cycles >= cycleMax then
timer 0
notice "Complete"
wait
end if
goto [reset]
end if
wait
[q]
close #1
unloadbmp "pin"
unloadbmp "ball"
unloadbmp "bg"
end