We have this class to make a window where we initialize the self.particles list that will contain the particles data
- another method will populate the list with the data (called when something happens … if win:…)
- another one (continuosly called) will move the particles changing the data
class Win: def __init__(self): couples = 32 cols, rows = (8,8) # tile_size = tile_size size = cols*tile_size, rows*tile_size self.screen = pygame.display.set_mode(size, pygame.RESIZABLE) pygame.display.set_caption("MatchPair 1.6") self.clock = pygame.time.Clock() self.game_played = -1 self.start_menu() def start_menu(self): ''' ------------------------- START MENU ------------------ ''' self.grid = Generator() self.particles = [] self.particles0 = [] self.particles2 = [] loop = 1 while loop: self.screen.fill(0) self.particles_run_glitter(random.randint(0, 8*tile_size), (255, 255, 255)) for event in pygame.event.get(): if event.type == pygame.QUIT: loop = 0 elif pygame.mouse.get_pressed()[0]: loop = 0 self.game_time = 0 self.game_loop() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_1: loop = 0 self.game_time = 0 self.game_loop() self.screen.blit(message("Press 1 for EASY"), (0,50)) self.screen.blit(message("Press 2 for NORMAL"), (0,70)) self.screen.blit(message("Press 3 for HARD"), (0,90)) m, mrect = message("Match pair 1.6", middle=1) self.screen.blit(m, mrect) pygame.draw.circle(self.screen, (255, 255, 255), pygame.mouse.get_pos(), 10) if self.game_played > 0: self.screen.blit(message(f"SCORE {int(score)} - TIME {self.game_time}"), (0,0)) self.clock.tick(60) pygame.display.flip() pygame.quit()
To make some particle effect, like an “explosion” create this class:
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[2], particle[0][1]+25*particle[2])) 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(f"+{10*multiply_score}"), (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 =============
This will make the particles move and then disappear.
But, firstly, we got to create the particles data like position, color etc.
def particle_explosion_create(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)])
When you want the explosion to happen, you call this create function
self.particle_explosion_create(r1, c1, r2, c2)
this will be put under a condition, an if statement.
The whole code is here https://github.com/formazione/matchpair
https://pythonprogramming.altervista.org/4562bda5-5be5-44b3-bd2e-19fa8cb5b0c1
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame