def __init__(self): super().__init__( Rect(0, Window.Instance.height * 2 / 3, Window.Instance.width, Window.Instance.height / 3)) self.set_color(Color(40, 40, 40, 230)) self.title = Label( Loader.get_font("ALoveOfThunder.ttf", 200).render("WEAPONS", False, (180, 10, 0))) self.title.constraints.add_width_constraint( RelativeMultConstraint(0.25)) self.title.constraints.add_height_constraint(AspectConstraint()) self.title.constraints.add_x_constraint(RelativeMultConstraint(1)) self.title.constraints.add_y_constraint(RelativeMultConstraint(1)) self.add_element(self.title) self.weaponListContainer = Container() self.weaponListContainer.set_color(Color(35, 35, 35, 230)) self.weaponListContainer.constraints.add_x_constraint( RelativeMultConstraint(1)) self.weaponListContainer.constraints.add_y_constraint( RelativeMultConstraint(1.15)) self.weaponListContainer.constraints.add_width_constraint( RelativeMultConstraint(1)) self.weaponListContainer.constraints.add_height_constraint( RelativeMultConstraint(0.85)) self.add_element(self.weaponListContainer) for i, weapon in enumerate(Weapons): weapon_img = weapon.HoldImage.copy() pygame.draw.rect(weapon_img, (255, 0, 0), weapon_img.get_rect(), 1) weapon_btn = Button(weapon_img) x = i // 2 * 0.05 + 0.15 y = 0.5 if i % 2 == 1 else 0.1 weapon_btn.constraints.add_x_constraint(RelativeAddConstraint(x)) weapon_btn.constraints.add_y_constraint(RelativeAddConstraint(y)) weapon_btn.constraints.add_width_constraint( RelativeMultConstraint(0.045)) weapon_btn.constraints.add_height_constraint(AspectConstraint()) weapon_btn.set_click_function(self.set_id_wrapper(i)) self.weaponListContainer.add_element(weapon_btn) self.explodeTimeLabel = Label( Loader.get_font("ALoveOfThunder.ttf", 200).render("3", False, (180, 10, 0))) self.explodeTimeLabel.constraints.add_x_constraint( RelativeAddConstraint(0.05)) self.explodeTimeLabel.constraints.add_y_constraint( RelativeAddConstraint(0.1)) self.explodeTimeLabel.constraints.add_width_constraint( RelativeMultConstraint(0.035)) self.explodeTimeLabel.constraints.add_height_constraint( AspectConstraint()) self.weaponListContainer.add_element(self.explodeTimeLabel) self.lastSelectedWeaponID = 0
class ClusterBomb(PhysicsObject): IMAGE = Loader.get_image("cluster_bomb") EXPLOSION_RADIUS = 20 def __init__(self, alive_sec, x, y): super().__init__(x, y, 5, 0.5, bounce_times=3, time_to_death_millis=alive_sec * 1000) def get_draw_position(self): return self._pos - GRENADE_OFFSET def death_action(self, world): world.explosion(int(self.x), int(self.y), ClusterBomb.EXPLOSION_RADIUS, 15, 1) for _ in range(random.randrange(9, 12)): angle = random.random() * math.pi * 2 cluster = Cluster(*self._pos) cluster.vel_x = math.cos(angle) * ClusterBomb.EXPLOSION_RADIUS cluster.vel_y = math.sin(angle) * ClusterBomb.EXPLOSION_RADIUS world.physicsObjects.append(cluster)
def __init__(self, config, graph): self.config = config self.graph = graph self.path = Path(config) self.parser = Parser(config) self.writer = Writer(config) self.loader = Loader(config, graph)
class Grenade(PhysicsObject): IMAGE = Loader.get_image("grenade") EXPLOSION_RADIUS = 25 def __init__(self, alive_sec, x, y): super().__init__(x, y, 5, 0.5, bounce_times=3, time_to_death_millis=alive_sec * 1000) def death_action(self, world): world.explosion(int(self.x), int(self.y), Grenade.EXPLOSION_RADIUS, 45, 2) def get_draw_position(self): return self._pos - GRENADE_OFFSET
class Cluster(PhysicsObject): IMAGE = Loader.get_image("cluster") CLUSTER_OFFSET = 2, 2 EXPLOSION_RADIUS = 8 def __init__(self, x, y): super().__init__(x, y, 1, 0.8, bounce_times=1, time_to_death_millis=3000) def get_draw_position(self): return self._pos - Cluster.CLUSTER_OFFSET def death_action(self, world): world.explosion(int(self.x), int(self.y), Cluster.EXPLOSION_RADIUS, 10, 1)
class Bullet(PhysicsObject): IMAGE = Loader.get_image("uzi_bullet") Type = PhysicsObjectType.Bullet def __init__(self, x: float, y: float, damage: int): super().__init__(x, y, 1, 1, 1, time_to_death_millis=3000, affected_by_gravity=0.1) self.damage: int = damage self.excludedEntities: List[PhysicsObject] = [] self.hitWorm = False def set_excluded_entities(self, entities): self.excludedEntities = entities def check_collisions(self, entities) -> bool: for worm in filter(lambda p: p.is_worm(), entities): if worm not in self.excludedEntities: if worm.health <= 0: continue if worm.pos.distance_to(self.pos) < worm.radius: worm.health -= self.damage worm.draw_health() for i in range(10): bl = Blood(*worm.pos) angle = random.random() * math.pi * 2 bl.vel_x = math.cos(angle) * 10 bl.vel_y = math.sin(angle) * 10 entities.append(bl) self.hitWorm = True return True return False
class WSniper(SimpleShooting): HoldImage = Loader.get_image("sniper") def __init__(self): super().__init__(SniperBullet, 1, 1, 150, 0)
class WMinigun(SimpleShooting): HoldImage = Loader.get_image("minigun") def __init__(self): super().__init__(MinigunBullet, 50, 0.03, 50, math.pi / 5)
class WUzi(SimpleShooting): HoldImage = Loader.get_image("uzi") def __init__(self): super().__init__(UziBullet, 50, 0.06, 80, math.pi / 4)
class WClusterBomb(SimpleThrowable): HoldImage = Loader.get_image("cluster_bomb") def __init__(self): super().__init__(ClusterBomb, 60)
class WGrenade(SimpleThrowable): HoldImage = Loader.get_image("grenade") def __init__(self): super().__init__(Grenade, 60)
class SniperBullet(Bullet): IMAGE = Loader.get_image("sniper_bullet") def __init__(self, x, y): super().__init__(x, y, 80)
class MinigunBullet(Bullet): IMAGE = Loader.get_image("minigun_bullet") def __init__(self, x: float, y: float): super().__init__(x, y, 4)
class UziBullet(Bullet): IMAGE = Loader.get_image("uzi_bullet") def __init__(self, x: float, y: float): super().__init__(x, y, 5)
def set_time(self, time: int): assert 1 <= time <= 5 self.explodeTimeLabel.set_image( Loader.get_font("ALoveOfThunder.ttf", 200).render(str(time), False, (180, 10, 0)))
class Blood(Debris): IMAGE = Loader.get_image("blood")
class Command(object): def __init__(self, config, graph): self.config = config self.graph = graph self.path = Path(config) self.parser = Parser(config) self.writer = Writer(config) self.loader = Loader(config, graph) # Public methods def new(self, filename): # TODO: parse out docid, maybe sign docid try: assert filename.endswith(self.config.source_ext) except AssertionError as e: print "File name must end with %s" % self.config.source_ext sys.exit(1) source_dir = self.path.get_source_dir() source_abspath = os.path.join(source_dir, filename) content = self._build_initial_source(filename) print "Creating file: %s" % source_abspath self._create_file(source_abspath, content) return source_abspath def edit(self, filename): # Open new file in the editor specified in the yaml config file editor = self.config.editor source_path = self.new(filename) process = "%s %s" % (editor, source_path) return subprocess.call(process.split()) def init(self): # Make sure author is pre-loaded in the database data = dict(username=self.config.username, name=self.config.name) author = self.graph.people.get_or_create("username", self.config.username, data) def build(self): # Create HTML fragments self.writer.run() def update(self): # Update blog entries self.loader.update_entries() # Execute one of the above methods def _execute(self, command_name, command_args): command = getattr(self, command_name) return command(*command_args) # Private methods def _create_file(self, source_abspath, content): self._make_dir(source_abspath) with open(source_abspath, "w") as fout: fout.writelines(content) def _build_initial_source(self, filename): # generat the source from template template_path = self.path.get_rst_template_path() template = get_template(template_path) params = self._get_params(filename) source = template.substitute(params) return source def _get_params(self, filename): # Get template params docid = uuid.uuid4().hex date = datetime.datetime.now().strftime("%Y-%m-%d") username = self.config.username or getpass.getuser() title = self._get_title(filename) title_line = "=" * len(title) params = dict(title=title, title_line=title_line, docid=docid, author=username, date=date) return params def _get_title(self, filename): stub = os.path.splitext(filename)[0] word_list = stub.split(self.config.separator) words = " ".join(word_list) title = titlecase(words) return title def _write_file(self, file_path, content): with open(file_path, "w") as fout: fout.write(content.encode('utf-8') + '\n') def _make_dir(self, path): # mkpath dirname = os.path.dirname(path) if not os.path.isdir(dirname): print "Creating dir: %s" % dirname os.makedirs(dirname)