none
Collision Help, multiple Class' with inheritance RRS feed

  • Question

  • for the life of me  i just cant seem to be working out what i am doing wrong, i have managed to make a player and make it shoot. also managed to make enemy spawn in at random locations/speeds in the picture box.

    i just cant seem to get any Collision to work, i am trying to keep all enemies in the picture box and bounce back and also bounce off each other, and then destroy them when shot.

    i have been trying to use  "Ball.Bounds.IntersectsWith(pictureBox2.Bounds)" but it seems that Ball.bounds is wrong.

                     
    Form1.cs
    
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Drawing;
    
    
    namespace shootyarcher
    {
    
        public partial class Form1 : Form
        {
            Archer player;
            List<Arrow> Shiv;
            List<Ball> Kill;
    
    
    
            public Form1()
            {
                InitializeComponent();
    
                Cursor.Hide();
    
                this.FormBorderStyle = FormBorderStyle.None;
                this.TopMost = true;
                this.Bounds = Screen.PrimaryScreen.Bounds;
    
    
                player = new Archer(0, 0);
                Shiv = new List<Arrow>();
                Kill = new List<Ball>();
                for (int i = 0; i < 400; i++)
                {
                    Ball temp = new Ball();
                    Kill.Add(temp);
                }
                
            }
              
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                player.Move();
                foreach (Arrow t in Shiv)
                {
                    t.Move();
                   
                }
                foreach (Ball m in Kill)
                {
                    m.Move();
                }
                 
            
                pictureBox1.Invalidate();
            }
    
            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.W)
                {
                    player.moveup = true;
                }
                if (e.KeyCode == Keys.S)
                {
                    player.movedown = true;
                }
                if (e.KeyCode == Keys.Space)
                {
                    Arrow temp = new Arrow(player.x, player.y);
                    Shiv.Add(temp);
                }
                if (e.KeyCode == Keys.Escape)
                {
                    this.Close();
                }
            }
            private void Form1_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.W)
                {
                    player.moveup = false;
                }
                if (e.KeyCode == Keys.S)
                {
                    player.movedown = false;
                }
            }
    
            private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                player.Draw(e.Graphics);
                foreach (Arrow t in Shiv)
                {
                    t.Draw(e.Graphics);
                }
                foreach (Ball m in Kill)
                {
                    m.Draw(e.Graphics);
                }
    
            }
    
        }
    }

    Ball.cs
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace shootyarcher
    {
        class Ball : Box
        {
         
       
            static private Random generator = new Random();
            private int countdown;
    
            public Ball ()
            {
                pic = Properties.Resources.Balloon;
                x = generator.Next(60, 1920);
                y = generator.Next(100, 1000);
                xspeed = generator.Next(-10, 4);
                yspeed = generator.Next(-10, 4);
                countdown = generator.Next(100, 200);
            }
            public new void Move() 
            {
                countdown--;
                if (countdown == 0)
                {
                    xspeed = generator.Next(-4, 4);
                    yspeed = generator.Next(-4, 4);
                    countdown = generator.Next(20, 40);
                }
                x = x + xspeed;
                y = y + yspeed;
            }
    
         
            
        }
    }
    

    box.cs

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; //pictures namespace shootyarcher { class Box { public float x; public float y; public float w; public float h; public float xspeed; public float yspeed; public Image pic; public Box() // constructor { x = 0; y = 0; xspeed = 0; yspeed = 0; } public void Move() { x = x + xspeed; y = y + yspeed; } public void Draw(Graphics g) { g.DrawImage (pic, x, y); } public float Width() { return pic.Width; } public float Height() { return pic.Height; } public float Left() { return x; } public float Right() { return x + Width(); } public float Top() { return y; } public float Bottom() { return y + Height(); } } }



    Saturday, August 18, 2018 5:06 PM

All replies

  • Ball does not have a property bounds.

    Lloyd Sheen

    Saturday, August 18, 2018 6:00 PM
  • Thank you.

    I was thinking it had something to do with this so i added to the box.cs

    public string bounds { get; set; }

    I don't know if i am just missing something really obvious because i have no clue how this can then relate to positioning of all my Balls.

    i don't want the answer really(I'im never going to learn that way!) but i have been stuck like this for a few days now.

    could someone point me in the right direction?

     





    • Edited by RussLee33 Saturday, August 18, 2018 6:24 PM
    Saturday, August 18, 2018 6:22 PM
  • Bounds is defined as a string. Bounds are generally rectangles. The IntersectsWith method wouldn't exist on String. Change your Bounds property to Rectangle and then the method will exist.

    public Rectangle Bounds { get; set; }

    Note that your code is still responsible for calculating Bounds so it probably shouldn't be settable. Maybe something like this.

    using System;
    using System.Drawing; //pictures
    
    namespace shootyarcher
    {
       class Box
       {
          //Don't make fields public
          private float w;
          private float h;
          private Image pic;
    
          //Going out on a limb and assuming the picture shouldn't change for the life of the box
          public Box ( Image image )
          {
             pic = image;
          }
    
          //These seem like properties you'll want to be able to change - consider using Point instead though
          public float X { get; set; }
          public float Y { get; set; }
    
          //Do you really need to set these properties?
          public float XSpeed { get; set; }
          public float YSpeed { get; set; }
        
          //Maybe speed should be parameters to this method??
          public void Move()
          {
             X += XSpeed;
             Y += YSpeed;
          }
    
          public void Draw(Graphics g)
          {
             g.DrawImage (pic, X, Y);
          }
    
          //In newer versions of C# use expression bodies
          public float Width => pic.Width;
          //public float Width
          //{
          //   get { return pic.Width; }
          //}
    
          //Use properties for get/set-like methods
          public float Height => pic.Height;
    
          public float Left => X;
          public float Right => X + Width;
    
          public float Top => Y;
          public float Bottom => Y + Height;
    
          public Rectangle Bounds => new Rectangle(X, Y, Width, Height);
        }
    }



    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, August 18, 2018 8:06 PM
    Moderator
  • thank you for the reply, its defiantly a lot of help as i was well and truly stumped.  
    Saturday, August 18, 2018 8:33 PM
  • Hi RussLee33,

    Is there any update, do you resolve the issue? if yes, please mark the helpful reply as answer, if no, please feel free let us know.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 20, 2018 1:49 AM
    Moderator