Post news RSS Some Experiments

Hey all, I've been programming with several programming techniques. One of these was to dynamically generate objects on a form, like controls. I'm gonna explain now step by step how this works and can be done within 5 minutes.

Posted by on

Hey all,

I've been working on several prototypes of elements of the game. The most important one I was planning on was how to dynamically generate controls and edit properties of them. I also wanted to add events to them, which change properties.

The first task was to generate the objects.

the code required for this goes as follows:

List<PictureBox> pictureboxes = new List<PictureBox>();
        Button pushthebutton = new Button();
        private void GenerateViewgrid()
        {

            for (int i = 1; i < 9; i++)
            {
                for (int j = 1; j < 9; j++)
                {
                    PictureBox picturebox = new PictureBox();
                    picturebox.Location = new Point((i * 33) + 10, (j * 33) + 10);
                    picturebox.Size = new Size(32, 32);
                    picturebox.BackColor = Color.Black;
                    picturebox.Click += new EventHandler(veranderkleurvanpicturebox);
                    pictureboxes.Add(picturebox);
                    this.Controls.Add(picturebox);
                }
            }

            
            pushthebutton.Location = new Point(330, 10);
            pushthebutton.Click += new EventHandler(Discooooo);
            pushthebutton.Text = "Start the disco!";
            pushthebutton.Size = new Size(120, 30);
            this.Controls.Add(pushthebutton);
            

        }

The result of this is the following:

Now, the code for changing properties is as follows:

private void changeColourOfPictureBox(object sender, EventArgs e)
        {
            PictureBox picturebox = new PictureBox();
            picturebox = sender as PictureBox;
            changePictureBoxColor(picturebox);
        }

        private void Discooooo(object sender, EventArgs e)
        {
            Random t = new Random();
            for (int i = 0; i < 300; i++ )
            {
                
                changePictureBoxColor(pictureboxes[t.Next(0, 63)]);
                System.Threading.Thread.Sleep(5);

            }
        }

        private void changePictureBoxColor(PictureBox picbox)
        {
            Random r = new Random();
            picbox.BackColor = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));
        }

This code has some inefficiency as you can see. For istance, I would only need one random variable, not 2. And there is more.

Well, of course, we could keep talking about this code, but I can show you what this does.

Here I clicked a picturebox, triggering the upper event in the code above. This event did a call to the lowest method. This can be repeated of course, with the result below:

Well, you could keep doing this forever, but why is there the "Start the disco!" button? This button triggers the middle event, which changes the colors at random items. Here below is the result:

And see here for a lovely disco dancefloor :)

This seems simple, and yes, it is! But you need to know how to do it before moving on to more advanced stuff.

What is the purpose of this for a game? Well, many games need to generate objects on a form dynamically (at least, if you don't want multiple forms to stack up). While this doesn't do all the tricks games need to do, it does create a basic framework to work from.

Oh and to tell a small thing: These items will contain the view of a solar system :)

Cheers,
Roberto Moretti

Comments
SAROMON
SAROMON

I will watch your progression mate. :)

Reply Good karma Bad karma+1 vote
Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.

News
Browse
News
New
Post news
Share
Related Games
Related Engines
XNA
XNA Public Domain
Related Groups
Darkmatter Development
Darkmatter Development Developer & Publisher