Skip to content

Instantly share code, notes, and snippets.

@jakiki6
Created July 28, 2021 17:32
Show Gist options
  • Save jakiki6/8acc5fde564888ba999cbc90a11fc324 to your computer and use it in GitHub Desktop.
Save jakiki6/8acc5fde564888ba999cbc90a11fc324 to your computer and use it in GitHub Desktop.
import pygame, random
from tkinter import filedialog, Tk
def get_color(num):
color = []
for i in range(0, 3):
color.append(num % 256)
num //= 256
return color
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
px, py = 0, 0
ticks = 0
color = 0
color_range = 128
def reset():
global px, py, ticks, color, color_range, screen
px = random.randint(0, width)
py = random.randint(0, height)
ticks = 0
color = random.randint(0, 256 ** 3)
# color_range = random.randint(1, 1024)
screen.fill((0, 0, 0))
running = True
speed = False
reset()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
speed = not speed
elif event.key == pygame.K_r:
reset()
elif event.key == pygame.K_s:
t = Tk()
f = filedialog.asksaveasfile(mode="wb", defaultextension=".png")
if f != None:
pygame.image.save(screen, f)
t.destroy()
px += random.choice([-1, 0, 1])
py += random.choice([-1, 0, 1])
color += random.randint(-color_range, color_range)
px %= width
py %= height
color %= 256 ** 3
screen.set_at((px, py), get_color(color))
if ticks % 10000 == 0 or not speed:
pygame.display.flip()
ticks += 1
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment