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);

Don't repeat yourself

The step of drawing the hour, minute, second hands are all very similar. It would be good to use a helper function:

public void Draw(SpriteBatch spriteBatch, Texture2D hand, int value, Vector2 vector2, float coef)
    spriteBatch.Draw(hand, center, null, null, vector2,
        value * Math.PI * coef,
        null, Color.White, SpriteEffects.None, 0);


Since the hour, minutes, seconds should not be visible outside the class, it would be good to make them explicitly private. (Thanks @Maxim for the clarification that the default access modifier is actually private.)

  • \$\begingroup\$ hour, minutes, seconds already are private since it is the default access modifier for a class member. But in my opinion it is much more clear if an access modifier is specified explicitly. \$\endgroup\$ – Maxim Oct 7 '17 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.