首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C64加载屏幕思想

C64加载屏幕思想
EN

Stack Overflow用户
提问于 2016-01-10 16:38:23
回答 2查看 125关注 0票数 0

我做了一个加载屏幕(飞溅屏幕),就像旧的C64。

我使用了一系列的图片框,只是改变颜色的图像使用一个定时器和一个案例陈述。

代码语言:javascript
复制
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;
}

看起来有点恶心,我该如何改进我的代码呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-10 17:06:27

你可以看看迭代器设计模式。您可以创建一个表示单个阶段的类,该类将对每个图片框具有属性,并将这些属性的值设置为资源文件中的相关项。

然后为每个加载阶段创建该对象的实例,将它们放在一个集合中,并编写一个迭代器来循环该集合。

票数 0
EN

Stack Overflow用户

发布于 2016-01-10 17:17:00

有两件事我会改进:

  • 将图像存储在数组中,这样就不必每次重复case X/imageX
  • 先处理常见的事情,然后集中精力处理特殊情况。

您可以将表单中的数组声明为“假”常量,因为它不会改变:

代码语言:javascript
复制
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
}

然后在计时器投手程序中使用以下代码:

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34707982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档