Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math, random, sys, time
- import pygame
- from pygame.locals import *
- # exit the program
- def quit():
- for event in pygame.event.get():
- if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
- return True
- return False
- def threshold_table(size, steps, reverse = False, opposite = False):
- t = 2.0 * size / (steps * (steps - 1))
- a = 0
- table = []
- for i in range(steps):
- a += t * i
- if opposite: table.append(size - a)
- else: table.append(a)
- if reverse: table.reverse()
- return table
- #print threshold_table(50, 20, True, True)
- #sys.exit()
- # define display surface
- W, H = 1280, 720
- HW, HH = W / 2, H / 2
- # define some colors
- FUCHSIA = (255, 0, 255)
- PURPLE = (128, 0, 128)
- TEAL = ( 0, 128, 128)
- LIME = ( 0, 255, 0)
- GREEN = ( 0, 128, 0)
- OLIVE = (128, 128, 0)
- YELLOW = (255, 255, 0)
- ORANGE = (255, 165, 0)
- RED = (255, 0, 0)
- MAROON = (128, 0, 0)
- SILVER = (192, 192, 192)
- GRAY = (128, 128, 128)
- BLUE = ( 0, 0, 255)
- NAVY = ( 0, 0, 128)
- AQUA = ( 0, 255, 255)
- WHITE = (255, 255, 255)
- BLACK = ( 0, 0, 0)
- # the areas of the display that need updating
- AREAS = [(0, 0, W, H)]
- ENEMY_SIZE = 45
- PARTICLE_SIZE = 10
- 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]
- PARTICLE_COUNT_TABLE_INDEX = 13
- PARTICLE_SPAWN_RADIUS_PERCENT = 0.20
- PARTICLE_MIN_DISTANCE = 25
- PARTICLE_MAX_DISTANCE = 200
- PARTILCE_DISTANCE_STEP = 25
- PARTICLE_DISOLVE_STEP = 20
- PARTICLE_SHRINK_TABLE = threshold_table(PARTICLE_SIZE, PARTICLE_DISOLVE_STEP, True)
- PARTICLE_COLOR_TABLE = threshold_table(255, PARTICLE_DISOLVE_STEP, True)
- 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)]
- PARTICLE_DISTANCE_TABLE_COUNT = len(PARTICLE_DISTANCE_TABLE) - 1
- VECTOR_TABLE = [[math.cos(math.radians(degrees)), math.sin(math.radians(degrees))] for degrees in range(360)]
- class particle:
- def __init__(s, a, x, y):
- global PARTICLE_SIZE, PARTICLE_DISTANCE_TABLE_COUNT
- s.angle = a
- s.distance_index = random.randint(0, PARTICLE_DISTANCE_TABLE_COUNT)
- s.originX, s.originY = x, y
- s.x, s.y = s.originX, s.originY
- def draw(s, index):
- global DS, WHITE
- global PARTICLE_SHRINK_TABLE, PARTICLE_COLOR_TABLE
- color = (255, PARTICLE_COLOR_TABLE[index], 0)
- pygame.draw.circle(DS, color, (int(s.x), int(s.y)), int(PARTICLE_SHRINK_TABLE[index]))
- def move(s, index):
- global VECTOR_TABLE, PARTICLE_DISTANCE_TABLE
- s.x = s.originX + VECTOR_TABLE[s.angle][0] * PARTICLE_DISTANCE_TABLE[s.distance_index][index]
- s.y = s.originY + VECTOR_TABLE[s.angle][1] * PARTICLE_DISTANCE_TABLE[s.distance_index][index]
- def do(s, index):
- s.draw(index)
- s.move(index)
- class explosion:
- def __init__(s, x, y, group_radius_size):
- global WHITE
- global VECTOR_TABLE
- global PARTICLE_COUNT_TABLE, PARTICLE_COUNT_TABLE_INDEX, PARTICLE_SPAWN_RADIUS_PERCENT
- s.dead = False
- s.disolveStepCount = 0
- minimum_group_radius = group_radius_size - group_radius_size * PARTICLE_SPAWN_RADIUS_PERCENT
- s.particles = []
- for degrees in range(0, 359, PARTICLE_COUNT_TABLE[PARTICLE_COUNT_TABLE_INDEX]):
- radius = random.randint(minimum_group_radius, group_radius_size)
- px = x + VECTOR_TABLE[degrees][0] * radius
- py = y + VECTOR_TABLE[degrees][1] * radius
- s.particles.append(particle(degrees, px, py))
- def do(s):
- global PARTICLE_DISOLVE_STEP
- if s.dead: return
- for p in s.particles:
- p.do(s.disolveStepCount)
- if s.disolveStepCount < PARTICLE_DISOLVE_STEP - 1:
- s.disolveStepCount += 1
- else:
- s.dead = True
- class explosions:
- def __init__(s):
- s.container = []
- def do(s):
- delete_list = []
- for e in s.container:
- e.do()
- if e.dead:
- delete_list.append(e)
- for dead in delete_list:
- s.container.remove(dead)
- def add(s, x, y, size):
- s.container.append(explosion(x, y, size))
- # initialise display
- pygame.init()
- FONT = pygame.font.SysFont(None, 72)
- DS = pygame.display.set_mode((W, H))
- pygame.display.set_caption("Explosion")
- FPS = 120
- SPF = 1.00 / FPS
- # start FPS monitoring
- FPSTime = time.time()
- # define some in game variables
- mouse_button_status = False
- e = explosions()
- # main loop
- while not quit():
- frameTime = time.time() - FPSTime
- if frameTime < SPF: continue
- FPSTime += frameTime
- # get status of the mouse
- mx, my = pygame.mouse.get_pos()
- mb = pygame.mouse.get_pressed()
- # if the button is pressed spawn an explosion
- if mouse_button_status == False and mb[0] == 1:
- e.add(mx, my, 20)
- mouse_button_status = True
- elif mouse_button_status == True and mb[0] == 0:
- mouse_button_status = False
- # handle all the explosions
- e.do()
- pygame.display.update(AREAS)
- DS.fill(BLACK)
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement