Skip to content

Instantly share code, notes, and snippets.

@mb6ockatf
Created August 9, 2023 21:35
Show Gist options
  • Save mb6ockatf/5b370bfed0d513fc1847aa6b6ec2b838 to your computer and use it in GitHub Desktop.
Save mb6ockatf/5b370bfed0d513fc1847aa6b6ec2b838 to your computer and use it in GitHub Desktop.
stunning pygame simulation
#!/usr/bin/env python3
from argparse import ArgumentParser
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
parser = ArgumentParser(prog="balls-equilibrium",
description="nice pygame simulation")
parser.add_argument("--width", type=int, default=200)
parser.add_argument("--height", type=int, default=200)
parser.add_argument("--fps", type=int, default=60)
args = parser.parse_args()
width, height, speed = args.width, args.height, args.fps
pygame.init()
info = pygame.display.Info()
screen_width, screen_height = info.current_w, info.current_h
if width > screen_width or height > screen_height:
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
width, height = screen_width, screen_height
else:
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
black, white = (0, 0, 0), (255, 255, 255)
is_running = True
cur_circle_rad = 10
cords = None
balls = []
screen.fill(black)
while is_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
if event.type == pygame.MOUSEBUTTONDOWN:
_cords = {"width": event.pos[0], "height": event.pos[1]}
_direction = {"left": True, "top": True}
ball = {"cords": _cords, "direction": _direction}
balls += [ball]
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
is_running = False
screen.fill(black)
for j in balls:
if j["cords"]["width"] <= 10 or width - 10 <= j["cords"]["width"]:
j["direction"]["left"] = not j["direction"]["left"]
elif j["cords"]["height"] <= 10 or height - 10 <= j["cords"]["height"]:
j["direction"]["top"] = not j["direction"]["top"]
if j["direction"]["left"]:
j["cords"]["width"] -= 10
if j["direction"]["top"]:
j["cords"]["height"] -= 10
else:
j["cords"]["height"] += 10
else:
j["cords"]["width"] += 10
if j["direction"]["top"]:
j["cords"]["height"] -= 10
else:
j["cords"]["height"] += 10
for j in balls:
cords = (j["cords"]["width"], j["cords"]["height"])
pygame.draw.circle(screen, white, cords, cur_circle_rad)
clock.tick(speed)
pygame.display.update()
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment