Click here to Skip to main content
15,890,438 members
Articles / Desktop Programming / MFC
Article

A cool game that uses the mouse

Rate me:
Please Sign up or sign in to vote.
1.24/5 (12 votes)
18 May 20041 min read 30.4K   956   12   3
A cool game that uses the mouse

Sample Image - Codecs_smileys.jpg

Introduction

Halo every one. Here is an interisting little game for you guys, it works with four timers in the background at all times. So we can say that this app is purely timer driven. I wont forget my friend Flesh_Eater who helped me a lot, i am still i newbie and i am trying to learn to do better.I hope you guys enjoy it.

Code sample 1

The following piece of code is activated when the Begin Button is pressed. The four timers are given interval values and the labels are given there positions.

cs"
int A = 0;
       private void Beginbutton_Click(object sender, System.EventArgs e)
       {
           //This sets the score counter to 100
           (this.label5.Text = "100";
           (if(A == 0)
           {
               //Here the four timers are started that control this app
               (this.timer1.Interval = 1;
               (this.timer1.Start();
               (this.timer2.Interval = 1;
               (this.timer2.Start();
               (this.timer3.Interval = 1;
               (this.timer3.Start();
               (this.timer4.Interval = 1000;
               (this.timer4.Start();
               (this.label3.Text = "0";
               //Here the two smiley face labels positions are set
               this.label1.Left = 19;
               this.label1.Top = 96;
               this.label2.Left = 264;
               this.label2.Top = 216;
               //Setting the begin button to Restart
               this.Beginbutton.Text = "Restart";
               A = 1;
           }
           else
           {
               //This is to prevent any errors if A = 1 then all
               //the timers are stopped.Kind of like a reboot.
               this.timer1.Stop();
               this.timer2.Stop();
               this.timer3.Stop();
               this.timer4.Stop();
               this.Beginbutton.Text = "Begin";
               A = 0;
           }
       }

Code sample 2

In the following piece of code i am explaining how one of the two timers that control the images so that they dont go out of the borders to you.

cs"
 //Declaring four new variables
static int X = 0;
static int Y = 0;
static int X1 = 0;
static int Y1 = 0;
private void timer1_Tick(object sender, System.EventArgs e)
{
    //This part is to make sure that the two images dont go past
    //The border of the game.
    //This program has two timers one controls the .left positions
    //And timer 2 controls the .top positions.
    switch(X)
    {
        case 0:
            //If th location of X is under a certain amount
            //then we give X a value.
            if(this.label1.Left >= 280)
            {
                X = 1;
            }
            //And give the label a new position
            this.label1.Left += 3;
            break;
        case 1:
            if(this.label1.Left <= 0)
            {
                X = 0;
            }
            this.label1.Left -= 2;
            break;
        default:
            break;
    }

Code sample 3

Here is the discription of the third timer.

cs"
 //This timer is important because it controls the score keeping
//and if the images colide they have to split up again.
private void timer3_Tick(object sender, System.EventArgs e)
{
    //Four new variables with location values of the first label
    int S1 = (this.label1.Location.X +17);
    int S2 = (this.label1.Location.X -17);
    int S3 = (this.label1.Location.Y +17);
    int S4 = (this.label1.Location.Y -17);

    //Here we are saying that if Label 1 is "LIKE" Label 2
    //in positions then bounse them of again.
    if(this.label2.Location.X <= S1)
    {
        if(this.label2.Location.X >= S2)
        {
            if(this.label2.Location.Y <= S3)
            {
                if(this.label2.Location.Y >= S4)
                {
                    //The score is counted on by one if the
                    //images colide.
                    int Score = int.Parse(this.label3.Text);
                    Score++;
                    this.label3.Text = Score.ToString();
                    this.label1.ImageIndex = 1;
                    this.label2.ImageIndex = 1;

                    //Here the variables receive new values and timer 1
                    //and timer 2 take care of the rest.
                    if (Y == 0)
                    {
                        Y = 1;
                    }
                    else
                    {
                        Y = 0;
                    }
                    if (Y1 == 0)
                    {
                        Y1 = 1;
                    }
                    else
                    {
                        Y1 = 0;
                    }
                    if (X == 0)
                    {
                        X = 1;
                    }
                    else
                    {
                        X = 0;
                    }
                    if (X1 == 0)
                    {
                        X1 = 1;
                    }
                    else
                    {
                        X1 = 0;
                    }
                }
            }
        }

Final Code Sample

This is the final timer in the app, it controls functions like calculating the time adn stopping the rest of the timers.

cs"
 //This is the last timer that keeps track of the time.
private void timer4_Tick(object sender, System.EventArgs e)
{
    int Time = int.Parse(this.label5.Text);
    Time--;
    this.label5.Text = Time.ToString();
    this.label1.ImageIndex = 0;
    this.label2.ImageIndex = 0;
    //When the time = 0 then all four timers stop and
    //Form2 "Scoreform" is displayed.
    if (Time == 0)
    {
        this.timer4.Stop();
        this.timer3.Stop();
        this.timer2.Stop();
        this.timer1.Stop();
        EndScore = int.Parse(this.label3.Text);
        Form2 F2 = new Form2();
        F2.Show();
    }
}

Problems

My major problems with this app was that my two images kept going over the borders, the result was they did not bounce. This was solved with my friends (Fles_Eater) help in Code Sample 1.This baby was deeloped during the boring hours of school so it is not perfect i know that. I hope everyone enjoys this app and hopefully you will see many more.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
South Africa South Africa
Halo my name is Willem i live in South Africa and it is really cool to finally be programming.

For the Last year i have been working as a techie for a local company, but then finally they saw that i did do Software Designing.So now i am working as an E-Work Designer and i am also looking after two SQL Servers.

I Studied for :
A+
Windows 2000 Pro
Windows 2000 Server
HDA Help Desk Analyst
SQL 2000 Administration
SQL 2000 Designer
C#.net
ASP.net
Com plus and finally
ADO.net

Comments and Discussions

 
GeneralI pwn3d your game Pin
OmegaSupreme19-May-04 1:34
OmegaSupreme19-May-04 1:34 
GeneralRe: I pwn3d your game Pin
Dubant19-May-04 3:50
Dubant19-May-04 3:50 
GeneralRe: I pwn3d your game Pin
Pure_Acid19-May-04 3:56
Pure_Acid19-May-04 3:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.