Advertisement
cookertron

Python Pygame Explosion

Sep 11th, 2019
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. import math, random, sys, time
  2. import pygame
  3. from pygame.locals import *
  4.  
  5. # exit the program
  6. def quit():
  7.     for event in pygame.event.get():
  8.         if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
  9.             return True
  10.     return False
  11.  
  12. def threshold_table(size, steps, reverse = False, opposite = False):
  13.     t = 2.0 * size / (steps * (steps - 1))
  14.     a = 0
  15.     table = []
  16.     for i in range(steps):
  17.         a += t * i
  18.         if opposite: table.append(size - a)
  19.         else: table.append(a)
  20.     if reverse: table.reverse()
  21.     return table
  22.  
  23. #print threshold_table(50, 20, True, True)
  24. #sys.exit()
  25.  
  26. # define display surface           
  27. W, H = 1280, 720
  28. HW, HH = W / 2, H / 2
  29.  
  30.  
  31. # define some colors
  32. FUCHSIA = (255,   0, 255)
  33. PURPLE  = (128,   0, 128)
  34. TEAL    = (  0, 128, 128)
  35. LIME    = (  0, 255,   0)
  36. GREEN   = (  0, 128,   0)
  37. OLIVE   = (128, 128,   0)
  38. YELLOW  = (255, 255,   0)
  39. ORANGE  = (255, 165,   0)
  40. RED     = (255,   0,   0)
  41. MAROON  = (128,   0,   0)
  42. SILVER  = (192, 192, 192)
  43. GRAY    = (128, 128, 128)
  44. BLUE    = (  0,   0, 255)
  45. NAVY    = (  0,   0, 128)
  46. AQUA    = (  0, 255, 255)
  47. WHITE   = (255, 255, 255)
  48. BLACK   = (  0,   0,   0)
  49.  
  50. # the areas of the display that need updating
  51. AREAS = [(0, 0, W, H)]
  52.  
  53. ENEMY_SIZE = 45
  54. PARTICLE_SIZE = 10
  55.  
  56. PARTICLE_COUNT_TABLE = [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 18, 20, 24, 30, 36, 40, 45, 60, 72, 90, 120, 180, 360]
  57. PARTICLE_COUNT_TABLE_INDEX = 13
  58. PARTICLE_SPAWN_RADIUS_PERCENT = 0.20
  59.  
  60. PARTICLE_MIN_DISTANCE = 25
  61. PARTICLE_MAX_DISTANCE = 200
  62. PARTILCE_DISTANCE_STEP = 25
  63. PARTICLE_DISOLVE_STEP = 20
  64.  
  65. PARTICLE_SHRINK_TABLE = threshold_table(PARTICLE_SIZE, PARTICLE_DISOLVE_STEP, True)
  66. PARTICLE_COLOR_TABLE = threshold_table(255, PARTICLE_DISOLVE_STEP, True)
  67.  
  68. PARTICLE_DISTANCE_TABLE = [threshold_table(particleDistance, PARTICLE_DISOLVE_STEP, True, True) for particleDistance in range(PARTICLE_MIN_DISTANCE, PARTICLE_MAX_DISTANCE + PARTILCE_DISTANCE_STEP, PARTILCE_DISTANCE_STEP)]
  69. PARTICLE_DISTANCE_TABLE_COUNT = len(PARTICLE_DISTANCE_TABLE) - 1
  70.  
  71. VECTOR_TABLE = [[math.cos(math.radians(degrees)), math.sin(math.radians(degrees))] for degrees in range(360)]
  72.  
  73. class particle:
  74.     def __init__(s, a, x, y):
  75.         global PARTICLE_SIZE, PARTICLE_DISTANCE_TABLE_COUNT
  76.  
  77.         s.angle = a
  78.         s.distance_index = random.randint(0, PARTICLE_DISTANCE_TABLE_COUNT)
  79.  
  80.         s.originX, s.originY = x, y
  81.         s.x, s.y = s.originX, s.originY
  82.  
  83.     def draw(s, index):
  84.         global DS, WHITE
  85.         global PARTICLE_SHRINK_TABLE, PARTICLE_COLOR_TABLE
  86.         color = (255, PARTICLE_COLOR_TABLE[index], 0)
  87.         pygame.draw.circle(DS, color, (int(s.x), int(s.y)), int(PARTICLE_SHRINK_TABLE[index]))
  88.  
  89.     def move(s, index):
  90.         global VECTOR_TABLE, PARTICLE_DISTANCE_TABLE
  91.         s.x = s.originX + VECTOR_TABLE[s.angle][0] * PARTICLE_DISTANCE_TABLE[s.distance_index][index]
  92.         s.y = s.originY + VECTOR_TABLE[s.angle][1] * PARTICLE_DISTANCE_TABLE[s.distance_index][index]
  93.  
  94.     def do(s, index):
  95.          s.draw(index)
  96.          s.move(index)
  97.  
  98. class explosion:
  99.     def __init__(s, x, y, group_radius_size):
  100.         global WHITE
  101.         global VECTOR_TABLE
  102.         global PARTICLE_COUNT_TABLE, PARTICLE_COUNT_TABLE_INDEX, PARTICLE_SPAWN_RADIUS_PERCENT
  103.  
  104.         s.dead = False
  105.        
  106.         s.disolveStepCount = 0
  107.  
  108.         minimum_group_radius = group_radius_size - group_radius_size * PARTICLE_SPAWN_RADIUS_PERCENT
  109.         s.particles = []
  110.         for degrees in range(0, 359, PARTICLE_COUNT_TABLE[PARTICLE_COUNT_TABLE_INDEX]):
  111.             radius = random.randint(minimum_group_radius, group_radius_size)
  112.             px = x + VECTOR_TABLE[degrees][0] * radius
  113.             py = y + VECTOR_TABLE[degrees][1] * radius
  114.             s.particles.append(particle(degrees, px, py))
  115.  
  116.     def do(s):
  117.         global PARTICLE_DISOLVE_STEP
  118.         if s.dead: return
  119.  
  120.         for p in s.particles:
  121.             p.do(s.disolveStepCount)
  122.        
  123.         if s.disolveStepCount < PARTICLE_DISOLVE_STEP - 1:
  124.             s.disolveStepCount += 1
  125.         else:
  126.             s.dead = True
  127.  
  128. class explosions:
  129.     def __init__(s):
  130.         s.container = []
  131.    
  132.     def do(s):
  133.         delete_list = []
  134.         for e in s.container:
  135.             e.do()
  136.             if e.dead:
  137.                 delete_list.append(e)
  138.         for dead in delete_list:
  139.             s.container.remove(dead)
  140.  
  141.     def add(s, x, y, size):
  142.         s.container.append(explosion(x, y, size))
  143.  
  144. # initialise display
  145. pygame.init()
  146. FONT = pygame.font.SysFont(None, 72)
  147. DS = pygame.display.set_mode((W, H))
  148. pygame.display.set_caption("Explosion")
  149. FPS = 120
  150. SPF = 1.00 / FPS
  151. # start FPS monitoring
  152. FPSTime = time.time()
  153.  
  154. # define some in game variables
  155. mouse_button_status = False
  156. e = explosions()
  157.  
  158. # main loop
  159. while not quit():
  160.     frameTime = time.time() - FPSTime
  161.     if frameTime < SPF: continue
  162.     FPSTime += frameTime
  163.  
  164.     # get status of the mouse
  165.     mx, my = pygame.mouse.get_pos()
  166.     mb = pygame.mouse.get_pressed()
  167.  
  168.     # if the button is pressed spawn an explosion
  169.     if mouse_button_status == False and mb[0] == 1:
  170.         e.add(mx, my, 20)
  171.         mouse_button_status = True
  172.     elif mouse_button_status == True and mb[0] == 0:
  173.         mouse_button_status = False
  174.    
  175.     # handle all the explosions
  176.     e.do()
  177.  
  178.     pygame.display.update(AREAS)
  179.     DS.fill(BLACK)
  180. pygame.quit()
  181. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement