Skip to content

Instantly share code, notes, and snippets.

@Fortyseven
Created April 25, 2024 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fortyseven/df0d9f0fb339c936d6bf03fd13350de1 to your computer and use it in GitHub Desktop.
Save Fortyseven/df0d9f0fb339c936d6bf03fd13350de1 to your computer and use it in GitHub Desktop.
import pygame
import math
import time
# Initialize Pygame
pygame.init()
# Set the screen dimensions
SCREEN_WIDTH = 320
SCREEN_HEIGHT = 240
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Set the title of the window
pygame.display.set_caption("Paletteless Plasma")
# Define some colors
BLACK = (0, 0, 0)
# Define the plasma effect
def plasma(x, y, t):
# Calculate the distance from the center of the screen
cx1 = SCREEN_WIDTH / 4 + math.sin(t) * 50
cy1 = SCREEN_HEIGHT / 2 + math.cos(t) * 50
cx2 = SCREEN_WIDTH * 3 / 4 + math.cos(t) * 50
cy2 = SCREEN_HEIGHT / 2 + math.sin(t) * 50
dx1 = x - cx1
dy1 = y - cy1
dx2 = x - cx2
dy2 = y - cy2
distance1 = math.sqrt(dx1 * dx1 + dy1 * dy1)
distance2 = math.sqrt(dx2 * dx2 + dy2 * dy2)
# Calculate the plasma value
plasma_value1 = math.sin(distance1 / 10.0 + t) * 128 + 128
plasma_value2 = math.sin((distance2 / 10.0) + (t * 2)) * 128 + 128
# Assign the plasma value to different color channels
r = plasma_value1 + math.sin(x / 16.0 + t) * 32
g = plasma_value2 + math.sin(y / 16.0 + t) * 32
b = (plasma_value1 + plasma_value2) / 2 + math.sin((x + y) / 16.0 + t) * 32
# Clamp the color values to the valid range
r = max(0, min(255, int(r)))
g = max(0, min(255, int(g)))
b = max(0, min(255, int(b)))
# Return the color
return r, g, b
# Main loop
running = True
t = 0
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fill the screen with black
screen.fill(BLACK)
# Draw the plasma effect
for x in range(SCREEN_WIDTH):
for y in range(SCREEN_HEIGHT):
color = plasma(x, y, t)
screen.set_at((x, y), color)
# Update the screen
pygame.display.flip()
# Increase the time
t += 0.25
# Quit Pygame
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment