UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on October 11, 2008, 06:17:04 PM

Title: Plotting the Second Hand on a clock
Post by: kevin on October 11, 2008, 06:17:04 PM
 Plotting the Second Hand on a clock

  This example converts the current second into an angle, so we can draw a representation of a second hand on a clock.  Might be useful for games that use timers

[pbcode]
ClockXpos=400
ClockYpos=300

do

   cls 0

   ; draw an open circle at 400x,300y with a radius of 100
   circle ClockXpos,ClockYpos,100,false

   ; get the current seconds
   Seconds=currentSecond()
   ; convert the seconds into an angle
   Angle#=(360*(Seconds/60.0))-90

   ; calc the end of the second hands end
   HandEndX=ClockXpos+Cos(Angle#)*90
   HandEndY=ClockYpos+Sin(Angle#)*90

   ; draw it
   line ClockXpos,ClockYpos,HandEndX,HandEndY

   ; display the seconds count   
   print seconds
   
   sync
loop

[/pbcode]


Related Examples
 Analogue Clock (http://www.underwaredesign.com/forums/index.php?topic=2944.0)  

Title: Re: Plotting the Second Hand on a clock
Post by: kevin on October 11, 2008, 06:28:07 PM
 Drawing a Speedo


[pbcode]

setfps 100

MaxSpeed#=200

do

   cls 0


   if Upkey()=true and CurrentSpeed#<MaxSpeed#
         CurrentSpeed#=CurrentSpeed#+2
   else
      if CurrentSpeed#>0      
         CurrentSpeed#=CurrentSpeed#-1
      endif
   endif
   
      
    Speedo(400,300,100,CurrentSpeed#,MaxSpeed#)

   print CurrentSpeed#
   print MaxSpeed#
   
   sync
loop



Function Speedo(Xpos,Ypos,Radius,CurrentSpeed#,MaxSpeed#)

   ; draw an open circle at 400x,300y with a radius of 100
   circle Xpos,Ypos,100,false

   ; convert the speed into an angle from 0-360  
   Angle#=(360*(CurrentSpeed#/MaxSpeed#))+90

   ; calc the end of the hands end
   HandEndX=Xpos+Cos(Angle#)*90
   HandEndY=Ypos+Sin(Angle#)*90

   ; draw it
   line xpos,Ypos,HandEndX,HandEndY

EndFUnction

[/pbcode]