1
\$\begingroup\$

I have made a loading screen (splash screen) just like the old C64. I have used a series of picture boxes and just change the coloured image using a timer and a case statement.

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

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
            timer2.Start();
            timer3.Start();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            Random rnd = new Random();
            int a = rnd.Next(1,8);
            int b = rnd.Next(1,8);
            int c = rnd.Next(1,8);
            int d= rnd.Next(1,8);
            int n= rnd.Next(1,8);
            int f= rnd.Next(1,8);
            int g= rnd.Next(1,8);
            int h = rnd.Next(1, 8);

            switch (a)
            {
                case 1: 
                pictureBox1.Image = Properties.Resources.image1;
                pictureBox8.Image = Properties.Resources.image1;
                pictureBox10.Image = Properties.Resources.image1;
                    pictureBox2.Image = Properties.Resources.image1;
                    pictureBox11.Image = Properties.Resources.image1;
                    pictureBox9.Image = Properties.Resources.image1;
                break;
                case 2:
                pictureBox1.Image = Properties.Resources.image2;
                pictureBox8.Image = Properties.Resources.image2;
                pictureBox10.Image = Properties.Resources.image2;
                break;
                case 3:
                pictureBox1.Image = Properties.Resources.image3;
                pictureBox8.Image = Properties.Resources.image3;
                pictureBox10.Image = Properties.Resources.image3;
                break;
                case 4:
                pictureBox1.Image = Properties.Resources.image4;
                pictureBox8.Image = Properties.Resources.image4;
                break;
                case 5:
                pictureBox1.Image = Properties.Resources.image5;
                pictureBox8.Image = Properties.Resources.image5;
                break;
                case 6:
                pictureBox1.Image = Properties.Resources.image6;
                pictureBox8.Image = Properties.Resources.image6;
                break;
                case 7:
                pictureBox1.Image = Properties.Resources.image7;
                pictureBox8.Image = Properties.Resources.image7;
                break;
                case 8:
                pictureBox1.Image = Properties.Resources.image8;
                pictureBox8.Image = Properties.Resources.image8;
                break;
            }

            switch (b)
            {
                case 1:
                    pictureBox2.Image = Properties.Resources.image1;
                    pictureBox11.Image = Properties.Resources.image1;
                    pictureBox9.Image = Properties.Resources.image1;
                    break;
                case 2:
                    pictureBox2.Image = Properties.Resources.image2;
                    pictureBox9.Image = Properties.Resources.image2;
                    break;
                case 3:
                    pictureBox2.Image = Properties.Resources.image3;
                    pictureBox11.Image = Properties.Resources.image3;
                    pictureBox9.Image = Properties.Resources.image3;
                    pictureBox18.Image = Properties.Resources.image3;
                    pictureBox18.Image = Properties.Resources.image4;
                    break;
                case 4:
                    pictureBox2.Image = Properties.Resources.image4;
                    pictureBox9.Image = Properties.Resources.image4;
                    break;
                case 5:
                    pictureBox2.Image = Properties.Resources.image5;
                    pictureBox9.Image = Properties.Resources.image5;
                    break;
                case 6:
                    pictureBox2.Image = Properties.Resources.image6;
                    pictureBox9.Image = Properties.Resources.image6;
                    pictureBox12.Image = Properties.Resources.image6;
                    break;
                case 7:
                    pictureBox2.Image = Properties.Resources.image7;
                    pictureBox9.Image = Properties.Resources.image7;
                    break;
                case 8:
                    pictureBox2.Image = Properties.Resources.image8;
                    pictureBox9.Image = Properties.Resources.image8;
                    break;
            }

            switch (c)
            {
                case 1:
                    pictureBox3.Image = Properties.Resources.image1;
                    pictureBox13.Image = Properties.Resources.image1;
                    break;
                case 2:
                    pictureBox3.Image = Properties.Resources.image2;
                    pictureBox13.Image = Properties.Resources.image2;
                    break;
                case 3:
                    pictureBox3.Image = Properties.Resources.image3;
                    break;
                case 4:
                    pictureBox3.Image = Properties.Resources.image4;
                    pictureBox1.Image = Properties.Resources.image2;
                pictureBox8.Image = Properties.Resources.image2;
                pictureBox10.Image = Properties.Resources.image2;
                    break;
                case 5:
                    pictureBox3.Image = Properties.Resources.image5;
                    pictureBox18.Image = Properties.Resources.image1;
                    pictureBox18.Image = Properties.Resources.image1;
                    pictureBox17.Image = Properties.Resources.image2;
                    break;
                case 6:
                    pictureBox3.Image = Properties.Resources.image6;
                    break;
                case 7:
                    pictureBox3.Image = Properties.Resources.image7;
                    break;
                case 8:
                    pictureBox3.Image = Properties.Resources.image8;
                    break;
            }

            switch (d)
            {
                case 1:
                    pictureBox4.Image = Properties.Resources.image1;
                    pictureBox14.Image = Properties.Resources.image1;
                    pictureBox17.Image = Properties.Resources.image2;
                pictureBox8.Image = Properties.Resources.image2;
                pictureBox10.Image = Properties.Resources.image2;
                    break;
                case 2:
                    pictureBox4.Image = Properties.Resources.image2;
                    pictureBox18.Image = Properties.Resources.image2;
                    pictureBox18.Image = Properties.Resources.image3;
                    break;
                case 3:
                    pictureBox4.Image = Properties.Resources.image3;
                    pictureBox17.Image = Properties.Resources.image5;
                    pictureBox18.Image = Properties.Resources.image8;
                    pictureBox18.Image = Properties.Resources.image7;
                    break;
                case 4:
                    pictureBox4.Image = Properties.Resources.image4;
                    break;
                case 5:
                    pictureBox4.Image = Properties.Resources.image5;
                    pictureBox14.Image = Properties.Resources.image5;


                    break;
                case 6:
                    pictureBox4.Image = Properties.Resources.image6;
                    pictureBox17.Image = Properties.Resources.image7;
                    break;
                case 7:
                    pictureBox4.Image = Properties.Resources.image7;
                    break;
                case 8:
                    pictureBox4.Image = Properties.Resources.image8;
                    break;
            }

            switch (n)
            {
                case 1:
                    pictureBox5.Image = Properties.Resources.image1;
                    pictureBox15.Image = Properties.Resources.image5;
                    break;
                case 2:
                    pictureBox5.Image = Properties.Resources.image2;
                    break;
                case 3:
                    pictureBox5.Image = Properties.Resources.image3;
                    pictureBox15.Image = Properties.Resources.image3;
                    break;
                case 4:
                    pictureBox5.Image = Properties.Resources.image4;
                    break;
                case 5:
                    pictureBox5.Image = Properties.Resources.image5;
                    break;
                case 6:
                    pictureBox5.Image = Properties.Resources.image6;
                    break;
                case 7:
                    pictureBox5.Image = Properties.Resources.image7;
                    break;
                case 8:
                    pictureBox5.Image = Properties.Resources.image8;
                    break;
            }

            switch (f)
            {
                case 1:
                    pictureBox5.Image = Properties.Resources.image1;
                    pictureBox16.Image = Properties.Resources.image3;
                    break;
                case 2:
                    pictureBox5.Image = Properties.Resources.image2;
                    break;
                case 3:
                    pictureBox5.Image = Properties.Resources.image3;
                    break;
                case 4:
                    pictureBox5.Image = Properties.Resources.image4;
                    break;
                case 5:
                    pictureBox5.Image = Properties.Resources.image5;
                    break;
                case 6:
                    pictureBox5.Image = Properties.Resources.image6;
                    break;
                case 7:
                    pictureBox5.Image = Properties.Resources.image7;
                    break;
                case 8:
                    pictureBox5.Image = Properties.Resources.image8;
                    break;
            }
            switch (g)
            {
                case 1:
                    pictureBox6.Image = Properties.Resources.image1;
                    break;
                case 2:
                    pictureBox6.Image = Properties.Resources.image2;
                    break;
                case 3:
                    pictureBox6.Image = Properties.Resources.image3;
                    break;
                case 4:
                    pictureBox6.Image = Properties.Resources.image4;
                    break;
                case 5:
                    pictureBox6.Image = Properties.Resources.image5;
                    break;
                case 6:
                    pictureBox6.Image = Properties.Resources.image6;
                    break;
                case 7:
                    pictureBox6.Image = Properties.Resources.image7;
                    break;
                case 8:
                    pictureBox6.Image = Properties.Resources.image8;
                    break;
            }
            switch (h)
            {
                case 1:
                    pictureBox7.Image = Properties.Resources.image1;
                    break;
                case 2:
                    pictureBox7.Image = Properties.Resources.image2;
                    break;
                case 3:
                    pictureBox7.Image = Properties.Resources.image3;
                    break;
                case 4:
                    pictureBox7.Image = Properties.Resources.image4;
                    break;
                case 5:
                    pictureBox7.Image = Properties.Resources.image5;
                    break;
                case 6:
                    pictureBox7.Image = Properties.Resources.image6;
                    break;
                case 7:
                    pictureBox7.Image = Properties.Resources.image7;
                    break;
                case 8:
                    pictureBox7.Image = Properties.Resources.image8;
                    break;
            }

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            pictureBox21.Visible = true;

        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            pictureBox21.Visible = false;
        }
    }
}

Is there a more efficient way to get this effect?

\$\endgroup\$
  • \$\begingroup\$ I cant link a picture (or at least I dont know how to) but if you google C64 loading screen it will help (even select a video) But the boxes are long line stripes across the page of one solid colour, that colour changes to another solid colour after a few ms. So I have roughly 21 of these boxes down the page and using timer I change them with a random number and a case statement (shown above) I have 8 case statements but its very untidy. \$\endgroup\$ – Chris Devine Jan 10 '16 at 17:20
  • \$\begingroup\$ timer2 and timer3 control the visibility of pictureBox21. What is special about pictureBox21? \$\endgroup\$ – 200_success Jan 10 '16 at 17:50
4
\$\begingroup\$

There's quite a lot of code duplication going on here... I would suggest refactoring the switch statements into a method.

I'm assuming there are 20 PictureBox objects and you want to randomize the shown image on each of those, because your provided code is a bit bizarre (e.g. pictureBox11.Image only ever gets assigned Properties.Resources.image1 or image3, and sometimes there are multiple assignments for the same pictureBox inside a case)

namespace c64 {
    public partial class Form1 : Form {
        private Random rng;
        private const Image[] Images = new Image[]{
            Properties.Resources.image1,
            Properties.Resources.image2,
            Properties.Resources.image3,
            Properties.Resources.image4,
            Properties.Resources.image5,
            Properties.Resources.image6,
            Properties.Resources.image7,
            Properties.Resources.image8
        };

        public Form1() {
            InitializeComponents();
            rng = new Random();
        }

        private void RandomizeImage(PictureBox pictureBox) {
            int index = rng.Next(0, Images.Length - 1);
            pictureBox.Image = Images[index];
        }

        private void timer1_Tick(object sender, EventArgs e) {
            RandomizeImage(pictureBox1);
            RandomizeImage(pictureBox2);
            RandomizeImage(pictureBox3);
            RandomizeImage(pictureBox4);
            RandomizeImage(pictureBox5);
            RandomizeImage(pictureBox6);
            RandomizeImage(pictureBox7);
            RandomizeImage(pictureBox8);
            RandomizeImage(pictureBox9);
            RandomizeImage(pictureBox10);
            RandomizeImage(pictureBox11);
            RandomizeImage(pictureBox12);
            RandomizeImage(pictureBox13);
            RandomizeImage(pictureBox14);
            RandomizeImage(pictureBox15);
            RandomizeImage(pictureBox16);
            RandomizeImage(pictureBox17);
            RandomizeImage(pictureBox18);
            RandomizeImage(pictureBox19);
            RandomizeImage(pictureBox20);
        }

        // rest of your code ...
    }
}

Of course, you could pack those pictureBoxX (X = 1 to 20) into their own array and iterate over them in a loop.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.