UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on January 08, 2024, 07:55:06 AM

Title: PlayBASIC Source Code Snippet - Ball Following Mouse - (2024-01-09)
Post by: kevin on January 08, 2024, 07:55:06 AM

PlayBASIC Source Code Snippet - Ball Following Mouse - (2024-01-09)

   in this example PlayBASIC source code, we create a ball that moves towards the mouse with what seems like some soft acceleration.  The code also includes method to decouple the programs execution speed from the performance from the machine running the code.   This is achieved by monitoring the how much time has past between updates and only moving the characters when enough time has past.   If too much time has past we step the movement as per the required number of frames past.   


  [pbcode]

   PlayerX# = GetScreenWidth()/2
   PlayerY# = GetScreenHeight()
   
   //  Frame rate we're targeting
   TicksPerFrame# = 1000.0/100

   //  Current frame we're on
   CurrentFRAME    = 0

   // Time when this program Starts
   FrameStart       = timer()
   do

      // Check how much time as Past since program started
      TimePast#=(timer()-FrameStart)

      //  Is it time to move characters or not ??
      if (CurrentFRAME*TicksPerFrame#)<TimePAST#
   
         //  See how much time as past since we might need to move
         //  things more than once this refresh.
         FramesPAST = ( TimePAST# - (CurrentFRAME*TicksPerFrame#) ) / TicksPerFrame#

         //  Move everything
         for FrameCOUNT =1 to FramesPAST
            gosub Update_Logic
            CurrentFRAME++         
         next
      endif

      
      //  Scroll Wheeel to force a frame rate (simulate slower computers)
      LImitedFPS=cliprange(LimitedFPS+mousemovez(),0,1000)
      if GetFps()<>LImitedFPS
          Setfps LImitedFPS
      endif
      

      //  Draw the scene
      cls
      print "Machine FPS:"+str$(LImitedFPS)   

       circle PlayerX#,PlayerY#,30,true
      sync


   loop




Update_Logic:

       mX#=mousex()
       mY#=mousey()
   
       PlayerX# = curvevalue( mX#,PlayerX#, 20)
       PlayerY# = curvevalue( mY#,PlayerY#, 20)
   return


  [/pbcode]