View Single Post
  #1 (permalink)  
Old February 11th, 2014, 07:02 PM
crossfire19 crossfire19 is offline
Authorized User
Points: 79, Level: 1
Points: 79, Level: 1 Points: 79, Level: 1 Points: 79, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2013
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default help with Collision Detection please

I am working on a project in C# and I am trying to set up collision detection for the sides of the screen. I have a 2d image on the screen that can be moved with the arrow keys, and automatically starts moving from top to bottom when the you first run the program. Here is my code that I am working with so that you can see what I have done

Code:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Threading;


public class project : Form
{
    public int width = 50, BorderWidth = 105;
    private int height = 50, BorderHeight = 105;
    Image pic = Image.FromFile("Tetris_piece.png");
    private const int speed = 10;
    private int x = 103, y = 103, x1 = 100, y1 = 100;
    private System.ComponentModel.IContainer components;
    private System.Windows.Forms.Timer ObjectMove;
    private char[] Key = {'a', 'd','w', 's'};


    public project()
    {
        Size = new Size(500, 500);
        BackColor = Color.Blue;
        pic.RotateFlip(RotateFlipType.Rotate90FlipX);
        InitializeComponent();
        vector2(x, y);
        
    }
    public int vector2(int x, int y)
    {
        if (x == 30)
        {
            return y;
        }
        return x;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.DrawRectangle(Pens.Black, x1, y1, BorderWidth, BorderHeight);
        g.DrawImage(pic, x, y, width, height);
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        
        if (e.KeyCode == Keys.Right)
        {
            x += speed;
            x1 += speed;
            if(vector2(x, y) == vector2(30, y))
            {
                
                e.SuppressKeyPress = false;
                x = 0;
                ObjectMove.Enabled = false;
            }
        }
        else if (e.KeyCode == Keys.Left)
        {
            x -= speed;
            x1 -= speed;
        }
        else if (e.KeyCode == Keys.Up)
        {
            y -= speed;
            y1 -= speed;
        }
        else if (e.KeyCode == Keys.Down)
        {
            y += speed;
            y1 += speed;
        }
        Thread.Sleep(10);
        Invalidate();
        base.OnKeyDown(e);
    }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if(char.IsLetterOrDigit(e.KeyChar))
            {
                if (Key[0] == e.KeyChar)
                {
                    x -= speed;
                    x1 -= speed;
                }
            }
            if (char.IsLetterOrDigit(e.KeyChar))
            {
                if (Key[1] == e.KeyChar)
                {
                    x += speed;
                    x1 += speed;
                }
            }
            if (char.IsLetterOrDigit(e.KeyChar))
            {
                if (Key[2] == e.KeyChar)
                {
                    y -= speed;
                    y1 -= speed;
                }
            }
            if (char.IsLetterOrDigit(e.KeyChar))
            {
                if (Key[3] == e.KeyChar)
                {
                    y += speed;
                    y1 += speed;
                }
            }
            Invalidate();
            base.OnKeyPress(e);
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                width -= 50;
                height -= 50;
                BorderWidth -= 50;
                BorderHeight -= 50;
                    
            }
            if (e.Button == MouseButtons.Right)
            {
                width += 50;
                height += 50;
                BorderWidth += 50;
                BorderHeight += 50;
            }
            Invalidate();
            base.OnMouseDown(e);
        }
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.ObjectMove = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // ObjectMove
            // 
            this.ObjectMove.Enabled = true;
            this.ObjectMove.Tick += new System.EventHandler(this.ObjectMove_Tick);
            // 
            // project
            // 
            this.ClientSize = new System.Drawing.Size(450, 605);
            this.DoubleBuffered = true;
            this.KeyPreview = true;
            this.Name = "project";
            this.ResumeLayout(false);

        }

        private void ObjectMove_Tick(object sender, EventArgs e)
        {
            y += 10;
            y1 += 10;
            if (x < 0 || x > 350)
            {
                x -= 0;
            }
            if (y > 560)
            {
                ObjectMove.Enabled = false;
            }
            Invalidate();
        }
         public static void Main()
         {
            Application.Run(new project());
         }

}
So basically I just want to know the steps to take in setting up the collision.
Any help would be appreciated
Thanks.
Reply With Quote