Advertisement
Guest User

classu.py

a guest
May 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.01 KB | None | 0 0
  1. def convert(value, min1, max1, min2, max2):
  2. # Insère une valeur comprise entre deux bornes et on renvoie une valeur proportionnelle à l'ancien rapport mais sur 2 nouvelles bornes
  3. proportion = (value - min1) / max1
  4. return proportion * (max2 - min2) + min2
  5.  
  6. def colision(rect1, rect2):
  7. # On teste si deux rectangles sont en contact
  8. # Pour cela, on demande, en entrer, 2 Liste ou tuple de la forme : (x, y, dimx, dimy)
  9. # Renvoie True si il y a contact et False si il n'y a rien
  10.  
  11. corner1 = {1:(rect1[0], rect1[1]), 2:(rect1[0], rect1[1] + rect1[3]), 3:(rect1[0] + rect1[2], rect1[1]), 4:(rect1[0] + rect1[2], rect1[1] + rect1[3])}
  12. corner2 = {1:(rect2[0], rect2[1]), 2:(rect2[0], rect2[1] + rect2[3]), 3:(rect2[0] + rect2[2], rect2[1]), 4:(rect2[0] + rect2[2], rect2[1] + rect2[3])}
  13.  
  14. if rect1[2] < rect2[2]:
  15. for pts in corner1:
  16. x, y = corner1.get(pts)
  17. if x >= rect2[0] and x <= rect2[0] + rect2[2] and y >= rect2[1] and y <= rect2[1] + rect2[3]:
  18. return True
  19.  
  20. else:
  21. for pts in corner2:
  22. x, y = corner2.get(pts)
  23. if x >= rect1[0] and x <= rect1[0] + rect1[2] and y >= rect1[1] and y <= rect1[1] + rect1[3]:
  24. return True
  25.  
  26. if rect1[3] < rect2[3]:
  27. for pts in corner1:
  28. x, y = corner1.get(pts)
  29. if x >= rect2[0] and x <= rect2[0] + rect2[2] and y >= rect2[1] and y <= rect2[1] + rect2[3]:
  30. return True
  31. else:
  32. for pts in corner2:
  33. x, y = corner2.get(pts)
  34. if x >= rect1[0] and x <= rect1[0] + rect1[2] and y >= rect1[1] and y <= rect1[1] + rect1[3]:
  35. return True
  36.  
  37. return False
  38.  
  39. class particule():
  40. # Classe permetant de créer des particules avec différents paramètres
  41. # Les particules possèdent une couleur, une taille, une direction et elles réduisent de taille au fûr et à mesure de leur temps de vie
  42.  
  43. def __init__(self, pos, color, variation, size, ecart, dir='rdm'):
  44. # Lors de la création de l'objet, l'utilisateur doit entrer, des positions (tuple ou liste), une couleur (tuple ou liste), si il souhaite ou non avoir des variations (boolean)
  45. # La taille initiale de la particule, l'écart de taille qui peut y avoir (0 = pas d'écart) et une direction (tuple) optionel.
  46.  
  47. self.x = pos[0]
  48. self.y = pos[1]
  49.  
  50. if variation:
  51. self.color = particule.color(color)
  52. else:
  53. self.color = color
  54.  
  55. if ecart:
  56. self.rad = rdm.randint(size - ecart, size + ecart)
  57. else:
  58. self.rad = size
  59.  
  60. if dir == 'rdm':
  61. self.dir = (rdm.randint(-5, 5), rdm.randint(-5, 5))
  62. else:
  63. self.dir = dir
  64.  
  65. def move(self):
  66. # On déplace les coordonnées de la particule en fonction de la direction
  67. self.x += self.dir[0]
  68. self.y += self.dir[1]
  69.  
  70. def reduct(self,c):
  71. # On résuit la taille de la particule, si celle-ci est égale à 0, on la supprime pour éviter trop de calculs inutiles
  72. self.rad -= 1
  73.  
  74. if self.rad <= 0:
  75. c.remove(self)
  76.  
  77. return c
  78.  
  79. def color(c):
  80. # On définie la coleur de la particule de manière aléatoire
  81. # Cette fonction est appelée lors de l'initialisation de l'objet
  82.  
  83. amax = []
  84. amin = []
  85.  
  86. for i in range(3):
  87. if c[i] + 20 > 255:
  88. amax.append(255)
  89. amin.append(235)
  90. elif c[i] - 20 < 0:
  91. amax.append(20)
  92. amin.append(0)
  93. else:
  94. amax.append(c[i] + 20)
  95. amin.append(c[i] - 20)
  96.  
  97. color = (rdm.randint(amin[0], amax[0]), rdm.randint(amin[1], amax[1]), rdm.randint(amin[2], amax[2]))
  98. return color
  99.  
  100. def draw(self):
  101. # Affiche la particule à l'écran
  102. pygame.draw.circle(fen1, self.color, (self.x, self.y), self.rad)
  103.  
  104. def loop(lst):
  105. # Boucle de toutes les particules
  106.  
  107. for part in lst:
  108. particule.move(part)
  109. particule.draw(part)
  110. lst = particule.reduct(part, lst)
  111.  
  112. return lst
  113.  
  114. class pbar():
  115. # barre de progression avec changement de couleur
  116.  
  117. def __init__(self, maxv, x, y, dim, colorshape, color1, color2, color3):
  118. # On donne un valeur maximale, des coordonnées de position,, une dimension (tuple ou liste), une couleur pour le cadre, et 3 couleurs pour
  119.  
  120. self.maxvalue = maxv
  121. self.value = maxv
  122. self.pourcentage = int(self.value / self.maxvalue) * 100
  123. self.x = x
  124. self.y = y
  125. self.dim = dim
  126. self.shapecolor = colorshape
  127. self.gclr = color1 #good color
  128. self.bclr = color2 #bad color
  129. self.lclr = color3 #lose color
  130. self.color = self.gclr
  131. self.direction = pbar.deterorient(self)
  132.  
  133. def valuedown(self,nb):
  134. if self.value + nb > nb:
  135. self.value -= nb
  136. else:
  137. self.value = 0
  138.  
  139. def setvalue(self,nb):
  140. if nb < 0:
  141. self.value = 0
  142. else:
  143. self.value = nb
  144.  
  145. def deterorient(self):
  146. if self.dim[0] > self.dim[1]:
  147. return 'horizontal'
  148. elif self.dim[0] == self.dim[1]:
  149. return 'square'
  150. else:
  151. return 'vertical'
  152.  
  153. def calcpourcentage(self):
  154. self.pourcentage = int((self.value/self.maxvalue)*100)
  155.  
  156. def testmax(self):
  157. if self.value == self.maxvalue:
  158. return True
  159. else:
  160. return False
  161.  
  162. def colorchoice(self):
  163. pbar.calcpourcentage(self)
  164.  
  165. R = (self.pourcentage*self.gclr[0] + abs(100-self.pourcentage)*self.bclr[0])/100
  166. G = (self.pourcentage*self.gclr[1] + abs(100-self.pourcentage)*self.bclr[1])/100
  167. B = (self.pourcentage*self.gclr[2] + abs(100-self.pourcentage)*self.bclr[2])/100
  168.  
  169. self.color = (R,G,B)
  170.  
  171. if self.pourcentage == 0:
  172. self.color = self.lclr
  173.  
  174. def valueup(self,nb):
  175. if self.value + nb <= self.maxvalue:
  176. self.value += nb
  177. else:
  178. self.value = self.maxvalue
  179.  
  180. def draw(self):
  181. pbar.colorchoice(self)
  182. pygame.draw.rect(fen1,self.shapecolor,((self.x,self.y),self.dim))
  183.  
  184. if self.direction == 'horizontal':
  185. valdim = (int(((self.dim[0]-6)*self.value)/self.maxvalue),self.dim[1]-6)
  186. x = self.x + 3
  187. pygame.draw.rect(fen1,self.color,((x,self.y+3),(valdim)))
  188. else:
  189. valdim = (self.dim[0]-6,int(((self.dim[1]-6)*self.value)/self.maxvalue))
  190. end = self.y + self.dim[1] - 3
  191. y = end - valdim[1]
  192. pygame.draw.rect(fen1,self.color,((self.x+3,y),(valdim)))
  193.  
  194. class scrollbar():
  195.  
  196. def __init__(self,x,y,dim,screen,minv=0,maxv=255,color=(50,50,50),colorb=(255,127,0),diff=(0,0),initval=0):
  197. self.x = x
  198. self.y = y
  199. self.dim = dim
  200. self.pressed = False
  201. self.color = color
  202. self.button = [[int(convert(initval, minv, maxv, self.x, self.x + self.dim[0])), self.y+int(self.dim[1]/2)], self.dim[1], colorb]
  203. self.borne = (minv,maxv)
  204. self.value = initval
  205. self.screen = screen
  206. self.diff = diff
  207.  
  208. def blit(self):
  209. pygame.draw.rect(self.screen,self.color,((self.x,self.y),self.dim))
  210. pygame.draw.circle(self.screen,self.button[2],(self.button[0][0],self.button[0][1]),(int(self.button[1]/2)+5))
  211.  
  212. def contact(self):
  213. if pygame.mouse.get_pressed()[0]:
  214. x,y = pygame.mouse.get_pos()
  215. x -= self.diff[0]
  216. y -= self.diff[1]
  217.  
  218. if (x >= self.button[0][0] - self.button[1] and x <= self.button[0][0] + self.button[1] and y >= self.button[0][1] and y <= self.button[0][1] + self.button[1]) or self.pressed:
  219. self.pressed = True
  220. if x >= self.x and x <= self.x + self.dim[0]:
  221. self.button[0][0] = x
  222. self.value = convert(x,self.x,self.dim[0],self.borne[0],self.borne[1])
  223. else:
  224. self.pressed = False
  225.  
  226. def get_value(self):
  227. return self.value
  228.  
  229.  
  230. def loop(self):
  231. scrollbar.contact(self)
  232. scrollbar.blit(self)
  233.  
  234. class InputBox():
  235.  
  236. def __init__(self, x, y, dim, screen, diff=(0,0), policesize=20, policename="Impact"):
  237. self.x = x
  238. self.y = y
  239. self.dim = dim
  240. self.printScrn = screen
  241. self.diff = diff
  242. self.screen = pygame.surface.Surface(self.dim)
  243. self.text = ""
  244. self.font = pygame.font.SysFont(policename, policesize)
  245. self.typing = False
  246.  
  247. def blit(self):
  248. text = self.font.render(self.text, False, (255,255,255))
  249. self.screen.fill((0,0,0))
  250.  
  251. pygame.draw.rect(self.screen, (50, 50, 50), ((0, 0), self.dim), 1)
  252. self.screen.blit(text, (2,2))
  253. self.printScrn.blit(self.screen, (self.x, self.y))
  254.  
  255. def addText(self, events):
  256. for event in events:
  257. if event.type == KEYDOWN:
  258. if event.key == K_RETURN:
  259. self.typing = False
  260. else:
  261. if self.typing:
  262. if event.key == K_BACKSPACE:
  263. try:
  264. self.text = self.text[:-1]
  265. except:
  266. pass
  267. else:
  268. self.text += event.unicode
  269.  
  270. def get_text(self):
  271. return self.text
  272.  
  273. def loop(self, events):
  274. InputBox.addText(self, events)
  275. InputBox.blit(self)
  276.  
  277. for event in events:
  278. if event.type == MOUSEBUTTONDOWN:
  279. if event.button == 1:
  280. x,y = event.pos[0] - self.diff[0], event.pos[1] - self.diff[1]
  281. if x >= self.x and x <= self.x + self.dim[0] and y >= self.y and y <= self.y + self.dim[1]:
  282. self.typing = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement