Last active
November 26, 2022 11:47
-
-
Save Staticity/74618f8f0b5e98f27c79fd3a1128a33c to your computer and use it in GitHub Desktop.
Deal or No Deal Mini-Arcade Simulator
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 os | |
import random | |
import time | |
import sys | |
import subprocess | |
import ffmpeg | |
import shutil | |
pygame.init() | |
def shuffle(board): | |
new_board = [row[:] for row in board] | |
n = len(board) | |
m = len(board[0]) | |
positions = [(i, j) for i in range(n) for j in range(m)] | |
random.shuffle(positions) | |
def is_valid(p): | |
return p[0] >= 0 and p[1] >= 0 and p[0] < n and p[1] < m | |
has_moved = set() | |
directions = [(0, 1), (1, 0), (-1, 0), (0, -1), (0, 0)] | |
for pos in positions: | |
if pos in has_moved: | |
continue | |
d = random.choice(directions) | |
if d == (0, 0): | |
continue | |
swap_pos = (pos[0] + d[0], pos[1] + d[1]) | |
if swap_pos in has_moved or not is_valid(swap_pos): | |
continue | |
a = new_board[pos[0]][pos[1]] | |
b = new_board[swap_pos[0]][swap_pos[1]] | |
new_board[pos[0]][pos[1]] = b | |
new_board[swap_pos[0]][swap_pos[1]] = a | |
has_moved.add(pos) | |
has_moved.add(swap_pos) | |
return new_board | |
unique_colors = False | |
board = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] | |
colors = {board[i][j]:pygame.Color(*([30 + int(i * 4 + j * 15)] * 3)) for i in range(4) for j in range(4)} | |
w, h = 500, 500 | |
rw, rh = w / 8, h / 8 | |
screen = pygame.display.set_mode([w, h]) | |
def get_screen_position(pos): | |
i, j = pos | |
return (h // 8 + i * w / 4, w // 8 + j * h / 4) | |
def add(a, b): | |
return [x + y for x, y in zip(a, b)] | |
def subtract(a, b): | |
return [x - y for x, y in zip(a, b)] | |
def times(a, x): | |
return [a * v for v in x] | |
output_dir = sys.argv[1] | |
num_swaps = int(sys.argv[2]) if len(sys.argv) > 2 else 20 | |
if os.path.exists(output_dir): | |
shutil.rmtree(output_dir) | |
os.makedirs(output_dir, exist_ok=True) | |
current_swaps = 0 | |
image_num = 0 | |
running = True | |
while running and current_swaps < num_swaps: | |
new_board = shuffle(board) | |
current_swaps += 1 | |
location_per_value = {} | |
for i in range(4): | |
for j in range(4): | |
location_per_value[new_board[i][j]] = [i, j] | |
steps = 20 | |
for step in range(steps + 1): | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
screen.fill((0, 0, 0)) | |
for i in range(4): | |
for j in range(4): | |
v = board[i][j] | |
xy = get_screen_position((i, j)) | |
new_xy = get_screen_position(location_per_value[v]) | |
vis_xy = add(xy, times(float(step)/steps, subtract(new_xy, xy))) | |
color = colors[v] if unique_colors else pygame.Color(255, 255, 255) | |
pygame.draw.rect(screen, color, pygame.Rect(vis_xy[0] - rw / 2, vis_xy[1] - rh / 2, rw, rh)) | |
pygame.image.save(screen, os.path.join(output_dir, f'{image_num:04}.png')) | |
image_num += 1 | |
pygame.display.flip() | |
pygame.quit() | |
image_files = [os.path.join(output_dir,img) | |
for img in os.listdir(output_dir) | |
if img.endswith(".png")] | |
ffmpeg.input(os.path.join(output_dir, '*.png'), pattern_type='glob', framerate=120).output(os.path.join(output_dir, 'video.mp4'), vcodec='libx264', pix_fmt='yuv420p').run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment