thauweer

main.py

Mar 31st, 2023 (edited)
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. import numpy as np
  2. from numpy import pi, sin
  3. import pygame
  4. import copy
  5.  
  6.  
  7. # colors
  8. BLACK = (0, 0, 0)
  9. WHITE = (255, 255, 255)
  10.  
  11. # size
  12. SIZE_ARR_POINTS = 11
  13.  
  14. SIZE_CELL = 60
  15. DOT_RADIUS = 2
  16.  
  17. pygame.init()
  18. canvas = pygame.display.set_mode((1900, 1000))
  19. speeds = np.linspace(1/SIZE_ARR_POINTS, 1, SIZE_ARR_POINTS)
  20.  
  21. init_points = [[0 for i in range(SIZE_ARR_POINTS)] for j in range(SIZE_ARR_POINTS)]
  22. prev_points = copy.deepcopy(init_points)
  23.  
  24. pygame.display.set_caption("trygonometry test")
  25. exit = False
  26.  
  27. i = 0
  28.  
  29. def get_step(speed, orientation):
  30.     return sin((i*speed-pi/2 if orientation else i*speed))*SIZE_CELL/2-DOT_RADIUS + DOT_RADIUS
  31.  
  32. def draw_speed(surface, position, orientation, speed):
  33.     field = pygame.Surface([SIZE_CELL, 20] if orientation else[20, SIZE_CELL])
  34.  
  35.     step = get_step(speed, orientation)
  36.     pygame.draw.circle(field, WHITE, [SIZE_CELL/2+step, 10] if orientation else [10, SIZE_CELL/2+step], DOT_RADIUS)
  37.  
  38.     surface.blit(field, position)
  39.  
  40. def draw_step(surface, pos, place):
  41.     point = pos[0] + get_step(speeds[place[0]], True), pos[1] + get_step(speeds[place[1]], False)
  42.     prev_point = prev_points[place[1]][place[0]]
  43.     pygame.draw.line(surface, WHITE, [point[0], point[1]] if prev_point == 0 else [prev_point[0], prev_point[1]], [point[0], point[1]])
  44.  
  45.     prev_points[place[1]][place[0]] = point
  46.  
  47. def restart():
  48.     i = 0
  49.     prev_points = copy.deepcopy(init_points)
  50.     canvas.fill(BLACK)
  51.  
  52. while not exit:
  53.     for event in pygame.event.get():
  54.         if event.type == pygame.QUIT:
  55.             exit = True
  56.         if event.type == pygame.KEYDOWN:
  57.             if event.key == pygame.K_r:
  58.                 restart()
  59.  
  60.     for j, speed in enumerate(speeds):
  61.         draw_speed(canvas, [135 + j*(SIZE_CELL+10), 100], True, speed)
  62.         draw_speed(canvas, [100, 135 + j*(SIZE_CELL+10)], False, speed)
  63.  
  64.     for j in range(SIZE_ARR_POINTS):
  65.         for k in range(SIZE_ARR_POINTS):
  66.             draw_step(canvas, [165 + k*(SIZE_CELL+10), 165 + j*(SIZE_CELL+10)], [k, j])
  67.  
  68.     i += pi/180
  69.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment