如何在 Python 中重现刮刮卡的效果?(逐渐去除一层以露出下面的图片?[复制]

How do I recreate the effect of scratching a scratchcard in Python? (remove a layer gradually in order to reveal a picture underneath?) [duplicate]

提问人:StudentProgrammer 提问时间:10/12/2023 最后编辑:StudentProgrammer 更新时间:10/12/2023 访问量:53

问:

我使用 Pygame 模块制作了一个移动精灵。它在屏幕上移动,当它碰到屏幕边框时会改变方向。我想逐渐显示一张图片,就好像精灵是一枚硬币,刮掉了刮刮卡的正面图像,露出下面的图片。到目前为止,我已经制作了很多小的方形白色精灵,它们覆盖了屏幕,并在与移动的精灵碰撞时被移除。这工作正常,但使过渡具有像素化的外观。我几乎可以肯定,如果我能制作出足够多的小精灵,这会起作用,但它非常耗费计算机,必须有另一种方法来达到同样的效果。(这是我的第一篇文章。我希望代码是可读的,格式正确。感谢您的阅读:))

`import pygame, sys
import random
pygame.init()
clock = pygame.time.Clock()
screen_width, screen_height = 800, 800
screen = pygame.display.set_mode((screen_width, screen_height))

pic_path = "absolute path"
BG_Pic = "BGIMG0.png"
Background_picture = pygame.transform.scale(pygame.image.load(pic_path + BG_Pic),(800, 800))
Background_picture_rect = Background_picture.get_rect()

###CLASSES###
class Moving_rect(pygame.sprite.Sprite):
    def __init__(self, x, y, widthheight, x_speed, y_speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([widthheight, widthheight])
        self.image.fill((87, 161, 230))
        self.rect = self.image.get_rect(centerx = x, centery = y)
        self.x_speed = x_speed
        self.y_speed = y_speed

    def draw(self, screen):
        screen.blit(self.image, (self.rect.x, self.rect.y))
    def update(self):
        self.rect.x += self.x_speed
        self.rect.y += self.y_speed
        if self.rect.right >= screen_width+0.5 or self.rect.left <= -0.5:
            self.x_speed *= -1
        if self.rect.bottom >= screen_height+0.5 or self.rect.top <= -0.5:
            self.y_speed *= -1

class BG_rect(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height, color):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([width, height])
        self.image.fill((color))
        self.rect = self.image.get_rect(topleft = (x,y))


    def draw(self, screen):
        screen.blit(self.image, (self.rect.x, self.rect.y))

####CLASSES####

Number_of_cubes = 1

block = pygame.sprite.Group()

for cube in range(0,Number_of_cubes):
    cube_x = random.randint(0, 480)
    cube_y = random.randint(0, 720)

    block.add(Moving_rect(cube_x,
                           cube_y,
                          20,
                          2,
                           2))

number_of_rects_pr_side= 100
other_rect_group = pygame.sprite.Group()

for i in range(0,number_of_rects_pr_side):
    for k in range(0,number_of_rects_pr_side):
        other_rect_group.add(BG_rect(k * (800/number_of_rects_pr_side)
                                        , i * (800/number_of_rects_pr_side)
                                        , (800 / number_of_rects_pr_side),
                                        (800 / number_of_rects_pr_side)
                                        , (255, 255, 255)))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.QUIT()
            sys.exit()
    screen.blit(Background_picture, Background_picture_rect)
    pygame.sprite.groupcollide(other_rect_group, block, True, False)
    block.update()
    block.draw(screen)
    other_rect_group.update()
    other_rect_group.draw(screen)

    pygame.display.flip()
    clock.tick(20)

`

python image pygame 精灵 透明度

评论

0赞 matszwecja 10/12/2023
制作一个坚实的表面并用透明的绘画怎么样?

答:

-1赞 Tricotou 10/12/2023 #1

So far I've made a lot of small squaresized white sprites, that cover the screen and are removed upon colliding with the moving sprite. This works okay, but gives the transition a pixelated look. I'm almost certain this would work if I could make enough small sprites

事实上,这不是你想这样做的方式^^

我建议使用口罩。基本上,创建一个黑色图像,并在“划痕”点移动时画一条白线,使用(见文档)。 并且是每次迭代移动前后的划痕位置。 将是划痕大小。这样你就可以随心所欲地移动(跳远),而不会增加成本。pygame.draw.line()start_posend_poswidth

你最终会得到一个黑白图像的结果,以非常便宜的CPU使用率。然后,在绘制调用之前,只需使用掩码来呈现结果(请参阅掩码文档)。基本上,你会有一个图像替换蒙版的黑暗部分,另一个图像替换白色部分

评论

0赞 StudentProgrammer 10/13/2023
嗨,特里科图!非常感谢您的回答。我无法用一张图像替换深色部分,而另一张图像替换白色部分。会很好心地解释:)