autumn-leaves/autumn_leaves.py
2024-10-22 05:17:58 +02:00

158 lines
3.8 KiB
Python

"""#!/usr/bin/python3"""
import pygame, sys, time, random, pygame.mixer
from pygame.locals import *
pygame.mixer.pre_init(44100, 16, 2, 4096) # initializes the Pygame mixer before Pygame, which improves audio.
pygame.init()
pygame.display.set_caption("The Falling Leaves...")
screen_width = 1000
screen_height = 600
a = (92, 61, 46)
b = (165,113,100)
c = (72, 56, 56)
d = (173, 139, 115)
e = (112, 79, 79)
rgb_colors = (a, b, c, d, e)
screen_backcolor = random.choice(rgb_colors)
screen = pygame.display.set_mode((screen_width, screen_height),0,32)
screen.fill(screen_backcolor)
playlist = [] #tracks.
lengths = [278000, 658000, 213000, 422000,
340000, 379000, 445000, 464000, 305000, 117000] #lengths of tracks
for value in range(1,11):
playlist.append(str(value) + ".ogg")
for p in playlist:
r = random.choice(playlist)
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.load(r) #picks a random track from playlist each time.
pygame.mixer.music.play(1) #loads background music.
lengths_playlist = dict(zip(playlist, lengths))
LENGTH = lengths_playlist.get(r)
leaves = [] #list of leaf pngs to be used in Leaf class.
for value in range(0,9):
leaves.append(str(value) + ".png")
class Leaf(pygame.sprite.Sprite): #class that defines and controls Leaf sprites.
def __init__(self):
pygame.sprite.Sprite.__init__(self)
"""object's apeparance"""
self.path = random.choice(leaves)
self.image = pygame.image.load(self.path).convert_alpha() # converting to alpha improves image handling performance.
self.rect = self.image.get_rect()
"""object's position"""
self.rect.x = random.randint(0, screen_width-100)
self.rect.y = -150
clock = pygame.time.Clock()
rains = []
rain1 = pygame.sprite.Group() #each sprite group contains leaves..
rains.append(rain1)
#...falling at different speeds
rain2 = pygame.sprite.Group()
rains.append(rain2) #three groups are defined.
rain3 = pygame.sprite.Group()
rains.append(rain3)
LEAF1 = pygame.USEREVENT + 5 #events t/control when e/type o/leaf appears
pygame.time.set_timer(LEAF1, 900)
LEAF2 = pygame.USEREVENT + 1
pygame.time.set_timer(LEAF2, 2000)
LEAF3 = pygame.USEREVENT + 2
pygame.time.set_timer(LEAF3, 10000)
TRACKLENGTH = pygame.USEREVENT + 3
pygame.time.set_timer(TRACKLENGTH, LENGTH)
kill_distance = screen_height + 50 #distance at which Leaf sprite is removed from group
while True:
screen.fill(screen_backcolor)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == TRACKLENGTH:
pygame.quit()
sys.exit()
if event.type == LEAF1:
leaf1 = Leaf()
rain1.add(leaf1)
if event.type == LEAF2:
leaf2 = Leaf()
rain2.add(leaf2)
if event.type == LEAF3:
leaf3 = Leaf()
rain3.add(leaf3)
for a in rain1:
a.rect.y += 1
if a.rect.y == kill_distance:
a.kill()
for b in rain2:
b.rect.y += 2
if b.rect.y == kill_distance:
b.kill()
for c in rain3:
c.rect.y += 5
if c.rect.y == kill_distance:
c.kill()
for rain in rains:
rain.update()
rain.draw(screen)
pygame.display.flip()
pygame.display.update()
clock.tick(30) # 30 frames per second.
### Things to consider:
# Event timer and gravities: somehow, not all gravities work for sprite to be killed when specified within loop, but I am unable to understand why. The following are pairs that work: (5:2500,5000), (1:500,1000), (2:2000,4000).
# A list of other nice RGB colors that can be used in the background: rgb(165,113,100) rgb(176, 155, 113) rgb(173, 139, 115) rgb(112, 79, 79) rgb(92, 61, 46)
# Apr 13th 2024. Changed pygame.time.set_timer(LEAF1, 500) to pygame.time.set_timer(LEAF1, 1200) and also clock tick from 40 to 30. also added line pygame.mixer.pre_init(44100, 16, 2, 4096)