SetFPS 60 Type tCharacter X# y# Angle# Colour EndType Dim Alien As tCharacter Alien.x=400 Alien.y=300 Alien.angle=Rnd(360) Alien.colour = $ff0000 Dim PLayer As tCharacter Player.X#=100 PLayer.Y#=100 PLayer.Colour=$00ff00 ; ------------------------------------------------------------- ; --[ Main Loop ]--------------------------------------------- ; ------------------------------------------------------------- Do ; clear screen to black at the start of this update Cls RGB(0,0,0) ; ------------------------------------------------------------- ; --[ Control Player ]----------------------------------------- ; ------------------------------------------------------------- ; left arrow turn player left TurnAmount#=0 Speed#=0 If LeftKey() Then TurnAmount#=-1 If RightKey() Then TurnAmount#=1 ; up arrow move player forward If UpKey() Speed#=5 EndIf If TurnAmount# Player.angle=WrapAngle(player.angle+TurnAmount#) EndIf If Speed#<>0 Player.x+=Cos(Player.Angle)*Speed# Player.y+=Sin(Player.Angle)*Speed# CLipToScreen(Player) EndIf ; ------------------------------------------------------------- ; --[ Control Alien ]----------------------------------------- ; ------------------------------------------------------------- ; get the angle from the Alien to the Player AngleToTarget#=GetAngle2D(alien.x#,alien.y#,player.x#,player.y#) ; work out how far apart these angles are AngleToAngle#=AngleDifference(Alien.Angle#,AngleToTarget#) ; Check if the Angle to player is = or > then 90 ; if so, the alien will turn one degree at a time If AngleToAngle#=>90 ; When the difference is large we turn faster ; and move slower, so the character appears to brake ; and turn TurnAndMoveToPLayer(Alien,3,0.1) ElseIf AngleToAngle#=>45 ; There's at least 45 degrees apart so we're getting closer ; so we start turning slower and movign faster. TurnAndMoveToPLayer(Alien,2,0.5) ElseIf AngleToAngle#=>15 ; Now they're pretty close as it want to turn slowly if at all and ; move faster again. TurnAndMoveToPLayer(Alien,1,1) ElseIf AngleToAngle#=>5 ; Almost faciung the target, so small turns and faster movement TurnAndMoveToPLayer(Alien,0.5,1.5) Else ; less than 5 degrees apart so don't worry about turning and ; speed towards the target TurnAndMoveToPLayer(Alien,0,2) EndIf ; ------------------------------------------------------------- ; --[ Draw Objects ]----------------------------------------- ; ------------------------------------------------------------- ; Draw the player and alien objects on screen DrawObject(Player) DrawObject(alien) ; refresh the display Sync Loop Function TurnAndMoveToPlayer(me As tCharacter,TurnSpeed#,Speed#) ; get the angle of the target AngleToTarget#=GetAngle2D(alien.x#,alien.y#,player.x#,player.y#) ; Get what direction we should turn Direction#=TurnDirection(me.Angle#,AngleToTarget#,1) ; turn the angle me.angle=WrapAngle(me.angle,Direction#*TurnSpeed#) me.x+=Cos(me.angle#)*speed# me.y+=Sin(me.angle#)*speed# ; clip the characters position to the screen bounds CLipToScreen(Me) EndFunction Function CLipToScreen(Me As tCharacter) me.x=ClipRange#(me.x,0,GetScreenWidth()) me.y=ClipRange#(me.y,0,GetScreenHeight()) EndFunction ; ------------------------------------------------------------- ; --[Draw Object ]--------------------------------------------- ; ------------------------------------------------------------- Function DrawObject(me As tCharacter) x#=me.x y#=me.y angle#=me.angle col=me.colour x2#=CosNewValue(x#,angle#,100) y2#=SinNewValue(y#,angle#,100) CircleC x#,y#,10,false,col LineC x#,y#,x2#,y2#,col d=180-25 x3#=CosNewValue(x2#,angle#-d,10) y3#=SinNewValue(y2#,angle#-d,10) LineC x2#,y2#,x3#,y3#,col x3#=CosNewValue(x2#,angle#+d,10) y3#=SinNewValue(y2#,angle#+d,10) LineC x2#,y2#,x3#,y3#,col CenterText x#,y#,Angle# EndFunction |