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();
}
Loading
VulpesPosted Mar 20, 2012, 1:03 PM
However, it's a bit rushed but here's something for you to work on. Sam will probably be able to come up with something better later today.
Notice that as this currently stands, you have to keep the shift key pressed down to move the ship within the dock as well as to enter it.
I'm assuming you're happy with the mouse interface as it stands, because you'd need 3 hands to hold do the shift and arrow keys whilst moving the mouse:
Sam HobbsPosted Mar 21, 2012, 7:53 AM
Sam HobbsPosted Mar 21, 2012, 7:49 AM
I think the following is equivalent to Vulpes's code. Note that if it were me, I would write the code so that the program could test for intersection without updating the position; inohter words, check to determine if the new position intersects and if it does then do the move only if it is allowed. That way the position does not need to be undone if the docking is not allowed.
{{}}{{}}Sam HobbsPosted Mar 20, 2012, 6:49 PM
Prime bPosted Mar 20, 2012, 5:57 PM
Sam HobbsPosted Mar 20, 2012, 5:15 PM
Vulpes and I are different. I prefer to get clarification before proceeding; Vulpes attempts to provide an answer immediately. Providing an answer immediately works well when the question is understood and the reply is the best solution for the problem. It can happen however that the person seeking assistance goes with the first suggestion and then they are not interested in anything better. Something else that i see happening here often is that the initial guess is incorrect, so a long discussion erupts in which a suggestion is made and then a little bit more of the requirements are determined and then another suggestion is attempted. I do not like to do that because it wastes time. What I like to do is to get the requirements clarified as much as possible first without investing time in the actual source code. It is frustrating for me, since if I spend time clarifying something, then someone else can use the results of my time and post sample source code. So even if I spend more time, the other person gets credit. When I say credit, I do not mean points; I mean that people get the impression that I am not trying to help. People confuse my attempts to get clarification as if they are answers.
Programmers need to learn that if they understand the problem, the solution is often easy. Many members here do not understand that. When the reply to a vague question is sample source code, the person seeking assistance does not learn how to analyze the problem.
I will try to look at this more, but emotionally it is very difficult for me.
Prime bPosted Mar 20, 2012, 2:09 PM
I just tried to make two different ways to move the ship, 1. you can use only mouse or 2. you can use arrow keys, but to make it more difficult the shift key should be held down to enter the dock( it just goes inside of the object ).
I dont have VS installed here, so once i get home I will see if it works.
Thank you!
Sam HobbsPosted Mar 20, 2012, 12:51 PM
Prime bPosted Mar 20, 2012, 12:18 PM
Prime bPosted Mar 20, 2012, 9:12 AM
Prime bPosted Mar 19, 2012, 7:10 PM
Sam HobbsPosted Mar 19, 2012, 6:53 PM
Actually, I do not see a question here.