Created
March 24, 2023 00:15
-
-
Save s4nyam/cc2e31e02eb932b309820a35330b9232 to your computer and use it in GitHub Desktop.
Check out this awesome #Python code using #Pygame to create a stunning #NorthernLights animation with realistic colors! ππ¨π¨βπ» #coding #programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import random | |
# Set up the Pygame window | |
pygame.init() | |
screen = pygame.display.set_mode((800, 600)) | |
pygame.display.set_caption("Northern Lights") | |
# Define some colors | |
black = (0, 0, 0) | |
white = (255, 255, 255) | |
purple = (160, 32, 240) | |
pink = (255, 105, 180) | |
green = (50, 205, 50) | |
# Create a list of stars | |
stars = [] | |
for i in range(200): | |
x = random.randrange(0, 800) | |
y = random.randrange(0, 600) | |
stars.append([x, y]) | |
# Create a function to draw the stars | |
def draw_stars(screen, stars): | |
for star in stars: | |
pygame.draw.circle(screen, white, star, 1) | |
# Create a function to draw the Northern Lights | |
def draw_lights(screen, color): | |
for i in range(50): | |
x = random.randrange(0, 800) | |
y = random.randrange(0, 600) | |
width = random.randrange(1, 4) | |
height = random.randrange(50, 100) | |
pygame.draw.ellipse(screen, color, [x, y, width, height]) | |
# Main loop | |
done = False | |
clock = pygame.time.Clock() | |
while not done: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
done = True | |
# Fill the background | |
screen.fill(black) | |
# Draw the stars | |
draw_stars(screen, stars) | |
# Draw the Northern Lights | |
draw_lights(screen, purple) | |
draw_lights(screen, pink) | |
draw_lights(screen, green) | |
# Update the screen | |
pygame.display.flip() | |
# Wait a bit before drawing the next frame | |
clock.tick(30) | |
# Quit Pygame | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Screen.Recording.2023-04-01.at.2.46.32.AM.mov