' We include FreeBASIC's built-in library and allow ' the usage of its additional constants and functions ' with Using FB. #include "fbgfx.bi" Using FB ' We set some needed constants. const fpslimit = 60 const FALSE = 0 const TRUE = 1 const PI = 3.141593 ' We declare the needed variables. DIM SHARED workpage AS INTEGER ' The following 3 variables are used for ' frames per second control. DIM SHARED st AS DOUBLE DIM SHARED frameintvl As Double = 1.0/fpslimit DIM SHARED sleepintvl As Integer DIM SHARED angledeg AS INTEGER ' Main object's angle in degrees DIM SHARED anglerad AS SINGLE ' Main object's angle in radians DIM SHARED mainx AS SINGLE ' Main object's x pos DIM SHARED mainy AS SINGLE ' Main object's y pos DIM SHARED mainspeed AS SINGLE ' Main object's speed DIM SHARED main_rotationspeed AS SINGLE ' Main object's rotation speed DIM SHARED resultangledeg AS INTEGER ' Angle between the cpu cont. object and main object in degrees DIM SHARED resultanglerad AS SINGLE ' Angle between the cpu cont. object and main object in radians DIM SHARED CPUobjectx AS SINGLE ' CPU controlled object's x pos DIM SHARED CPUobjecty AS SINGLE ' CPU controlled object's y pos DIM SHARED CPUobj_angledeg AS INTEGER ' CPU controlled object's angle in degrees DIM SHARED CPUobj_anglerad AS SINGLE ' CPU controlled object's angle in radians DIM SHARED CPUobject_rotationspeed AS SINGLE ' Rotation speed of the CPU controlled object DIM SHARED CPUobjectspeed AS SINGLE ' CPU controlled object's speed DIM SHARED ASmode AS INTEGER ' artificial smarts control (on/off) ' We set our screen to 640 width, 480 height, 32 color bit-depth, ' 2 work pages and full screen. SCREENRES 640, 480, 32, 2, GFX_FULLSCREEN ' We set the initial values for the main object's ' and CPU controlled object's positions/angles/speeds. mainx = 320 mainy = 220 mainspeed = 4 main_rotationspeed = 3 angledeg = 0 CPUobjectx = 200 CPUobjecty = 200 CPUobj_angledeg = 20 CPUobj_anglerad = (CPUobj_angledeg*PI)/180 CPUobject_rotationspeed = 3 CPUobjectspeed = 2 ASMode = TRUE ' Your average do loop. DO st = Timer ' Record the current time into st variable ' (used to limit FPS; no related to the ' topic of the example program). screenlock ' Lock our screen (nothing will be ' displayed until we unlock the screen). screenset workpage, workpage xor 1 ' Swap work pages. ' The control keys for the main (player's) object. IF MULTIKEY(SC_LEFT) THEN angledeg = angledeg - main_rotationspeed IF MULTIKEY(SC_RIGHT) THEN angledeg = angledeg + main_rotationspeed ' The following lines keep the angle between ' 0 and 360. IF angledeg<0 THEN angledeg=angledeg+360 IF angledeg>359 THEN angledeg=angledeg-360 anglerad = (angledeg*PI)/180 ' Convert the angle from degrees to radians IF MULTIKEY(SC_UP) THEN mainx = mainx + sin(anglerad)*mainspeed mainy = mainy - cos(anglerad)*mainspeed END IF ' For turning AS on/off. IF MULTIKEY(SC_1) THEN ASMode = TRUE IF MULTIKEY(SC_2) THEN ASMode = FALSE ' The following lines calculate the angle of direction ' from the controlled object toward the main object. resultanglerad = ATAN2((-1)*(mainy-CPUobjecty),(mainx-CPUobjectx)) resultanglerad = PI/2 - resultanglerad IF resultanglerad < 0 THEN resultanglerad = resultanglerad + 2*PI ' The following line converts the result angle between the ' two objects from radians to degrees. resultangledeg = (resultanglerad*180)/PI CLS ' Clear the screen ' We draw our objects (represented with small circles) and ' lines that emulate these objects' directions. LINE (CPUobjectx, CPUobjecty)-(CPUobjectx+sin(CPUobj_anglerad)*20,CPUobjecty-cos(CPUobj_anglerad)*20), RGB(2,117, 250) CIRCLE (CPUobjectx, CPUobjecty), 3, RGB(2,117, 250) LINE (mainx, mainy)-(mainx+sin(anglerad)*20,mainy-cos(anglerad)*20), RGB(200, 0, 0) CIRCLE (mainx, mainy), 3, RGB(200, 0, 0) workpage xor = 1 ' Swap work pages. screenunlock ' Unlock the page to display what has been drawn. ' Keep the FPS value within fpslimit (set above). sleepintvl = Cint((st + frameintvl - Timer)*1000.0) If sleepintvl>1 Then Sleep sleepintvl end if LOOP UNTIL MULTIKEY(SC_ESCAPE) ' Do loop until ESC is pressed.