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

Viewed 52

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

    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.


        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

1 Answers

I suspect the issue is the initial re-scaling of the player class images:

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.

    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. Probably size the the original images to player.width and .height on start.

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.

Related