SlimDX RawInput with WPF - c#

I'm trying to set up keyboard and mouse controls for a space game using SlimDX and RawInput. My current code is as follows:
Device.RegisterDevice(UsagePage.Generic, UsageId.Keyboard, DeviceFlags.None);
Device.KeyboardInput += new EventHandler<KeyboardInputEventArgs>(keyboardInput);
Device.RegisterDevice(UsagePage.Generic, UsageId.Mouse, DeviceFlags.None);
Device.MouseInput += new EventHandler<MouseInputEventArgs>(mouseInput);
However I read here: http://code.google.com/p/slimdx/issues/detail?id=785 that for WPF I need to use a different overload for Device.RegisterDevice(), as well as assigning a HandleMessage using Device.HandleMessage(IntPtr message)
I've found the correct overload for RegisterDevice() which is:
RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, bool addThreadFilter)
What I can't work out, though, is:
1) Now that I have to use a target, what am I meant to set as a target?
2) Where do I get this IntPtr message from?

This might help, I made this for a game of pong. Hope it helps
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
spaceKeyDown = true;
}
if (e.KeyCode == Keys.Up)
{
spaceKeyUp = true;
}
if (e.KeyCode == Keys.W)
{
spaceKeyW = true;
}
if (e.KeyCode == Keys.S)
{
spaceKeyS = true;
}
if (direction == 0)
{
if (e.KeyCode == Keys.Space)
{
Random rand = new Random();
direction = rand.Next(1, 5);
}
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
spaceKeyDown = false;
}
if (e.KeyCode == Keys.Up)
{
spaceKeyUp = false;
}
if (e.KeyCode == Keys.W)
{
spaceKeyW = false;
}
if (e.KeyCode == Keys.S)
{
spaceKeyS = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (spaceKeyDown == true)
{
if (p2y < picPicture.Height - Paddle2.Height)
p2y += 15;
}
if (spaceKeyUp == true)
{
if (p2y > 0)
p2y -= 15;
}
if (spaceKeyS == true)
{
if (p1y < picPicture.Height - Paddle1.Height)
p1y += 15;
}
if (spaceKeyW == true)
{
if (p1y > 0)
p1y -= 15;
}
}

Related

hold longer will jump higher (C# winforms)

I am trying to make a 2D game in winforms. i want my character to be able to double jump and if i hold the space key (jump) for a long time i will jump higher or if i hold the space key (jump) less i will jump lower (Note that although holding longer will jump higher, but only up to a fixed level, not to infinity). but I can only double jump and only jump 1 fixed distance, not hold the space longer to jump higher or hold it shorter to jump lower, someone help me, below is my code.
public partial class GamePlay_Page : Form
{
bool goRight, goLeft;
int gravity = 16;
int force;
bool jump;
int jumpTimes = 2;
public GamePlay_Page()
{
InitializeComponent();
}
private void GamePlay_Page_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D)
{
goRight = true;
Trex.Image = Properties.Resources.running;
}
if (e.KeyCode == Keys.A)
{
goLeft = true;
Trex.Image = Properties.Resources.running2;
}
if (e.KeyCode == Keys.W && jumpTimes > 0)
{
jump = true;
force = gravity;
jumpTimes -= 1;
}
private void gameT(object sender, EventArgs e)
{
if (goRight == true && Trex.Right < 600)
{
Trex.Left += 5;
}
if (goLeft == true && Trex.Left > 10)
{
Trex.Left -= 5;
}
if (jump == true)
{
Trex.Top -= force;
force -= 1;
}
if (Trex.Top + Trex.Height >= backgroundAbove.Height)
{
Trex.Top = backgroundAbove.Height - Trex.Height;
}
else
{
Trex.Top += 3;
}
if (Trex.Top + Trex.Height == backgroundAbove.Height)
{
jumpTimes = 2;
}
private void GamePlay_Page_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D) { goRight = false; }
if (e.KeyCode == Keys.A) { goLeft = false; }
}
I am not sure if this is what you mean, and change it according to your needs.
Please see my example:
Add a stopwatch to judge the length of the pressing time, in order to prevent the intensity from bursting.
What I set is to give a fixed force for pressing more than 3s, otherwise give an initial force for gravity.
Change it according to your needs.
You can completely change it to a force multiplied by time.
Stopwatch watch =new Stopwatch();
watch.Start();//start the timer
watch.Stop(); //stop the timer
watch.ElapsedMilliseconds// Show duration in milliseconds(Type is long)
watch.Reset();//reset the timer
Put the timing start into the KeyDown event:
if (e.KeyCode == Keys.W && jumpTimes > 0)
{
//jump = true;
//force = gravity;
//jumpTimes -= 1;
watch.Start();
}
Put the timing end and trigger the jump into the KeyUp event:
if (e.KeyCode == Keys.W && jumpTimes > 0)
{
watch.Stop();
jump = true;
jumpTimes -= 1;
if (watch.ElapsedMilliseconds > 300)//>3s
{
force = 30;
}
else
{
force = gravity;
}
}
watch.Reset();
All the codes:
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace GamePlay_Page {
public partial class GamePlay_Page : Form {
public GamePlay_Page () {
InitializeComponent();
}
bool goRight, goLeft;
int gravity = 16;
int force;
bool jump;
int jumpTimes = 2;
Stopwatch watch =new Stopwatch();
private void GamePlay_Page_Load (object sender, EventArgs e) {
}
private void GamePlay_Page_KeyDown (object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.D)
{
goRight = true;
Trex.Image = Properties.Resources.ch;
}
if (e.KeyCode == Keys.A)
{
goLeft = true;
Trex.Image = Properties.Resources.ca;
}
if (e.KeyCode == Keys.W && jumpTimes > 0)
{
//jump = true;
//force = gravity;
//jumpTimes -= 1;
watch.Start();
}
}
private void GamePlay_Page_KeyUp (object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.W && jumpTimes > 0)
{
watch.Stop();
if (e.KeyCode == Keys.W && jumpTimes > 0)
{
watch.Stop();
jump = true;
jumpTimes -= 1;
if (watch.ElapsedMilliseconds > 300)//>3s
{
force = 30;
}
else
{
force = gravity;
}
}
}
watch.Reset();
if (e.KeyCode == Keys.D)
{
goRight = false;
}
if (e.KeyCode == Keys.A)
{
goLeft = false;
}
}
private void Timer1_Tick (object sender, EventArgs e) {
if (goRight == true && Trex.Right < 600)
{
Trex.Left += 5;
}
if (goLeft == true && Trex.Left > 10)
{
Trex.Left -= 5;
}
if (jump == true)
{
Trex.Top -= force;
force -= 1;
}
if (Trex.Top + Trex.Height >= backgroundAbove.Height)
{
Trex.Top = backgroundAbove.Height - Trex.Height;
}
else
{
Trex.Top += 3;
}
if (Trex.Top + Trex.Height == backgroundAbove.Height)
{
jumpTimes = 2;
}
G.Text = gravity.ToString();
F.Text = force.ToString();
T.Text = Trex.Top.ToString();
L.Text = Trex.Left.ToString();
J.Text = jumpTimes.ToString();
W.Text = watch.ElapsedMilliseconds.ToString();
}
}
}
main page:
Output:
If you have questions about my code, please comment below and I will follow up as soon as possible.

How to make a PictureBox move using arrow/WASD keys?

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.

How can i capitalize first letter of every word using keypress event with c#?

I must capitalize first letter of every word but with keypress event using C#. Right now every letter in the textbox gets capitalized, I added the code I use. I cant figure out how to capitalize just the first letter, or what am I doing wrong. Can you help me?
private void txt_name_KeyPress(object sender, KeyPressEventArgs e)
{
e.KeyChar = (e.KeyChar.ToString()).ToUpper().ToCharArray()[0];
}
You will need to keep track of previous key presses if you must go this route:
private char PreviousChar;
private void txt_name_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsWhiteSpace(PreviousChar) || PreviousChar == '\0')
{
e.KeyChar = Char.ToUpper(e.KeyChar);
}
PreviousChar = e.KeyChar;
}
I got by using Keydown
private void tbxName_KeyDown(object sender, KeyEventArgs e)
{
if (tbxName.Text.Length == 0 || tbxName.SelectedText.Length == tbxName.Text.Length)
{
if ((e.Key >= Key.A) && (e.Key <= Key.Z))
{
tbxName.Text = e.Key.ToString().ToUpper();
tbxName.SelectionStart = tbxName.Text.Length;
e.Handled = true;
}
}
if (tbxName.Text.Length > 0)
{
int selectionStart = tbxName.SelectionStart;
string character = tbxName.Text.Substring(selectionStart - 1, 1);
if (character.Trim() == string.Empty)
{
if ((e.Key >= Key.A) && (e.Key <= Key.Z))
{
tbxName.Text = tbxName.Text.Insert(selectionStart, e.Key.ToString().ToUpper());
tbxName.SelectionStart = selectionStart + 1;
e.Handled = true;
}
}
}
if (e.Key == Key.Enter || e.Key == Key.Tab)
{
if (tbxName.Text.Length > 2)
{
tbxDegree.Focus();
if (e.Key == Key.Tab)
e.Handled = true;
}
else
{
MessageBox.Show("Kindly verify the Name Entered");
tbxName.Focus();
}
}
}

How to make 30+ labels colide with my "player" label that is moveable?

Is there a way to group 30+ labels to be able to control them all at once. What I want to do is this with 30 labels.
if (player.Bounds.IntersectsWith(label1.Bounds))
{
if (right == true)
{
right = false;
left = true;
}
else if (left == true)
{
left = false;
right = true;
}
else if (up == true)
{
up = false;
down = true;
}
else if (down == true)
{
down = false;
up = true;
}
And then where the label1 is checking if it is colided I want it to check all 30 labels if they have colided. And preferably not with 30x this code and just change the number. =)
I just want to add this is a maze game and the left, right etc. is the players movement defined outside what I posted here. I hope you understand!
All of my code:
namespace mazeGame
{
public partial class Form1 : Form
{
bool down;
bool left;
bool right;
bool up;
// new List<int> blocks = new List[5];
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (label1.Bounds.IntersectsWith(label10.Bounds))
{
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
down = false;
up = false;
right = true;
left = false;
}
if (e.KeyCode == Keys.Left)
{
left = true;
down = false;
up = false;
right = false;
}
if (e.KeyCode == Keys.Up)
{
up = true;
down = false;
right = false;
left = false;
}
if (e.KeyCode == Keys.Down)
{
down = true;
up = false;
right = false;
left = false;
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
right = true;
left = false;
up = false;
down = false;
}
if (e.KeyCode == Keys.Left)
{
left = true;
right = false;
up = false;
down = false;
}
if (e.KeyCode == Keys.Up)
{
up = true;
left = false;
right = false;
down = false;
}
if (e.KeyCode == Keys.Down)
{
down = true;
left = false;
up = false;
right = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (player.Bounds.IntersectsWith(label1.Bounds))
{
if (right == true)
{
right = false;
left = true;
}
else if (left == true)
{
left = false;
right = true;
}
else if (up == true)
{
up = false;
down = true;
}
else if (down == true)
{
down = false;
up = true;
}
}
var labels = this.??????? // here is where i need help.
if (right == true)
{
player.Left += 1;
}
if (left == true)
{
player.Left -= 1;
}
if (up == true)
{
player.Top -= 1;
}
if (down == true)
{
player.Top += 1;
}
}
}
OfType will get all controls of the same type from a control. In this case it will be labels from your form:
var labels = this.myForm.Controls.OfType<Label>()
Then you can iterate through your collection of labels.
EDIT:
Then, looping through your code it would look like this:
private void timer1_Tick(object sender, EventArgs e)
{
var labels = this.Form1.Controls.OfType<Label>()
foreach(var label in labels)
{
if(player.Bounds.IntersectsWith(label.Bounds))
//...
As for me it is good decision to create array or even list (witch you can dynamically update) of all your 30+ labels and then to manipulate them in one short method. Of course, if the actions as much simple as you write up and nothing else.

I can't seem to move the location of a Picture box in c#

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);

Categories