Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 11th, 2014, 06:02 PM
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
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dungeon Quest Collision Issue Ixion BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 0 September 30th, 2007 04:40 AM
browser detection Adam H-W Javascript 0 January 20th, 2007 08:55 AM
flash detection catchrohith Classic ASP Basics 0 November 18th, 2006 12:29 AM
User control ID collision arcuza ASP.NET 1.0 and 1.1 Professional 1 May 26th, 2006 08:00 AM
Infinite Loop Detection jtdang77 Classic ASP Basics 0 October 14th, 2003 05:32 PM



All times are GMT -4. The time now is 09:07 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.