I'm making a mini game. I want to rotate my player with this code when I turn left and right picPlayer.Image.RotateFlip(RotateFlipType.RotateNoneFlipX) My moving code is:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//when one of the movement keys are pressed,
//makes it's variable true.
if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)
{
moveRight = true;
}
}
Also, my timer's program is:
private void tmrMovementPlayer_Tick(object sender, EventArgs e)
{
//whenever right arrow is pressed,
if (moveRight == true)
{
//decrease the x variable by 5 (moves right)
x = x + PLAYER_SPEED;
//check for boundaries (if the player is out of the screen)
if (x >= this.ClientSize.Width - picPlayer.Width)
{
//if yes, set it back to the boundary.
x = this.ClientSize.Width - picPlayer.Width;
}
//check the subprogram for info
MovePlayer();
}
}
What should I do at this point? Thanks.
So thanks to the other answer, I found the solution. It was a little bit different. I created another boolean called "goingRight" just for the rotation.
if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)
{
//make move right true
moveRight = true;
//if i was going left,
if (goingRight == false)
{
//say it im going right
goingRight = true;
//and flip it (only if i was going left before)
picPlayer.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
}
}
Again, thanks for all the help.
Try something like...
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//when one of the movement keys are pressed,
//makes it's variable true.
if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)
{
if (!moveRight)
{
picPlayer.Image.RotateFlip(RotateFlipType.RotateNoneFlipX)
moveRight = true;
}
}
}
Related
So I want to make a Global hotkey and have Mouse 4 or 5 auto click my mouse.
The way I'm global binding is a bit interesting.
So on the form, I have a label that will set active control when clicked to listen to show what the keybind is when pressed.
private Keys clickerHotkey;
private void leftClickHotkeyLbl_Click(object sender, EventArgs e)
{
this.ActiveControl = leftClickHotkeyLbl; // set active to able to respond to the key down event.
leftClickHotkeyLbl.Text = "[...]";
}
private void leftClickHotkeyLbl_KeyDown(object sender, KeyEventArgs e)
{
leftClickHotkeyLbl.TabStop = false;
if (!((e.KeyValue >= 16 && e.KeyValue <= 18) || (e.KeyValue >= 21 && e.KeyValue <= 25) || (e.KeyValue >= 28 && e.KeyValue <= 31) || e.KeyValue == 229 || (e.KeyValue >= 91 && e.KeyValue <= 92))) // this gets rid of non sense keys...
{
KeyBindManager.KeysConverter.UnregisterHotKey(this.Handle, (int)clickerHotkey); // unregister previous key.
clickerHotkey = e.KeyData;
if (clickerHotkey == Keys.XButton1) // doesn't work :(
{
Console.WriteLine("Mouse 5 Detected");
}
if (clickerHotkey == Keys.Escape) // if the key is escape, return
{
UnsetHotkey(clickerHotkey);
leftClickHotkeyLbl.Text = "[-]";
this.ActiveControl = null;
return;
}
clickerModifiers = ExtractModifier(clickerModifiers, e);
SetHotkey(clickerModifiers, clickerHotkey);
leftClickHotkeyLbl.Text = $"[{KeyBindManager.KeysConverter.Convert(clickerHotkey)}]";
this.ActiveControl = null; // set to null so its no longer being edited.
}
}
Any help is much appreciated! Cheers!
You are trying to detect a mouse event/mouse button via a keyboard key...
You cant use Keys.Button for a mouse event as it's specific to keyboard keys, chence the name Keys.
if (clickerHotkey == Keys.XButton1) // doesn't work :(
{
Console.WriteLine("Mouse 5 Detected");
}
If you want to detect a mouse event/mouse button click you can change it to the following:
if (clickerHotkey == MouseButtons.XButton1) // or you can use XButton2
{
Console.WriteLine("Mouse 5 Detected");
}
This intern specifies that you want to listen for a mouse clicks.
Due to your event handler being a Keyboard specific one, you need to add a seperate event handler for mouse clicks eg.
private void mouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.XButton1)
{
Console.WriteLine("Mouse 5 Detected");
}
}
I'm trying to use this code to make a pong game, but for what ever reason, my code isn't working. As far as I can tell, everything should be correct.
I have the Timer enabled and the code compiles, because the program boots without errors, but the PictureBoxes aren't moving.
Can someone spot my issue?
public partial class Form1 : Form
{
bool P1Up, P1Down, P2Up, P2Down;
int Speed = 12;
public Form1()
{
InitializeComponent();
}
private void MoveTriggerTick(object sender, EventArgs e)
{
if (P1Up == true) Player1Paddle.Top += Speed;
if (P1Down == true) Player1Paddle.Top -= Speed;
if (P2Up == true) Player2Paddle.Top += Speed;
if (P2Down == true) Player2Paddle.Top -= Speed;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int P1Y = Player1Paddle.Location.Y;
int P2Y = Player2Paddle.Location.Y;
if (e.KeyCode.ToString() == "w") P1Up = true;
if (e.KeyCode.ToString() == "s") P1Down = true;
if (e.KeyCode == Keys.Up) P2Up = true;
if (e.KeyCode == Keys.Down) P2Down = true;
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
int P1Y = Player1Paddle.Location.Y;
int P2Y = Player2Paddle.Location.Y;
if (e.KeyCode.ToString() == "w") P1Up = false;
if (e.KeyCode.ToString() == "s") P1Down = false;
if (e.KeyCode == Keys.Up) P2Up = false;
if (e.KeyCode == Keys.Down) P2Down = false;
}
}
Footnote: Wanted to get paddle movement implemented before looking at adding the ball or score.
if (e.KeyCode.ToString() == "w")
should be
if (e.KeyCode == Keys.W)
and so on.
It's in the System.Windows.Forms namespace (see the documentation).
Also check that MoveTriggerTick is correctly registered as timer handler.
I have been trying to make a button in C# move based on the user pressing various arrow keys under a Form1 KeyDown method, but i cannot figure out how to make the value of e equal to the users input.
So far i have:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
button1.Location = new Point(button1.Top, button1.Left + 1);
}
else if (e.KeyCode == Keys.Right)
{
button1.Location = new Point(button1.Top, button1.Right + 1);
}
else if (e.KeyCode == Keys.Up)
{
button1.Location = new Point(button1.Top +1, button1.Right);
}
else if (e.KeyCode == Keys.Down)
{
button1.Location = new Point(button1.Top-1, button1.Right);
}
}
and then to call it I use
Form1_KeyDown(sender: Keys.A ,e: KeyDown);
but i have no idea what to put in for the value of either sender or e. If it helps, my goal is to be able to have the user maneuver a button around the screen at a set speed. Thanks!
I am working on a gaming project with a base to help get me started. The base is overall extremely helpful, however there is a part of the code that confuses me as to how to manipulate correctly.
When this code is run, the player sprite will indefinitely move in a direction until the other key press is used. If a playerMoveY is added, it will get stuck moving in diagonals.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
playerMoveX = -1;
}
else if (e.KeyCode == Keys.Right)
{
playerMoveX = 1;
}
Without seeing your full code, it's going to be hard to say for sure, but it is probably because you don't have a KeyUp event to reset the move value.
Similarly to how you created a KeyDown event to use the Form1_KeyDown method, try something like this for KeyUp
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
playerMoveX = 0;
}
else if (e.KeyCode == Keys.Right)
{
playerMoveX = 0;
}
}
I'm trying to make it so that when I press the up arrow, it moves the picture box up, the down arrow moves down, ect. But I can't seem to get it to work. It is giving me the error:
Can't modify the return value of
'System.Windows.Forms.Control.Location' because it is not a variable
This is my code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Up)
{
ImgGuy.Location.Y--;
}
else if (e.KeyCode == Keys.Down)
{
ImgGuy.Location.Y++;
}
else if (e.KeyCode == Keys.Left)
{
ImgGuy.Location.X--;
}
else if (e.KeyCode == Keys.Right)
{
ImgGuy.Location.X++;
}
Any help is greatly appreciated.
You have to recreate new Location:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Point l;
if(e.KeyCode == Keys.Up)
{
l = new Point(ImgGuy.Location.X, ImgGuy.Location.Y - 1);
}
else if (e.KeyCode == Keys.Down)
{
l = new Point(ImgGuy.Location.X, ImgGuy.Location.Y + 1);
}
else if (e.KeyCode == Keys.Left)
{
l = new Point(ImgGuy.Location.X - 1, ImgGuy.Location.Y);
}
else if (e.KeyCode == Keys.Right)
{
l = new Point(ImgGuy.Location.X + 1, ImgGuy.Location.Y);
}
ImgGuy.Location = l;
}
Try this:
ImgGuy.Location = new Point(ImgGuy.Location.X+1, ImgGuy.Location.Y+1) // etc
The problem is that Location returns a copy of the location.
Alternatively, set Control.Left and Control.Top instead.
You need to produce a new point
In this case X is increased aka move left
Pic.Location = new Point(Pic.Location.X + 1, Pic.Location.Y);