有没有办法简化这个矩形绘制代码?

huangapple go评论71阅读模式
英文:

Is there a way I can simplify this rectangle drawing code?

问题

以下是代码的翻译部分:

我在思考是否可以简化这部分代码它看起来有点重复

def show(self, W, screen):
    x = self.i * W
    y = self.j * W
    rect = pygame.Rect(x, y, W, W)
    pygame.draw.rect(screen, BG, rect)
    # TODO 优化此代码
    if self.new:
        # TODO 更好的弹出动画
        rect.inflate(-50, -50)
        pygame.draw.rect(screen,(255, 255, 255), rect)
        rect.inflate(10, 10)
        pygame.draw.rect(screen, (255, 255, 255), rect)
        self.new = False
    else:
        if self.value == 2:
            pygame.draw.rect(screen, Sqr2, rect)
        elif self.value == 4:
            pygame.draw.rect(screen, Sqr4, rect)
        elif self.value == 8:
            pygame.draw.rect(screen, Sqr8, rect)
        elif self.value == 16:
            pygame.draw.rect(screen, Sqr16, rect)
        elif self.value == 32:
            pygame.draw.rect(screen, Sqr32, rect)
        elif self.value == 64:
            pygame.draw.rect(screen, Sqr64, rect)
        elif self.value == 128:
            pygame.draw.rect(screen, Sqr128, rect)
        elif self.value == 256:
            pygame.draw.rect(screen, Sqr256, rect)
        elif self.value == 512:
            pygame.draw.rect(screen, Sqr512, rect)
        elif self.value == 1024:
            pygame.draw.rect(screen, Sqr1024, rect)
        pygame.draw.rect(screen, BORDER, rect, 10)

Sqr2、Sqr4、Sqr8等是从另一个文件中导入的颜色元组。

Sqr2 = (245, 245, 245)
Sqr4 = (245, 245, 220)
Sqr8 = (255, 160, 122)
Sqr16 = (255, 127, 80)
Sqr32 = (255, 99, 71)
Sqr64 = (255, 0, 0)
Sqr128 = (255, 250, 96)
Sqr256 = (240, 224, 80)
Sqr512 = (240, 224, 16)
Sqr1024 = (250, 208, 0)
英文:

I was wondering if this part of code can be simplified, it looks a bit repetitive

def show(self, W, screen):
    x = self.i * W
    y = self.j * W
    rect = pygame.Rect(x, y, W, W)
    pygame.draw.rect(screen, BG, rect)
    # TODO optimize this code
    if self.new:
        # TODO better popup animation
        rect.inflate(-50, -50)
        pygame.draw.rect(screen,(255, 255, 255), rect)
        rect.inflate(10, 10)
        pygame.draw.rect(screen, (255, 255, 255), rect)
        self.new = False
    else:
        if self.value == 2:
            pygame.draw.rect(screen, Sqr2, rect)
        elif self.value == 4:
            pygame.draw.rect(screen, Sqr4, rect)
        elif self.value == 8:
            pygame.draw.rect(screen, Sqr8, rect)
        elif self.value == 16:
            pygame.draw.rect(screen, Sqr16, rect)
        elif self.value == 32:
            pygame.draw.rect(screen, Sqr32, rect)
        elif self.value == 64:
            pygame.draw.rect(screen, Sqr64, rect)
        elif self.value == 128:
            pygame.draw.rect(screen, Sqr128, rect)
        elif self.value == 256:
            pygame.draw.rect(screen, Sqr256, rect)
        elif self.value == 512:
            pygame.draw.rect(screen, Sqr512, rect)
        elif self.value == 1024:
            pygame.draw.rect(screen, Sqr1024, rect)
        pygame.draw.rect(screen, BORDER, rect, 10)

Sqr2,4,8 ... are color tuples from another file that i've imported

Sqr2 = (245, 245, 245)
Sqr4 = (245, 245, 220)
Sqr8 = (255, 160, 122)
Sqr16 = (255, 127, 80)
Sqr32 = (255, 99, 71)
Sqr64 = (255, 0, 0)
Sqr128 = (255, 250, 96)
Sqr256 = (240, 224, 80)
Sqr512 = (240, 224, 16)
Sqr1024 = (250, 208, 0)

答案1

得分: 3

我不确定你如何导入这些文件 - 如果你将它们导入一个dict中,可能会更清晰,例如:

d = {2:(245,245,245),4:(245,245,220),...#等等}

否则
    pygame.draw.rect(screen, d[self.value], rect)
pygame.draw.rect(screen, BORDER, rect, 10)
英文:

I'm not sure how you import the files - if you imported them in a dict it could be cleaner eg:

d = {2:(245,245,245),4:(245,245,220),...#etc}

else:
    pygame.draw.rect(screen,d[self.value],rect)
pygame.draw.rect(screen,BORDER,rect,10)

答案2

得分: 2

你可以按以下方式在字典中定义Sqr变量:

Sqrs = {2: (245, 245, 245),
        4: (245, 245, 220),
        8: (255, 160, 122),
        16: (255, 127, 80),
        32: (255, 99, 71),
        64: (255, 0, 0),
        128: (255, 250, 96),
        256: (240, 224, 80),
        512: (240, 224, 16),
        1024: (250, 208, 0)}

然后可以使用以下方式绘制形状:

pygame.draw.rect(screen, Sqrs[self.value], rect)
英文:

You could define the Sqr variables in a dictionary as follows:

Sqrs = {2: (245, 245, 245),
        4: (245, 245, 220),
        8: (255, 160, 122),
        16: (255, 127, 80),
        32: (255, 99, 71),
        64: (255, 0, 0),
        128: (255, 250, 96),
        256: (240, 224, 80),
        512: (240, 224, 16),
        1024: (250, 208, 0)}

And then just draw the shape using

pygame.draw.rect(screen, Sqrs[self.value], rect)

huangapple
  • 本文由 发表于 2020年1月6日 18:47:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定