moving a image without selecting the buttons - C# - c#

I want to create a game (Multicolor maze), involves using a image ove using arrow keys, but during runtime, the buttons are selected and not the image when arrow keys are pressed?
Runtime
[UPDATED]
public Form1()
{
InitializeComponent();
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
System.Console.WriteLine("here");
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// for the image move
int x = Convert.ToInt32(HeartSprite.Location.X);
int y = Convert.ToInt32(HeartSprite.Location.Y);
if (e.KeyCode == Keys.Right) x = x += 25;
else if (e.KeyCode == Keys.Left) x -= 25;
else if (e.KeyCode == Keys.Up) y -= 25;
else if (e.KeyCode == Keys.Down) y += 25;
HeartSprite.Location = new Point(x, y);
e.Handled = true;
}

Try setting e.Handled = true; at the end of the function to block the buttons stealing the input
Your code becomes:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// for the image move
int x = Convert.ToInt32(HeartSprite.Location.X);
int y = Convert.ToInt32(HeartSprite.Location.Y);
if (e.KeyCode == Keys.Right) x += 25;
else if (e.KeyCode == Keys.Left) x -= 25;
else if (e.KeyCode == Keys.Up) y -= 25;
else if (e.KeyCode == Keys.Down) y += 25;
HeartSprite.Location = new Point(x, y);
e.Handled = true;
}
}

Related

How do I move an object using a timer and the key press event (Like snake)

I want to move a button automatically using a timer and want it to change direction when another key is pressed, kinda like what happens in snake
I have used a string to set what the direction should be, but this doesn't work.
string x = "right";
public Form1()
{
InitializeComponent();
timer1.Interval = 1000;
timer1.Start();
}
private void Player_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
string x = "up";
}
else if (e.KeyCode == Keys.S)
{
string x = "down";
}
else if (e.KeyCode == Keys.D)
{
string x = "right";
}
else if (e.KeyCode == Keys.A)
{
string x = "left";
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Timer1_Tick(object sender, EventArgs e)
{
while (x == "up")
{
Player.Top -= 10;
}
while (x == "down")
{
Player.Top += 10;
}
while (x == "right")
{
Player.Left += 10;
}
while (x == "left")
{
Player.Left -= 10;
}
}
}
The button just disappears, but I want it to move by ten until it gets a key press like "D" then it changes direction
1: You are creating variable 'x' every time means locally in player_keyDown event so create it globally.
2: you are using while loop it, not needed as you are already using timer_tick.
3: Instead of Top, Left use Location property of button it gives you X, Y co-ordinate
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
timer1.Interval = 1000;
timer1.Start();
}
string x = "right";
private void Player_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
x = "up";
}
else if (e.KeyCode == Keys.S)
{
x = "down";
}
else if (e.KeyCode == Keys.D)
{
x = "right";
}
else if (e.KeyCode == Keys.A)
{
x = "left";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (x == "up")
{
Player.Location = new System.Drawing.Point(Player.Location.X, Player.Location.Y - 10);
}
if (x == "down")
{
Player.Location = new System.Drawing.Point(Player.Location.X, Player.Location.Y + 10);
}
if (x == "right")
{
Player.Location = new System.Drawing.Point(Player.Location.X + 10, Player.Location.Y);
}
if (x == "left")
{
Player.Location = new System.Drawing.Point(Player.Location.X - 10, Player.Location.Y);
}
}
}

C# KeyDown not working

I have a picturebox that I want to relocate when a key is pressed.
I'm making a game of pong. With the code below I want to make the paddle of player 1, which is a picturebox that should move up and down while a key is pressed. The following code doesn't seem to work:
public partial class Form1 : Form
{
bool upPressed, downPressed;
public Form1()
{
InitializeComponent();
}
private void PongTimer_Tick(object sender, EventArgs e)
{
if(upPressed){
paddlePlayer1.Location = new Point(paddlePlayer1.Location.X, paddlePlayer1.Location.Y + 5);
}
if (downPressed)
{
MessageBox.Show("Numlock");
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Up){
upPressed = true;
}
if (e.KeyCode == Keys.NumLock)
{
downPressed = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Up){
upPressed = false;
}
}
}
I used the Numlock to see if the problem was in the picture, this isn't the case. I've put the KeyPreview propery to true but this didnt solve it either. The timer does work properly.
I hope somebody here can tell me what I'm doing wrong.
This is a code example to move a PictureBox:
Drag/drop a PictureBox on a form (in this case Form1).
Change the Background color of the PictureBox to e.g. red. (This way it is visible during runtime).
Implement the KeyDown event of Form1 (in this case Form1_KeyDown).
Run the application.
Start pressing an arrow key and the PictureBox will move.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.Right) { x += 1; }
else if (e.KeyCode == Keys.Left) { x -= 1; }
else if (e.KeyCode == Keys.Up) { y -= 1; }
else if (e.KeyCode == Keys.Down) { y += 1; }
pictureBox1.Location = new Point(x, y);
}

How to prevent two events about the same thing happening

Basically,I am a beginner in C# and I just started working on a simple platformer game following a basic Visual Studio 2015 tutorial.This is the code:
public partial class Form1 : Form
{
bool goleft = false;
bool goright = false;
bool jumping = false;
int JumpSpeed = 10;
int force = 8;
int score = 0;
public Form1()
{
InitializeComponent();
}
private void keyisdown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
goleft = true;
}
if (e.KeyCode == Keys.Right)
{
goright = true;
}
if (e.KeyCode == Keys.Space && !jumping)
{
jumping = true;
}
}
private void keyisup(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
goleft = false;
}
if (e.KeyCode == Keys.Right)
{
goright = false;
}
if (jumping)
{
jumping = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
player.Top += JumpSpeed;
if (jumping && force < 0)
{
jumping = false;
}
if (goleft)
{
player.Left -= 5;
}
if (goright)
{
player.Left += 5;
}
if (jumping)
{
JumpSpeed = -12;
force -= 1;
}
else
{
JumpSpeed = 12;
}
foreach (Control x in this.Controls)
{
if (x is PictureBox && x.Tag == "p")
{
if (player.Bounds.IntersectsWith(x.Bounds) && !jumping)
{
force = 8;
player.Top = x.Top - player.Height;
}
}
if (player.Bounds.IntersectsWith(Door.Bounds))
{
timer1.Stop();
MessageBox.Show("You Won!");
this.Close();
}
}
}
}
The gravity force and everything works great EXCEPT for one big problem with the player that I definitely can't solve without some help.
The problem is: the player is completely glitched and it keeps going up and down. I realised that this is because of two lines in private void timer1_Tick(object sender, EventArgs e):
player.Top += JumpSpeed;
and
player.Top = x.Top - player.Height;
Both of these try to change player.Top and interfere with each other. The trouble is that I can't just cut one of the two without destroying the gravity force code. What I think might work is applying player.Top += JumpSpeed; ONLY if(player.Bounds.IntersectsWith(x.Bounds) && !jumping) = false but I don't know how to write the code this way at all (previous tries always ended up with errors).
Thanks in advance for any help and please forgive any grammar errors that I made because I am not a native english speaker.
I don't know how your Form looks like, but with what you provided I would change the code like that:
public partial class Form1 : Form
{
bool goleft = false;
bool goright = false;
int JumpSpeed = 0; //current vertical speed
int force = 8; //initial jump speed
int score = 0;
public Form1()
{
InitializeComponent();
}
private void keyisdown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
goleft = true;
}
if (e.KeyCode == Keys.Right)
{
goright = true;
}
if (e.KeyCode == Keys.Space && JumpSpeed==0) //JumpSpeed==0 to prevent "double jump"
{
JumpSpeed = force; //start jumping with full power
}
}
private void keyisup(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
goleft = false;
}
if (e.KeyCode == Keys.Right)
{
goright = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (goleft)
{
player.Left -= 5;
}
if (goright)
{
player.Left += 5;
}
JumpSpeed -= 1; //decrement the vertical speed = gravity -> choose higher value to bring him back on the ground quicker
player.Top -= JumpSpeed;
if (player.Bounds.IntersectsWith(Door.Bounds))
{
timer1.Stop();
MessageBox.Show("You Won!");
this.Close();
}
foreach (PictureBox x in this.Controls.OfType<PictureBox>())
{
if (x.Tag == "p")
{
if (player.Bounds.IntersectsWith(x.Bounds)) //if the player should be able to jump/walk on something the code is
{
player.Top = x.Top - player.Height;
JumpSpeed = 0; //the player stops falling
}
}
}
}
}
To make sure to player does not fall out of the window place PictureBoxes with tag == p on the floor!
PS:
Feel free to leave a comment!

c# need help by moving a object in two direction

I need help by moving a object on a Panel in two different direction. In example in the right and down direction by pressing the right and left buttons. Java have a a key release method do c# have such a similiar method?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace runningRectangle
{
enum Position {Left, Right, Down, Up, LeftUp, LeftDown, RightUp, RightDown }
public partial class Form1 : Form
{
private int x;
private int y;
private Position objPos;
private int speed = 10;
private bool stopPressed = false;
public Form1()
{
InitializeComponent();
x = 50;
y = 50;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Red, x, y, 50, 50);
}
private void timer1_Tick(object sender, EventArgs e)
{
if(objPos == Position.Right)
{
x += speed;
}else if(objPos == Position.Left)
{
x -= speed;
}
else if (objPos == Position.Down)
{
y += speed;
}
else if (objPos == Position.Up)
{
y -= speed;
} else if (objPos == Position.RightUp)
{
x += speed;
y -= speed;
}
else if (objPos == Position.RightDown)
{
x += speed;
y += speed;
}
else if (objPos == Position.LeftUp)
{
x -= speed;
y += speed;
}
else if (objPos == Position.LeftDown)
{
x -= speed;
y -= speed;
}
Invalidate();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
objPos = Position.Right;
}
else if (e.KeyCode == Keys.Left)
{
objPos = Position.Left;
}
else if (e.KeyCode == Keys.Down)
{
objPos = Position.Down;
}
else if (e.KeyCode == Keys.Up)
{
objPos = Position.Up;
}
else if(e.KeyCode == Keys.Right && e.KeyCode == Keys.Down)
{
objPos = Position.RightDown;
}
else if (e.KeyCode == Keys.Right && e.KeyCode == Keys.Up)
{
objPos = Position.RightUp;
}
else if (e.KeyCode == Keys.Left && e.KeyCode == Keys.Down)
{
objPos = Position.LeftDown;
}
else if (e.KeyCode == Keys.Left && e.KeyCode == Keys.Up)
{
objPos = Position.LeftUp;
}
else if(e.KeyCode == Keys.Space)
{
if (!stopPressed)
{
speed = 0;
stopPressed = true;
}
else if(stopPressed)
{
speed += 10;
stopPressed = false;
}
}
}
}
}
Form1_KeyDown and Form1_KeyUp exist in pair.
enum Position { None = -1, Left, Right, Down, Up, LeftUp, LeftDown, RightUp, RightDown }
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
objPos = (Position)(-1);
}

How to activate keydown event by clicking specific button?

I am developing 2D game(pacman)to improve myself in VB 2013 via c#,
and I want my key-down events activated by clicking specific button.(That is restart button shown when the game is over).Thanks for your help.
//these are my keydown codes
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int r = pictureBox5.Location.X;
int t = pictureBox5.Location.Y;
if (pictureBox5.Top >= 33)
{
if (e.KeyCode == Keys.Up)
{
t = t - 15;
}
}
if (pictureBox5.Bottom <= 490)
{
if (e.KeyCode == Keys.Down)
{
t = t + 15;
}
}
if (pictureBox5.Right <= 520)
{
if (e.KeyCode == Keys.Right)
{
r = r + 15;
}
}
if (pictureBox5.Left >= 30)
{
if (e.KeyCode == Keys.Left)
{
r = r - 15;
}
}
if (e.KeyCode == Keys.Up && e.KeyCode == Keys.Right)
{
t = t - 15;
r = r + 15;
}
pictureBox5.Location = new Point(r, t);
}
//and that's the button I wanted to interlace with keydown event
private void button1_Click(object sender, EventArgs e)
{
}
A bit of refactoring could help here. Suppose that if you hit the button the keycode to use is Keys.Down. In this scenario you can move all the code inside the Form_KeyDown to a different method called HandleKey
private void HandleKey(Keys code)
{
int r = pictureBox5.Location.X;
int t = pictureBox5.Location.Y;
if (pictureBox5.Top >= 33)
{
if (code == Keys.Up)
t = t - 15;
}
if (pictureBox5.Bottom <= 490)
{
if (code == Keys.Down)
t = t + 15;
}
if (pictureBox5.Right <= 520)
{
if (code == Keys.Right)
r = r + 15;
}
if (pictureBox5.Left >= 30)
{
if (code == Keys.Left)
r = r - 15;
}
// This is simply impossible
if (code == Keys.Up && code == Keys.Right)
{
t = t - 15;
r = r + 15;
}
pictureBox5.Location = new Point(r, t);
}
Now you can call this method from the Form_KeyDown event
private void Form_KeyDown(object sender, KeyEventArgs e)
{
// Pass whatever the user presses...
HandleKey(e.KeyCode);
}
and from the button click
private void button1_Click(object sender, EventArgs e)
{
// Pass your defined key for the button click
HandleKey(Keys.Down);
}

Categories

Resources