Click here to Skip to main content
15,910,358 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I built this simple cannon game, well the problem is my mousedown event that is responsible for shooting projectile doesn't work....
Help me please!

You can download the game here
http://www.4shared.com/rar/EPqDla1s/Canon1.html[^]



C#
   public partial class Form1 : Form
    {
        const int SCREEN_WIDTH = 600;
        const int SCREEN_HEIGHT = 480;

        const int PROJECTILE_SPEED = 1;
        const int BASE_TARGET_SPEED = 8;
        const float COLLISION_DISTANCE = 20.0f;

        const int STARTING_LIVES = 3;
        const int GAME_DIFFICULTY_CONTROL = 3;
        const int LIVES_TILE_WIDTH = 16;

        int targetVelocityX;
        int targetVelocityY;

        Random random;

        bool isGameRunning = false;
        int lives;
        int level;
        public Form1()
        {
            InitializeComponent();
            this.ClientSize = new Size(SCREEN_WIDTH,SCREEN_HEIGHT);

            random = new Random();

        }
        #region events

        private void Form1_Load(object sender, EventArgs e)
        {
            Player.Load("player.png");
            Target.Load("target.png");
            Projectile.Load("projectile.png");
            LivesDisplay.Load("lives.png");
            TitleScreen.Load("target.png");
            TitleScreen.Top = 0;
            TitleScreen.Left = 0;
            TitleScreen.Width = SCREEN_WIDTH;
            TitleScreen.Height = SCREEN_HEIGHT;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            Player.Left = e.X - Player.Width/2;
            Player.Top = SCREEN_HEIGHT - Player.Height;
        }

        private void GameUpdateTimer_Tick(object sender, EventArgs e)
        {
            Projectile.Top -= PROJECTILE_SPEED;

            Target.Top += targetVelocityY;
            Target.Left += targetVelocityX;

            CheckGroundCollision();
            CheckAirCollision();
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if(isGameRunning)
            {
              FirePorjectile();
            }
            else
            {
               BeginGame();
            }


            
        }

        #endregion

        #region gameplay methods

        private void BeginGame()
        {
            isGameRunning = true;
            lives = STARTING_LIVES;
            level = 1;

            DisplayLives();
            ResetPorjectile();
            ResetTarget();

            TitleScreen.Hide();
            Cursor.Hide();
        }
        private void EndGame()
        {
            isGameRunning = false;
            TitleScreen.Show();
            Cursor.Show();
        }
        private void KillPlayer()
        {
            
            ResetTarget();
            lives--;
            DisplayLives();

            if (lives == 0)
            {
                EndGame();
            }

        }
        private void KillTarget()
        {
            level++;
            ResetTarget();
            ResetPorjectile();
        }
        private void DisplayLives()
        {
            LivesDisplay.Width = lives * LIVES_TILE_WIDTH;
        }

        private void FirePorjectile()
        {
            if (Projectile.Bottom < 0)
            {
                Projectile.Left = Player.Left + Player.Width / 2 - Projectile.Width / 2;
                Projectile.Top = Player.Top - Projectile.Height;
            }
        }

        private void ResetPorjectile()
        {
            Projectile.Top = -Projectile.Height;
        }

        private void ResetTarget()
        {
            targetVelocityX = random.Next(2,6);
            targetVelocityY = BASE_TARGET_SPEED + level/ GAME_DIFFICULTY_CONTROL ;

            if (random.Next(2) == 0)
            {
                targetVelocityX *= -1;
            }

            Target.Top = -Target.Height;
            Target.Left = SCREEN_WIDTH / 2 - Target.Width / 2;
        }

        #endregion

        #region collision methods

        private void CheckGroundCollision()
        {
            if (Target.Bottom > SCREEN_HEIGHT)
            {
                KillPlayer();
            }
        }

        private void CheckAirCollision()
        {
            Point projectileCenter = GetCenter(Projectile);
            Point targetCenter = GetCenter(Target);

            if (Distance(projectileCenter, targetCenter) < COLLISION_DISTANCE)
            {
                KillTarget();
            }
        }

        #endregion

        #region utility methods

        private Point GetCenter(Control c)
        {
            return new Point(c.Left + c.Width/2,c.Top + c.Height/2);
        }

        private float Distance(Point pointA, Point pointB)
        {
            int a = pointA.X - pointB.X;
            int b = pointA.Y - pointB.Y;
            float c = (float)Math.Sqrt((a * a) + (b*b));
            return c;
        }

        #endregion

    }
}





Design class


C#
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.Player = new System.Windows.Forms.PictureBox();
        this.Target = new System.Windows.Forms.PictureBox();
        this.Projectile = new System.Windows.Forms.PictureBox();
        this.GameUpdateTimer = new System.Windows.Forms.Timer(this.components);
        this.LivesDisplay = new System.Windows.Forms.PictureBox();
        this.TitleScreen = new System.Windows.Forms.PictureBox();
        ((System.ComponentModel.ISupportInitialize)(this.Player)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.Target)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.Projectile)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.LivesDisplay)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.TitleScreen)).BeginInit();
        this.SuspendLayout();
        //
        // Player
        //
        this.Player.Location = new System.Drawing.Point(92, 12);
        this.Player.Name = "Player";
        this.Player.Size = new System.Drawing.Size(32, 32);
        this.Player.TabIndex = 0;
        this.Player.TabStop = false;
        //
        // Target
        //
        this.Target.Enabled = false;
        this.Target.Location = new System.Drawing.Point(92, 106);
        this.Target.Name = "Target";
        this.Target.Size = new System.Drawing.Size(32, 32);
        this.Target.TabIndex = 1;
        this.Target.TabStop = false;
        //
        // Projectile
        //
        this.Projectile.Enabled = false;
        this.Projectile.Location = new System.Drawing.Point(137, 200);
        this.Projectile.Name = "Projectile";
        this.Projectile.Size = new System.Drawing.Size(9, 9);
        this.Projectile.TabIndex = 2;
        this.Projectile.TabStop = false;
        //
        // GameUpdateTimer
        //
        this.GameUpdateTimer.Enabled = true;
        this.GameUpdateTimer.Interval = 16;
        this.GameUpdateTimer.Tick += new System.EventHandler(this.GameUpdateTimer_Tick);
        //
        // LivesDisplay
        //
        this.LivesDisplay.Location = new System.Drawing.Point(4, 4);
        this.LivesDisplay.Name = "LivesDisplay";
        this.LivesDisplay.Size = new System.Drawing.Size(48, 16);
        this.LivesDisplay.TabIndex = 3;
        this.LivesDisplay.TabStop = false;
        //
        // TitleScreen
        //
        this.TitleScreen.Enabled = false;
        this.TitleScreen.Location = new System.Drawing.Point(172, 68);
        this.TitleScreen.Name = "TitleScreen";
        this.TitleScreen.Size = new System.Drawing.Size(100, 50);
        this.TitleScreen.TabIndex = 3;
        this.TitleScreen.TabStop = false;
        //
        // Form1
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.TitleScreen);
        this.Controls.Add(this.LivesDisplay);
        this.Controls.Add(this.Projectile);
        this.Controls.Add(this.Target);
        this.Controls.Add(this.Player);
        this.Enabled = false;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
        this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
        ((System.ComponentModel.ISupportInitialize)(this.Player)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.Target)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.Projectile)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.LivesDisplay)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.TitleScreen)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion
Posted
Updated 29-Dec-12 9:31am
v5
Comments
Sergey Alexandrovich Kryukov 28-Dec-12 14:00pm    
Does not work means... what?
Does the mouse works on laptop? Does the game works on other computers?
—SA
Alexander Alekseyevich Fedoseev 28-Dec-12 14:15pm    
Hi, i have a laptop trackpad( not sure if it makes a difference). I did connect mouse to see if it would fix the problem and it didn't.
Sergey Alexandrovich Kryukov 28-Dec-12 14:46pm    
It should not make any difference, especially to your code, but you can always check up by connecting a real mouse to the notebook.
In such cases, I sometimes say: "If you out of clever ideas, try a stupid one". Believe or not, sometimes it worked for me. :-)
—SA
Sergey Alexandrovich Kryukov 28-Dec-12 14:51pm    
You got an "answer" in Solution 1 suggesting that you might not have a handle in place. You did not answer if it works for you on other computers, but I assume it does (otherwise the question would not make any sense at all). Your problem is that the application works on one computer and does not on another, is that right?

But in general, you should better provide smaller code sample but complete. Yes, you should develop a short but complete sample specially in order to ask a question. First, chances are, you will find the solution by yourself while trying to reproduce the problem. And you can really reveal the problem only on complete code. What's incomplete in yours? You don't show how you add an event handler to the invocation list of a event. You should show all your '+=' operators.


Oh, I see them now, thank you...

—SA
Alexander Alekseyevich Fedoseev 28-Dec-12 17:22pm    
where did you

1 solution

Not sure if your not hitting your MouseDown event or the mousedown event isn't working when your in it but there is an order behind what occurs first.

Here is a link to reivew the process of mousedown that might help:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousedown(v=vs.71).aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900