C64加载屏幕的想法
C64 loading screen thoughts
我做了一个加载屏幕(启动画面),就像旧的 C64 一样。
我使用了一系列图片框,只是使用计时器和 case 语句更改彩色图像。
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;
}
它看起来有点难看,我该如何改进我的代码?
你可以看看Iterator design pattern。您可以创建一个代表单个阶段的 class,class 将具有每个图片框的属性,您可以将这些属性的值设置为资源文件中的相关项目。
然后您为每个加载阶段创建该对象的实例,将它们放入一个集合中并编写一个迭代器来循环该集合。
有两点我要改进:
将您的图像存储在一个数组中,因此您不必每次都重复case X/imageX
。
先处理常见的事情,再处理特殊情况。
您可以将表单中的数组声明为 "fake" 常量,因为它不会改变:
private readonly static Image[] myImages = new[] {
Properties.Resources.image1,
Properties.Resources.image2,
Properties.Resources.image3,
Properties.Resources.image4,
Properties.Resources.image5,
Properties.Resources.image6,
Properties.Resources.image7,
Properties.Resources.image8
}
然后在您的计时器处理程序中使用以下代码:
Image image = myImages[a-1];
pictureBox1.Image = image;
pictureBox8.Image = image;
// special cases
if (a == 1)
{
pictureBox2.Image = image;
pictureBox11.Image = image;
pictureBox9.Image = image;
}
if (a >= 1 && a <= 3)
{
pictureBox10.Image = image;
}
我做了一个加载屏幕(启动画面),就像旧的 C64 一样。
我使用了一系列图片框,只是使用计时器和 case 语句更改彩色图像。
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;
}
它看起来有点难看,我该如何改进我的代码?
你可以看看Iterator design pattern。您可以创建一个代表单个阶段的 class,class 将具有每个图片框的属性,您可以将这些属性的值设置为资源文件中的相关项目。
然后您为每个加载阶段创建该对象的实例,将它们放入一个集合中并编写一个迭代器来循环该集合。
有两点我要改进:
将您的图像存储在一个数组中,因此您不必每次都重复
case X/imageX
。先处理常见的事情,再处理特殊情况。
您可以将表单中的数组声明为 "fake" 常量,因为它不会改变:
private readonly static Image[] myImages = new[] {
Properties.Resources.image1,
Properties.Resources.image2,
Properties.Resources.image3,
Properties.Resources.image4,
Properties.Resources.image5,
Properties.Resources.image6,
Properties.Resources.image7,
Properties.Resources.image8
}
然后在您的计时器处理程序中使用以下代码:
Image image = myImages[a-1];
pictureBox1.Image = image;
pictureBox8.Image = image;
// special cases
if (a == 1)
{
pictureBox2.Image = image;
pictureBox11.Image = image;
pictureBox9.Image = image;
}
if (a >= 1 && a <= 3)
{
pictureBox10.Image = image;
}