;------------------------------------------------------------------------------- ; Trigonometry: return horizontal vector Function VectorX# ( Distance# , Angle# ) Return Sin ( Angle ) * Distance End Function ; Trigonometry: return vertical vector Function VectorY# ( Distance# , Angle# ) Return Sin ( Angle - 90 ) * Distance End Function ; Trigonometry: return distance using vector Function VectorDistance# ( X# , Y# ) Return Sqr ( X * X + Y * Y ) End Function ; Trigonometry: return angle using vector Function VectorAngle# ( X# , Y# ) Return 180 - Atan2 ( X , Y ) End Function ;------------------------------------------------------------------------------- ; Blitz graphics window mode states Const WindowMode_AutoDetect = 0 Const WindowMode_FullScreen = 1 Const WindowMode_Windowed = 2 Const WindowMode_Scaled = 3 ; Desired screen settings Const ScreenWidth = 640 Const ScreenHeight = 480 Const ColorDepth = 0 Const WindowMode = WindowMode_AutoDetect ; Ship motion control Const KeyUp = 200 Const KeyDown = 208 Const KeyLeft = 203 Const KeyRight = 205 ; Game control Const KeyEsc = 1 ; Ship thrust speed in pixels per frame Const ThrustSpeed# = 0.12 ; Ship rotation speed in degrees per frame Const RotateSpeed# = 4 ; Ship maximum velocity in pixels per frame Const MaxSpeed = 5 ;------------------------------------------------------------------------------- ; Ship position Local PosX# Local PosY# ; Current ship angle Local Angle# ; Current ship heading (direction) Local Heading# ; Current ship speed (velocity) Local Speed# ; Ship speed vector Local SpeedX# Local SpeedY# ; Ship thrust vector Local ThrustX# Local ThrustY# ; Original ship image (pointing upwards) Local ShipImage ; Temporarily rotated ship (any direction) Local RotatedShipImage ;------------------------------------------------------------------------------- ; Setup screen Graphics ScreenWidth , ScreenHeight , ColorDepth , WindowMode SetBuffer BackBuffer () ; Load and center ship image ShipImage = LoadImage ( "Ship Pre Final.PNG" ) MidHandle ShipImage ; Disable rotation/scaling smoothing TFormFilter False ; Start in center of screen PosX = ScreenWidth / 2 PosY = ScreenHeight / 2 Repeat ; Input ; - Rotate left If KeyDown ( KeyLeft ) Angle = Angle - RotateSpeed If Angle < 0 Then Angle = Angle + 360 EndIf ; - Rotate right If KeyDown ( KeyRight ) Angle = Angle + RotateSpeed If Angle >= 360 Then Angle = Angle - 360 EndIf ; - Thrust If KeyDown ( KeyDown ) ; Combine thrust speed and ship angle to make the thrust vector ThrustX = VectorX ( ThrustSpeed , Angle ) ThrustY = VectorY ( ThrustSpeed , Angle ) Else ; Reset thrust vector ThrustX = 0 ThrustY = 0 EndIf ; Update ; - Take the current velocity vector (speed and heading) ; - Then add the thrust vector to it (thrust and angle) ; - Which becomes the new velocity vector SpeedX = VectorX ( Speed , Heading ) + ThrustX SpeedY = VectorY ( Speed , Heading ) + ThrustY ; - Now update the current speed and heading with the new velocity vector Speed = VectorDistance ( SpeedX , SpeedY ) Heading = VectorAngle ( SpeedX , SpeedY ) ; - Limit the speed to the maximum If Speed > MaxSpeed Speed = MaxSpeed ; Apply the new speed to the velocity vector SpeedX = VectorX ( Speed , Heading ) SpeedY = VectorY ( Speed , Heading ) EndIf ; - Finally add the velocity vector to the ship position PosX = PosX + SpeedX PosY = PosY + SpeedY ; - Screen edge wrap when crossing screen boundaries If PosX < 0 PosX = PosX + ScreenWidth ElseIf PosX >= ScreenWidth PosX = PosX - ScreenWidth EndIf If PosY < 0 PosY = PosY + ScreenHeight ElseIf PosY >= ScreenHeight PosY = PosY - ScreenHeight EndIf ; Render Cls ; - Ship image ; - Duplicate the original image RotatedShipImage = CopyImage ( ShipImage ) ; - Rotate it to the current angle RotateImage RotatedShipImage , Angle ; - Then draw it on screen DrawImage RotatedShipImage , PosX , PosY ; - And clear the rotated image FreeImage RotatedShipImage Flip Until KeyHit( KeyEsc ) ; Blitz automagically cleans up End