Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2009
    Posts
    11

    Cannon 2 help.....

    Not sure what is going on here, but for some reason my game is failing to work properly.

    Followed all the videos and have no build errors but my UpdateLivesDisplay is not working properly..sometimes the game fails to start with 3 lives or more specifically showing the full width of the picture box. Sometimes one ground collision causes 2 lives to vanish...oh my head hurts.. I have looked through all the code trying to find it and can't seem to...

    now the fun.

    GameItem.cs:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace Cannon2
    {
        class GameItem
        {
            private Point position;
            private Point velocity;
            private PictureBox texture;
    
            #region properties
    
    
    
            public Point Position
            {
                get { return position; }
                set { position = value; }
            }
    
            public Point Velocity
            {
                get { return velocity; }
                set { velocity = value; }
            }
       
    
            public PictureBox Texture
            {
                get { return texture; }
                set { texture = value; }
            }
    
            public Point Origin
            {
                get
                {
                    return new Point(texture.Width / 2, texture.Height / 2);
                }
            }
            #endregion
    
    
            # region methods
    
            public void Update()
            {
                this.position = new Point(this.position.X + this.velocity.X,
                                            this.position.Y + this .velocity.Y);
            }
    
    
            public void Draw()
            {
                this.texture.Location = new Point (this.position.X - this.Origin.X, 
                                                    this.position.Y - this.Origin.Y);
            }
            #endregion
    
        }
    }


    HudItem.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace Cannon2
    {
        class HudItem
        {
            private Point position;
            private PictureBox texture;
            private int width;
            private int height;
    
    
            #region properties
    
            public Point Position
            {
                get {return position; }
                set { position = value; }
            }
    
            public PictureBox  Texture
            {
                get { return texture; }
                set { texture = value; }
            }
    
            public int Width
            {
                get { return width; }
                set { width = value; }
            }
    
            public int Height
            {
                get { return height; }
                set { height = value; }
            }
    
            #endregion
    
    
            public void Draw()
            {
                this.texture.Left = this.position.X;
                this.texture.Top = this.position.Y;
                this.texture.Width = this.width;
                this.texture.Height = this.height;
    
            }
        }
    }


    Game1.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Cannon2
    {
        public partial class Game1 : Form
        {
            const int SCREEN_WIDTH = 640;
            const int SCREEN_HEIGHT = 480;
    
            const int PROJECTILE_SPEED = 12;
            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;
            const int LIVES_DISPLAY_OFFSET = 4;
    
    
            bool isGameRunning = false;
            int lives;
            int level;
    
            Random random;
    
            GameItem player;
            GameItem target;
            GameItem projectile;
    
            HudItem titleScreen;
            HudItem livesDisplay;
    
    
    
    //***********************************************************************************************************
    
            public Game1()
            {
                InitializeComponent();
                this.ClientSize = new Size(SCREEN_WIDTH, SCREEN_HEIGHT);
                
    
                Initialize();
            }
    
    //***********************************************************************************************************
            #region events
            private void Game1_Load(object sender, EventArgs e)
            {
                LoadGraphicsContent();
            }
    
    
            private void UpdateTimer_Tick(object sender, EventArgs e)
            {
                UpdateScene();
            }
    
            private void DrawTimer_Tick(object sender, EventArgs e)
            {
                DrawScene();
            }
    
    
            #endregion
    
    
    
    //***********************************************************************************************************
    
    
    
            #region engine methods
    
            private void Initialize()
            {
                random = new Random();
    
    
                player = new GameItem();
    
                target = new GameItem();
    
    
    
                projectile = new GameItem();
                projectile.Velocity = new Point(0, -PROJECTILE_SPEED);
    
                titleScreen = new HudItem();
                titleScreen.Position = new Point(0, 0);
                titleScreen.Width = SCREEN_WIDTH;
                titleScreen.Height = SCREEN_HEIGHT;
    
                livesDisplay = new HudItem();
                livesDisplay.Position = new Point(LIVES_DISPLAY_OFFSET, LIVES_DISPLAY_OFFSET);
                livesDisplay.Width = LIVES_TILE_WIDTH * STARTING_LIVES;
                livesDisplay.Height = LIVES_TILE_WIDTH;
    
                
            }
    
            private void LoadGraphicsContent()
            {
                PlayerPictureBox.Load("player.png");
                player.Texture = PlayerPictureBox;
    
                TargetPictureBox.Load("target.png");
                target.Texture = TargetPictureBox;
    
                ProjectilePictureBox.Load("projectile.png");
                projectile.Texture = ProjectilePictureBox;
    
                TitleScreenPictureBox.Load("title_screen.png");
                titleScreen.Texture = TitleScreenPictureBox;
    
                LivesDisplayPictureBox.Load("lives.png");
                livesDisplay.Texture = LivesDisplayPictureBox;
            }
    
            private void UpdateScene()
            {
    
                if (isGameRunning)
                {
                    UpdatePlayer();
                    target.Update();
                    projectile.Update();
    
                    CheckFire();
    
                    CheckAirCollision();
                    CheckGroundCollision();
                }
                else if (MouseButtons == MouseButtons.Left)
                {
                    BeginGame();
                }
            }
    
            private void DrawScene()
            {
                if (isGameRunning)
                {
    
                    player.Draw();
                    target.Draw();
                    projectile.Draw();
                    livesDisplay.Draw();
                }
                else
                {
                    titleScreen.Draw();
                }
            }
    
    
            #endregion
    
    
    // **********************************************************************************************
    
    
    
            #region Gameplay Methods
    
            private void BeginGame()
            {
                isGameRunning = true;
                lives = STARTING_LIVES;
                level = 1;
    
    
               
                UpdateLivesDisplay();
                ResetProjectile();
                ResetTarget();
                UpdatePlayer();
    
                TitleScreenPictureBox.Hide();
    
    
            }
    
            private void EndGame()
            {
                isGameRunning = false;
                TitleScreenPictureBox.Show();
              
    
            }
    
            private void KillPlayer()
            {
                ResetTarget();
    
                lives--;
    
                UpdateLivesDisplay();
                if (lives == 0)
                {
                    EndGame();
                }
    
            }
    
            private void KillTarget()
            {
                level++;
    
                ResetTarget();
                ResetProjectile();
    
    
            }
    
            private void UpdateLivesDisplay()
            {
                livesDisplay.Width = lives * LIVES_TILE_WIDTH;
            }
    
            #endregion
    
    //***************************************************************************************************
    
            #region mechanics methods
    
            private void UpdatePlayer()
            {
                int playerX = this.PointToClient(MousePosition).X;
                int playerY = SCREEN_HEIGHT - player.Origin.Y;
    
                player.Position = new Point(playerX, playerY);
            }
    
            private void CheckFire()
            {
                if (MouseButtons == MouseButtons.Left && ProjectilePictureBox.Bottom < 0)
                {
                    int projectileX = player.Position.X;
                    int projectileY = SCREEN_HEIGHT - PlayerPictureBox.Height - projectile.Origin.Y;
                    projectile.Position = new Point(projectileX, projectileY);
                }
            }
    
            private void ResetTarget()
            {
                int velocityX = random.Next(2, 6);
                int velocityY = BASE_TARGET_SPEED + level / GAME_DIFFICULTY_CONTROL;
    
                if (random.Next(2) == 0)
                {
                   velocityX *= -1;
                }
    
                target.Position = new Point(SCREEN_WIDTH / 2, -target.Origin.Y);
                target.Velocity = new Point(velocityX, velocityY);
            }
    
            private void ResetProjectile()
            {
                projectile.Position = new Point(0, -projectile.Origin.Y);
            }
    
    
            #endregion
    
    
    //********************************************************************************************
    
    
            #region collision methods
    
    
            private void CheckGroundCollision()
            {
                if (TargetPictureBox.Bottom > SCREEN_HEIGHT)
                {
                    KillPlayer();
    
                }
            }
    
    
            private void CheckAirCollision()
            {
                if (Distance(projectile.Position, target.Position) < COLLISION_DISTANCE)
                {
                    KillTarget();
    
                }
            }
    
           
            #endregion
    
    
            //*********************************************************************************************
    
    
            #region utility methods
    
    
            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
        }
    }

  2. #2
    Join Date
    Dec 2009
    Posts
    11
    I keep trying to debug it but can't seem to get it to show me the problem.

    Now some things to note. It will sometimes start just fine 3 lives (full width box) and lose 1 life per ground collision, then on the next start it will take 2 lives on first ground collision then on the next game start with only 2 lives. I know somewhere it is messing up the width of the DisplayLives picture box but just not sure where...



    Arg losing my mind I just can't seem to cause the problem when debugging.

    I think it may have something to do with shooting the projectile or clicking the mouse to initiate the problem.
    Last edited by AllenG; 02-15-2010 at 07:05 PM.

  3. #3
    Join Date
    Jan 2010
    Posts
    7
    I just had a quick scan and noticed that your draw function is a little different to mine...

    Code:
            public void Draw() 
            {
                this.Texture.Left = this.position.X;
                this.Texture.Top = this.position.Y;
                this.texture.Width = this.width;
                this.texture.Height = this.height;
            }
    *note the caps on Texture.

    Maybe thats the cause?

  4. #4
    Join Date
    Mar 2003
    Location
    Denmark
    Posts
    2,882
    Put some breakpoints on the lives themselves, and see what happens in each event.
    See how they change, and work from there.
    Swoop - Buzzed Pirate!
    " If nobodys talking to me, im talking to me" - Swoop

    Reflection Design || Swoops Blog

    2D Tutorials Compilation

    The Speed Painting Thread || ((post your Concept Art thread))


  5. #5
    Join Date
    Dec 2009
    Posts
    11
    Quote Originally Posted by Swoop View Post
    Put some breakpoints on the lives themselves, and see what happens in each event.
    See how they change, and work from there.

    I guess I don't really know how to use the break points...I seem to get mixed results depending on where I put them. The major problem though is I can't seem to recreate the problem while debugging. I see the lives decrementing by 1 from 3 each loop, but the problem never occurs. Likely because once it finishes the first time it stops and won't reset. (the game that is)



    i can get crazy results if i set isGameRunning var to true. Then the lives go negative.
    Last edited by AllenG; 02-17-2010 at 12:37 AM.

  6. #6
    Join Date
    Mar 2003
    Location
    Denmark
    Posts
    2,882
    Investigate each event by hand.
    What happens on each collision. My guess is that you likely have a reset of decrement in a wrong place
    Swoop - Buzzed Pirate!
    " If nobodys talking to me, im talking to me" - Swoop

    Reflection Design || Swoops Blog

    2D Tutorials Compilation

    The Speed Painting Thread || ((post your Concept Art thread))


  7. #7
    Join Date
    Dec 2009
    Posts
    11
    Quote Originally Posted by Swoop View Post
    Investigate each event by hand.
    What happens on each collision. My guess is that you likely have a reset of decrement in a wrong place

    Nelso in the 3dbuzz IRC solved the problem. He said it sounded like the timers. So i checked the properties of the timers and realized I had set one to 33 instead of 16. I then realized in the tutorials they had me do it to show a reason to use separate timers.

    The timer who's interval was set to 33 was the draw timer. Explains the reason during debug I didn't see the problem and the lives was decrementing appropriately.

    Anyway, set that draw timers interval to 16 and problem solved.

    Thanks again Nelso, I will say it again, You are a genius!

  8. #8
    Join Date
    Mar 2003
    Location
    Denmark
    Posts
    2,882
    Glad to hear you solved it, and nice of you to post the solutions for others who might be having similar issues now, or in the future
    Swoop - Buzzed Pirate!
    " If nobodys talking to me, im talking to me" - Swoop

    Reflection Design || Swoops Blog

    2D Tutorials Compilation

    The Speed Painting Thread || ((post your Concept Art thread))


  9. #9
    Join Date
    Dec 2009
    Posts
    11
    I know how finding a needle in a haystack can stress a persons patience.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •