Restricting picture box movement inside panel - c#

i am creating a game application in c#, there is panel and picturebox inside that panel. now i want to restrict that picture box within that panel boundary. i am moving picture box with keydown event.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
pictureBox1.Left -= 10;
//po = position.Left;
}
else if (e.KeyCode == Keys.Right)
{
pictureBox1.Left += 10;
// po = position.Right;
}
else if (e.KeyCode == Keys.Up)
{
pictureBox1.Top -= 10;
// po = position.Up;
}
else if (e.KeyCode == Keys.Down)
{
pictureBox1.Top +=10;
// po = position.Down;
}
}

It is preferred that you use a switch statement in your case.
The below code is tested and working. You just need to set the Max values calculated from the height/width of the PictureBox and Panel.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int borderMargin = 5;
int stepSize = 10;
switch (e.KeyCode)
{
case Keys.Left:
{
int newLeft = Math.Max(0, pictureBox1.Left - stepSize);
pictureBox1.Left = newLeft;
break;
}
case Keys.Right:
{
int maxVal = panel1.Width - pictureBox1.Width - borderMargin;
int newLeft = Math.Min(maxVal, pictureBox1.Left + stepSize);
pictureBox1.Left = newLeft;
break;
}
case Keys.Up:
{
int newTop = Math.Max(0, pictureBox1.Top - stepSize);
pictureBox1.Top = newTop;
break;
}
case Keys.Down:
{
int maxVal = panel1.Height - pictureBox1.Height - borderMargin;
int newTop = Math.Min(maxVal, pictureBox1.Top + stepSize);
pictureBox1.Top = newTop;
break;
}
}
}
Also set the borderMargin and stepSize according to your needs based on your Form configuration.

Related

Change a back color that exists in a different form

Hello I'm trying to change a square color pnlPlayer in PlayingField.cs however the choice must exist on ColorChoice.cs
Whenever I try to change the pnlPlayer.BackColor to a different color the pnlPlayer tells me it does not exist in the current context.
how can I change the backColor of pnlPlayer that exists in PlayingField from ColorChoice
Here is the relevant code.
ColorChoice.cs
private void ColorChoice_Load(object sender, EventArgs e)
{
//set colors to combobox
//ComboBox ColorComboBox = new ComboBox();
ColorComboBox.Items.Add("Red");
ColorComboBox.Items.Add("Blue");
ColorComboBox.Items.Add("Green");
ColorComboBox.Items.Add("Black");
//add to combobox form
this.Controls.Add(ColorComboBox);
}
public void ColorConfirm_Click(object sender, EventArgs e)
{
PlayingField PF = new PlayingField();
PF.Show();
switch (ColorComboBox.SelectedIndex)
{
case 1:
pnlPlayer.BackColor = Color.Red; //The problems are here
break;
case 2:
pnlPlayer.BackColor = Color.Blue; //The problems are here
break;
case 3:
pnlPlayer.BackColor = Color.Green; //The problems are here
break;
case 4:
pnlPlayer.BackColor = Color.Black; //The problems are here
break;
}
PlayingField.cs
public PlayingField()
{
InitializeComponent();
StartGame();
}
public void StartGame()
{
pnlPlayer.Location = new Point(200, 200);
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.J || e.KeyCode == Keys.Left) //LEFT
{
int newX = (pnlPlayer.Location.X - 10 >= 0) ? pnlPlayer.Location.X - 10 : 0;
pnlPlayer.Location = new Point(newX, pnlPlayer.Location.Y);
}
else if (e.KeyCode == Keys.I || e.KeyCode == Keys.Up) //UP
{
int newY = (pnlPlayer.Location.Y - 10 >= 0) ? pnlPlayer.Location.Y - 10 : 0;
pnlPlayer.Location = new Point(pnlPlayer.Location.X, newY);
}
else if (e.KeyCode == Keys.L || e.KeyCode == Keys.Right) //RIGHT
{
int newX = (pnlPlayer.Location.X + 10 <= 400) ? pnlPlayer.Location.X + 10 : 400;
pnlPlayer.Location = new Point(newX, pnlPlayer.Location.Y);
}
else if (e.KeyCode == Keys.K || e.KeyCode == Keys.Down) //DOWN
{
int newY = (pnlPlayer.Location.Y + 10 <= 400) ? pnlPlayer.Location.Y + 10 : 400;
pnlPlayer.Location = new Point(pnlPlayer.Location.X, newY);
}
else return; //user pressed a key that does not have a function in the program
}

C# End image movement by pressing key

I am new to C# and wanted to know how I could make it so that once the user presses the enter key, the current location of the image becomes its fixed location. I was thinking that the best way to do it would be using a while loop. Help would really be appreciated. The following is my code for moving my image:
private void pictureBox1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
{
if (e.KeyCode == Keys.Right) x += 50;
else if (e.KeyCode == Keys.Left) x -= 50;
else if (e.KeyCode == Keys.Up) y -= 50;
else if (e.KeyCode == Keys.Down) y += 50;
pictureBox1.Location = new Point(x, y);
}
}
In this solution i have used a global bool object and changed flag to true once an enter key is pressed.
I have a form with Picture Box and on the form_KeyDown event i placed your code with small change.
bool bIsEnterKeyPressed = false;
private void frmSampleJson_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
bIsEnterKeyPressed = true;
}
if (!bIsEnterKeyPressed)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
{
if (e.KeyCode == Keys.Right) x += 50;
else if (e.KeyCode == Keys.Left) x -= 50;
else if (e.KeyCode == Keys.Up) y -= 50;
else if (e.KeyCode == Keys.Down) y += 50;
pictureBox1.Location = new Point(x, y);
}
}
}
Once enter is pressed the bIsEnterKeyPressed is changed to true and the position will not be changed after that.

c# variables reset when code is moved from keydown event into a function - can anyone explain why?

I am using a C# windows forms application in VS Express 2010. I'm playing around with some code for the purposes of learning. I have a form object, which I want to continually move in one direction with a key press - original code for this works fine.
In the process of trying to tidy it by moving the code for "movement" into a function, the code no longer works as before. Instead of my object moving from its current position in the direction selected, it now resets its position every time a key is pressed. I was wondering why this is happening, as the change I have made is absolutely minimal. Please see code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
direction = 4;
}
if (e.KeyCode == Keys.Up)
{
direction = 2;
}
if (e.KeyCode == Keys.Right)
{
direction = 3;
}
if (e.KeyCode == Keys.Left)
{
direction = 1;
}
while (direction != 0)
{
Application.DoEvents();
if (direction == 1)
{
X = X - 1;
}
else if (direction == 2)//up
{
Y = Y - 1;
}
else if (direction == 3)
{
X = X + 1;
}
else if (direction == 4)//down
{
Y = Y + 1;
}
Thread.Sleep(100);
label1.Location = new Point(X, Y);
}
}
when I move the while loop into a movement function, the variables X and Y are reset to 0 upon every key press. code for this looks as follows:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
direction = 4;
}
if (e.KeyCode == Keys.Up)
{
direction = 2;
}
if (e.KeyCode == Keys.Right)
{
direction = 3;
}
if (e.KeyCode == Keys.Left)
{
direction = 1;
}
movement(X, Y, direction);
}
I feel like I'm missing something obvious here but I don't understand why it's behaving differently. Thanks for any help :)
EDIT 1: The code for the movement function:
movement(int X, int Y, int direction)
{
while (direction != 0)
{
Application.DoEvents();
if (direction == 1)
{
X = X - 1;
}
else if (direction == 2)//up
{
Y = Y - 1;
}
else if (direction == 3)
{
X = X + 1;
}
else if (direction == 4)//down
{
Y = Y + 1;
}
Thread.Sleep(100);
label1.Location = new Point(X, Y);
}
}
I guess your movement method changes X and Y which were supplied as parameters. However, the parameters X and Y are passed 'by value', not 'by reference'.
If you want to make this work, you either have to use class variables, and remove the parameters in the method call, or use ref, which I will demonstrate:
movement(ref X, ref Y, direction);
And:
private void movement(ref int X, ref int Y, int direction)
{ }
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down) direction = 4;
else if (e.KeyCode == Keys.Up) direction = 2;
else if (e.KeyCode == Keys.Right) direction = 3;
else if (e.KeyCode == Keys.Left) direction = 1;
while (direction != 0)
{
Application.DoEvents();
if (direction == 1) X--;
else if (direction == 2) Y--;
else if (direction == 3) X++;
else if (direction == 4) Y++;
Thread.Sleep(100);
label1.Location = new Point(X, Y);
}
}
Could be rewritten :
private void SetDirection(KeyEventArgs e)
{
if (e.KeyCode == Keys.Down) direction = 4;
else if (e.KeyCode == Keys.Up) direction = 2;
else if (e.KeyCode == Keys.Right) direction = 3;
else if (e.KeyCode == Keys.Left) direction = 1;
}
private void ApplyMovement()
{
while (direction != 0)
{
Application.DoEvents();
if (direction == 1) X--;
else if (direction == 2) Y--;
else if (direction == 3) X++;
else if (direction == 4) Y++;
Thread.Sleep(100);
label1.Location = new Point(X, Y);
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
SetDirection(e);
ApplyMovement();
}
You have to understand that the variables X, Y, and direction are class members, so they are accessible from anywhere in your class, and when you call movement(X, Y, direction) then movement plays with copies of those variables, so when you do X = X + 1 inside your movement method, you are not actually changing the X value of your class, but only to the copy of it.
This means that the variables X and Y (i.e. class members) are not reset to 0 upon every key press. In fact they are never set to anything else.
Note that you could also use switch statements instead of all those ifs.
switch (direction)
{
case 1: X--; break; // left
case 2: Y--; break; // up
case 3: X++; break; // right
case 4: Y++; break; // down
}

Count ++ when collision between two pictureboxes plus random drop of pictureBox

I coding a very simple program.
A pictureBox drops from above and another pictureBox (located at button) have to grab the pictureBox (collision between those two picturesBoxes) and then you get one point.
My issue is when the collision happens then Point are counting up as long the collision between the two pictureBoxes happens.
How to stop this and only count ++ when a collision happens?
- I'm total rookie coding c# so please don't answer a very advanced answer :)
namespace Animation
{
public partial class Form1 : Form
{
Point p1 = new Point();
Point p2 = new Point();
int bredde;
int højde;
int score;
int count = 0;
public Form1()
{
InitializeComponent();
p1.X = pictureBox1.Location.X;
p1.Y = pictureBox1.Location.Y;
p2.X = pictureBox2.Location.X;
p2.Y = pictureBox2.Location.Y;
højde = ClientSize.Height;
bredde = ClientSize.Width;
}
Boolean max = true;
void collision()
{
if (pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds))
{
score++;
label1.Text = Convert.ToString(score);
max = true;
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.W && p1.Y > 0)
p1.Y -= 3;
if (e.KeyData == Keys.D && p1.X + pictureBox1.Width < bredde)
p1.X +=3;
if (e.KeyData == Keys.A && p1.X > 0)
p1.X -=3;
if (e.KeyData == Keys.S && p1.Y + pictureBox1.Height < højde)
p1.Y +=3;
pictureBox1.Location = p1;
if (e.KeyData == Keys.W && p2.Y > 0)
if (e.KeyData == Keys.D && p2.X + pictureBox2.Width < bredde)
if (e.KeyData == Keys.A && p2.X > 0)
if (e.KeyData == Keys.S && p2.Y + pictureBox2.Height < højde)
pictureBox2.Location = p2;
}
private void timer1_Tick(object sender, EventArgs e)
{
collision();
p2.Y +=5;
pictureBox2.Location = p2;
}
private void buttonStart_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void buttonStop_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
}
}

how do I move a panel with directional keyboard

In a label docking on panel control I used this code:
(this is for move my panel with the mouse pointer)
Point m_pntPosPanel = new Point();
private void lblMove_MouseUp(object sender, MouseEventArgs e)
{
pnlCost.Location = new Point(e.X - m_pntPosPanel.X + pnlCosto.Location.X, e.Y - m_pntPosPanel.Y + pnlCosto.Location.Y);
Cursor.Current = Cursors.Default;
m_blnMouseDown = false;
}
private void lblMove_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
m_pntPosPanel.X = e.X;
m_pntPosPanel.Y = e.Y;
Cursor.Current = Cursors.Hand;
m_blnMouseDown = true;
}
}
how do I make to move that panel with directional keyboard?
I would register for the KeyUp event on the control and process as follows:
private void lblMove_KeyUp(object server, KeyEventArgs e)
{
Point location = button1.Location;
switch(e.KeyCode)
{
case Keys.Up:
location.Y = location.Y -1;
break;
case Keys.Down:
location.Y = location.Y + 1;
break;
case Keys.Right:
location.X = location.X + 1;
break;
case Keys.Left:
location.X = location.X - 1;
break;
}
button1.Location = location;
}

Categories