def draw_view_slow(surface, board, block_size, player): scan_steps = 200 max_scan_distance = screen.get_width() for x in range(0, surface.get_width()): angle = ((x / surface.get_width()) - 0.5) * player.fov + player.angle # absolute map angle horizon_point = max_scan_distance * V2(cos(angle), sin(angle)) step = (horizon_point - player.pos) / scan_steps # scan along ray, checking for intersections scan_pos = V2(player.pos.x, player.pos.y) # copy player position for i in range(scan_steps): scan_pos += step grid_x, grid_y = int(scan_pos.x / block_size), int(scan_pos.y / block_size) if grid_x < 0 or grid_y < 0: break if grid_x >= len(board[0]) or grid_y >= len(board): break if board[grid_y][grid_x]: distance = player.pos.distance_to(scan_pos) n = distance / max_scan_distance color = (n * 255, n * 255, n * 255) height = 20000 / distance pg.draw.line(surface, color, (x, surface.get_height() // 2 - height // 2), (x, surface.get_height() // 2 + height // 2), 1) break
def draw_move_fight(self): if self.battle.fight_t == self.start_to_run: self.vel = self.final_vel if self.target is None or self.target.dead: self.draw_move_fight_notarget() self.time_frome_last_direction_change += 1 return target_pos = V2(self.target.rect.center) self_pos = V2(self.rect.center) delta = target_pos - self_pos self.refresh_dxdy(delta.x, delta.y) ######################################################################## near_target = abs(delta.x) < DFIGHT and abs(delta.y) < DFIGHT if near_target: #fighting (unit doesnt move) if self.time_frome_last_direction_change > NFRAMES_DIRECTIONS: self.refresh_direction_target() frame = self.get_frame_near_target() if not self.target.dead and not self.dead: self.fight_against_target_near() else: #walking (so we have to move the unit) frame = self.refresh_not_near_target() self.rect.center = self.pos frame += self.isprite img = self.unit.imgs_z_t[self.z][frame] self.time_frome_last_direction_change += 1
def reset_to_start(self): self.heartbeat = 0 for node in self.nodes: node.pos = V2(self.starting_positions[node]) node.vel = V2(0, 0) for muscle in self.muscles: dist = muscle.node_a.pos.distance_to(muscle.node_b.pos) muscle.desired_length = dist
def process_laser(self): if self.id > 1: if p.game.laser > 0: dx = abs(self.pos.x - p.game.hero.pos.x) if dx < (p.LASER_W + self.rect.w) // 2: self.life = -1 if p.DEBRIS: graphics.generate_debris_hit(V2(self.pos), V2(self.vel), self.debris)
def shoot(self, vel): if self.bullets > 0: if p.SOUND: p.game.sounds.bullet.play() if len(p.game.bullets) > p.MAX_BULLET_NUMBER: p.game.bullets.popleft() v = V2(vel) p.game.bullets.append( Bullet(V2(self.pos) - p.BULLET_SIZE_ON_2, v, self.id)) self.bullets -= 1
def __init__(self, pos, mass, radius=None, color=None, label=None): self.mass = mass self.radius = radius if radius else (mass**(1 / 2)) / BODY_DENSITY self.color = (randint(127, 255), randint(127, 255), randint(127, 255)) self.pos = V2(pos) self.vel = V2(0, 0) self.label = label self.locked = False self.charge = self.mass # directly proportional
def shoot_rocket(self, vel): if self.rockets > 0: if p.SOUND: p.game.sounds.rocket.play() if len(p.game.rockets) > p.MAX_ROCKET_NUMBER: p.game.rockets.popleft() v = V2(vel) p.game.rockets.append( Rocket(V2(self.pos) - p.ROCKET_SIZE_ON_2, v, self.id)) self.rockets -= 1
def generate(self, q, n, v_range, omega_range, angle_range): angle_range = (angle_range[0] * 100, angle_range[1] * 100) v_range = (v_range[0] * 100, v_range[1] * 100) for i in range(n): angle = random.randint(angle_range[0], angle_range[1]) / 100. velocity = random.randint(v_range[0], v_range[1]) / 100. omega = random.randint(omega_range[0], omega_range[1]) v = V2(0, velocity).rotate(angle) ## print(" ==>", self.color, q) self.debris.append(Debris(V2(q), v, omega, self))
def __init__(self, pos): self.pos = V2(pos) # Pixels self.vel = V2(0, 0) # Pixels / Frame self.turn_vel = 0.0 # Radians / Frame self.angle = 0.0 # Radians self.radius = 20 self.speed = 1 # Pixels / Frame self.turn_speed = 0.1 # Radians / Frame self.fov = 2 * pi * 0.3 # Radians
def draw_view_proper(surface, board, player): walls = [ V2(x, y) for x in range(len(board[0])) for y in range(len(board)) if board[y][x] ] # print(walls) dist_from_screen = (surface.get_width() / 2) / tan(player.fov / 2) for x in range(0, surface.get_width()): x_angle = atan2(x - surface.get_width() // 2, dist_from_screen) sight_ray = (player.pos, (player.pos.x + 1000 * cos(x_angle + player.angle), player.pos.y + 1000 * sin(x_angle + player.angle))) # print("sight ray:", sight_ray) for wall in walls: sides = [(wall * block_size, (wall + V2(1, 0)) * block_size), (wall * block_size, (wall + V2(0, 1)) * block_size), ((wall + V2(1, 0)) * block_size, (wall + V2(1, 1)) * block_size), ((wall + V2(0, 1)) * block_size, (wall + V2(1, 1)) * block_size)] hits = [] for side in sides: intersection = line_intersection(side, sight_ray) if intersection is not None: if V2(intersection).distance_to(player.pos) > 0.00001: hits.append( (side, V2(intersection).distance_to(player.pos))) closest_side, distance = min(hits, key=lambda x: x[1]) print(closest_side, distance, "\n")
def process_bullets(self): for bullet in p.game.bullets: if bullet.from_id != self.id: if bullet.visible: if self.collide(bullet.pos): bullet.visible = False self.life -= 1 if self.debris and p.DEBRIS: graphics.generate_debris_hit( V2(bullet.pos + (0, -10)), V2(bullet.v), self.debris)
def __init__(self): self.lm = None self.cell_rect = pygame.Rect(0, 0, 0, 0) self.e_hmap = None self.box_hmap = None self.campos = V2() self.rcam = None self.rmouse = None self.world_size = V2() self.nx, self.ny = 0, 0 self.img_hmap = None
def draw_view_fast(surface, board, block_size, player): # Draw Scenery pg.draw.rect(surface, (135, 206, 250), pg.Rect(0, 0, surface.get_width(), surface.get_height() // 2), 0) # Sky pg.draw.rect(surface, (87, 59, 12), pg.Rect(0, surface.get_height() // 2, surface.get_width(), surface.get_height() // 2), 0) # Ground # Draw Walls focal_length = (surface.get_width() / 2) / tan(player.fov / 2) scan_steps = 300 secondary_scan_steps = 30 max_scan_distance = 1000 for x in range(0, surface.get_width()): angle = ((x / (surface.get_width() - 1)) - 0.5) * player.fov + player.angle # absolute map angle horizon_point = max_scan_distance * V2(cos(angle), sin(angle)) step = (horizon_point - player.pos) / scan_steps secondary_step = -step / secondary_scan_steps # scan along ray, checking for intersections scan_pos = V2(player.pos.x, player.pos.y) # copy player position for _ in range(scan_steps): scan_pos += step grid_x, grid_y = int(scan_pos.x / block_size), int(scan_pos.y / block_size) if grid_x < 0 or grid_y < 0: break if grid_x >= len(board[0]) or grid_y >= len(board): break if board[grid_y][grid_x]: # After finding a rough intersection point, scan backwards along the ray with a smaller step until open square is found for _ in range(secondary_scan_steps): scan_pos += secondary_step grid_x, grid_y = int(scan_pos.x / block_size), int( scan_pos.y / block_size) if not board[grid_y][grid_x]: distance = player.pos.distance_to(scan_pos) n = distance / max_scan_distance n = max(0.1, n) color = (n * 255, n * 255, n * 255) # height = 20000 / distance effective_distance = distance * cos(angle - player.angle) height = block_size / effective_distance * focal_length pg.draw.line( surface, color, (x, surface.get_height() // 2 - height // 2), (x, surface.get_height() // 2 + height // 2), 1) break break
def __init__(self, pos=None, vel=None): if pos: self.pos = V2(pos) else: self.pos = V2(0, 0) if vel: self.vel = V2(vel) else: self.vel = V2(0, 0) self.starting_vel = V2(self.vel)
def draw_aarectangle(surf, rect, color, width): width += 1 - width % 2 off_x = V2(width // 2, 0) off_y = V2(0, width // 2) lines = [ (rect.topleft + off_y, rect.topright + off_y), (rect.topright - off_x - (1, 0), rect.bottomright - off_x - (1, 0)), (rect.bottomright - off_y - (0, 1), rect.bottomleft - off_y - (0, 1)), (rect.bottomleft + off_x, rect.topleft + off_x) ] for line in lines: draw_aaline(surf, *line, color, width)
def process_mouse_navigation(self): #cam can move even with no mousemotion! if pygame.key.get_mods() & pygame.KMOD_LSHIFT: pos = pygame.mouse.get_pos() d = V2(pos) - self.cam.map_rect.center if d != (0,0): intensity = 2e-8*d.length_squared()**1.5 if intensity > 1.: intensity = 1. d.scale_to_length(intensity) delta = V2(self.cam.correct_move(d)) self.cam.move(delta) self.cam.set_mg_pos_from_rcam() self.ap.add_alert_countdown(self.e_ap_move, guip.DELAY_HELP * self.fps)
def __init__(self, battle, unit, direction, zoom_level, pos): self.battle = battle self.terrain_bonus = unit.get_terrain_bonus() self.unit = unit self.z = zoom_level self.rect = self.unit.imgs_z_t[self.z][0].get_rect() self.rect.center = pos self.pos = V2(pos) self.init_pos = V2(pos) self.direction = direction ## self.final_vel = self.unit.max_dist * ANIM_VEL * (0.8 + random.random()/3.) self.final_vel = ANIM_VEL * (0.8 + random.random() / 3.) self.vel = self.final_vel self.tandom = None self.target = None self.opponents = None self.friends = None self.targeted_by = [] # self.next_to_target = False self.time_frome_last_direction_change = 1000 self.cannot_see = random.random() self.final_stage = False # self.dxdy = 0, 0 self.start_to_run = random.randint(0, 1000) self.frame = 0 self.frame0 = random.randint(0, 12) self.nframes = None self.isprite = None self.z = self.z self.direction = "die" self.refresh_sprite_type() self.dead_img = self.unit.imgs_z_t[self.z][self.isprite + self.nframes - 1] self.direction = "head" self.refresh_sprite_type() irand = random.randint(0, self.nframes - 1) self.head = self.unit.imgs_z_t[self.z][self.isprite + irand] dhx = random.randint(self.battle.cell_size // 2, self.battle.cell_size) dhy = random.choice([-1, 1]) * random.randint( 0, self.battle.cell_size // 4) self.delta_head = (dhx, dhy) if unit.str_type == "wizard": self.delta_head = None self.direction = LEFT self.refresh_sprite_type() self.dead = False global ID self.id = ID ID += 1
def __init__(self, pos, vel, mass, color=None, elasticity=None): self.pos = V2(pos) self.vel = V2(vel) self.mass = mass if color: self.color = color else: self.color = (randint(0, 255), randint(0, 255), randint(0, 255)) if elasticity: self.elasticity = elasticity else: self.elasticity = 1.0
def agitate(self): n = len(self.bodies) forces = [ V2(uniform(-1, 1), uniform(-1, 1)) * AGITATION_MAGNITUDE for _ in range(n) ] # ensure total momentum remains zero total_force = V2(0, 0) for f in forces: total_force += f offset = total_force / n for f in forces: f -= offset for b, f in zip(self.bodies, forces): b.apply_impulse(f, 1)
def ia(self): if self.collide_hero(): self.container_action() self.life = -1 self.can_explode = False else: self.vel += V2(0, 1) * p.ENGINE_FORCE_IA * self.speed
def draw(self): # refresh screen self.e_background.blit() for s in self.ships: if s.shadow: self.screen.blit(s.shadow, V2(s.rect.topleft)+p.SHADOW_POS) self.screen.blit(s.img, s.rect)
def refresh(self): self.process_physics() self.move(self.vel) self.process_bullets() self.process_rockets() self.process_laser() if self.life <= 0: #if is dying p.game.ships.remove(self) if self is p.game.hero: p.game.hero_dead.activate() p.game.add_alert("dead", duration=400, pos=(p.W / 2, p.H / 2)) elif self.is_friend: if self.collide_hero(): self.can_explode = False self.debris = None p.game.add_alert("item", pos=self.pos) elif self.mesh.id in p.game.hints_ids: if self.rect.bottom < p.game.hero.rect.top: p.game.add_alert("nice", pos=self.pos) p.game.score += 1 else: if self.rect.bottom < p.game.hero.rect.top: p.game.add_alert("bad", pos=self.pos) p.game.ennemy_prob += p.PROB_INCREASE print(p.game.ennemy_prob) if self.debris and p.DEBRIS: graphics.generate_debris_explosion(V2(self.pos), self.debris) if self.can_explode: if p.DEBRIS: graphics.add_explosion(self) self.at_explode()
def get_coord_at_pix(self, pix): pos = V2(self.get_dpix()) + pix - self.map_rect.topleft pos.x *= self.nx / self.map_rect.w pos.y *= self.ny / self.map_rect.h ## return (int(pos.x) + self.lm.current_x - 1, ## int(pos.y) + self.lm.current_y - 1) return (int(pos.x) + self.lm.current_x, int(pos.y) + self.lm.current_y)
def collide_walls(self, room_width, room_height, g): old_vel = V2(self.vel) hit = False if self.pos.x < self.radius: self.vel.x *= -self.elasticity self.pos.x = self.radius hit = True elif self.pos.x > room_width - self.radius: self.vel.x *= -self.elasticity self.pos.x = room_width - self.radius self.play_collision_sound((self.vel - old_vel).length()) hit = True if self.pos.y < self.radius and self.vel.y < 0: # correct velocity by removing energy gained between actual collision and collision detection ke = self.vel.y**2 - 2 * g * (self.radius - self.pos.y) self.vel.y = -(abs(ke)**0.5) self.vel.y *= -self.elasticity self.pos.y = self.radius hit = True # elif self.pos.y > room_height - self.radius: # self.vel.y *= -self.elasticity # self.pos.y = room_height - self.radius # hit = True if hit: self.play_collision_sound((self.vel - old_vel).length()) return hit
def process_mouse_navigation( self): #cam can move even with no mousemotion! if pygame.key.get_mods() & pygame.KMOD_LSHIFT: pos = pygame.mouse.get_pos() center_of_map_layout = self.W // 2, self.H // 2 d = V2(pos) - center_of_map_layout if d != (0, 0): intensity = 1e-6 * d.length_squared()**1.5 if intensity > 1.: intensity = 1. d.scale_to_length(intensity) #comment out if map is limited in space: ## marginy = -2 ## marginx = -5 ## nx_displayable = self.W // self.cell_size ## ny_displayable = self.H // self.cell_size ## if self.cam.pos_cells.x + nx_displayable + marginx > self.cam.nx and d.x > 0: ## d.x = 0 ## if self.cam.pos_cells.x - marginy < 0 and d.x < 0: ## d.x = 0 ## if self.cam.pos_cells.y + ny_displayable + marginx > self.cam.ny and d.y > 0: ## d.y = 0 ## elif self.cam.pos_cells.y - marginy < 0 and d.y < 0: ## d.y = 0 delta = d * MOUSE_NAV_FACTOR self.cam.move(delta) self.delta_cam += delta self.cam.refresh_gm_pos() for e in self.alert_elements: self.ap.add_alert_countdown(e, guip.DELAY_HELP * self.fps) self.refresh_neigh_maps()
def func_reac_mousemotion(self, e): ## if pygame.key.get_mods() & pygame.KMOD_CTRL: if pygame.mouse.get_pressed()[0]: delta = -V2(e.rel) #/self.cam.cell_rect.w #assuming square cells self.move_cam_and_refresh(delta) self.cell_info.last_cell_clicked = self.cam.get_cell_at_pix(e.pos) for e in self.alert_elements: self.ap.add_alert_countdown(e, guip.DELAY_HELP * self.fps)
def __init__(self, cam, sg, coord, delta): self.cam = cam self.sg = sg self.coord = coord self.delta = delta self.pos = self.cam.get_rect_at_coord(self.coord).center self.old_pos = V2(self.pos) self.refresh_pos()
def __init__(self, pos): super().__init__() self.image = pygame.Surface((5, 5)) self.image.fill('white') self.rect = self.image.get_rect(center=scale(WIN_SIZE, 2)) self.speed = randint(10, 50) * V2(0.5 - random.random(), 0.5 - random.random()).normalize() self.pos = pos
def __init__(self): self.heading = math.radians(random.uniform(0, 359)) self.velocity = V2([math.cos(self.heading), math.sin(self.heading)]) * random.uniform(.5, 1) self.size = random.randint(STAR_SIZE[0], STAR_SIZE[1]) self.center = V2([SIZE[0] / 2, SIZE[1] / 2]) self.center += V2(math.cos(self.heading), math.sin( self.heading)) * random.uniform(*CENTER_RADIUS) ColorType = random.choice([0] * 2 + [1] * 100 + [2] * 200) if ColorType == 0: self.color = (random.randint(175, 255), random.randint(0, 100), 0) if ColorType == 1: self.color = (random.randint(0, 50), random.randint(130, 255), random.randint(175, 255)) if ColorType == 2: self.color = (220, random.randint(210, 255), random.randint(210, 255))
def ia(self): r = self.rect if r.colliderect(p.game.hero.rect): p.game.hero.life = -1 self.life = -1 if random.random() < 0.01: self.shoot( V2(p.game.hero.pos - self.pos).normalize() * p.BULLET_SPEED / 2.)