none
how to detect collion between the PictureBox and CreateGraphics().DrawImage() RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace packman
    {
    
        public partial class random_gneration : Form
        {
            public random_gneration()
            {
                InitializeComponent();
                game_time.Start();
                pckman.Start();
            }
            Random rand = new Random();
    
            int count = 0, y_axis = 0, speed = 8;
            bool down, up, left, right;
    
            private void Pckman_Tick(object sender, EventArgs e)
            {
                if (left)
                {
                    pack_man.Left -= speed;
                }
                else if (right)
                {
                    pack_man.Left += speed;
                }
                else if (up)
                {
                    pack_man.Top -= speed;
                }
                else if (down)
                {
                    pack_man.Top += speed;
                }
                
               //what will be condition for detection of collion with image
            }
    
            private void key_DOWN(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Up)
                {
                    up = true;
                    pack_man.Image = Properties.Resources.ezgif_4_6f0e13cdcf82;
                    down = left = right = false;
                }
                if (e.KeyCode == Keys.Down)
                {
                    down = true;
                    pack_man.Image = Properties.Resources.ezgif_4_3befc8f66dcc;
                    up = left = right = false;
                }
                if (e.KeyCode == Keys.Left)
                {
    
                    left = true;
                    pack_man.Image = Properties.Resources.ezgif_4_867ed1223b37;
                    down = up = right = false;
                }
                if (e.KeyCode == Keys.Right)
                {
                    right = true;
                    pack_man.Image = Properties.Resources.right;
                    down = left = up = false;
                }
            }
    
            private void Game_time_Tick(object sender, EventArgs e)
            {
                this.CreateGraphics().DrawImage(Properties.Resources.wallblock,
                    new PointF(rand.Next(20, 600), y_axis));
                if (count % (rand.Next(5, 10)) == 0)
                {
                    y_axis = (y_axis + rand.Next(90, 200)) % 600;
                }
                count++;
                if (count == 30)
                {
                    game_time.Stop();
                }
                
    
            }
        }
    }

    please help 

    • Moved by CoolDadTx Monday, April 8, 2019 9:01 PM Winforms related
    Monday, April 8, 2019 5:28 PM

Answers

  • Hi question_asker,

    Here are some MSDN documents you can refer to:

    Random Class;

    PictureBox Class;

    Rectangle Struct.

    Regards,

    Kyle


    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.

    • Marked as answer by question_asker Wednesday, April 24, 2019 11:52 AM
    Wednesday, April 10, 2019 6:57 AM
    Moderator

All replies

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace packman
    {
    
        public partial class random_gneration : Form
        {
            public random_gneration()
            {
                InitializeComponent();
                game_time.Start();
                pckman.Start();
            }
            Random rand = new Random();
    
            int count = 0, y_axis = 0, speed = 8;
            bool down, up, left, right;
    
            private void Pckman_Tick(object sender, EventArgs e)
            {
                if (left)
                {
                    pack_man.Left -= speed;
                }
                else if (right)
                {
                    pack_man.Left += speed;
                }
                else if (up)
                {
                    pack_man.Top -= speed;
                }
                else if (down)
                {
                    pack_man.Top += speed;
                }
            }
    
            private void key_DOWN(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Up)
                {
                    up = true;
                    pack_man.Image = Properties.Resources.ezgif_4_6f0e13cdcf82;
                    down = left = right = false;
                }
                if (e.KeyCode == Keys.Down)
                {
                    down = true;
                    pack_man.Image = Properties.Resources.ezgif_4_3befc8f66dcc;
                    up = left = right = false;
                }
                if (e.KeyCode == Keys.Left)
                {
    
                    left = true;
                    pack_man.Image = Properties.Resources.ezgif_4_867ed1223b37;
                    down = up = right = false;
                }
                if (e.KeyCode == Keys.Right)
                {
                    right = true;
                    pack_man.Image = Properties.Resources.right;
                    down = left = up = false;
                }
            }
    
            private void Game_time_Tick(object sender, EventArgs e)
            {
                this.CreateGraphics().DrawImage(Properties.Resources.wallblock,
                    new PointF(rand.Next(20, 600), y_axis));
                if (count % (rand.Next(5, 10)) == 0)
                {
                    y_axis = (y_axis + rand.Next(90, 200)) % 600;
                }
                count++;
                if (count == 30)
                {
                    game_time.Stop();
                }
                
    
            }
        }
    }



    what will

    be the detection 

    condition

    please help 

    Monday, April 8, 2019 5:20 PM
  • Collisions are detected with Rectangle.IntersectsWith

    (I used that + pixel transparency detection in this sample for "perfect pixel collision")

    Monday, April 8, 2019 6:21 PM
  • You need to know the size of your wallblock resource and your PacMan resources.  You can then use Rectangle.IntersectsWith to determine when the two collide.

    Note that it is error-prone to have four boolean variables like that.  Instead, just create an enumeration with the 4 directions and have one variable that is "current direction".

    It is quite possible that your "count % (rand.Next(5,10)) == 0" expression will never be true.  Can you describe in ordinary words what were you trying to express there?


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, April 8, 2019 7:12 PM
  • Hello,

    Thanks for posting here.

    This forum is about c++ development. For your case which is more related to Windows Forms, I will move this post to Windows Forms forum for better support.

    Thanks for your understanding and Cooperation.

    Best Wishes,

    Jack Zhang


    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.

    Tuesday, April 9, 2019 1:56 AM
  • sir how can i make randomly PictureBoxes or rectangles

    so that i can detect its collion bounds

    i am a bigginier of(between(0.5,0.8))months

    please help

    if you know

     
    Tuesday, April 9, 2019 10:17 AM
  • Hi question_asker,

    Here are some MSDN documents you can refer to:

    Random Class;

    PictureBox Class;

    Rectangle Struct.

    Regards,

    Kyle


    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.

    • Marked as answer by question_asker Wednesday, April 24, 2019 11:52 AM
    Wednesday, April 10, 2019 6:57 AM
    Moderator