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))
def load(self): self.world_size = Size(3000, 3000) self.camera = Camera(self.size, self.world_size, 1000, 10) self._tool = None self.tool = None self.batch = graphics.Batch() self.background = CameraGroup(graphics.OrderedGroup(0), self.camera) self.foreground = CameraGroup(graphics.OrderedGroup(1), self.camera) self.playerg = CameraGroup(graphics.OrderedGroup(2), self.camera) self.world_ui = CameraGroup(graphics.OrderedGroup(3), self.camera) self.ui = graphics.OrderedGroup(2) self.space = Space() self.space.gravity = (0.0, 0.0) buffer = 100 borders = Body() borders.position = (0, 0) left = Segment(borders, (-buffer, -buffer), (-buffer, self.world_size.height+buffer), buffer) bottom = Segment(borders, (-buffer, -buffer), (self.world_size.width+buffer, -buffer), buffer) right = Segment(borders, (self.world_size.width+buffer, self.world_size.height+buffer), (self.world_size.width+buffer, -buffer), buffer) top = Segment(borders, (self.world_size.width+buffer, self.world_size.height+buffer), (-buffer, self.world_size.height+buffer), buffer) self.space.add_static(left, bottom, right, top) self.stars = Stars(self.world_size, self.batch, self.background) self.asteroids = Asteroid.populate(50, 100, self.world_size, self.batch, self.foreground, self.space) if not self.asteroids: print("None of a particular resource on this asteroid belt, that'd be unfair. Trying again.") self.end(Main()) return self.home_world = choice([asteroid for asteroid in self.asteroids if asteroid.position.y > self.world_size.height/4*3]) self.home_world.type = "home" self.home_world.populated = True x, y = self.home_world.position self.camera.move(Vector(x-self.size.width/2, y-self.size.height/2)) # Let's make stuff a bit more interesting. for asteroid in self.asteroids: if not asteroid.type == "home": asteroid.body.apply_impulse((triangular(-20000, 20000, 0), triangular(-20000, 20000, 0))) x, y = self.home_world.position self.player = Person(x+150, y+150, self.batch, self.playerg, self.space) self.mouse = x+150, y+150 centre = Vector(self.size.width/2, self.size.height/2) image = centre_image(resource.image("logo.png")) self.logo = sprite.Sprite(image, centre.x, centre.y, batch=self.batch, group=self.ui) self.logo.opacity = 255 self.fade = True self.faded = False planet = centre_image(resource.image("planet.png")) x = self.world_size.width/2 y = planet.height/2 self.planet_sprite = sprite.Sprite(planet, x, y, batch=self.batch, group=self.world_ui) self.win_box = BB(x-200, y-200, x+200, y+200) #self.tools = sorted([tool(self.space) for tool in Tool.__subclasses__()], key=attrgetter("order"), reverse=True) #self.buttons = {tool: Button(30, 30+number*50, tool.image, tool.description, self.use_tool(tool), self.ui, self.batch) for number, tool in enumerate(self.tools)} self.constraints = set()
class Main(Scene): FADE_SPEED = 75 def load(self): self.world_size = Size(3000, 3000) self.camera = Camera(self.size, self.world_size, 1000, 10) self._tool = None self.tool = None self.batch = graphics.Batch() self.background = CameraGroup(graphics.OrderedGroup(0), self.camera) self.foreground = CameraGroup(graphics.OrderedGroup(1), self.camera) self.playerg = CameraGroup(graphics.OrderedGroup(2), self.camera) self.world_ui = CameraGroup(graphics.OrderedGroup(3), self.camera) self.ui = graphics.OrderedGroup(2) self.space = Space() self.space.gravity = (0.0, 0.0) buffer = 100 borders = Body() borders.position = (0, 0) left = Segment(borders, (-buffer, -buffer), (-buffer, self.world_size.height+buffer), buffer) bottom = Segment(borders, (-buffer, -buffer), (self.world_size.width+buffer, -buffer), buffer) right = Segment(borders, (self.world_size.width+buffer, self.world_size.height+buffer), (self.world_size.width+buffer, -buffer), buffer) top = Segment(borders, (self.world_size.width+buffer, self.world_size.height+buffer), (-buffer, self.world_size.height+buffer), buffer) self.space.add_static(left, bottom, right, top) self.stars = Stars(self.world_size, self.batch, self.background) self.asteroids = Asteroid.populate(50, 100, self.world_size, self.batch, self.foreground, self.space) if not self.asteroids: print("None of a particular resource on this asteroid belt, that'd be unfair. Trying again.") self.end(Main()) return self.home_world = choice([asteroid for asteroid in self.asteroids if asteroid.position.y > self.world_size.height/4*3]) self.home_world.type = "home" self.home_world.populated = True x, y = self.home_world.position self.camera.move(Vector(x-self.size.width/2, y-self.size.height/2)) # Let's make stuff a bit more interesting. for asteroid in self.asteroids: if not asteroid.type == "home": asteroid.body.apply_impulse((triangular(-20000, 20000, 0), triangular(-20000, 20000, 0))) x, y = self.home_world.position self.player = Person(x+150, y+150, self.batch, self.playerg, self.space) self.mouse = x+150, y+150 centre = Vector(self.size.width/2, self.size.height/2) image = centre_image(resource.image("logo.png")) self.logo = sprite.Sprite(image, centre.x, centre.y, batch=self.batch, group=self.ui) self.logo.opacity = 255 self.fade = True self.faded = False planet = centre_image(resource.image("planet.png")) x = self.world_size.width/2 y = planet.height/2 self.planet_sprite = sprite.Sprite(planet, x, y, batch=self.batch, group=self.world_ui) self.win_box = BB(x-200, y-200, x+200, y+200) #self.tools = sorted([tool(self.space) for tool in Tool.__subclasses__()], key=attrgetter("order"), reverse=True) #self.buttons = {tool: Button(30, 30+number*50, tool.image, tool.description, self.use_tool(tool), self.ui, self.batch) for number, tool in enumerate(self.tools)} self.constraints = set() def use_tool(self, tool): """For callback usage.""" def f(): self.tool = tool return f @property def tool(self): return self._tool @tool.setter def tool(self, tool): if tool: if self._tool and not self._tool == tool: self._tool.end_selecting() self.buttons[self._tool].normal() self.window.set_mouse_cursor(self.window.get_system_mouse_cursor(self.window.CURSOR_DEFAULT)) self.selecting = False self.selection = [] self.window.set_mouse_cursor(self.window.get_system_mouse_cursor(self.window.CURSOR_CROSSHAIR)) self.selecting = True self.buttons[tool].using() else: if self._tool: self._tool.end_selecting() self.buttons[self._tool].normal() self.window.set_mouse_cursor(self.window.get_system_mouse_cursor(self.window.CURSOR_DEFAULT)) self.selecting = False self.selection = [] self._tool = tool def key_pressed(self, symbol, modifiers): self.fade = True #self.camera.key_pressed(symbol) def key_released(self, symbol, modifiers): pass #self.camera.key_released(symbol) def mouse_pressed(self, x, y, key, modifiers): self.fade = True #for button in self.buttons.values(): # if button.point_over(x, y): # button.callback() # return if self.selecting: for asteroid in self.asteroids: clicked = self.camera.translate(x, y) if asteroid.point_over(*clicked): self.selection.append((asteroid, clicked)) self.tool = self.tool.selection(self.selection, self.constraints) return self.tool = None return def mouse_motion(self, x, y, dx, dy): self.mouse = self.camera.translate(x, y) #for button in self.buttons.values(): # if button.point_over(x, y): # button.on_mouse_over() # else: # button.on_mouse_leave() def mouse_drag(self, x, y, dx, dy, buttons, modifiers): if buttons & window.mouse.RIGHT: self.camera.mouse_dragged(dx, dy) def update(self, frame_time): self.constraints = {constraint for constraint in self.constraints if not constraint.update(self.batch, self.foreground)} if self.fade and not self.faded: self.logo.opacity -= Main.FADE_SPEED*frame_time if self.logo.opacity < 0: self.logo.opacity = 0 del self.logo self.faded = True self.player.target = self.mouse self.player.update() x, y = self.player.body.position self.camera.x, self.camera.y = x-self.size.width/2, y-self.size.height/2 self.camera.update(frame_time) self.space.step(1/60) if self.win_box.contains_vect(self.player.body.position): self.end(Win()) def draw(self): self.batch.draw()