Catch Me If You Can

Introduction

This is a small game I have written just for fun. It's a Windows form application and the main purpose of the game was to provide an easy way to add controls dynamically.

Game Target

The target of the game is to catch the button before it moves to an another position. When you have caught the button a few times, another button will be added; you also need to catch that one. When you have caught that one too, a third button will be added, and so on. I am still thinking to write a real target for the game. Currently, the game will never end.

Dynamically Added Controls

The game consists only of buttons that are dynamically added. The AddButton() function is used to add a new button and looks like this:

private void AddButton()
{
   Point p    = new Point(10, 10);
   Button btn = new Button();

   btn.Name      = nButtonID.ToString();
   btn.Text      = nButtonID.ToString();
   btn.FlatStyle = FlatStyle.Flat;
   btn.Height    = 23;
   btn.Width     = 54;
   btn.Location  = p;

   btn.MouseEnter += new System.EventHandler(this.btn_MouseEnter);
   btn.Click      += new System.EventHandler(this.btn_Click);
   btn.KeyDown    +=
       new System.Windows.Forms.KeyEventHandler(this.btn_KeyDown);

   this.Controls.Add(btn);
   nButtonID++;
}

This function is simple and straightforward. In the first line, we create a new object of the Point class. In the constructor of the class, I have added the coordinates 10,10—so that each new button will be placed at the top left corner. The seven lines after that just create the button and set the properties. One important property is the Name property. This property is the ID of the button, so that we refer to it later. For that purpose, I am using a simple integer that will be incremented each time a new button is added. That way, each button will have an unique Name. After the properties have been set, I am assigning three eventhandlers to the button. The first one is used to track when the mouse is over the button. The second one is used when the button is clicked, and the third one is used whenever somebody tries to hit the button with the spacebar or Enter keys.

Btn_MouseEnter

As mentioned, the btn_MouseEnter is fired whenever the mouse is over the button. Keep in mind that the mouse events are fired in the following order:

  • MouseEnter
  • MouseMove
  • MouseHover/MouseDown/MouseWheel
  • MouseUp
  • MouseLeave

The problem here was that the button was positioned to fast, so that the user wasn't able to catch it. To avoid this, I have implemented a Timer that waits for approximately 150ms before the button is moved somewhere else. I think that the player has enough time to catch it. Here is how I created the timer in the btn_MouseEnter event.

...
   System.Timers.Timer t = new System.Timers.Timer();
   t.Elapsed+=new System.Timers.ElapsedEventHandler(OnTimedEvent);
   t.Interval = 150;
   t.Start();
   t.Enabled = true;
   t.AutoReset = false;
...

Remember to set the AutoReset property to false; otherwise, the timer will run all the time and not only one time. And, here is what happens in 150ms in the OnTimedEvent:

private void OnTimedEvent(object source,
                          System.Timers.ElapsedEventArgs e)
{
   int y = NextHeight();
   int x = NextWidth();

   Point p = new Point(x,y);
   btnPublic.Location = p;

   this.Text = "X: "+x.ToString() + " Y: " + y.ToString();
}

The functions NextHeight() and NextWidth() always return a random position for the button. The rest of the code is just straightforward and simple; I don't think that it requires further explanation.



About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website http://dotnetslackers.com. DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at: http://dotnetslackers.com/community/blogs/sonukapoor/

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: July 30, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this upcoming eSeminar that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base …

  • Mobile is introducing sweeping changes throughout your workplace. As a senior stakeholder driving mobile initiatives in your organization, you may be lost in a sea of technologies and claims from vendors promising rapid delivery of applications to your employees, customers, and partners. To help explain some of the topics you will need to be aware of, and to separate the must-haves from the nice-to-haves, this reference guide can help you with applying a mobile strategy in the context of application …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds