I wrote an analog watch simulation in C# and Monogame as exercise.
The application consists of a background texture. On top of that I have the hour-, minute- and second hand.
The program gets its time from DateTime.Now.
The second hand rotates by multiplying the seconds with 1/60 of a total lap in radians.
It's the same with the minute hand and hour hand.
Source code:
class Clock
{
int hour;
int minutes;
int seconds;
// Textures
public Texture2D CaseBack;
public Texture2D HourHand;
public Texture2D MinuteHand;
public Texture2D SecondHand;
Vector2 center = new Vector2(63, 63); // Center of watch
// Gets called 60 times/second
public void Update(GameTime gameTime)
{
hour = DateTime.Now.Hour;
minutes = DateTime.Now.Minute;
seconds = DateTime.Now.Second;
}
// Gets called 60 times/second
public void Draw(SpriteBatch spriteBatch)
{
// Background/Case back
spriteBatch.Draw(CaseBack, Vector2.Zero, Color.White);
// Second hand
spriteBatch.Draw(SecondHand, center, null, null, new Vector2(3, 50),
((float)seconds*(float)Math.PI*2 / 60),
// This is the rotation. Works by multiplying Seconds with a 1/60 of a "circle's angle".
null, Color.White, SpriteEffects.None, 0);
// Minute hand - basically the same, but with minutes
spriteBatch.Draw(MinuteHand, center, null, null, new Vector2(3, 40),
((float)minutes * (float)Math.PI * 2 / 60),
null, Color.White, SpriteEffects.None, 0);
// Hour hand - same here
spriteBatch.Draw(HourHand, center, null,null, new Vector2(4, 30),
((float)hour * (float)Math.PI * 2 / 12),
null, Color.White, SpriteEffects.None, 0);
}