从动态列表PictureBox中移动图像

时间:2014-08-09 03:49:29

标签: c# winforms list picturebox

感谢Stack Overflow,我正在开始编程。 我在c#中进行的游戏包括几个围绕桌面飞行的蜜蜂,我必须给它点击,SCORE将会增加一定时间。 我做了以下几点:

  • 动态创建列表PictureBox(在运行时):确定
  • 随机加载带有这些GIF图像的PictureBox:确定

![蜜蜂游戏] [1]

在这部分我被困住了:

  • 将这些随机的PictureBox放置在表单的底部。
  • 使PictureBox随机移动(或多或少标记路线)。
  • 每个PictureBox单击事件,更改PictureBox图像并隐藏(可见= false),并在SCORE + 1中增加。

我需要帮助。 我的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MiPrimerJuego
{
    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int x = 20;
            int y = 600;
            List<System.Windows.Forms.PictureBox> objeto = new List<PictureBox>();
            for (int i = 0; i < 10; i++, x += 90)
            {
                PictureBox pBox = new PictureBox();                
                pBox.Height = 80;
                pBox.Width = 50;
                pBox.Location = new System.Drawing.Point(x, y);
                objeto.Add(pBox);
                pBox.SizeMode = PictureBoxSizeMode.StretchImage;                
                Controls.Add(pBox);

                var rand = new Random();
                var files = Directory.GetFiles(Application.StartupPath + @"/Images", "*.gif");
                pBox.Image = System.Drawing.Bitmap.FromFile(files[rand.Next(files.Length)]);
            }
        }        
    }
}

2 个答案:

答案 0 :(得分:0)

将函数/ void之外的for-loop和objeto之外的rand和文件声明为Private成员变量:

    private List<PictureBox> objeto = new List<PictureBox>();

    private void button1_Click(object sender, EventArgs e)
    {
        var files = Directory.GetFiles(Application.StartupPath + @"/Images", "*.gif");                          
        int x = 20;
        int y = 600;
        var rand = new Random();
        for (int i = 0; i < 10; i++) 
         {
            x += 90;
            PictureBox pBox = new PictureBox();                
            pBox.Height = 80;
            pBox.Width = 50;
            pBox.Location = new System.Drawing.Point(x, y);
            objeto.Add(pBox);
            pBox.SizeMode = PictureBoxSizeMode.StretchImage;                
            Controls.Add(pBox);

            pBox.Image = System.Drawing.Bitmap.FromFile(files[rand.Next(files.Length)]);
        }
    }        
}       

答案 1 :(得分:0)

抱歉,我没有正确解释。

我的工作是:

1使PictureBox从表单底部随机移动(或多或少标记的路径)。

2在每个PictureBox的Click事件中,更改PictureBox图像并隐藏(visible = false),并在SCORE + 1中增加。

我的代码如下:

using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
using System.IO;

namespaceMiPrimerJuego
{
publicpartialclassForm1 : Form
    {        
public Form1()
        {
InitializeComponent();
        }

privatevoid button1_Click(object sender, EventArgs e)
{
int x = 20;
int y = 600;
List<System.Windows.Forms.PictureBox>objeto = newList<PictureBox>();
for (inti = 0; i< 10; i++, x += 90)
            {
PictureBoxpBox = newPictureBox();                
pBox.Height = 80;
pBox.Width = 50;
pBox.Location = newSystem.Drawing.Point(x, y);
objeto.Add(pBox);
pBox.SizeMode = PictureBoxSizeMode.StretchImage;                
Controls.Add(pBox);

var rand = newRandom();
var files = Directory.GetFiles(Application.StartupPath + @"/Images", "*.gif");
pBox.Image = System.Drawing.Bitmap.FromFile(files[rand.Next(files.Length)]);
            }
        }        
    }
}

enter image description here