Page 228 - Learn to Program - Basic-256
P. 228
Chapter 15: Arrays – Collections of Information. Page 212
9
10 # position of the balls - start them all at 0,0
11 dim x(balls) fill 0
12 dim y(balls) fill 0
13
14 # speed of the balls (set randomly)
15 dim dx(balls)
16 dim dy(balls)
17
18 # color of the balls (set randomly)
19 dim colors(balls)
20
21 for b = 0 to balls-1
22 # speed in x and y directions
23 dx[b] = rand * r + 2
24 dy[b] = rand * r + 2
25 # each ball has it's own color
26 colors[b] = rgb(rand*256, rand*256, rand*256)
27 next b
28
29 color green
30 rect 0,0,300,300
31
32 while true
33 # erase screen
34 clg
35 # now position and draw the balls
36 for b = 0 to balls -1
37 # move ball to new location
38 x[b] = x[b] + dx[b]
39 y[b] = y[b] + dy[b]
40 # if off the edges turn the ball around
41 if x[b] < 0 or x[b] > graphwidth then
42 dx[b] = dx[b] * -1
43 end if
44 # if off the top of bottom turn the ball
around
45 if y[b] < 0 or y[b] > graphheight then
46 dy[b] = dy[b] * -1
© 2019 James M. Reneau (CC BY-NC-SA 3.0 US)

