Advertisement
trodland

Partikkelsystem - ferdig kode

Apr 15th, 2020
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. import pygame, math, random
  2. pygame.init()
  3. clock = pygame.time.Clock()
  4. WIDTH = 800
  5. HEIGHT = 600
  6. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  7.  
  8. partikler = []
  9.  
  10. class Partikkel():
  11.     def __init__(self,x,y):
  12.         # x,y,fartx,farty,liv
  13.         self.x = x
  14.         self.y = y
  15.         self.fart_x = random.randint(-10,10)
  16.         self.fart_y = random.randint(-20,2)
  17.         self.liv = random.randint(6,15)
  18.         self.farge = 255
  19.    
  20.     def update(self):
  21.         self.x += self.fart_x
  22.         self.y += self.fart_y
  23.         self.fart_y += 0.5
  24.         self.liv -= 0.5
  25.         self.farge -= 7
  26.        
  27.         if self.x > WIDTH or self.x < 0:
  28.             self.fart_x *= -1
  29.        
  30.         if self.y > HEIGHT:
  31.             self.fart_y *= -1
  32.        
  33.         if self.liv < 1:
  34.             partikler.remove(self)
  35.             del self
  36.    
  37.     def draw(self):
  38.         pygame.draw.circle(screen,(255,self.farge,0),(int(self.x),int(self.y)),int(self.liv))
  39. # FERDIG MED PARTIKKELKLASSEN
  40.  
  41. def eksplosjon(mx,my):
  42.     for i in range(60):
  43.         minPartikkel = Partikkel(mx,my)
  44.         #partikler.append(minPartikkel)
  45.         partikler.insert(0,minPartikkel)
  46.  
  47. while True:
  48.    
  49.     screen.fill((0,0,0))
  50.    
  51.     for partikkel in partikler[::-1]:
  52.         partikkel.update()
  53.         partikkel.draw()
  54.  
  55.    
  56.     clock.tick(30)
  57.     pygame.display.update()
  58.    
  59.     for event in pygame.event.get():
  60.         if event.type == pygame.QUIT:
  61.             running = False
  62.         if event.type == pygame.MOUSEMOTION:
  63.             mx,my = pygame.mouse.get_pos()
  64.             eksplosjon(mx,my)
  65.                
  66. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement