' Snake moving around the screen ' ' Change 'sects' to alter body length ' DECLARE FUNCTION wrap% (v%, low%, high%) PRINT "QBSnake" RANDOMIZE TIMER SLEEP 1 SCREEN 7, 1, 0: db = 1 '=============== ' Constants '=============== CONST sects = 30, bsize = 4, hsize = bsize * 1.2: lsize = hsize * 1.2 CONST w = 320, h = 200, bkg = 9, pi = 3.141592 '=========================== ' Vars - Define/Init '=========================== DIM s(359), c(359) FOR d = 0 TO 359 s(d) = SIN(d * (pi / 180)) 'Generate the Sine/Cosine lookup tables c(d) = COS(d * (pi / 180)) NEXT d TYPE coords x AS SINGLE 'X position of snake part y AS SINGLE 'Y position of snake part END TYPE DIM sn(sects) AS coords DIM snDir AS INTEGER 'Direction to rotate the snakes head DIM snAngle AS INTEGER 'Angle in which to move the snake DIM snSpeed AS SINGLE 'Speed of snakes movement sn(0).x = RND * w: sn(0).y = RND * h FOR d = 1 TO sects: sn(d).x = sn(0).x: sn(d).y = sn(0).y: NEXT d '=========================== ' Main Loop '=========================== DO SCREEN 7, 1, db, 1 - db 'send drawings to hidden page LINE (0, 0)-(w, h), bkg, BF 'clear the page in background colour GOSUB MoveSnake 'update and draw snake SCREEN 7, 1, db, db 'show the hidden screen WAIT &H3DA, 8 'wait for vertical sync db = 1 - db 'toggle the db flag LOOP UNTIL INKEY$ = CHR$(27) 'exit on ESC key END '=========================== ' Test/Update/Move Snake '=========================== MoveSnake: IF RND > .92 THEN snSpeed = (RND * 2.8) + .1 ' ------------------ IF RND < .05 THEN snDir = RND * 7 - RND * 7 ' This code rotates snAngle = wrap%(snAngle + snDir, 0, 359 + 1) ' and moves the sn(0).x = sn(0).x + c(snAngle) * snSpeed ' head in the given sn(0).y = sn(0).y + s(snAngle) * snSpeed ' angle direction. IF sn(0).x < 0 THEN sn(0).x = 0: snAngle = 0 ' It also keeps the IF sn(0).x > w THEN sn(0).x = w: snAngle = 180 ' snake inside the IF sn(0).y < 0 THEN sn(0).y = 0: snAngle = 90 ' display area. IF sn(0).y > h THEN sn(0).y = h: snAngle = 270 ' ------------------ snAngle = wrap%(snAngle, 0, 359 + 1) ' FOR d = sects TO 1 STEP -1 ' ------------------ dx = sn(d - 1).x - sn(d).x ' Here, we update dy = sn(d - 1).y - sn(d).y ' each section of sn(d).x = sn(d).x + dx / 5 ' the body to catch sn(d).y = sn(d).y + dy / 5 ' up with the part CIRCLE (sn(d).x, sn(d).y), bsize, 10 ' just above it. NEXT d ' ------------------ CIRCLE (sn(0).x, sn(0).y), hsize, 13 LINE -(sn(0).x + c(snAngle) * lsize, sn(0).y + s(snAngle) * lsize), 15 'LOCATE 1, 1: COLOR 14, 6: PRINT lifecount RETURN '=============================================================== ' 270 These values show the angles which determine ' | the direction in which the snake moves ' 180-+-0/360 ' | the value is stored in 'snAngle' ' 90 FUNCTION wrap% (v%, low%, high%) ' ' Simple function to wrap a number between two values ' wrap% = v% IF v% < low% THEN wrap% = high% - (low% - v%) IF v% >= high% THEN wrap% = low% + (v% - high%) ' END FUNCTION