Skip to content

Instantly share code, notes, and snippets.

@Orpheon
Created November 7, 2011 15:40
Show Gist options
  • Save Orpheon/1345300 to your computer and use it in GitHub Desktop.
Save Orpheon/1345300 to your computer and use it in GitHub Desktop.
from __future__ import division, print_function
import pygame, math
from pygame.locals import *
import random
import function
import character
class Rocket(entity.Entity):
sprite = function.load_image("projectiles/rockets/0")
max_flight_time = 15
damage = 35
blastradius = 65
knockback = 200
def __init__(self, game, state, sourceweapon):
gameobject.Gameobject.__init__(self, game, state)
self.direction = 0.0
self.flight_time = 0.0
self.sourceweapon = sourceweapon
srcwep, srcplayer = state.entities[sourceweapon], state.entities[state.entities[sourceweapon].owner]
self.x = srcwep.x
self.y = srcwep.y
self.fade = 0
self.direction = srcwep.direction
self.speed = 500
self.hspeed = math.cos(math.radians(self.direction)) * self.speed
self.vspeed = math.sin(math.radians(self.direction)) * -self.speed
def destroy(self, game, state):
if not self.fade:
for obj in state.entities.values():
if isinstance(obj, character.Character) and math.hypot(self.x - obj.x, self.y - obj.y) < self.blastradius:
force = (1-(math.hypot(self.x - obj.x, self.y - obj.y)/self.blastradius))*self.knockback
obj.hspeed += force*((obj.x-self.x)/math.hypot(self.x - obj.x, self.y - obj.y))
obj.vspeed += force*((obj.y-self.y)/math.hypot(self.x - obj.x, self.y - obj.y))/3
gameobject.Gameobject.destroy(self, state)
def step(self, game, state, frametime):
self.speed += 30# Copied from GMK-GG2; should simulate some very basic acceleration+air resistance.
self.speed *= 0.92
self.hspeed = math.cos(math.radians(self.direction)) * self.speed
self.vspeed = math.sin(math.radians(self.direction)) * -self.speed
# calculate direction
self.direction = function.point_direction(self.x - self.hspeed, self.y - self.vspeed, self.x, self.y)
def endstep(self, game, state, frametime):
gameobject.Gameobject.endstep(self, game, state, frametime)
self.flight_time += frametime
image = pygame.transform.rotate(self.sprite, self.direction)
mask = pygame.mask.from_surface(image)
if game.map.collision_mask.overlap(mask, (int(self.x), int(self.y))) or self.flight_time > self.max_flight_time:
self.destroy(game, state)
def draw(self, game, state, frametime):
image = pygame.transform.rotate(self.sprite, self.direction)
game.draw_world(image, (self.x, self.y))
def interpolate(self, next_object, alpha):
gameobject.Gameobject.interpolate(self, next_object, alpha)
self.direction = function.interpolate_angle(self.direction, next_object.direction, alpha)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment