Merry Christmas everyone with this little script to show some fun Christmas particles with pygame.
A merry Christmas… with pygame
#!/usr/bin/python3.4 import pygame, sys, random from pygame.locals import * pygame.init() pygame.mixer.init() screen = pygame.display.set_mode((1376, 900),0,32) clock = pygame.time.Clock() pygame.mixer.music.load("christmas.mp3") pygame.mixer.music.play() trees = pygame.image.load("trees.png").convert() pygame.display.set_caption('Fireworks') # [loc, velocity, timer] particles = [] pygame.mouse.set_visible(False) # ======================================= FPS ==== font = pygame.font.SysFont("Arial", 20) def show_fps(): "shows the frame rate on the screen" # global clock fr = str(int(clock.get_fps())) frt = font.render(fr, 1, pygame.Color("coral")) screen.blit(frt, (0, 0)) class Particles: def __init__(self): pass def particles_gen(pnum): for i in range(pnum): mx, my = pygame.mouse.get_pos() particles.append([ # actual Position [mx, my], # direction [random.randint(0, 42) / 6 - 30.5, random.randint(0, 42) / 6 - 3.5], # radius random shape random.randint(4, 6)]) move_particles(particles) def randomcolor(): a, b, c = (random.randrange(0,255), random.randrange(0,255), random.randrange(0,255)) return a, b, c def move_particles(particles): for particle in particles: particle[0][0] += particle[1][0] # new position + dir x particle[0][1] += particle[1][1] # new pos + dir y particle[2] -= 0.035 # shrinking particle[1][1] += 0.15 # falls down same speed # this conditions to remove unseen particles optimize the velocity conditions = particle[0][0] < 0 or particle[0][0] > 1376 or particle[0][1] > 900 or particle[0][1] < 0 particles.remove(particle) if (particle[2] == 0 or conditions) else pygame.draw.circle(screen, randomcolor(), [int(particle[0][0]), int(particle[0][1])], int(particle[2])) # ==================== Makes things go on ==== sx = 0 # scrolling variable while True: # screen.fill((0,0,0)) # fills the screen in black sx -= 1 screen.blit(trees, (sx, 0)) screen.blit(trees, (sx + 1376, 0)) if sx == - 1376: sx = 0 particles_gen(pnum=50) show_fps() # ========= FPS ============ # Buttons ------------------- # for event in pygame.event.get(): if event.type == pygame.QUIT or ( event.type == pygame.KEYDOWN and event.key== pygame.K_ESCAPE): pygame.quit() pygame.display.flip() clock.tick(60)
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame