【问题标题】:Moving images from a dynamic list PictureBox从动态列表 PictureBox 中移动图像
【发布时间】:2014-08-09 03:49:28
【问题描述】:

感谢 Stack Overflow,我开始着手编程。 我在c#中做的游戏,由几只蜜蜂在桌面上飞来飞去,我必须给它点击,分数会增加一定的时间。 为此我做了以下事情:

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

![蜜蜂游戏][1]

在这部分我被卡住了:

  • 随机放置这些图片框(在表单底部)。
  • 让 PictureBox 随机移动(或多或少标记的路线)。
  • 每个PictureBox点击事件,改变PictureBox图片并隐藏(可见=假),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)]);
            }
        }        
    }
}

【问题讨论】:

  • 不确定如何准确地说,但如果你被告知 StackOverflow 是一个可以教你编程的地方,那么你就被误导了。本网站旨在帮助在特定代码位上遇到特定问题的程序员,而不是提供有关如何编程或如何设计程序的建议。我建议您找到一些有关 C# 游戏编程的 Internet 教程。当您遇到某些特定代码的特定问题时,请回到这里。
  • 也就是说,我很确定您将需要一个 Timer,并且您需要设置 Timer 来调用 Tick 事件处理程序,并且您的大部分代码可能是在 Tick 事件处理程序中。祝你好运。

标签: c# winforms list picturebox


【解决方案1】:

将for循环外的rand和files和function/void外的objeto声明为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)]);
        }
    }        
}       

【讨论】:

    【解决方案2】:

    对不起,我没有正确解释。

    我要做的是:

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

    【讨论】:

    • 代码格式有问题,大部分空白已删除,无法阅读。
    • 另外,提出问题的人最好通过编辑问题而不是发布答案来提供附加信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    相关资源
    最近更新 更多