demo_pyglet.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import pyglet
  2. import math
  3. from pyglet import shapes
  4. import random
  5. import time
  6. class _Particale():
  7. def __init__(self,x,y,xvel,yvel,radius,color):
  8. self.x = x
  9. self.y = y
  10. self.xvel = xvel
  11. self.yvel = yvel
  12. self.radius = radius
  13. self.color = color
  14. self.time = time.time()
  15. self.start = time.time()
  16. self.start2 = random.randint(1,20)/10.
  17. if self.start2 > 1.8:
  18. self.start2 += random.randint(1,20)/10.
  19. self.colors = [(255,255,0),(255,210,0),(255,90,0)]
  20. self.color = random.choice(self.colors)
  21. def draw(self,win):
  22. if time.time() > self.time+0.05:
  23. self.x += self.xvel
  24. self.y += self.yvel
  25. self.time = time.time()
  26. if self.start+self.start2 < time.time():
  27. self.radius -= 0.1
  28. #if time.time() > self.time+0.2:
  29. #pygame.draw.circle(win, color, (int(self.x),int(self.y)),self.radius)
  30. color = self.color
  31. x= round(self.x)
  32. y= round(self.y)
  33. r = round(self.radius)
  34. if len(color) == 3:
  35. color = list(color)
  36. color.append(0)
  37. #pygame.gfxdraw.filled_circle(win, x,y ,r,color )#[0,0,255])
  38. #pygame.gfxdraw.aacircle(win, x,y ,r,color )#[0,0,255])
  39. r = round(r)
  40. #img3 = img2.copy()
  41. #img3 = colorize(img2, color ) #(0, 0, 255,15) )
  42. #img3 = colorize(img2,(255, 120, 255,15) )
  43. #img3 = colorize(img2,color )
  44. #img3 = pygame.transform.scale(img3, (r, r))
  45. #player_rect3 = img3.get_rect(center=(x,y))
  46. #window.blit(img3, player_rect3)
  47. if r > 0:
  48. batch1 = pyglet.graphics.Batch()
  49. circle = shapes.Circle(x,y, r+5, color=(50, 225, 30), batch=batch1)
  50. batch1.draw()
  51. #print("ok")
  52. return [x,0,y,0,color]
  53. class Particales():
  54. def __init__(self):
  55. self.data = []
  56. def add(self,x,y):
  57. for z in range(random.randint(1,1)):
  58. s = 10
  59. xvel = random.randint(0,s) -(s/2)
  60. yvel = random.randint(0,s) -(s/2)
  61. r = random.randint(1,2)
  62. p = _Particale(x ,y ,xvel ,yvel,r,(255,255,255))
  63. self.data.append(p)
  64. def draw(self,win=None):
  65. rem = []
  66. for p in self.data:
  67. p.draw(win)
  68. if p.radius <= 0:
  69. rem.append(p)
  70. for p in rem:
  71. self.data.remove(p)
  72. particales = Particales()
  73. class Planet():
  74. def __init__(self,x,y,ang=0):
  75. self._pos_center = (x,y)
  76. self._quadrant = 0
  77. self._ang = ang
  78. self._ang_dir = 1
  79. self._r = 2 #
  80. self._orbit = 60 # orbit,umlaufbahn
  81. self._color_org = [255,255,0]
  82. self._color = [0,255,0]
  83. self._x=0
  84. self._y=0
  85. self._ix = 0
  86. self._iy = 0
  87. def rotate(self):
  88. q = 0
  89. if self._ang_dir:
  90. self._ang += 2 # degree
  91. else:
  92. self._ang -= 1 # degree
  93. if self._ang >= 360:
  94. self._ang = 0 #self._ang -360
  95. elif self._ang < 0:
  96. self._ang = 360
  97. ang = self._ang
  98. self._quadrant = ang//90
  99. ang -= self._quadrant * 90
  100. self._ix = math.sin(math.radians(ang))*self._orbit
  101. self._iy = math.sqrt(self._orbit**2 - self._ix**2)
  102. y = self._iy
  103. x = self._ix
  104. if self._quadrant == 1:
  105. self._iy = -x
  106. self._ix = y
  107. elif self._quadrant == 2:
  108. self._iy = -y
  109. self._ix = -x
  110. elif self._quadrant == 3:
  111. self._iy = x
  112. self._ix = -y
  113. def draw(self,x,y):
  114. self._pos_center = (x,y)
  115. self.rotate()
  116. self._x = int(self._pos_center[0] + self._ix)
  117. self._y = int(self._pos_center[1] + self._iy)
  118. if self._ang > 300:
  119. f = (self._ang -300) / 60
  120. f = 1-f
  121. rgb = self._color_org # = [255,255,0]
  122. self._color = [ int(rgb[0]*f) , int(rgb[1]*f) ,int(rgb[2]*f) ]
  123. elif self._ang < 60:
  124. f = self._ang / 60
  125. rgb = self._color_org # = [255,255,0]
  126. self._color = [ int(rgb[0]*f) , int(rgb[1]*f) ,int(rgb[2]*f) ]
  127. #print("ang {} {} {:3} {:3} {}".format( self._ang,self._quadrant,self._x,self._y,self._color))
  128. #print(self,"Q:",int(self._quadrant),self._ang)
  129. return (self._x,self._y,self._color)
  130. class Animation():
  131. def __init__(self,x=20,y=20,speed=1,_dir=1):
  132. self.pos_x=x
  133. self.pos_x_dir = 1
  134. self.pos_y=y
  135. self.pos_y_dir = 1
  136. self.r = 7
  137. self.r_dir = 1
  138. self.speed = speed
  139. self.ang = 0
  140. self.ix=0
  141. self.iy=0
  142. self.planetes = []
  143. a = 360
  144. d = 3
  145. for i in range(d+1):
  146. i=i+1
  147. p = Flow(self.pos_x,self.pos_y,ang=a/d*i)
  148. p._ang_dir = _dir
  149. self.planetes.append(p)
  150. def rotate(self):
  151. self.ix = math.sin(math.radians(0))*self.r
  152. self.iy = math.sqrt(self.r**2 - self.ix**2)
  153. self.ang+=1
  154. if self.ang >= 360:
  155. self.ang = 0
  156. def draw(self,color=[255,255,255,255]):
  157. self.rotate()
  158. #pixel_array = pygame.PixelArray(window)
  159. pixel_array = {}
  160. self.color = [255,255,255,255] #pygame.Color(color[0],color[1],color[2],color[3])
  161. x=self.pos_x
  162. y=self.pos_y
  163. for i,planet in enumerate(self.planetes):
  164. px,py,pcolor = planet.draw(x,y)
  165. k = "{}.{}:{},{}:{}".format(i,px,px+10,py,py+10)
  166. pixel_array[k] = (px,px,py,py , pcolor )
  167. if self.pos_x > 300:
  168. self.pos_x_dir = 0
  169. if self.pos_x <= self.speed:
  170. self.pos_x_dir = 1
  171. if self.pos_x_dir:
  172. self.pos_x += self.speed
  173. else:
  174. self.pos_x -= self.speed
  175. if self.r > 20:
  176. self.r_dir = 0
  177. if self.r <=7:
  178. self.r_dir = 1
  179. if self.r_dir:
  180. self.r+=1
  181. else:
  182. self.r-=1
  183. return pixel_array
  184. class Gobo1():
  185. def __init__(self,x=20,y=20,speed=1,_dir=1):
  186. self.pos_x=x
  187. self.pos_x_dir = 1
  188. self.pos_y=y
  189. self.pos_y_dir = 1
  190. self.r = 17
  191. self.r_dir = 1
  192. self.speed = speed
  193. self.ang = 0
  194. self.ix=0
  195. self.iy=0
  196. self.planetes = []
  197. a = 360
  198. d = 3
  199. for i in range(d+1):
  200. i=i+1
  201. p = Planet(self.pos_x,self.pos_y,ang=a/d*i)
  202. p._ang_dir = _dir
  203. self.planetes.append(p)
  204. def rotate(self):
  205. self.ix = math.sin(math.radians(0))*self.r
  206. self.iy = math.sqrt(self.r**2 - self.ix**2)
  207. self.ang+=1
  208. if self.ang >= 360:
  209. self.ang = 0
  210. def draw(self,color=[255,255,255]):
  211. self.rotate()
  212. #pixel_array = pygame.PixelArray(window)
  213. pixel_array = {}
  214. self.color = [255,255,255,255] #pygame.Color(color[0],color[1],color[2])
  215. x=self.pos_x
  216. y=self.pos_y
  217. for i,planet in enumerate(self.planetes):
  218. px,py,pcolor = planet.draw(x,y)
  219. k = "{}.{}:{},{}:{}".format(i,px,px+10,py,py+10)
  220. pixel_array[k] = (px,px,py,py , pcolor )
  221. if self.pos_x > 1600:
  222. self.pos_x_dir = 0
  223. if self.pos_x <= self.speed:
  224. self.pos_x_dir = 1
  225. if self.pos_x_dir:
  226. self.pos_x += self.speed
  227. else:
  228. self.pos_x -= self.speed
  229. if self.r > 20:
  230. self.r_dir = 0
  231. if self.r <=7:
  232. self.r_dir = 1
  233. if self.r_dir:
  234. self.r+=1
  235. else:
  236. self.r-=1
  237. return pixel_array
  238. def display_get_width():
  239. display = pyglet.canvas.get_display()
  240. screen = display.get_default_screen()
  241. return screen.width
  242. def display_get_height():
  243. display = pyglet.canvas.get_display()
  244. screen = display.get_default_screen()
  245. return screen.height
  246. sw = display_get_width()
  247. sh = display_get_height()
  248. print(sw, sh)
  249. window = pyglet.window.Window(sw//2,sh//2)
  250. gobo1 = Gobo1()
  251. def loop(event=None):
  252. #print(event)
  253. batch = pyglet.graphics.Batch()
  254. x = 100
  255. y = 100
  256. rectangle = shapes.BorderedRectangle(x, y, y+100, y+100, border=1, color=(255, 255, 255), border_color=(100, 100, 100), batch=batch)
  257. rectangle2 = shapes.BorderedRectangle(x, y , x+100,y+100, border=1, color=(255, 255, 255), border_color=(100, 100, 100), batch=batch)
  258. circle = shapes.Circle(x,y, 100, color=(50, 225, 30), batch=batch)
  259. window.clear()
  260. batch.draw()
  261. batch1 = pyglet.graphics.Batch()
  262. d1 = gobo1.draw()
  263. for k in d1:
  264. i = d1[k]
  265. #print("i",i)
  266. x=i[0] +200
  267. y=i[2] +200
  268. circle = shapes.Circle(x,y, 10, color=(50, 225, 30), batch=batch1)
  269. particales.add(x,y)
  270. batch1.draw()
  271. particales.draw()
  272. #print("ok")
  273. @window.event
  274. def on_draw():
  275. loop()
  276. pyglet.clock.schedule_interval(loop, 0.001)
  277. pyglet.app.run()