如何在Python中重新创建刮刮卡的效果? (逐渐去掉一层,才能露出下面的图片?)

问题描述 投票:0回答:1

我使用 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 sprite transparency
1个回答
0
投票

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()
绘制一条白线(请参阅文档here)。
start_pos
end_pos
将是每次迭代移动之前和之后的划痕位置。
width
是划痕大小。这样你就可以随心所欲地移动(跳远),而无需成倍增加成本。

您最终会得到黑白图像的结果,并且 CPU 使用率非常低。然后,在绘制调用之前,您只需使用掩码来渲染结果(请参阅mask 文档)。基本上你会用一张图像替换蒙版的深色部分,另一张图像替换白色部分

© www.soinside.com 2019 - 2024. All rights reserved.