TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Prime b
NA
810
346.9k
Two object interact
Mar 19 2012 3:23 PM
What I am trying to do is if shift and one of the arrow keys pressed( simultaneously )then two object can interact with each other else not!
the two objects named ship and dock, so when shift + up arrow key pressed then ship can enter the dock.
My code
namespace Learning_how_to_Paint
{
public partial class Form1 : Form
{
private Rectangle ship;
private Rectangle dock;
private bool isDragging;
public Form1()
{
InitializeComponent();
ship = new Rectangle(new Point(10, 10), new Size(10, 10));
dock = new Rectangle(new Point(30, 30), new Size(40, 40));
isDragging = false;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillEllipse(Brushes.RoyalBlue, ship);
if (ship.IntersectsWith(dock))
{
g.FillRectangle(Brushes.Yellow, dock);
}
else
{
g.FillRectangle(Brushes.Black, dock);
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
ship.Location = e.Location;
this.Refresh();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
int formWidth = this.ClientRectangle.Width;
int formHeight = this.ClientRectangle.Height;
isDragging = !(e.X + ship.Width > formHeight || e.X < 0 || e.Y + ship.Height > formHeight || e.Y < 0);
if (isDragging)
{
ship.Location = e.Location;
this.Refresh();
}
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Shift) && (e.KeyCode == Keys.Up))
{
if(ship.IntersectsWith(dock))
{
MessageBox.Show("Interact");
}
else
{
MessageBox.Show("You cant enter the object, shift key or arrow key must be pressed simultaneously");
}
}
//if ((e.Shift) && (e.KeyCode == Keys.Down))
//{
// if (ship.IntersectsWith(dock))
// {
// MessageBox.Show("Interact");
// }
// else
// {
// MessageBox.Show("You cant enter the object, shift key or arrow key must be pressed simultaneously");
// }
//}
//if ((e.Shift) && (e.KeyCode == Keys.Left))
//{
// if (ship.IntersectsWith(dock))
// {
// MessageBox.Show("Interact");
// }
// else
// {
// MessageBox.Show("You cant enter the object, shift key or arrow key must be pressed simultaneously");
// }
//}
//if ((e.Shift) && (e.KeyCode == Keys.Right))
//{
// if (ship.IntersectsWith(dock))
// {
// MessageBox.Show("Interact");
// }
// else
// {
// MessageBox.Show("You cant enter the object, shift key or arrow key must be pressed simultaneously");
// }
//}
// move object up
if (e.KeyCode == Keys.Up)
{
ship.Y = ship.Y - 5;
this.Refresh();
}
//move object down
else if (e.KeyCode == Keys.Down)
{
ship.Y = ship.Y + 5;
this.Refresh();
}
// move oboject left
else if (e.KeyCode == Keys.Left)
{
ship.X = ship.X - 5;
this.Refresh();
}
//move object right
else if (e.KeyCode == Keys.Right)
{
ship.X = ship.X + 5;
this.Refresh();
}
Reply
Answers (
12
)
Norton Deletes My Exe files??????
Converting Pseudocode to C#