66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
# Kyler Olsen
|
|
# Feb 2025
|
|
|
|
import pygame
|
|
import sys
|
|
import math
|
|
from compiler import compile, Context, CompilerError
|
|
|
|
# Initialize Pygame
|
|
pygame.init()
|
|
|
|
# Screen dimensions
|
|
|
|
# Screen setup
|
|
# screen = pygame.display.set_mode((800, 600))
|
|
screen = pygame.display.set_mode()
|
|
pygame.display.set_caption("Equation Plotter")
|
|
pygame.mouse.set_visible(False)
|
|
MOD_KEYS = pygame.key.get_mods()
|
|
|
|
def main(filename):
|
|
SCREEN_WIDTH = screen.get_width()
|
|
SCREEN_HEIGHT = screen.get_height()
|
|
HALF_WIDTH = SCREEN_WIDTH // 2
|
|
HALF_HEIGHT = SCREEN_HEIGHT // 2
|
|
|
|
context = compile(filename)
|
|
|
|
if isinstance(context, CompilerError):
|
|
running = False
|
|
else:
|
|
running = True
|
|
|
|
# Main loop
|
|
mouse_x_last, mouse_y_last = pygame.mouse.get_pos()
|
|
while running:
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
running = False
|
|
|
|
if isinstance(context, Context):
|
|
# Clear the screen
|
|
screen.fill((0,0,0))
|
|
|
|
# for prev_x, prev_y, x, y, c, w in equ.plot((0 + off, (8 * math.pi) + off, math.pi/32), (100, 100)):
|
|
# pygame.draw.line(screen, c, (prev_x + HALF_WIDTH, HALF_HEIGHT - prev_y), (x + HALF_WIDTH, HALF_HEIGHT - y), w)
|
|
|
|
pygame.display.flip()
|
|
pygame.time.Clock().tick(context.screen.fps)
|
|
|
|
context.step()
|
|
|
|
mouse_x, mouse_y = pygame.mouse.get_pos()
|
|
if 5 < math.sqrt(pow(mouse_x_last - mouse_x, 2) + pow(mouse_y_last - mouse_y, 2)):
|
|
running = False
|
|
keys = pygame.key.get_pressed()
|
|
if any(keys[i] for i in range(len(keys))):
|
|
running = False
|
|
if pygame.key.get_mods() != MOD_KEYS:
|
|
running = False
|
|
|
|
pygame.quit()
|
|
sys.exit()
|
|
|
|
main("example.graph")
|