Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在多人游戏中向下处理多个键_C#_.net_2d Games - Fatal编程技术网

C# 在多人游戏中向下处理多个键

C# 在多人游戏中向下处理多个键,c#,.net,2d-games,C#,.net,2d Games,我正在使用Windows窗体应用程序编写自己的游戏。这应该是一个多人游戏。每个球员都可以控制自己的砖块使球保持在球场上,但问题是两名球员不能同时按下控制按钮。在第一个玩家移动过程中,每当第二个玩家按下键,第一个玩家的砖块就会停止。但是,如果他们同时按下按键,两块砖块都会移动。我使用了向下键事件: private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.W &am

我正在使用Windows窗体应用程序编写自己的游戏。这应该是一个多人游戏。每个球员都可以控制自己的砖块使球保持在球场上,但问题是两名球员不能同时按下控制按钮。在第一个玩家移动过程中,每当第二个玩家按下键,第一个玩家的砖块就会停止。但是,如果他们同时按下按键,两块砖块都会移动。我使用了向下键事件:

  private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.W && one.BrickLocationY > 0)
        {
            one.BrickLocationY -= 17;
        }
        if (e.KeyCode == Keys.S && one.BrickLocationY + Brick.BrickHeight < screenHeight)
        {
            one.BrickLocationY += 17;
        }
        if (e.KeyCode == Keys.Up)
        {
            two.BrickLocationY -= 17;
        }
        if (e.KeyCode == Keys.Down && two.BrickLocationY + Brick.BrickHeight < screenHeight)
        {
            two.BrickLocationY += 17;
        }
        if (e.KeyCode == Keys.Escape)
        {
            Application.Exit();
        }
    }
我也尝试过使用KeyUpKeyPress组合来实现这一点,但没有成功。我唯一想到的是穿砖头,但不知道怎么做。我想知道有没有办法不用线程就能处理这样的多人游戏控件


另外,键盘能够同时处理多个按钮

我相信您需要自己跟踪这一点。基本上按下键你识别W并认为它一直保持到键释放W为止。s键也一样


然后在跑步循环中,您只需查找哪些键处于活动状态,并在那里执行方向逻辑。

当KeyDown事件触发时,您需要检查每次按下的按钮。这是我在快速搜索该主题时发现的

using System; 
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form1_FormClosing);
      tm.Tick += new System.EventHandler(DoSomethingWithKeyboardInput);
      this.Load += new System.EventHandler(Form1_Load);
      textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(textbox1_KeyDown);
      textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(textbox1_KeyDown);
    }

    private Timer tm = new Timer();
    private List<System.Windows.Forms.Keys> MovementKeys = new List<System.Windows.Forms.Keys>();
    private _MyInputKeys MyInputKeys = new _MyInputKeys();

    private struct _MyInputKeys
    {
      public bool Jump;
      public bool Left;
      public bool Right;
    }

    private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
    {
      tm.Stop();
    }

    public void DoSomethingWithKeyboardInput(object sender, EventArgs e)
    {
      textBox1.Text = (MyInputKeys.Left ? "(left)" : "") + 
        (MyInputKeys.Right ? "(right)" : "") + (MyInputKeys.Jump ? "(jump)" : "");
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
      //define keys used for movement

      MovementKeys.Add(Keys.Up); //Jump ?
      MovementKeys.Add(Keys.Left); //Left Arrow - Move Left
      MovementKeys.Add(Keys.Right); //Rigth Arrow - Move Right
      tm.Interval = 50;
      tm.Start();
    }

    private void textbox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
      if (MovementKeys.IndexOf(e.KeyCode) != -1)
      {
        e.Handled = true;
        MyInputKeys.Jump = IsKeyDown(Keys.Up);
        MyInputKeys.Left = IsKeyDown(Keys.Left);
        MyInputKeys.Right = IsKeyDown(Keys.Right);
      }
    }

    public static bool IsKeyDown(Keys key)
    {
      return (GetKeyState(Convert.ToInt16(key)) & 0X80) == 0X80;
    }
    /// <summary>
    ///  If the high-order bit is 1, the key is down; otherwise, it is up.
    ///  If the low-order bit is 1, the key is toggled. 
    ///  A key, such as the CAPS LOCK key, is toggled if it is turned on. 
    ///  The key is off and untoggled if the low-order bit is 0. 
    ///  A toggle key's indicator light (if any) on the keyboard will be on when 
    ///  the key is toggled, and off when the key is untoggled.
    /// </summary>
    /// <param name="nVirtKey"></param>
    [DllImport("user32.dll")]
    public extern static Int16 GetKeyState(Int16 nVirtKey);
  }
}
使用系统;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
使用System.Collections.Generic;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
this.FormClosing+=新系统.Windows.Forms.FormClosingEventHandler(Form1\u FormClosing);
tm.Tick+=新的System.EventHandler(DoSomethingWithKeyboardInput);
this.Load+=new System.EventHandler(Form1\u Load);
textBox1.KeyDown+=new System.Windows.Forms.KeyEventHandler(textBox1\u KeyDown);
textBox1.KeyUp+=新系统.Windows.Forms.KeyEventHandler(textBox1_KeyDown);
}
专用定时器tm=新定时器();
私有列表移动键=新列表();
private _myinputkeysmyinputkeys=new _MyInputKeys();
私有结构\u MyInputKeys
{
公共跳跃;
公共布尔左;
公共财产权;
}
私有作废Form1\u FormClosing(对象发送方,System.Windows.Forms.FormClosingEventArgs e)
{
tm.Stop();
}
public void DoSomethingWithKeyboardInput(对象发送方,事件参数e)
{
textBox1.Text=(MyInputKeys.Left?”(Left)”:“”+
(MyInputKeys.Right?”(Right)”:“”)+(MyInputKeys.Jump?”(Jump)”:“”);
}
私有void Form1_加载(对象发送方,System.EventArgs e)
{
//定义用于移动的关键点
MovementKeys.Add(Keys.Up);//跳转?
MovementKeys.Add(Keys.Left);//向左箭头-向左移动
MovementKeys.Add(Keys.Right);//向右箭头-向右移动
tm.间隔=50;
tm.Start();
}
私有void textbox1\u KeyDown(对象发送方,System.Windows.Forms.KeyEventArgs e)
{
if(MovementKeys.IndexOf(e.KeyCode)!=-1)
{
e、 已处理=正确;
MyInputKeys.Jump=IsKeyDown(Keys.Up);
MyInputKeys.Left=IsKeyDown(Keys.Left);
MyInputKeys.Right=IsKeyDown(Keys.Right);
}
}
公共静态bool IsKeyDown(按键)
{
返回(GetKeyState(Convert.ToInt16(key))&0X80)=0X80;
}
/// 
///如果高阶位为1,则键为向下;否则,键为向上。
///如果低阶位为1,则切换键。
///如果钥匙(如CAPS LOCK钥匙)处于启用状态,则会切换该钥匙。
///如果低阶位为0,则钥匙关闭且未切换。
///当启动时,键盘上的切换键指示灯(如果有)将点亮
///该键处于切换状态,未切换时处于关闭状态。
/// 
/// 
[DllImport(“user32.dll”)]
公共外部静态Int16 GetKeyState(Int16 nVirtKey);
}
}

winforms不用于游戏开发。使用XNA或至少WPF。
using System; 
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form1_FormClosing);
      tm.Tick += new System.EventHandler(DoSomethingWithKeyboardInput);
      this.Load += new System.EventHandler(Form1_Load);
      textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(textbox1_KeyDown);
      textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(textbox1_KeyDown);
    }

    private Timer tm = new Timer();
    private List<System.Windows.Forms.Keys> MovementKeys = new List<System.Windows.Forms.Keys>();
    private _MyInputKeys MyInputKeys = new _MyInputKeys();

    private struct _MyInputKeys
    {
      public bool Jump;
      public bool Left;
      public bool Right;
    }

    private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
    {
      tm.Stop();
    }

    public void DoSomethingWithKeyboardInput(object sender, EventArgs e)
    {
      textBox1.Text = (MyInputKeys.Left ? "(left)" : "") + 
        (MyInputKeys.Right ? "(right)" : "") + (MyInputKeys.Jump ? "(jump)" : "");
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
      //define keys used for movement

      MovementKeys.Add(Keys.Up); //Jump ?
      MovementKeys.Add(Keys.Left); //Left Arrow - Move Left
      MovementKeys.Add(Keys.Right); //Rigth Arrow - Move Right
      tm.Interval = 50;
      tm.Start();
    }

    private void textbox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
      if (MovementKeys.IndexOf(e.KeyCode) != -1)
      {
        e.Handled = true;
        MyInputKeys.Jump = IsKeyDown(Keys.Up);
        MyInputKeys.Left = IsKeyDown(Keys.Left);
        MyInputKeys.Right = IsKeyDown(Keys.Right);
      }
    }

    public static bool IsKeyDown(Keys key)
    {
      return (GetKeyState(Convert.ToInt16(key)) & 0X80) == 0X80;
    }
    /// <summary>
    ///  If the high-order bit is 1, the key is down; otherwise, it is up.
    ///  If the low-order bit is 1, the key is toggled. 
    ///  A key, such as the CAPS LOCK key, is toggled if it is turned on. 
    ///  The key is off and untoggled if the low-order bit is 0. 
    ///  A toggle key's indicator light (if any) on the keyboard will be on when 
    ///  the key is toggled, and off when the key is untoggled.
    /// </summary>
    /// <param name="nVirtKey"></param>
    [DllImport("user32.dll")]
    public extern static Int16 GetKeyState(Int16 nVirtKey);
  }
}