Advertisement
nikitast

Kristall

Apr 14th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.32 KB | None | 0 0
  1. import pygame, sys
  2. import random
  3. import math
  4. import datetime
  5. p = 10
  6. o=p/100
  7. pygame.init()
  8. width=640
  9. height=640
  10. pygame.display.set_caption('2D-модель роста кристаллов')
  11. screen = pygame.display.set_mode([width,height])
  12. screen.fill([255,255,255])
  13. x = width//2
  14. y = height//2
  15. pygame.draw.rect(screen, (255,0,0), ((x,y),(1,1)), 0)
  16. pygame.display.flip()
  17. '''des=0
  18. while des<10:
  19.    x = random.uniform(0,640)
  20.    y = random.uniform(0,640)
  21.    pygame.draw.rect(screen, (255,0,0), ((x,y),(1,1)), 0)
  22.    pygame.display.flip()
  23.    des=des+1'''
  24.    
  25. x = width//2
  26. y = height//2
  27. radius=100 # радиус основной окружности
  28. j=0
  29. r=0
  30. f=0
  31. Pi=3.14
  32. red=0
  33. black=0
  34. blue=0
  35. Violet=0
  36. pink=0
  37. parametr=1 #меняет радиус окружности, на которой появляются доп. кластеры
  38.  
  39. # отрисовка доп. кластеров
  40. phi=random.uniform(0,2*Pi)
  41. xo = x+round(radius/parametr*(math.cos(phi)))
  42. yo = y+round(radius/parametr*(math.sin(phi)))
  43. pygame.draw.rect(screen, (0,0,0), ((xo,yo),(1,1)), 0)
  44.  
  45. phi=random.uniform(0,2*Pi)
  46. xo1 = x+round(radius/parametr*(math.cos(phi)))
  47. yo1 = y+round(radius/parametr*(math.sin(phi)))
  48. pygame.draw.rect(screen, (0,0,255), ((xo1,yo1),(1,1)), 0)
  49.  
  50. phi=random.uniform(0,2*Pi)
  51. xo2 = x+round(radius/parametr*(math.cos(phi)))
  52. yo2 = y+round(radius/parametr*(math.sin(phi)))
  53. pygame.draw.rect(screen, (25,25,112), ((xo2,yo2),(1,1)), 0)
  54.  
  55. phi=random.uniform(0,2*Pi)
  56. xo3 = x+round(radius/parametr*(math.cos(phi)))
  57. yo3 = y+round(radius/parametr*(math.sin(phi)))
  58. pygame.draw.rect(screen, (255,20,147), ((xo3,yo3),(1,1)), 0)
  59.  
  60. pygame.draw.circle(screen, [67,232,12], [x,y], radius, 1)
  61. pygame.display.flip()
  62.  
  63. while f<o:
  64.     # динамический радиус(не используется)
  65.     '''radius=10+round(j/10)'''
  66.     # создание первых координат блуждающей клетки
  67.     dradius=random.uniform(0, radius)
  68.     phi=random.uniform(0,2*Pi)
  69.     x1= x+round(dradius*(math.cos(phi)))
  70.     y1= y+round(dradius*(math.sin(phi)))
  71.  
  72.    
  73.     r=0
  74.     while r==0:
  75.         #проверка на попадание в существующий атом
  76.         if ((screen.get_at((x1, y1))[:3]==(255,0,0)) or (screen.get_at((x1, y1))[:3]==(0,0,0)) or
  77.             (screen.get_at((x1, y1))[:3]==(0,0,255)) or (screen.get_at((x1, y1))[:3]==(25,25,112)) or
  78.             (screen.get_at((x1, y1))[:3]==(255,20,147))):
  79.             dradius=random.uniform(0, radius)
  80.             phi=random.uniform(0,2*Pi)
  81.             x1= x+round(dradius*(math.cos(phi)))
  82.             y1= y+round(dradius*(math.sin(phi)))
  83.      
  84.         else:
  85.             #проверка на наличие соседей
  86.             if ((screen.get_at((x1+1, y1+1))[:3]==(255,0,0)) or (screen.get_at((x1+1, y1))[:3]==(255,0,0)) or
  87.                 (screen.get_at((x1+1, y1-1))[:3]==(255,0,0)) or (screen.get_at((x1, y1+1))[:3]==(255,0,0)) or
  88.                 (screen.get_at((x1, y1-1))[:3]==(255,0,0)) or (screen.get_at((x1-1, y1+1))[:3]==(255,0,0)) or
  89.                 (screen.get_at((x1-1, y1))[:3]==(255,0,0)) or (screen.get_at((x1-1, y1-1))[:3]==(255,0,0)) or
  90.                 (screen.get_at((x1+1, y1+1))[:3]==(0,0,0)) or (screen.get_at((x1+1, y1))[:3]==(0,0,0)) or
  91.                 (screen.get_at((x1+1, y1-1))[:3]==(0,0,0)) or (screen.get_at((x1, y1+1))[:3]==(0,0,0)) or
  92.                 (screen.get_at((x1, y1-1))[:3]==(0,0,0)) or (screen.get_at((x1-1, y1+1))[:3]==(0,0,0)) or
  93.                 (screen.get_at((x1-1, y1))[:3]==(0,0,0)) or (screen.get_at((x1-1, y1-1))[:3]==(0,0,0)) or
  94.                 (screen.get_at((x1+1, y1+1))[:3]==(0,0,255)) or (screen.get_at((x1+1, y1))[:3]==(0,0,255)) or
  95.                 (screen.get_at((x1+1, y1-1))[:3]==(0,0,255)) or (screen.get_at((x1, y1+1))[:3]==(0,0,255)) or
  96.                 (screen.get_at((x1, y1-1))[:3]==(0,0,255)) or (screen.get_at((x1-1, y1+1))[:3]==(0,0,255)) or
  97.                 (screen.get_at((x1-1, y1))[:3]==(0,0,255)) or (screen.get_at((x1-1, y1-1))[:3]==(0,0,255)) or
  98.                 (screen.get_at((x1+1, y1+1))[:3]==(25,25,112)) or (screen.get_at((x1+1, y1))[:3]==(25,25,112)) or
  99.                 (screen.get_at((x1+1, y1-1))[:3]==(25,25,112)) or (screen.get_at((x1, y1+1))[:3]==(25,25,112)) or
  100.                 (screen.get_at((x1, y1-1))[:3]==(25,25,112)) or (screen.get_at((x1-1, y1+1))[:3]==(25,25,112)) or
  101.                 (screen.get_at((x1-1, y1))[:3]==(25,25,112)) or (screen.get_at((x1-1, y1-1))[:3]==(25,25,112)) or
  102.                 (screen.get_at((x1+1, y1+1))[:3]==(255,20,147)) or (screen.get_at((x1+1, y1))[:3]==(255,20,147)) or
  103.                 (screen.get_at((x1+1, y1-1))[:3]==(255,20,147)) or (screen.get_at((x1, y1+1))[:3]==(255,20,147)) or
  104.                 (screen.get_at((x1, y1-1))[:3]==(255,20,147)) or (screen.get_at((x1-1, y1+1))[:3]==(255,20,147)) or
  105.                 (screen.get_at((x1-1, y1))[:3]==(255,20,147)) or (screen.get_at((x1-1, y1-1))[:3]==(255,20,147))):
  106.  
  107.                 #выявление к какому именно кластеру присоединился атом
  108.                 if ((screen.get_at((x1+1, y1+1))[:3]==(255,0,0)) or (screen.get_at((x1+1, y1))[:3]==(255,0,0)) or
  109.                     (screen.get_at((x1+1, y1-1))[:3]==(255,0,0)) or (screen.get_at((x1, y1+1))[:3]==(255,0,0)) or
  110.                     (screen.get_at((x1, y1-1))[:3]==(255,0,0)) or (screen.get_at((x1-1, y1+1))[:3]==(255,0,0)) or
  111.                     (screen.get_at((x1-1, y1))[:3]==(255,0,0)) or (screen.get_at((x1-1, y1-1))[:3]==(255,0,0))):
  112.                         pygame.draw.rect(screen, (255,0,0), ((x1,y1),(1,1)), 0)
  113.                         pygame.display.flip()
  114.                         red=+1
  115.                 if ((screen.get_at((x1+1, y1+1))[:3]==(0,0,0)) or (screen.get_at((x1+1, y1))[:3]==(0,0,0)) or
  116.                     (screen.get_at((x1+1, y1-1))[:3]==(0,0,0)) or (screen.get_at((x1, y1+1))[:3]==(0,0,0)) or
  117.                     (screen.get_at((x1, y1-1))[:3]==(0,0,0)) or (screen.get_at((x1-1, y1+1))[:3]==(0,0,0)) or
  118.                     (screen.get_at((x1-1, y1))[:3]==(0,0,0)) or (screen.get_at((x1-1, y1-1))[:3]==(0,0,0))):
  119.                         pygame.draw.rect(screen, (0,0,0), ((x1,y1),(1,1)), 0)
  120.                         pygame.display.flip()
  121.                         black=+1
  122.                 if ((screen.get_at((x1+1, y1+1))[:3]==(0,0,255)) or (screen.get_at((x1+1, y1))[:3]==(0,0,255)) or
  123.                     (screen.get_at((x1+1, y1-1))[:3]==(0,0,255)) or (screen.get_at((x1, y1+1))[:3]==(0,0,255)) or
  124.                     (screen.get_at((x1, y1-1))[:3]==(0,0,255)) or (screen.get_at((x1-1, y1+1))[:3]==(0,0,255)) or
  125.                     (screen.get_at((x1-1, y1))[:3]==(0,0,255)) or (screen.get_at((x1-1, y1-1))[:3]==(0,0,255))):
  126.                         pygame.draw.rect(screen, (0,0,255), ((x1,y1),(1,1)), 0)
  127.                         pygame.display.flip()
  128.                         blue=+1
  129.                 if ((screen.get_at((x1+1, y1+1))[:3]==(25,25,112)) or (screen.get_at((x1+1, y1))[:3]==(25,25,112)) or
  130.                     (screen.get_at((x1+1, y1-1))[:3]==(25,25,112)) or (screen.get_at((x1, y1+1))[:3]==(25,25,112)) or
  131.                     (screen.get_at((x1, y1-1))[:3]==(25,25,112)) or (screen.get_at((x1-1, y1+1))[:3]==(25,25,112)) or
  132.                     (screen.get_at((x1-1, y1))[:3]==(25,25,112)) or (screen.get_at((x1-1, y1-1))[:3]==(25,25,112))):
  133.                         pygame.draw.rect(screen, (25,25,112), ((x1,y1),(1,1)), 0)
  134.                         pygame.display.flip()
  135.                         Violet=+1
  136.                 if ((screen.get_at((x1+1, y1+1))[:3]==(255,20,147)) or (screen.get_at((x1+1, y1))[:3]==(255,20,147)) or
  137.                     (screen.get_at((x1+1, y1-1))[:3]==(255,20,147)) or (screen.get_at((x1, y1+1))[:3]==(255,20,147)) or
  138.                     (screen.get_at((x1, y1-1))[:3]==(255,20,147)) or (screen.get_at((x1-1, y1+1))[:3]==(255,20,147)) or
  139.                     (screen.get_at((x1-1, y1))[:3]==(255,20,147)) or (screen.get_at((x1-1, y1-1))[:3]==(255,20,147))):
  140.                         pygame.draw.rect(screen, (255,20,147), ((x1,y1),(1,1)), 0)
  141.                         pygame.display.flip()
  142.                         pink=+1
  143.                 r=1
  144.                 j=+1
  145.                 # подсчёт процентного содержания атомов в окружности
  146.                 f=j/round(Pi*radius*radius)
  147.                 # вывод времени и кол-ва атомов кластеров(не используется)
  148.                 '''now = datetime.datetime.now()
  149.                print(j,';',(now.minute*60+now.second))'''
  150.             else:
  151.                 #проверка на выход за окружность
  152.                 if ((x1>100) or (x1<0) or (y1>100) or (y1<0)):
  153.                     dradius=random.uniform(0, radius)
  154.                     phi=random.uniform(0,2*Pi)
  155.                     x1= x+round(dradius*(math.cos(phi)))
  156.                     y1= y+round(dradius*(math.sin(phi)))
  157.                
  158.                 else:
  159.                     #шаг блуждающего атома
  160.                     x1= random.randint(x1-1,x1+1)
  161.                     y1= random.randint(y1-1,y1+1)
  162.                
  163.  
  164. print('red= ', red)
  165. print('black= ', black)
  166. print('blue= ', blue)
  167. print('Violet= ', Violet)
  168. print('pink= ', pink)
  169.  
  170.    
  171. running = True
  172. while running:
  173.     for event in pygame.event.get():
  174.         if event.type == pygame.QUIT:
  175.             running = False
  176. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement