Page 251 - Learn to Program - Basic-256
P. 251

Chapter 16: Mathematics – More Fun With Numbers.                                Page 235


                2       # rewrite of moveball.kbs using the modulo operator
                        to wrap the ball around the screen
                3
                4       print "use i for up, j for left, k for right, m for
                        down, q to quit"
                5
                6       fastgraphics
                7       clg
                8       ballradius = 20
                9
                10      # position of the ball
                11      # start in the center of the screen
                12      x = graphwidth /2
                13      y = graphheight / 2
                14
                15      # draw the ball initially on the screen
                16      call drawball(x, y, ballradius)
                17
                18      # loop and wait for the user to press a key
                19      while true
                20         k = key
                21         if k = asc("I") then
                22            # y can go negative, + graphheight keeps it
                        positive
                23            y = (y - ballradius + graphheight) %
                        graphheight
                24            call drawball(x, y, ballradius)
                25         end if
                26         if k = asc("J") then
                27            x = (x - ballradius + graphwidth) % graphwidth
                28            call drawball(x, y, ballradius)
                29         end if
                30         if k = asc("K") then
                31            x = (x + ballradius) % graphwidth
                32            call drawball(x, y, ballradius)
                33         end if
                34         if k = asc("M") then
                35            y = (y + ballradius) % graphheight
                36            call drawball(x, y, ballradius)


                                                           © 2019 James M. Reneau (CC BY-NC-SA 3.0 US)
   246   247   248   249   250   251   252   253   254   255   256