locked
How can I make a "Mouse Chaser" app? RRS feed

  • Question

  • Some background to this question: a long time ago, your main PC is a Win95 machine and happen upon a copy of "Galaxy of Games 2" for cheap at your local electronics store. You take it home and let your young son play on it. Your son comes across a "game" that, when opened, made a little cat chase your mouse. Your son was mesmerized by it. Fast forward some years and the son has grown up and is tinkering with PC's. Wanting to relive nostalgia, he installs a virtual machine and Windows 98. He finds said Galaxy of Games 2 and installs it in the VM. He finds the cat & mouse "game" (edit: Found the name: TopCat by Robert Dannbauer. Binged it and only found 1 relevant result.) and, due to his interest in coding, he wants to build a version of it for modern OS's like Windows 10. However, due to his very basic knowledge of coding, he isn't quite sure how to make it. He managed to make a Windows Form App in C# that had coordinates chase the mouse, but they kept up with the mouse frame by frame, and had no cute animations. Frustrated, he goes to MSDN and asks what he should try.

    TL;DR: How can I make an app like this in C#: https://www.youtube.com/watch?v=TKNk749iFMA&feature=youtu.be (Sorry for poor quality, that PC isn't strong enough for a screen recorder, let alone a screen recorder + VMWare running)

    I was playing with the options in TopCat, and I realized some basics on how it works. In the options, you can set the Update Frequency (how often the cat moves toward the mouse; animation goes to next frame every tick) and Update distance (how far the cat moves in a single update). The default settings are Freq: 100ms and Dist: 12 pixels. With these settings, it gives an illusion of the cat actually moving.

    Thanks to any and all that even look at this!

    Sunday, April 10, 2016 2:24 AM

Answers

  • I think this code snippet should get you started.

    This makes a small borderless window that will follow the cursor around.  (Right click its icon on the task bar and choose "Close window" to exit.)

    From here you can experiment with mostly the look and feel of it.  Make a cat graphic in a picture box, or whatever you want.  Also, you might want to experiment with making the window topmost, or stay slightly out of the way of the pointer, or make it not intercept mouse clicks, etc.  You can experiment with transparency and such too.  You can increase the complexity of the target seeking heuristic and try to give the cat some personality.

       public partial class Form1 : Form
        {
            // A timer component to drive the animation.
            Timer timer;
    
            // Variables to hold the position of the window with sub-pixel precision.
            double pos_x;
            double pos_y;
    
            public Form1()
            {
                InitializeComponent();
    
                // No border on this window, please.
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    
                // Record the initial position.
                // We'll use floating point to make the small increments work nicely.
                pos_x = (double)Location.X;
                pos_y = (double)Location.Y;
    
                // Create the timer component.
                timer = new Timer();
    
                // Set the interval between ticks in milliseconds.
                const int framesPerSecond = 60;
                timer.Interval = 1000 /*milliseconds per second*/ / framesPerSecond;
    
                // Set a tick handler
                timer.Tick += timer_Tick;
    
                // Enable the timer.
                timer.Enabled = true;
            }
    
            protected override void OnCreateControl()
            {
                base.OnCreateControl();
                // Set the size of this window to something small
                // Need to do this after the control is created.
                this.Size = new Size( 48, 48 );
            }
    
            void timer_Tick( object sender, EventArgs e )
            {
                // This happens once per tick to cause animation.
    
                // Move the window towards the target, but no more than some max distance at a time.
                
                // Desired target location.
                Point target = Control.MousePosition;
    
                // horizontal and vertical step is initially the whole distance to the target.
                double dx = (double)target.X - pos_x;
                double dy = (double)target.Y - pos_y;
                
                // Limit to this max distance
                double maxDistance = 10;
                
                // Compute the actual distance.
                double distance = Math.Sqrt( dx * dx + dy * dy );
    
                if( distance > maxDistance ) {
                    // Shorten the step to at most maxDistance units.
                    dx /= (distance/maxDistance);
                    dy /= (distance/maxDistance);
                }
    
                // add the step to our floating point position
                pos_x += dx;
                pos_y += dy;
    
                // Update the position of the window.
                Location = new Point( (int)pos_x, (int)pos_y );
            }
        }
    
    • Proposed as answer by Kristin Xie Friday, April 15, 2016 9:23 AM
    • Marked as answer by GhostHunterJon1 Friday, April 15, 2016 7:12 PM
    Monday, April 11, 2016 1:31 AM

All replies

  • I think this code snippet should get you started.

    This makes a small borderless window that will follow the cursor around.  (Right click its icon on the task bar and choose "Close window" to exit.)

    From here you can experiment with mostly the look and feel of it.  Make a cat graphic in a picture box, or whatever you want.  Also, you might want to experiment with making the window topmost, or stay slightly out of the way of the pointer, or make it not intercept mouse clicks, etc.  You can experiment with transparency and such too.  You can increase the complexity of the target seeking heuristic and try to give the cat some personality.

       public partial class Form1 : Form
        {
            // A timer component to drive the animation.
            Timer timer;
    
            // Variables to hold the position of the window with sub-pixel precision.
            double pos_x;
            double pos_y;
    
            public Form1()
            {
                InitializeComponent();
    
                // No border on this window, please.
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    
                // Record the initial position.
                // We'll use floating point to make the small increments work nicely.
                pos_x = (double)Location.X;
                pos_y = (double)Location.Y;
    
                // Create the timer component.
                timer = new Timer();
    
                // Set the interval between ticks in milliseconds.
                const int framesPerSecond = 60;
                timer.Interval = 1000 /*milliseconds per second*/ / framesPerSecond;
    
                // Set a tick handler
                timer.Tick += timer_Tick;
    
                // Enable the timer.
                timer.Enabled = true;
            }
    
            protected override void OnCreateControl()
            {
                base.OnCreateControl();
                // Set the size of this window to something small
                // Need to do this after the control is created.
                this.Size = new Size( 48, 48 );
            }
    
            void timer_Tick( object sender, EventArgs e )
            {
                // This happens once per tick to cause animation.
    
                // Move the window towards the target, but no more than some max distance at a time.
                
                // Desired target location.
                Point target = Control.MousePosition;
    
                // horizontal and vertical step is initially the whole distance to the target.
                double dx = (double)target.X - pos_x;
                double dy = (double)target.Y - pos_y;
                
                // Limit to this max distance
                double maxDistance = 10;
                
                // Compute the actual distance.
                double distance = Math.Sqrt( dx * dx + dy * dy );
    
                if( distance > maxDistance ) {
                    // Shorten the step to at most maxDistance units.
                    dx /= (distance/maxDistance);
                    dy /= (distance/maxDistance);
                }
    
                // add the step to our floating point position
                pos_x += dx;
                pos_y += dy;
    
                // Update the position of the window.
                Location = new Point( (int)pos_x, (int)pos_y );
            }
        }
    
    • Proposed as answer by Kristin Xie Friday, April 15, 2016 9:23 AM
    • Marked as answer by GhostHunterJon1 Friday, April 15, 2016 7:12 PM
    Monday, April 11, 2016 1:31 AM
  • Thank you! Haven't been home much, but when I have been home I've been playing with this snippet. It really helped me.
    Friday, April 15, 2016 7:13 PM