微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 pygame 使图像变大

如何解决如何使用 pygame 使图像变大

在我正在制作的游戏中,我有一张图片需要从 (old_width,old_height) 缩放到 (new_width,new_height),这样它看起来会越来越大,我需要多次这样做比赛期间。

到目前为止,我已经尝试过 pygame.trasform.smoothscale,但由于某些原因,它所做的只是将图像沿屏幕向右移动。

这对我来说似乎很奇怪,因为我不明白发生了什么。

这是处理图像的类:

function disableunsafeWindow() {
    // disable UnsafeWindow
}

function enableunsafeWindow() {
    // Enable unsafeWindow
}
function withUnsafeWindow() {
    enableunsafeWindow()
    // Run the code using unsafeWindow
}

function withoutUnsafeWindow() {
    disableunsafeWindow();
    // Remove unsafeWindow access and execute the code without unsafeWindow
}

withUnsafeWindow()
withoutUnsafeWindow()

这就是调用方法增长的地方。对于这个问题,我删除了所有其他多余的部分,因此理论上图像此时应该无休止地增长:

class GrowingImage:

    def __init__(self,image_path,x,y,width,height):
        self.width = width
        self.height = height

        self.object_image = pygame.image.load(image_path)

        self.image = pygame.transform.scale(self.object_image,(self.width,self.height))

        self.x_pos = x
        self.y_pos = y

    def draw(self,background):
        background.blit(self.image,(self.x_pos,self.y_pos))

    def grow(self):
        self.image = pygame.transform.smoothscale(self.object_image,self.height))
        self.width += 1
        self.height += 1

好吧,它没有,我完全不知道为什么会发生这种情况。有人知道我在这里做错了什么吗?

解决方法

图像并没有真正移动。它可能看起来像向右下角移动,因为它越来越大,即它的宽度和高度在增加,记住,从图像的左上角开始。这也是它被绘制的地方。所以为了让它看起来像它的增长,你可以偏移它绘制的宽度和高度的一半,这基本上意味着偏移它以便它被绘制在中心。替换

background.blit(self.image,(self.x_pos,self.y_pos))

background.blit(self.image,(self.x_pos - (self.width/2),self.y_pos - (self.height/2)))

工作示例

import pygame

class GrowingImage:

    def __init__(self,image_path,x,y,width,height):
        self.width = width
        self.height = height

        self.object_image = image_path

        self.image = pygame.transform.scale(self.object_image,(self.width,self.height))

        self.x_pos = x
        self.y_pos = y

    def draw(self,background):
        background.blit(self.image,self.y_pos - (self.height/2)))

    def grow(self):
        self.image = pygame.transform.smoothscale(self.object_image,self.height))
        self.width += 1
        self.height += 1


pygame.init()

d = pygame.display.set_mode((600,600))
image = pygame.Surface((1,1))
growingImage = GrowingImage(image,300,20,20)

while True:
    d.fill((255,255,255))
    pygame.event.get()

    growingImage.draw(d)
    growingImage.grow()

    pygame.display.update()
    
,

blit 的第二个参数可以是一个矩形。使用 pygame.Rect 对象,相对于其中心缩放图像:

class GrowingImage:

    def __init__(self,height):
        
        self.object_image = pygame.image.load(image_path)
        self.image = pygame.transform.scale(self.object_image,self.height))

        self.rect = self.image.get_rect(topleft = (x,y))

    def draw(self,self.rect)

    def grow(self):
    
        w = self.rect.width + 1
        h = self.rect.height + 1
        self.image = pygame.transform.smoothscale(self.object_image,(w,h))
        
        self.rect = self.image.get_rect(center = self.rect.center)

另见Transform scale and zoom surface

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。