简体   繁体   English

Fish Simulator 鱼的生长方式快速和变形。 如何修复 Pygame

[英]Fish Simulator Fish Growing Way To Fast And Growing Out Of Shape. How To Fix Pygame

so I am trying to make my fish grow little by little and grow like its original image but when ever it eats a small fish it grows way to fast even though I set the grow rate to 1 for the small fishes and also the WIDTH of my fish always seems bigger then my height of my fish is there a way I can fish this problem VIDEO as you can see in the video the fish is growing like its height bigger but its width smaller and it ends up looking like that and it also grows like super fast each time I eat the small fish even though I set the grow rate to 1所以我试图让我的鱼一点一点地长大,并像它原来的形象一样长大,但是当它吃到一条小鱼时,它会长得很快,即使我将小鱼的生长速率和我的宽度设置为 1鱼总是看起来比我的鱼的高度大 有没有办法让我钓到这个问题VIDEO正如你在视频中看到的那样,鱼的高度越来越大,但它的宽度越来越小,它最终看起来像那样并且它也在增长每次吃小鱼时都非常快,即使我将生长率设置为 1

    class player:
        def __init__(self,x,y,height,width,color):
           #[.....]

        def get_rect(self):
            self.rect.topleft = (self.x,self.y)
            return self.rect
            pygame.draw.rect(self.color,self.rect)


                
        def draw(self):
          #[............]

        def collide(self, other_rect):
            return self.rect.colliderect(other_rect)



        def grow(self, dx, dy):
            self.width += 1
            self.height += 1
            self.right = [pygame.transform.scale(image, (self.width,self.height)) for image in self.right_original]
            self.left = [pygame.transform.scale(image, (self.width,self.height)) for image in self.left_original]
            playerman.rect = pygame.Rect(self.x, self.y, self.width, self.height)




then in my main loop when ever I collide with the fish I add it by 1 but its adding to much for my width and a little for my height and the fish ends up looking weird and also the fish when ever it eats it grows way to fast.然后在我的主循环中,每当我与鱼发生碰撞时,我都会将它加 1,但它会增加我的宽度和高度,鱼最终看起来很奇怪,而且鱼吃东西时也会长到快速地。


        for blac in blacs:
            if playerman.collide(blac):
                blac.x = 880            
                playerman.grow(1, 1)
                blac.y = random.randint(0,800)
                print("collid")


my full code script我的完整代码脚本

I suspect the issue is the initial re-scaling of the player class images:我怀疑问题是player class 图像的初始重新缩放:

class player:

    def __init__( self, ... )
        # ...
        #   HERE---vvv  The //8 size change is ignored later
        self.right = [pygame.transform.scale(image,(image.get_width()//8,image.get_height()//8)) for image in self.right_original]
        self.left = [pygame.transform.scale(image,(image.get_width()//8,image.get_height()//8)) for image in self.left_original]

But when the fish grows in player.grow() , the images are taken from the originally loaded images, rather than the resized ones.但是当鱼在player.grow()中长大时,图像是从最初加载的图像中获取的,而不是调整大小后的图像。

    def grow(self, dx, dy):
        self.width += 1
        self.height += 1
        # HERE ---vvv  Using un-resized images
        self.right = [pygame.transform.scale(image, (self.width,self.height)) for image in self.right_original]
        self.left = [pygame.transform.scale(image, (self.width,self.height)) for image in self.left_original]
        playerman.rect = pygame.Rect(self.x, self.y, self.width, self.height)

    

Probably the best way to approach this, is to correctly maintain the target-size of the fish.解决这个问题的最好方法可能是正确地保持鱼的目标大小。 So take into account both the /8 resize and the ++ eating increments.因此,请同时考虑 /8 调整大小和 ++ 进食增量。 Probably size the the original images to player.width and .height on start.开始时可能将原始图像调整为player.width.height

class player:
    def __init__(self,x,y,height,width,color):
        self.x      = x
        self.y      = y
        self.height = height
        self.width  = width
        self.color  = color
        self.speed  = 3
        self.isJump = True

        # load the sprite images
        rights = [pygame.image.load("r" + str(i) + ".png") for i in range(1, 13)]
        lefts  = [pygame.image.load("l" + str(i) + ".png") for i in range(1, 13)]
        # re-scale to make originals
        new_size = ( self.width, self.height )
        self.right_original = [ pygame.transform.scale( image, new_size ) for image in rights ]
        self.left_original  = [ pygame.transform.scale( image, new_size ) for image in lefts ]

This gives you a set of original images at the correct size.这会为您提供一组尺寸正确的原始图像。 You can then take a working-copy to re-size on the fly.然后你可以拿一份工作副本来即时调整大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM