2015-12-30 42 views
2

我在C#上很新,但我试图移动一个picturebox,通过按下键盘上的左右键,在网格TableLayoutPanel(我在运行时创建了网格)。网格是23x23,具有砖块图像边框(边框上的每个单元格在图像箱中包含砖块图像)以及在中间具有鼠标的图像箱。我正在尝试的是通过按下上面提到的其中一个控制键将鼠标图像从另一个单元格中的中央单元格(11x11)中移出。看来我无法理解eventHandler的想法......。代码工作非常好,直到我想让picturebox移动。我把整个代码,也许问题是在我没有注意到的地方,但我认为问题是KeyDown + =新的KeyEventHandler(Form2_KeyDown)或/结束私人无效Form2_KeyDown(对象发件人,KeyEventArgs e){ ...}。如何从键盘移动TableLayoutPanel网格中的PictureBox?

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 IndividualProject 
{ 
    public partial class Form2 : Form 
    { 
    PictureBox picturebox5 = new PictureBox 
    { 
     Visible = true, 
     Anchor = AnchorStyles.Top, 
     SizeMode = PictureBoxSizeMode.Normal, 
     Dock = DockStyle.Fill, 
     Margin = new Padding(0) 
    }; 

    public Form2() 
    { 
     InitializeComponent(); 
     // MaximizeBox = false; 

     //size 
     int h = Screen.PrimaryScreen.WorkingArea.Height/2; 
     int w = Screen.PrimaryScreen.WorkingArea.Width/2; 
     Size = new Size(w/2 + w/7, h + h/4 + h/7); 

     //location 
     StartPosition = FormStartPosition.CenterScreen; 

     //form style 
     FormBorderStyle = FormBorderStyle.FixedSingle; 

     //menuStrip1.BackColor = Color.Beige; 

     //lives and score container 
     #region livesAndScore 
     lasContainer.Size = new Size(Width/2 + Width/3 + Width/7, Height/13); 
     lasContainer.BackColor = Color.Lavender; 
     lasContainer.BorderStyle = BorderStyle.Fixed3D; 
     lasContainer.Dock = DockStyle.Top; 
     lasContainer.SplitterDistance = Width/2 - Width/69; 
      //labels 
     lives.Location = new Point(lasContainer.Panel1.Width/12, lives.Height/2); 
     score.Location = new Point(lasContainer.Panel2.Width/12, score.Height/2); 
      //picturebox 
     live3.Location = new Point(lasContainer.Panel1.Width/3, lives.Height/2); 
     live2.Location = new Point(lasContainer.Panel1.Width/2, lives.Height/2); 
     live1.Location = new Point(lasContainer.Panel1.Width/2 + lasContainer.Panel1.Width/6, lives.Height/2); 
     #endregion livesAndScore 

     //gamePanel 
     gamePanel.Dock = DockStyle.Fill; 
     gamePanel.BackColor = Color.SkyBlue; 
     gamePanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single; // REMOVE WHEN FINISHED !!!!!!!!!!! 

     //making the grid 
     #region grid 
     gamePanel.ColumnCount = 23; 
     gamePanel.RowCount = 23; 
     gamePanel.ColumnStyles.Clear(); 
     gamePanel.RowStyles.Clear(); 
     int iIndex, jIndex = 0; 

     for (iIndex = 0; iIndex < gamePanel.ColumnCount; iIndex++) 
     { 
      gamePanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 4.34F)); 
      gamePanel.RowStyles.Add(new RowStyle(SizeType.Percent, 4.34F)); 
     } 
     #endregion grid 

     while(jIndex < gamePanel.ColumnCount) 
     { 
      #region picturebox1 
      PictureBox picturebox1 = new PictureBox 
      { 
       Visible = true, 
       Anchor = AnchorStyles.Top, 
       SizeMode = PictureBoxSizeMode.Normal, 
       BackColor = Color.Sienna, 
       Dock = DockStyle.Fill, 
       Margin = new Padding(0) 
      }; 
      if(jIndex < gamePanel.ColumnCount - 1) 
      { 
       gamePanel.Controls.Add(picturebox1, jIndex, 0); 
       picturebox1.ImageLocation = @"..\..\ResourcesPh\brickblock.png"; 
      } 
      #endregion picturebox1 

      #region picturebox2 
      PictureBox picturebox2 = new PictureBox 
      { 
       Visible = true, 
       Anchor = AnchorStyles.Top, 
       SizeMode = PictureBoxSizeMode.Normal, 
       BackColor = Color.Sienna, 
       Dock = DockStyle.Fill, 
       Margin = new Padding(0) 
      }; 
      if (jIndex < gamePanel.ColumnCount - 1) 
      { 
       gamePanel.Controls.Add(picturebox2, 0, jIndex + 1); 
       picturebox2.ImageLocation = @"..\..\ResourcesPh\brickblock.png"; 
      } 
      #endregion picturebox2 

      #region picturebox3 
      PictureBox picturebox3 = new PictureBox 
      { 
       Visible = true, 
       Anchor = AnchorStyles.Top, 
       SizeMode = PictureBoxSizeMode.Normal, 
       BackColor = Color.Sienna, 
       Dock = DockStyle.Fill, 
       Margin = new Padding(0) 
      }; 
      if(jIndex < gamePanel.ColumnCount - 1) 
      { 
       gamePanel.Controls.Add(picturebox3, gamePanel.ColumnCount - 1 - jIndex, gamePanel.RowCount - 1); 
       picturebox3.ImageLocation = @"..\..\ResourcesPh\brickblock.png"; 
      } 
      #endregion picturebox3 

      #region picturebox4 
      PictureBox picturebox4 = new PictureBox 
      { 
       Visible = true, 
       Anchor = AnchorStyles.Top, 
       SizeMode = PictureBoxSizeMode.Normal, 
       BackColor = Color.Sienna, 
       Dock = DockStyle.Fill, 
       Margin = new Padding(0), 
      }; 
      if(jIndex < gamePanel.ColumnCount - 1) 
      { 
       gamePanel.Controls.Add(picturebox4, gamePanel.ColumnCount - 1, gamePanel.RowCount - 1 - jIndex - 1); 
       picturebox4.ImageLocation = @"..\..\ResourcesPh\brickblock.png"; 
      } 
      #endregion picturebox4 

      jIndex++; 
     } 

     //the starting point of the mouse 
     #region mouseStartPoint 
     //PictureBox picturebox5 = new PictureBox 
     //{ 
     // Visible = true, 
     // Anchor = AnchorStyles.Top, 
     // SizeMode = PictureBoxSizeMode.Normal, 
     // BackColor = Color.Sienna, 
     // Dock = DockStyle.Fill, 
     // Margin = new Padding(0) 
     //}; 
     gamePanel.Controls.Add(picturebox5, 11, 11); 
     picturebox5.ImageLocation = @"..\..\ResourcesPh\mouse.png"; 
     #endregion mouseStartPoint 

     KeyDown += new KeyEventHandler(Form2_KeyDown); 
    } 

    private void Form2_KeyDown(object sender, KeyEventArgs e) 
    { 
     int x = 11, y = 11; 

     if (e.KeyCode == Keys.Right) 
      x += 1; 
     if (e.KeyCode == Keys.Left) 
      x -= 1; 
     if (e.KeyCode == Keys.Up) 
      y -= 1; 
     if (e.KeyCode == Keys.Down) 
      y += 1; 

     gamePanel.Controls.Remove(picturebox5); 
     gamePanel.Controls.Add(picturebox5, x, y); 
     picturebox5.ImageLocation = @"..\..\ResourcesPh\mouse.png"; 
     Refresh(); 
    } 

    private void howToPlayToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     Hide(); 
     Form3 f3 = new Form3(); 
     f3.FormClosed += (s, args) => Close(); //event handler on closing Form2 after Form3 is opened 
     f3.Show(); 
    } 

    private void exitToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     Application.Exit(); 
    } 

    } 
} 

TableLayoutPanel Grid

回答

0

你总是设置起始位置回到11,11上的每个按键的点击。将您的声明范围之外:

int x = 11, y = 11; 

private void Form2_KeyDown(object sender, KeyEventArgs e) { 
+0

它仍然没有工作,但我知道你在说什么。我甚至没有考虑它。谢谢。 –

+0

@ Ioana.M你必须定义什么“仍然不工作”的意思。我将你的PictureBox移动到我的表单上,并附上你的代码。 – LarsTech

+0

我从私人无效Form2_KeyDown中取出x和y(对象发件人,KeyEventArgs e){... },但它并没有移动......我把它们放在上面,就像在你的答案中一样 - 在范围之外,但它没有任何作用。 –

0

下面是移动鼠标都大大简化的方式,我得到开门见山把每一件事情到最低限度,而有效:d

enter image description here

下面是我用的砖:

enter image description here enter image description here enter image description here

这里的代码!

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Windows.Forms; 

namespace WindowsFormsApplication3 
{ 
    public partial class Form1 : Form 
    { 
     private Dictionary<int, Image> _dictionary; 
     private Size _imageSize; 
     private int[] _level; 
     private Size _levelSize; 
     private Point _mousePos; 

     public Form1() 
     { 
      InitializeComponent(); 
      pictureBox1.Paint += PictureBox1_Paint; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      InitializeLevel(); 
      Redraw(); 
     } 

     private void InitializeLevel() 
     { 
      var imageEmpty = Image.FromFile("empty.png"); 
      var imageMouse = Image.FromFile("mouse.png"); 
      var imageWall = Image.FromFile("wall.png"); 
      _level = new[] 
      { 
       0, 0, 0, 0, 0, 
       0, 1, 1, 1, 0, 
       0, 1, 1, 1, 0, 
       0, 1, 1, 1, 0, 
       0, 0, 0, 0, 0 
      }; 
      _levelSize = new Size(5, 5); 
      _imageSize = new Size(25, 25); 
      _dictionary = new Dictionary<int, Image>(); 
      _dictionary.Add(0, imageWall); 
      _dictionary.Add(1, imageEmpty); 
      _dictionary.Add(2, imageMouse); 
      _mousePos = new Point(); 
     } 

     private void Redraw() 
     { 
      pictureBox1.Invalidate(); 
     } 

     private void PictureBox1_Paint(object sender, PaintEventArgs e) 
     { 
      var graphics = e.Graphics; 
      graphics.Clear(Color.Transparent); 
      // draw level 
      var i = 0; 
      foreach (var tile in _level) 
      { 
       var x = i % _levelSize.Width; 
       var y = i/_levelSize.Width; 
       var image = _dictionary[tile]; 
       graphics.DrawImage(image, new Point(x * _imageSize.Width, y * _imageSize.Height)); 
       i++; 
      } 
      // draw hero ! 
      graphics.DrawImage(_dictionary[2], _mousePos); 
     } 

     protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
     { 
      var up = keyData == Keys.Up; 
      var down = keyData == Keys.Down; 
      var left = keyData == Keys.Left; 
      var right = keyData == Keys.Right; 

      var processed = up || down || left || right; 
      if (!processed) return base.ProcessCmdKey(ref msg, keyData); 

      // move the hero ! 
      var x = 0; 
      var y = 0; 
      if (left) x--; 
      if (right) x++; 
      if (up) y--; 
      if (down) y++; 
      _mousePos.X += x; 
      _mousePos.Y += y; 
      Redraw(); 
      return true; 
     } 
    } 
} 

显然,你仍然需要检查的碰撞,定义你的逻辑和这样的......

去,并给它一个试试吧!

欢迎SO和好运气:d

+0

我在C#编程中仍然是新手,所以我需要查看代码中的一些内容,但非常感谢您花时间编写它:-) –

+0

乐意提供帮助! – Aybe