Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import pygame.gfxdraw
- import time
- import random
- import math
- W, H = 1280, 720
- HW, HH = int(W / 2), int(H / 2)
- FPS = 120
- class ease:
- def __init__(s, height, frames):
- if frames < 2 or not height:
- s.gravity = 0
- s.velocity = 0
- else:
- s.gravity = 2 * height / (frames * (frames - 1))
- s.velocity = s.gravity * (frames - 1)
- s.iFrames = int(frames)
- s.frames = frames
- def pv(s, index):
- return s.gravity * index
- def nv(s, index):
- return s.velocity - s.gravity * index
- # game time
- class gTime:
- def __init__(s, fps): # input = frames per second
- s.fps = fps
- s.tpf = 1 / fps * 1000000000
- s.time = 0
- s.timeStamp = time.time_ns()
- def tick(s):
- while time.time_ns() - s.timeStamp < s.tpf:
- pass
- s.timeStamp = time.time_ns()
- s.time += 1
- # laser cannon settings
- LASER_CANNON_RADIUS = 15
- LASER_CANNON_RELOAD_DELAY = 15 # frames
- LASER_SPEED = 10 # pixels
- LASER_LENGTH = 20 # pixels
- class laserCannon:
- class laser:
- def __init__(s, parent):
- global LASER_CANNON_RADIUS
- global TIME
- s.p = parent # keep a parental reference
- # setup starting coords
- s.x1 = s.x2 = s.p.x
- s.y1 = s.y2 = s.p.y
- # get direction
- mx, my = pygame.mouse.get_pos()
- s.dx, s.dy = mx - s.x1, my - s.y1
- s.d = math.sqrt((s.dx * s.dx) + (s.dy * s.dy))
- if s.d < LASER_CANNON_RADIUS:
- s.dx = random.uniform(-1, 1)
- s.dy = random.uniform(-1, 1)
- else:
- s.dx /= s.d
- s.dy /= s.d
- s.ts = TIME.time
- s.dead = False
- def draw(s):
- global DS
- pygame.gfxdraw.line(DS, int(s.x1), int(s.y1), int(s.x2), int(s.y2), [255, 255, 255])
- def do(s):
- global LASER_SPEED
- global TIME
- l = TIME.time - s.ts - 1
- if l > 120: # laser exists for 120 frames
- s.dead = True
- return
- # move the laser
- s.x1 += s.dx * LASER_SPEED
- s.y1 += s.dy * LASER_SPEED
- # get the distance between the cannon and the front tip of the laser
- # if the distance means there's an overlap between the laser and the cannon
- # truncate the laser length
- # this is to make sure the laser doesn't appear out of the back of the cannon
- d = math.hypot(s.x1 - s.p.x, s.y1 - s.p.y)
- if d - LASER_LENGTH < LASER_CANNON_RADIUS:
- ll = LASER_LENGTH - (LASER_LENGTH - d)
- else:
- ll = LASER_LENGTH
- # calculate the end tip of the laser
- s.x2 = s.x1 - s.dx * ll
- s.y2 = s.y1 - s.dy * ll
- def __init__(s):
- global LASER_CANNON_RADIUS
- global TIME
- global HW, HH
- # position the cannon
- s.x = HW
- s.y = -LASER_CANNON_RADIUS
- # cannon will drop into position
- s.e = ease(HH + LASER_CANNON_RADIUS, 240)
- # set up a few variables
- s.ts = TIME.time # timestamp to control rate of fire
- s.triggerPressed = False # only fire when lmb released
- s.reloadDelay = 0 # time delay before being allowed to fire another laser
- s.mode = s.fallingIntoPosition # current mode of the cannon (falling/ready)
- s.laserContainer = [] # list of laser objects
- def draw(s):
- global LASER_CANNON_RADIUS
- global DS
- pygame.gfxdraw.filled_circle(DS, s.x, int(s.y), LASER_CANNON_RADIUS, [255, 255, 255])
- for l in s.laserContainer:
- l.draw()
- def do(s):
- s.draw()
- s.mode()
- def fallingIntoPosition(s):
- global TIME
- global HH
- l = TIME.time - s.ts - 1
- if l == s.e.iFrames: # if the cannon has finished falling, move onto ready mode
- s.mode = s.ready
- else:
- s.y += s.e.nv(l)
- def ready(s):
- global LASER_CANNON_RELOAD_DELAY
- # if mouse button is pressed fire a laser
- lmb = pygame.mouse.get_pressed()[0]
- if lmb and not s.triggerPressed:
- s.triggerPressed = True
- elif not lmb and s.triggerPressed:
- s.triggerPressed = False
- if not s.reloadDelay:
- s.laserContainer.append(s.laser(s))
- s.reloadDelay = LASER_CANNON_RELOAD_DELAY # set re-fire delay
- # count down fire delay tp zero
- if s.reloadDelay:
- s.reloadDelay -= 1
- dead = [] # container for dead lasers
- for l in s.laserContainer:
- l.do()
- if l.dead: dead.append(l) # add dead lasers to list
- for dl in dead: # remove dead lasers from container
- s.laserContainer.remove(dl)
- # move the cannon using wasd keys
- k = pygame.key.get_pressed()
- if k[pygame.K_w]:
- s.y -= 2
- elif k[pygame.K_s]:
- s.y += 2
- if k[pygame.K_d]:
- s.x += 2
- elif k[pygame.K_a]:
- s.x -= 2
- pygame.init()
- DS = pygame.display.set_mode((W, H))
- TIME = gTime(FPS)
- L = laserCannon()
- # press ESCAPE to exit demo
- while True:
- e = pygame.event.get()
- if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
- DS.fill([0, 0, 0]) # clear screen
- L.do() # call laser routine
- pygame.display.update()
- TIME.tick() # halt program for tpf (time per frame)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement