 This time a real classic Commodore Basic miniature “The bouncing ball”. I think this classic is one of the Basic programs that when I first saw a version of it, I think almost 38 years ago (mmm am I really that …), I wanted to know how a ball could just bounce around. As a small puzzle moment today I tried to recreate a version of it as I remember it, see the resultFirst let me show the code for the most classic the single ball bouncing on the screen from side to side. I tried to keep the code clean and simple.

10 printchr\$(147):poke214,24:print:poke211,10:print”single bouncing ball”
20 sm = 1024:x=int(rnd(1)*24):y=1:dy=1:dx=1
30 poke sm+x+(y*40), 32
40 if y>=22 or y < 1 then dy=dy*-1
50 if x>=39 or x < 1 then dx=dx*-1
60 x=x+(1*dx):y=y+(1*dy):poke sm+x+(y*40),81:fors=1to100:next: goto 30

Second a variation on the above just not cleaning the last position of the ball (character 81) which results in a ball leaving a trail.

10 printchr\$(147):poke214,24:print:poke211,10:print”line bouncing ball”
20 sm = 1024:x=int(rnd(1)*24):y=1:dy=1:dx=1
30 if y>=22 or y < 1 then dy=dy*-1
40 if x>=39 or x < 1 then dx=dx*-1
50 x=x+(1*dx):y=y+(1*dy):poke sm+x+(y*40), 81 :fors=1to100:next: goto 30

And a third variation using a small trick while changing direction to swap color between screen blue and green this gives a pattern effect.

10 printchr\$(147):poke214,24:print:poke211,8:print”pattern bouncing ball”
20 sm = 1024:x=int(rnd(1)*24):y=1:dy=1:dx=1
30 poke 55296 +x+(y*40), 5+dy : rem swap between green en screen blue
40 if y>=22 or y < 1 then dy=dy*-1
50 if x>=39 or x < 1 then dx=dx*-1
60 x=x+(1*dx):y=y+(1*dy):poke sm+x+(y*40), 81 :fors=1to50:next: goto 30

Categories:

Tags: