def generate_geometry(surface, space): for s in space.shapes: if hasattr(s, 'generated') and s.generated: space.remove(s) def sample_func(point): try: p = int(point.x), int(point.y) color = surface.get_at(p) return color.hsla[2] # use lightness except: return 0 line_set = pymunk.autogeometry.PolylineSet() def segment_func(v0, v1): line_set.collect_segment(v0, v1) pymunk.autogeometry.march_soft(BB(0, 0, 599, 599), 60, 60, 90, segment_func, sample_func) for polyline in line_set: line = pymunk.autogeometry.simplify_curves(polyline, 1.) for i in range(len(line) - 1): p1 = line[i] p2 = line[i + 1] shape = pymunk.Segment(space.static_body, p1, p2, 1) shape.friction = .5 shape.color = pygame.color.THECOLORS['red'] shape.generated = True space.add(shape)
def generate_geometry(surface, space): for s in space.shapes: if hasattr(s, "generated") and s.generated: space.remove(s) def sample_func(point): try: p = int(point.x), int(point.y) color = surface.get_at(p) return color.hsla[2] # use lightness except: return 0 line_set = pymunk.autogeometry.PolylineSet() def segment_func(v0, v1): line_set.collect_segment(v0, v1) pymunk.autogeometry.march_soft( BB(0,0,xSize - 1,ySize - 1), 250, 250, 92, segment_func, sample_func) #generateBounding for polyline in line_set: line = pymunk.autogeometry.simplify_curves(polyline, 0.8) for i in range(len(line)-1): p1 = line[i] p2 = line[i+1] shape = pymunk.Segment(space.static_body, p1, p2, 2) shape.friction = 10 shape.color = THECOLORS["grey"] shape.generated = True shape.collision_type = COLLTYPE_TERRAIN space.add(shape)
def handleInputs(events): global activeWeapon try: actors[aID[0]][aID[1]].handleActive() except: pass for event in events: if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and not ( pygame.key.get_mods() & KMOD_CTRL): actors[aID[0]][aID[1]].shoot() elif event.type == KEYDOWN and event.key == K_TAB: nextActor() elif event.type == KEYDOWN and event.key == K_q: nextTeam() elif event.type == KEYDOWN and event.key == K_k: createActor(pygame.mouse.get_pos(), 0) elif event.type == KEYDOWN and event.key == K_l: createActor(pygame.mouse.get_pos(), 1) elif event.type == pygame.MOUSEBUTTONUP and event.button == 1 and ( pygame.key.get_mods() & KMOD_CTRL): update_geometry(terrain_surface, space, BB(0, 0, xSize - 1, ySize - 1)) elif event.type == KEYDOWN and event.key in weaponsDict: activeWeapon = weaponsDict[event.key] if pygame.mouse.get_pressed()[0] and (pygame.key.get_mods() & KMOD_CTRL): color = THECOLORS["green"] pos = pygame.mouse.get_pos() pygame.draw.circle(terrain_surface, color, pos, 20)
def generate_geometry(self,surface, space): for s in space.shapes: if hasattr(s, "generated") and s.generated: space.remove(s) def sample_func(point): try: p = int(point.x), int(point.y) color = surface.get_at(p) return color.hsla[2] # use lightness except: return 0 line_set = pymunk.autogeometry.PolylineSet() def segment_func(v0, v1): line_set.collect_segment(v0, v1) pymunk.autogeometry.march_soft(BB(0,0,1280,960), 60, 60, 80, segment_func, sample_func) filter = pymunk.ShapeFilter(categories=0x2) for polyline in line_set: line = pymunk.autogeometry.simplify_curves(polyline, 1.) for i in range(len(line)-1): p1 = line[i] p2 = line[i+1] shape = pymunk.Segment(space.static_body, p1, p2, 5) shape.friction = 1 shape.elasticity =1 shape.color = pygame.color.THECOLORS["black"] shape.filter = filter shape.generated = True space.add(shape)
def explode(self): self.exploded = True position = self._get_body()._get_position() expl = Explosion(position, self.explosionSize) all_sprites.add(expl) pygame.draw.circle( terrain_surface, THECOLORS["white"], [int(position[0]), int(position[1])], self.explosionSize) update_geometry( terrain_surface, space, BB(position[0] - self.explosionSize * 4, position[1] - self.explosionSize * 4, position[0] + self.explosionSize * 4, position[1] + self.explosionSize * 4)) if self in bombs: bombs.remove(self) if (self.cluster): self.clusterSpawn() try: space.remove(self.body, self) except: pass for t in actors: for a in t: a.modHP(-clip( int(self.explosionSize * 200000.0 / (clip( a.getDistance(position) - self.explosionSize / 1.5, 1, 200000))**4), 0, self.explosionSize * 2.5))
def shoot(self): global turnRemaining mouseOffset = pygame.mouse.get_pos() - self.getPosition() mouseVector = unitVector(mouseOffset) if activeWeapon != 4: turnRemaining = min(turnRemaining, 4) if activeWeapon == 1 and self.shot == 0: velocity = mouseVector * clip( self.getDistance(pygame.mouse.get_pos()), 0, 350) * 3 body = makeMissileR(self.getPosition(), 20, 4, velocity) elif activeWeapon == 2 and self.shot == 0: velocity = mouseVector * 800 body = makeGrenadeR(self.getPosition() - [0, 2], 50, 5, 5, velocity) elif activeWeapon == 3 and self.shot == 0: velocity = mouseVector * 500 body = makeGrenadeR(self.getPosition() - [0, 2], 20, 2, 5, velocity, True) elif activeWeapon == 4 and self.shot < 15: torchPos = self.getPosition() + mouseVector * 10 pygame.draw.circle( terrain_surface, THECOLORS["white"], [int(torchPos[0]), int(torchPos[1])], 15) update_geometry( terrain_surface, space, BB(torchPos[0] - 50, torchPos[1] - 50, torchPos[0] + 50, torchPos[1] + 50)) self.shot += 1
def generate(self): def segment_func(v0, v1): line_set.collect_segment(v0, v1) def sample_func(point): try: p = round(point.x), round(point.y) color = self.__track.get_at(p) return 255 if color == self.__track_color else 0 except: return 0 self.__window.instant_message("Generating Track...", (1100, 200), "white") for s in self.__window.space.shapes: if hasattr(s, "generated") and s.generated: self.__window.space.remove(s) line_set = pymunk.autogeometry.PolylineSet() pymunk.autogeometry.march_soft( BB(0, 0, self.__window.width - 1, self.__window.height - 1), self.__window.width // 4, self.__window.height // 4, 1, segment_func, sample_func) for polyline in line_set: line = pymunk.autogeometry.simplify_curves(polyline, 1) for i in range(len(line) - 1): p1 = line[i] p2 = line[i + 1] shape = pymunk.Segment(self.__window.space.static_body, p1, p2, 0) shape.color = pygame.color.THECOLORS["black"] shape.generated = True self.__window.space.add(shape)
def generate_geometry(surface, space): for s in space.shapes: if hasattr(s, "generated") and s.generated: space.remove(s) def sample_func(point): try: p = int(point[0]), int(point[1]) color = surface.get_at(p) return color.hsla[2] # use lightness except Exception as e: print(e) return 0 line_set = pymunk.autogeometry.march_soft( BB(0, 0, 599, 599), 60, 60, 90, sample_func ) for polyline in line_set: line = pymunk.autogeometry.simplify_curves(polyline, 1.0) for i in range(len(line) - 1): p1 = line[i] p2 = line[i + 1] shape = pymunk.Segment(space.static_body, p1, p2, 1) shape.friction = 0.5 shape.color = pygame.Color("red") shape.generated = True space.add(shape)
def generateTerrain(): color = THECOLORS["green"] terrain_surface.fill(THECOLORS["white"]) pygame.draw.line(terrain_surface, color, (0, ySize - 10), (xSize, ySize - 10), 20) pygame.draw.line(terrain_surface, color, (0, 0), (0, ySize), 5) pygame.draw.line(terrain_surface, color, (xSize, 0), (xSize, ySize), 5) for i in range(random.randint(3, 5)): cSize = random.randint(7, 15) coords = [ random.randint(0, xSize), random.randint(ySize / 3, ySize * 2 / 5) ] while coords[1] < ySize + 100: pygame.draw.circle(terrain_surface, color, coords, cSize) coords = [ coords[0] + random.randint(int(-xSize / 16), int(xSize / 16)), coords[1] + random.randint(cSize // 4, cSize // 2) ] cSize += random.randint(1, 3) for i in range(random.randint(6, 10)): cSize = random.randint(4, 10) coords = [ random.randint(0, xSize), random.randint(ySize * 2 / 3, ySize) ] while coords[1] < ySize + 100: pygame.draw.circle(terrain_surface, color, coords, cSize) coords = [ coords[0] + random.randint(int(-xSize / 16), int(xSize / 16)), coords[1] + random.randint(cSize // 2, cSize) ] cSize += random.randint(2, 5) for i in range(random.randint(50, 70)): cSize = random.randint(8, 13) coords = [ random.randint(0, xSize), random.randint(ySize * 7 / 8, ySize) ] while coords[1] < ySize + 100: pygame.draw.circle(terrain_surface, color, coords, cSize) coords = [ coords[0] + random.randint(int(-xSize / 16), int(xSize / 16)), coords[1] + random.randint(cSize // 2, cSize) ] cSize += random.randint(3, 5) update_geometry(terrain_surface, space, BB(0, 0, xSize - 1, ySize - 1))