Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def convert(value, min1, max1, min2, max2):
- # Insère une valeur comprise entre deux bornes et on renvoie une valeur proportionnelle à l'ancien rapport mais sur 2 nouvelles bornes
- proportion = (value - min1) / max1
- return proportion * (max2 - min2) + min2
- def colision(rect1, rect2):
- # On teste si deux rectangles sont en contact
- # Pour cela, on demande, en entrer, 2 Liste ou tuple de la forme : (x, y, dimx, dimy)
- # Renvoie True si il y a contact et False si il n'y a rien
- 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])}
- 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])}
- if rect1[2] < rect2[2]:
- for pts in corner1:
- x, y = corner1.get(pts)
- if x >= rect2[0] and x <= rect2[0] + rect2[2] and y >= rect2[1] and y <= rect2[1] + rect2[3]:
- return True
- else:
- for pts in corner2:
- x, y = corner2.get(pts)
- if x >= rect1[0] and x <= rect1[0] + rect1[2] and y >= rect1[1] and y <= rect1[1] + rect1[3]:
- return True
- if rect1[3] < rect2[3]:
- for pts in corner1:
- x, y = corner1.get(pts)
- if x >= rect2[0] and x <= rect2[0] + rect2[2] and y >= rect2[1] and y <= rect2[1] + rect2[3]:
- return True
- else:
- for pts in corner2:
- x, y = corner2.get(pts)
- if x >= rect1[0] and x <= rect1[0] + rect1[2] and y >= rect1[1] and y <= rect1[1] + rect1[3]:
- return True
- return False
- class particule():
- # Classe permetant de créer des particules avec différents paramètres
- # 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
- def __init__(self, pos, color, variation, size, ecart, dir='rdm'):
- # 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)
- # La taille initiale de la particule, l'écart de taille qui peut y avoir (0 = pas d'écart) et une direction (tuple) optionel.
- self.x = pos[0]
- self.y = pos[1]
- if variation:
- self.color = particule.color(color)
- else:
- self.color = color
- if ecart:
- self.rad = rdm.randint(size - ecart, size + ecart)
- else:
- self.rad = size
- if dir == 'rdm':
- self.dir = (rdm.randint(-5, 5), rdm.randint(-5, 5))
- else:
- self.dir = dir
- def move(self):
- # On déplace les coordonnées de la particule en fonction de la direction
- self.x += self.dir[0]
- self.y += self.dir[1]
- def reduct(self,c):
- # On résuit la taille de la particule, si celle-ci est égale à 0, on la supprime pour éviter trop de calculs inutiles
- self.rad -= 1
- if self.rad <= 0:
- c.remove(self)
- return c
- def color(c):
- # On définie la coleur de la particule de manière aléatoire
- # Cette fonction est appelée lors de l'initialisation de l'objet
- amax = []
- amin = []
- for i in range(3):
- if c[i] + 20 > 255:
- amax.append(255)
- amin.append(235)
- elif c[i] - 20 < 0:
- amax.append(20)
- amin.append(0)
- else:
- amax.append(c[i] + 20)
- amin.append(c[i] - 20)
- color = (rdm.randint(amin[0], amax[0]), rdm.randint(amin[1], amax[1]), rdm.randint(amin[2], amax[2]))
- return color
- def draw(self):
- # Affiche la particule à l'écran
- pygame.draw.circle(fen1, self.color, (self.x, self.y), self.rad)
- def loop(lst):
- # Boucle de toutes les particules
- for part in lst:
- particule.move(part)
- particule.draw(part)
- lst = particule.reduct(part, lst)
- return lst
- class pbar():
- # barre de progression avec changement de couleur
- def __init__(self, maxv, x, y, dim, colorshape, color1, color2, color3):
- # On donne un valeur maximale, des coordonnées de position,, une dimension (tuple ou liste), une couleur pour le cadre, et 3 couleurs pour
- self.maxvalue = maxv
- self.value = maxv
- self.pourcentage = int(self.value / self.maxvalue) * 100
- self.x = x
- self.y = y
- self.dim = dim
- self.shapecolor = colorshape
- self.gclr = color1 #good color
- self.bclr = color2 #bad color
- self.lclr = color3 #lose color
- self.color = self.gclr
- self.direction = pbar.deterorient(self)
- def valuedown(self,nb):
- if self.value + nb > nb:
- self.value -= nb
- else:
- self.value = 0
- def setvalue(self,nb):
- if nb < 0:
- self.value = 0
- else:
- self.value = nb
- def deterorient(self):
- if self.dim[0] > self.dim[1]:
- return 'horizontal'
- elif self.dim[0] == self.dim[1]:
- return 'square'
- else:
- return 'vertical'
- def calcpourcentage(self):
- self.pourcentage = int((self.value/self.maxvalue)*100)
- def testmax(self):
- if self.value == self.maxvalue:
- return True
- else:
- return False
- def colorchoice(self):
- pbar.calcpourcentage(self)
- R = (self.pourcentage*self.gclr[0] + abs(100-self.pourcentage)*self.bclr[0])/100
- G = (self.pourcentage*self.gclr[1] + abs(100-self.pourcentage)*self.bclr[1])/100
- B = (self.pourcentage*self.gclr[2] + abs(100-self.pourcentage)*self.bclr[2])/100
- self.color = (R,G,B)
- if self.pourcentage == 0:
- self.color = self.lclr
- def valueup(self,nb):
- if self.value + nb <= self.maxvalue:
- self.value += nb
- else:
- self.value = self.maxvalue
- def draw(self):
- pbar.colorchoice(self)
- pygame.draw.rect(fen1,self.shapecolor,((self.x,self.y),self.dim))
- if self.direction == 'horizontal':
- valdim = (int(((self.dim[0]-6)*self.value)/self.maxvalue),self.dim[1]-6)
- x = self.x + 3
- pygame.draw.rect(fen1,self.color,((x,self.y+3),(valdim)))
- else:
- valdim = (self.dim[0]-6,int(((self.dim[1]-6)*self.value)/self.maxvalue))
- end = self.y + self.dim[1] - 3
- y = end - valdim[1]
- pygame.draw.rect(fen1,self.color,((self.x+3,y),(valdim)))
- class scrollbar():
- def __init__(self,x,y,dim,screen,minv=0,maxv=255,color=(50,50,50),colorb=(255,127,0),diff=(0,0),initval=0):
- self.x = x
- self.y = y
- self.dim = dim
- self.pressed = False
- self.color = color
- 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]
- self.borne = (minv,maxv)
- self.value = initval
- self.screen = screen
- self.diff = diff
- def blit(self):
- pygame.draw.rect(self.screen,self.color,((self.x,self.y),self.dim))
- pygame.draw.circle(self.screen,self.button[2],(self.button[0][0],self.button[0][1]),(int(self.button[1]/2)+5))
- def contact(self):
- if pygame.mouse.get_pressed()[0]:
- x,y = pygame.mouse.get_pos()
- x -= self.diff[0]
- y -= self.diff[1]
- 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:
- self.pressed = True
- if x >= self.x and x <= self.x + self.dim[0]:
- self.button[0][0] = x
- self.value = convert(x,self.x,self.dim[0],self.borne[0],self.borne[1])
- else:
- self.pressed = False
- def get_value(self):
- return self.value
- def loop(self):
- scrollbar.contact(self)
- scrollbar.blit(self)
- class InputBox():
- def __init__(self, x, y, dim, screen, diff=(0,0), policesize=20, policename="Impact"):
- self.x = x
- self.y = y
- self.dim = dim
- self.printScrn = screen
- self.diff = diff
- self.screen = pygame.surface.Surface(self.dim)
- self.text = ""
- self.font = pygame.font.SysFont(policename, policesize)
- self.typing = False
- def blit(self):
- text = self.font.render(self.text, False, (255,255,255))
- self.screen.fill((0,0,0))
- pygame.draw.rect(self.screen, (50, 50, 50), ((0, 0), self.dim), 1)
- self.screen.blit(text, (2,2))
- self.printScrn.blit(self.screen, (self.x, self.y))
- def addText(self, events):
- for event in events:
- if event.type == KEYDOWN:
- if event.key == K_RETURN:
- self.typing = False
- else:
- if self.typing:
- if event.key == K_BACKSPACE:
- try:
- self.text = self.text[:-1]
- except:
- pass
- else:
- self.text += event.unicode
- def get_text(self):
- return self.text
- def loop(self, events):
- InputBox.addText(self, events)
- InputBox.blit(self)
- for event in events:
- if event.type == MOUSEBUTTONDOWN:
- if event.button == 1:
- x,y = event.pos[0] - self.diff[0], event.pos[1] - self.diff[1]
- if x >= self.x and x <= self.x + self.dim[0] and y >= self.y and y <= self.y + self.dim[1]:
- self.typing = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement