Skip to content

Instantly share code, notes, and snippets.

@appblue
Created December 1, 2022 15:29
Show Gist options
  • Select an option

  • Save appblue/8a8af257b6deb47b4dda6e5b3e5015ef to your computer and use it in GitHub Desktop.

Select an option

Save appblue/8a8af257b6deb47b4dda6e5b3e5015ef to your computer and use it in GitHub Desktop.
Simple PyGame Animation for Mikas
import random
import pygame
import sys
import math
FPS = 80
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
NRECTS = 16
RECT_SIZE = 64
def clamp(n, smallest, largest):
return max(smallest, min(n, largest))
pygame.init()
size = width, height = 800, 600
black = 0, 0, 0
frame = 0
r = 128
g = 128
b = 128
flags = pygame.DOUBLEBUF | pygame.SCALED
screen = pygame.display.set_mode(size, flags, vsync=1)
clk = pygame.time.Clock()
while True:
clk.tick(60)
frame += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(black)
# draw NRECTS rectangles
for i in range(NRECTS, 1, -1):
x = (WINDOW_WIDTH / 2 - 50) * math.sin(1.0 * 3.14 * (frame - i * 4) / FPS) + (WINDOW_WIDTH - RECT_SIZE) / 2
y = (WINDOW_HEIGHT / 2 - 50) * math.cos(1.2 * 3.14 * (frame - i * 4) / FPS) + (WINDOW_HEIGHT - RECT_SIZE) / 2
# Initialing Color
color = (clamp(r - 10 * i, 0, 255),
clamp(g - 10 * i, 0, 255),
clamp(b - 10 * i, 0, 255))
# Drawing Rectangle
pygame.draw.rect(pygame.display.get_surface(), color, pygame.Rect(x, y, RECT_SIZE, RECT_SIZE))
r = clamp(r + random.randint(-1, 1), 0, 255)
g = clamp(g + random.randint(-1, 1), 0, 255)
b = clamp(b + random.randint(-1, 1), 0, 255)
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment