def _generate_image(self): t = clamp((self.time + self.TOTAL_TIME) * (1 / self.TOTAL_TIME), 0, 1) t2 = clamp((self.time - (self.KILL_TIME - 0.25)) * 4, 0, 1) self._height = t * 50 + 1 self._width = (1 - t2) * 250 self.image = pygame.Surface((self._width, self._height), pygame.SRCALPHA) if self.time < 0: self.image.fill(PICO_WHITE) else: self.image.fill(PICO_BLACK) s1 = text.render_multiline(self.name, "small", PICO_YELLOW, 220, True) self.image.blit(s1, (self._width // 2 - s1.get_width() // 2, 8)) s2 = text.render_multiline(self.description, "small", PICO_WHITE, 220, True) self.image.blit(s2, (self._width // 2 - s2.get_width() // 2, self._height // 2 + 8 - s2.get_height() // 2)) if self._height >= 4: pygame.draw.rect(self.image, PICO_PINK, (1, 1, self._width - 2, self._height - 2), 1) if self.time >= 0 and self.time < self.DITHER_TIME: self.image.blit(self.dither_image, (0, 0)) self._recalc_rect()
def _generate_image(self): self._width, self._height = (self.radius * 2 + 14, self.radius * 2 + 14) self.image = pygame.Surface((self._width, self._height), pygame.SRCALPHA) t = self.hover_to_select_t ** 2 a1 = math.pi * 3 / 4 a2 = a1 + t * math.pi * 2 color = PICO_WHITE if (self.time * 4) % 1 > 0.5 else PICO_LIGHTGRAY if t < 1: carrot_size = (1 - t) * 4 poly = [ (5,5), (4 - carrot_size, 5), (5, 4 - carrot_size) ] pygame.draw.polygon(self.image, color, poly, 0) if t > 0: rect = (5,5,self._width-12,self._height-10) t1 = clamp(t * 2,0,1) t2 = clamp(t * 2 - 1,0,1) pygame.draw.line(self.image, color, (rect[0],rect[1]), (rect[0] + rect[2] * t1, rect[1])) pygame.draw.line(self.image, color, (rect[0],rect[1]), (rect[0], rect[1] + rect[3] * t1)) if t2 > 0: pygame.draw.line(self.image, color, (rect[0] + rect[2],rect[1]), (rect[0] + rect[2], rect[1] + rect[3] * t2)) pygame.draw.line(self.image, color, (rect[0],rect[1] + rect[3]), (rect[0] + rect[2] * t2, rect[1] + rect[3])) #pygame.draw.rect(self.image, color, , 1) self._recalc_rect()
def __init__(self, text, target=None): self.color_index = 0 size = "big" if target: size = "small" super().__init__(text.upper(), size, V2(0, 0), color=COLORS[0], border=False, multiline_width=res.RES[0], shadow=PICO_BLACK) self.pos = V2(res.RES[0] / 2, res.RES[1] / 2) if target: if isinstance(target, V2): self.pos = V2( clamp(target.x, self.width / 2, res.RES[0] - self.width / 2), clamp(target.y, self.height / 2, res.RES[1] - self.height / 2)) else: self.pos = V2( clamp(target.pos.x, self.width / 2, res.RES[0] - self.width / 2), clamp(target.pos.y, self.height / 2, res.RES[1] - self.height / 2)) self.offset = (0.5, 0.5) self._recalc_rect()
def update(self): """ Overrides the update function of the Sprite class. Handles movement. """ if self._moving: #checks if path is empty if not self._path: #notify not moving self._moving = False return #There's a path to move on else: #If we're at the next tile remove it if (self.tile_x, self.tile_y) == self._path[0]: self._path.pop(0) if not self._path: return #get values for calcs path_x, path_y = self._path[0] #determine deltas dx = helper.clamp(path_x - self.tile_x, -FRAME_MOVE_SPEED, FRAME_MOVE_SPEED) dy = helper.clamp(path_y - self.tile_y, -FRAME_MOVE_SPEED, FRAME_MOVE_SPEED) #angle properly self.face_vector((dx, dy)) #set the new value self.tile_x += dx self.tile_y += dy
def _generate_image(self): self._width, self._height = self.radius * 2 + 8, self.radius * 2 + 8 t1 = math.cos(clamp(self.time, 0, 1) * 3.14159) * -0.5 + 0.5 t2 = math.cos(clamp(self.time - 0.65, 0, 1) * 3.14159) * -0.5 + 0.5 self.image = pygame.Surface((self._width, self._height), pygame.SRCALPHA) a1 = 3.14159 / 2 + t2 * 6.2818 a2 = 3.14159 / 2 + t1 * 6.4 #pygame.draw.arc(self.image, PICO_BLUE, (4,4,self._width - 8, self._height - 8), a1, a2, 1) center = V2(self._width / 2, self._height / 2) pts = [] steps = int(a2 * 20) for i in range(steps): theta = i / 20 if theta > a1: pts.append((helper.from_angle(-theta) * self.radius + center - V2(1, 1))) if len(pts) >= 2: pygame.draw.lines(self.image, PICO_BLUE, False, pts, 2) vs = helper.from_angle(-a2) vf = V2(vs.y, -vs.x) pt = vs * self.radius + center p1 = pt + vs * 3 + vf * -3 p2 = pt + -vs * 3 + vf * -3 pygame.draw.lines(self.image, PICO_BLUE, False, [tuple(p1), pt, tuple(p2)], 2) self._recalc_rect()
def press(self, dir): c = self.get_current_control() if c is None: return if dir == "down": y = min(self.control_pos[1] + 1, len(self.controls) - 1) x = clamp(self.control_pos[0], 0, len(self.controls[y]) - 1) self.control_pos = (x, y) self.update_hover() self._generate_image() elif dir == "up": y = max(self.control_pos[1] - 1, 0) x = clamp(self.control_pos[0], 0, len(self.controls[y]) - 1) self.control_pos = (x, y) self.update_hover() self._generate_image() if dir == "left": if isinstance(c, Slider): c.set_value(c.value - 1) else: x = max(self.control_pos[0] - 1, 0) y = self.control_pos[1] self.control_pos = (x, y) self.update_hover() self._generate_image() if dir == "right": if isinstance(c, Slider): c.set_value(c.value + 1) else: y = self.control_pos[1] x = min(self.control_pos[0] + 1, len(self.controls[y]) - 1) self.control_pos = (x, y) self.update_hover() self._generate_image()
def _generate_image(self): yo = 5 w = game.Game.inst.game_resolution.x t = clamp((self.time - START_TIME) / FINISH_TIME, 0, 1)**1.5 t3 = text.render_multiline(self.description, "small", PICO_LIGHTGRAY, wrap_width=400, center=False) h = clamp(t * 8, 0, 1) * (55 + t3.get_height()) self.image = pygame.Surface((w, h + yo * 2), pygame.SRCALPHA) dx1 = t * w * 0.6 dx2 = t * w * 0.6 + h z = clamp((self.time - KILL_TIME + 0.3) * 4, 0, 1) * ((h / 2) - yo) points = [ (int(w / 2 - dx1), 0 + z + yo), (int(w / 2 + dx1), 0 + z + yo), (int(w / 2 + dx2), h - z - yo), (int(w / 2 - dx2), h - z - yo), ] for x in range(int(w / 2 - dx1 - 8 + (self.time * 23) % 8), int(w / 2 + dx1), 8): l = 4 pygame.draw.line(self.image, PICO_WHITE, (x, yo + z - 2), (x + l, yo + z - 2), 1) pygame.draw.line(self.image, PICO_WHITE, (w - x, h - z - yo + 2), (w - x + l, h - z - yo + 2), 1) pygame.draw.polygon(self.image, PICO_DARKBLUE, points, 0) pygame.draw.polygon(self.image, PICO_WHITE, points, 1) #pygame.draw.polygon(self.image, PICO_BLACK, points, 1) pygame.draw.line(self.image, PICO_WHITE, points[3], points[0], 2) pygame.draw.line(self.image, PICO_WHITE, points[2], points[1], 2) if t >= 0.25 and self.time < KILL_TIME - 0.35: t1 = text.render_multiline("- SECTOR %d -" % self.number, "small", PICO_BLUE, wrap_width=500, center=False) self.image.blit(t1, (w / 2 - t1.get_width() / 2, 11)) t2 = text.render_multiline(self.name, "big", PICO_YELLOW, wrap_width=500, center=False) self.image.blit(t2, (w / 2 - t2.get_width() / 2, 29)) i = int( clamp((self.time - START_TIME - FINISH_TIME) * 0.8, 0, 1) * len(self.description)) t4 = text.render_multiline(self.description[0:i], "small", PICO_WHITE, wrap_width=400, center=False) self.image.blit(t4, (w / 2 - t3.get_width() / 2, 49)) self._width = w self._height = h self._recalc_rect()
def update(self, dt): if len(self.civ.get_all_combat_ships()) >= self.civ.get_fleet_soft_cap(): self.expand_timer = clamp(self.expand_timer + dt / EXPAND_TIME, 0, 1) else: self.expand_timer = clamp(self.expand_timer - dt / EXPAND_TIME, 0, 1) self._generate_image() self.y = self.y * 0.5 + self.target_y * 0.5 return super().update(dt)
def sphere_get(offset, planet_pos): spherize = 0.25 + pow(planet_pos.length(), 1.75) / 55.0 dist, angle = planet_pos.as_polar() angle *= 3.14159 / 180 angle += wavy_angle p2 = offset + helper.from_angle(angle) * dist * spherize p2.x = clamp(p2.x, 0, ww - 1) p2.y = clamp(p2.y, 0, wh - 1) color = wavy.get_at((int(p2.x), int(p2.y))) return color
def set_radius(self, rad): rad /= 4 if self.obj_type.endswith('planet'): self.size = clamp(rad - 8, 1, 10) self.desc.set_text(str(int(self.size))) self.radius = self.size + 8 else: self.size = clamp(rad - 8, 1, 10) self.radius = clamp(rad, 5, 25) self._generate_image()
def on_joy_direction_press(self, direction): node = self.joy_controls_order[self.joy_node_index] node.on_mouse_exit(V2(0,0)) if direction == 'up': self.joy_node_index = clamp(self.joy_node_index - 1, 0, len(self.joy_controls_order)-1) node = self.joy_controls_order[self.joy_node_index] if direction == 'down': self.joy_node_index = clamp(self.joy_node_index + 1, 0, len(self.joy_controls_order)-1) node = self.joy_controls_order[self.joy_node_index] node.on_mouse_enter(V2(0,0)) node.on_mouse_down(V2(0,0))
def update_len(self): t = self.time * 1750 l1 = clamp(t, 0, self.len) self.pt2 = self.pt_start + self.delta * l1 l2 = clamp(t - 800, 0, self.len) self.pt1 = self.pt_start + self.delta * l2 if l1 >= self.len: if self.next_line and self.next_line.time < 0: self.next_line.start() if l2 >= self.len: self.kill() self._generate_image() self.pos = self.pt1
def on_direction_press(self, direction): if self.hover_option_index is None: self.on_option_hover(0) elif direction == "down": self.on_option_hover( clamp(self.hover_option_index + 1, 0, len(self.options) - 1)) elif direction == "up": self.on_option_hover( clamp(self.hover_option_index - 1, 0, len(self.options) - 1)) else: self.options[self.hover_option_index].input_direction(direction)
def battle(char1, char2): """ :type char2: model.CharStats :type char1: model.CharStats """ battleIsRunning = True timer = 0 winner = None regTimer = 0.0 char1AttackTimeStamp = char1.AttackSpeed char2AttackTimeStamp = char2.AttackSpeed char1Alive = True char2Alive = True while battleIsRunning and timer < 100: # char1 attack if timer > char1AttackTimeStamp: attack(char1, char2) char1AttackTimeStamp = timer + char1.AttackSpeed # char2 attack if timer > char2AttackTimeStamp: attack(char2, char1) char2AttackTimeStamp = timer + char2.AttackSpeed # char1 death if char1.HealthPoints < 0: char1Alive = False battleIsRunning = False # char1 death if char2.HealthPoints < 0: char2Alive = False battleIsRunning = False # health regen if timer > regTimer: regTimer = timer + 0.1 # char1 regen char1.HealthPoints = hlp.clamp( char1.HealthRegenPerSecond / 10 + char1.HealthPoints, 0, char1.MaxHealthPoints) # char2 regen char2.HealthPoints = hlp.clamp( char2.HealthRegenPerSecond / 10 + char2.HealthPoints, 0, char2.MaxHealthPoints) timer += 0.1 if char1Alive and not char2Alive: winner = char1 if char2Alive and not char1Alive: winner = char2 char1.HealthPoints = char1.MaxHealthPoints char2.HealthPoints = char2.MaxHealthPoints return winner
def generate_grid(self, objects): self.all_objects = [] self.grid = [] for y in range(math.ceil(self.height / self.grid_size)): self.grid.append([]) for x in range(math.ceil(self.width / self.grid_size)): self.grid[-1].append([]) for obj in objects: cx = clamp(obj.pos.x // self.grid_size, 0, len(self.grid[0]) - 1) cy = clamp(obj.pos.y // self.grid_size, 0, len(self.grid) - 1) self.grid[int(cy)][int(cx)].append(obj) self.all_objects.append(obj) #print(self.get_objects_near.cache_info()) self._get_objects_near.cache_clear()
def generate_image(self): t = min(self.time / self.lifetime, 1) ci = clamp(int(len(self.colors) * t), 0, len(self.colors) - 1) size = self.scale_fn(t) * (self.max_size - 2) #print(t, self.colors[ci]) self.image.fill((0, 0, 0, 0)) #temp = pygame.Surface((self.max_size * 2, self.max_size * 2), pygame.SRCALPHA) if t <= 1: polycircle.draw_polycircle(self.image, self.colors[ci], (self.max_size, self.max_size), size, round(self.line_width)) #pygame.draw.circle(temp, self.colors[ci], (self.max_size, self.max_size), size, 0) #temp.blit(self.image, (0,0)) innersize = clamp(size - self.line_width, 0, 999) #pygame.draw.circle(temp, (0,0,0,0), (self.max_size, self.max_size), innersize, 0) self.size = size
def process(self, input): if (input < -1): return -1 elif (input > 1): return 1 return clamp(self.activationFunction(input))
def _generate_image(self): self.image = self.full_image.copy() pxs = (1 - (clamp(self.o2,0, O2_MAX) / O2_MAX)) * 60 + 19 self.image.blit(self.empty_image, (0,0), (0, 0, pxs, self.image.get_height())) ts = get_time_string(self.o2) text.render_multiline_to(self.image, (39, -1), ts, "tiny", PICO_BLACK) text.render_multiline_to(self.image, (38, -2), ts, "tiny", PICO_WHITE)
def prepare_bullet_mods(self): base_damage = self.BASE_DAMAGE if not self.owning_civ.is_enemy and self.SHIP_BONUS_NAME == "fighter": fighter_damage_curve = [1, 1.5, 1.9, 2.2] base_damage *= fighter_damage_curve[ self.scene.game.run_info.ship_levels["fighter"] - 1] damage_add = 0 extra_speed = (self.get_max_speed() / self.MAX_SPEED) - 1 damage_add += self.get_stat("ship_weapon_damage_speed") * clamp( extra_speed, 0, 1) damage_add += self.get_stat("ship_weapon_damage") damage_mul = self.get_stat("%s_damage_mul" % self.SHIP_BONUS_NAME) mods = { 'damage_base': base_damage, 'damage_mul': damage_mul, 'damage_add': damage_add, 'missile_speed': self.get_stat("ship_missile_speed"), 'raze_upgrade': self.get_stat("raze_upgrade"), 'color': PICO_LIGHTGRAY, } damage_factor = (mods['damage_mul'] * mods['damage_base'] + mods['damage_add']) / mods['damage_base'] if damage_factor > 1: mods['trail'] = PICO_PINK mods['trail_length'] = 0.5 + (damage_factor - 1) if self.SHIP_BONUS_NAME == "fighter": mods['iron_on_hit'] = self.get_stat("fighter_damage_iron") return mods
def update(self, dt): self.time += dt if self.mode == "active": self.hover_to_select_t = clamp(self.hover_to_select_t + dt * 8, 0, 1) # TODO: Optimize? Generate only when needed? self._generate_image() return super().update(dt)
def update(self, dt): self.t = clamp(self.t + dt * 0.85, 0, 1) zt = math.cos(self.t * 3.14159) * -0.5 + 0.5 zt = zt**1.25 self.pos = V2(self.initial_pos.x, self.initial_pos.y * (1 - zt) + self.target_pos.y * zt) return super().update(dt)
def update(self, dt): super().update(dt) if self.phase == PHASE_REVIVING: # Make ships go into cinematic mode so they don't fight. for ship in self.scene.get_ships(): ship.cinematic_no_combat = True for p in self.scene.get_planets(): p.cinematic_disable = True self.scene.player_civ.frozen.iron = 2 self.scene.player_civ.frozen.ice = 2 self.scene.player_civ.frozen.gas = 2 self.reviving_time += dt self.scene.game_speed = clamp(self.reviving_time / 4, 1, 20) #self.scene.game_speed = 20 self.mothership.collidable = False if self.mothership.state == self.mothership.STATE_GAME_WAITING: self.post_cinematic_timer -= dt if self.post_cinematic_timer <= 0: self.phase = PHASE_2 self.mothership.collidable = True self.scene.game_speed = 1 self.scene.sm.transition(levelstates.PlayState(self.scene)) self.mothership.health_bar.visible = True self.mothership.health_bar.stay = True for p in self.scene.get_planets(): p.cinematic_disable = False
def __init__(self, pos, colors, lifetime, max_size, scale_fn=None, line_width=1.5, velocity=None): super().__init__(V2(pos)) self.colors = colors self.lifetime = lifetime self.max_size = max_size + 2 self.line_width = line_width self.velocity = velocity or V2(0, 0) if isinstance(scale_fn, str): self.scale_fn = { "log": lambda t: clamp(math.log(t * 10 + 1) / 4 + t / 2.5, 0, 1) }[scale_fn] else: self.scale_fn = scale_fn or (lambda t: t) self.time = 0 self.image = pygame.Surface((self.max_size * 2, self.max_size * 2), pygame.SRCALPHA) self._width, self._height = self.max_size * 2, self.max_size * 2 self._offset = (0.5, 0.5) self._recalc_rect() self.generate_image()
def _get_objects_near(self, x, y, radius, ignore_cache=False): x1 = clamp(math.floor((x - radius) / self.grid_size), 0, len(self.grid[0]) - 1) x2 = clamp(math.ceil((x + radius) / self.grid_size), 0, len(self.grid[0]) - 1) y1 = clamp(math.floor((y - radius) / self.grid_size), 0, len(self.grid) - 1) y2 = clamp(math.ceil((y + radius) / self.grid_size), 0, len(self.grid) - 1) objs = [] for y in range(y1, y2 + 1): for x in range(x1, x2 + 1): objs.extend(self.grid[y][x]) return objs
def update(self, dt): if self.last_mouse_pos: if (self.last_mouse_pos.x > self._width * 0.2 and self.last_mouse_pos.x < self._width * 0.8 and self.last_mouse_pos.y < 25): self._scroll_y = clamp(self._scroll_y - dt * 150, 0, self.get_max_scroll()) self._generate_image() if (self.last_mouse_pos.x > self._width * 0.2 and self.last_mouse_pos.x < self._width * 0.8 and self.last_mouse_pos.y > self.frame_height + 25): self._scroll_y = clamp(self._scroll_y + dt * 150, 0, self.get_max_scroll()) self._generate_image() return super().update(dt)
def update(self, dt): self.time -= dt self.pos += self.vel * dt i = int((1 - self.time / self.initial_time) * self._num_frames) if self.time <= 0: self.kill() else: self.frame = clamp(i, 0, self._num_frames - 1)
def update(self, dt): if self.planet.owning_civ is None or not self.planet.owning_civ.is_player: self.visible = False else: self.visible = True if self.visible: target_interp = clamp( self.planet.population / self.planet.get_max_pop(), 0, 1) if target_interp < self.interp_value: self.interp_value = clamp(self.interp_value - dt * 0.5, target_interp, 1) self._generate_image() elif target_interp > self.interp_value: self.interp_value = clamp(self.interp_value + dt * 0.5, 0, target_interp) self._generate_image() return super().update(dt)
def _generate_image(self): z = clamp((self.time) / self.rate, 0, 1) t = 1 - ((1 - z)**2) self._width = self.base_width + t * 200 self._height = 100 self.image = pygame.Surface((self._width + 20, self._height), pygame.SRCALPHA) self._draw_text(self.image, self.line, self.color, V2(10, 0), self._width) self._recalc_rect()
def __init__(self, envs, bestPlayersToKeepFactor = 0.01, solvedFitness = None, mutationRate = 0.02): self.players = [Player(NeuralNetwork(4, 8, 1), env) for env in envs] self.envs = envs self.alive = len(envs) self.bestPlayersToKeepFactor = clamp(bestPlayersToKeepFactor, 0, 1) self.solvedFitness = solvedFitness self.mutationRate = mutationRate self.stepsSurvived = 0 self.populationSurvivors = 0
def position_nicely(self, scene): x = self.panel_for.x - self._width / 2 y = self.panel_for.y - self._height / 2 if x > game.RES[0] / 2: x = self.panel_for.x - self.panel_for._width / 2 - self._width - 10 else: x = self.panel_for.x + self.panel_for._width / 2 + 10 y = clamp(y, 2, game.RES[1] - self._height - 2 - 40) self.pos = V2(x,y) self._reposition_children()
def turret_angle(self, value): self._turret_target_angle = helper.clamp(value, *self._turret_angle_range)
def notify(self, event): """ Called by an event in the message queue. """ if isinstance(event, TickEvent): ticks = pygame.time.get_ticks() state = self.model.state # update the model pause state self.model.paused = self.view.transitioning # step the model if it is time if self.can_step_model(ticks, state): self.evman.Post(StepGameEvent()) # update the playtime countdown self.playtime_countdown(ticks, state) for event in pygame.event.get(): # always handle window closing events if event.type == QUIT: self.evman.Post(QuitEvent()) # all key downs if event.type == KEYDOWN: if event.key == K_F11: self.view.toggle_fullscreen() if state == STATE_MENU: self.menu_keys(event) elif state in (STATE_PHASE1, STATE_PHASE2): self.puzzle_keys(event) elif state in (STATE_PHASE3, STATE_REPRIEVE): self.arcade_keys(event) elif state == STATE_LEVELDONE: self.level_done_keys(event) elif state == STATE_HELP: self.help_keys(event) else: # allow escaping from unhandled states if (event.key in (K_ESCAPE, K_RETURN, K_SPACE)): self.model.escape_state() elif event.type == MOUSEBUTTONDOWN: if state in (STATE_PHASE3, STATE_REPRIEVE): pos = self.view.convert_screen_to_arcade(event.pos) self.model.fire_missile(pos) elif event.type == MENU_TICK_EVENT: self.view.menu_ticker_step() elif isinstance(event, StateEvent): # reset the time passed counter on state changes self.time_left = PLAYTIME.get(self.model.state, 0) # set the arcade speed per level speed_index = helper.clamp(self.model.level - 1, 0, len(ARCADE_SPEEDS) - 1) self.arcade_update_freq = ARCADE_SPEEDS[speed_index] # set the puzzle speed per level speed_index = helper.clamp(self.model.level - 1, 0, len(PUZZLE_SPEEDS) - 1) self.puzzle_update_freq = PUZZLE_SPEEDS[speed_index] # set a menu timer for text animations if event.state == STATE_MENU: pygame.time.set_timer(MENU_TICK_EVENT, 1000) else: pygame.time.set_timer(MENU_TICK_EVENT, 0)