How to add fx to Matchpair 1.9, a game in Pygame

I am still adding stuff to this game to complete it. Now I added some fx, among other stuff.


Fx

These 2 methods of the class Win are responsible of the fx

  • explosion_fx (called when the event of winning happens)
  • particles_explosion (called every frame, but visible only when it’s triggered (filling the particles list with data for generating them) until the particles are removed (when they are too small)

So, you win, there’s a call to explosion_fx to create 10 particles filling the self.particles list. These list makes the particles appear and transform by the particle_explosion method that is called every frame. When the particles became small the are removed and so the effect is not visible untile next won.

    def explosion_fx(self, r1, c1, r2, c2):     
        for i in range(10):

            # self.particles2.append(
            #     [
            #      [pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]],
            #      [random.randint(0, 42) / 6 - 3.5, random.randint(0, 42) / 6 - 3.5],
            #      random.randint(4, 6)])
            self.particles2.append(
                [
                 [c1*tile_size + 50, r1*tile_size + 50],
                 [random.randint(0, 42) / 6 - 3.5, random.randint(0, 42) / 6 - 3.5],
                 random.randint(4, 6)])
            self.particles2.append(
                [
                 [c2*tile_size + 50, r2*tile_size + 50],
                 [random.randint(0, 42) / 6 - 3.5, random.randint(0, 42) / 6 - 3.5],
                 random.randint(4, 6)])

    def particles_explosion(self, pcolor):
        ''' call this in a for i in range(10) '''
        scale_x = 0
        for particle in self.particles2:
            particle[0][0] += particle[1][0]
            loc_str = str(int(particle[0][0] / tile_size)) + ';' + str(int(particle[0][1] / tile_size))
            # if loc_str in tile_map:
            #     particle[1][0] = -0.7 * particle[1][0]
            #     particle[1][1] *= 0.95
            #     particle[0][0] += particle[1][0] * 2
            particle[0][1] += particle[1][1]
            loc_str = str(int(particle[0][0] / tile_size)) + ';' + str(int(particle[0][1] / tile_size))
            # if loc_str in tile_map:
            #     particle[1][1] = -0.7 * particle[1][1]
            #     particle[1][0] *= 0.95
            #     particle[0][1] += particle[1][1] * 2
            particle[2] -= 0.035
            particle[1][1] += 0.15
            # self.surf.set_colorkey(pcolor)
            # ======================================= EXPLOSION ================================
            scaling = (100-int(particle[1][1]*10))
            if scaling > 0:
                scale_x = scaling
            self.screen.blit(
                pygame.transform.scale(surf_star, (scale_x, scale_x)), 
                (particle[0][0]+25, particle[0][1]+25))
            self.screen.blit(
                pygame.transform.scale(self.surf, (scale_x, scale_x)), 
                (particle[0][0], particle[0][1]))
            self.screen.blit(
                pygame.transform.scale(message("+10"), 
                    (scale_x, scale_x)), 
                (particle[0][0], particle[0][1]))
            pygame.draw.circle(self.screen, pcolor, [int(particle[0][0]), int(particle[0][1])], int(particle[2]))
            if particle[2] <= 0:
                self.particles2.remove(particle)
            # ====================================== End showing particles when win =============

Here it’s the trigger

when the 10 particles are generated calling self.explosion_fx

To make the animation of the particles, you call, instead, self.particles_explosion(self.color) that takes care of moving the particles, making them smaller and deleting them at the end.

            if self.matches > 0:
                self.particles_explosion(self.pcolor)

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.