#Nature of code - Oscillation - Walker with Angular Motion #********************************************************** # Here, we will again create a Walker but this time, in the form of an "Asteroids" spaceship. # we will no longer use Perlin Noise to generate direction and movement. Instead we will increase/decrease the angle by keypress (left/right) # As a fun adittion we also will adapt the speed by keypress (up/down) but the rocketship cannot exceed a speedlimit or go backward # #Setup gosub ScreenSetup gosub ObjectSetup gosub KeyMapping #Draw while 1 gosub Steering gosub Update gosub CheckEdges gosub Show refresh pause 0.002 end while #Subroutines #********************** ScreenSetup: Fastgraphics # write to video buffer. global setseed setSeed=0 # initialize random number generator t=0 width=600 height=600 # good practice to put every constant in a variable. graphsize width,height color black rect(0, 0, width-1, height-1) refresh return ObjectSetup: #set up the rocketship's initial state pos = {width/2, height/2} # initial position of the rocketship mass = 1 radius= 20 Angle = pi # starting angle incSpeed =1 return KeyMapping: ar_down=16777237 ar_up=16777235 ar_left=16777234 ar_right=16777236 return Steering: # change the angle by an increment determined by keypress and cap the speed if keypressed(ar_down) then incSpeed = incSpeed-0.1 if incSpeed < 0 then incSpeed = 0 else if keypressed(ar_up) then incSpeed = incSpeed + 0.1 if incSpeed > 8 then incSpeed = 8 endif endif # change the angle by an increment determined by keypress if keypressed(ar_left) then incAngle = -0.05 else if keypressed(ar_right) then incAngle = 0.05 else incAngle = 0 endif endif Angle = Angle + incAngle return Update: #using the angle, we calculate the new velocity vel = {cos(Angle)*incSpeed,sin(Angle)*incSpeed} pos[0] = pos[0] - vel[0] pos[1] = pos[1] - vel[1] return Show: #blank the canvas color rgb(0,0,0) rect(0, 0, width-1, height-1) # draw the rocketship. # we want to rotate around the middle of the rocketship, so we shift the drawing of the ship half a radius back middle = {pos[0] - cos(Angle)*radius/2, pos[1] - sin(Angle)*radius/2} color white line (middle[0], middle[1], middle[0] + cos(Angle-0.3)*radius, middle[1] + sin(Angle-0.3)*radius) line (middle[0], middle[1], middle[0] + cos(Angle+0.3)*radius, middle[1] + sin(Angle+0.3)*radius) line (middle[0] + cos(Angle+0.3)*radius, middle[1] + sin(Angle+0.3)*radius, middle[0] + cos(Angle-0.3)*radius, middle[1] + sin(Angle-0.3)*radius) return CheckEdges: # we constrain the Walker to the canvas if pos[0] > width then pos[0] = 0 if pos[0] < 0 then pos[0] = width if pos[1] > height then pos[1] = 0 if pos[1] < 0 then pos[1] = height return