def part2(rows): slope1 = part1(rows, v.Vector2D(1,1)) slope2 = part1(rows, v.Vector2D(3,1)) slope3 = part1(rows, v.Vector2D(5,1)) slope4 = part1(rows, v.Vector2D(7,1)) slope5 = part1(rows, v.Vector2D(1,2)) return slope1 * slope2 * slope3 * slope4 * slope5
def __init__(self): ss = config.sprite_size super(Paddle, self).__init__() y = config.play_size.h - 3 * ss x = config.play_size.x self.pos = vector.Vector2D(x, y) self.vel = vector.Vector2D(0, 0) self.rect = pygame.Rect(x, y, 6 * ss, 2 * ss) self.image = racket_image self.ttn = config.firing_interval
def update(self, time, blocks): nextpos = self.pos + (self.vel * time) self.lastTime += time if self.lastTime > Ball.ttn: self.image = self.animation[self.animation.currentFrame()] self.animation.nextFrame() self.lastTime %= Ball.ttn """ interact with walls """ nextpos = self.border_collision(nextpos, config.play_size) """ interact with blocks """ bounced = False speed = self.vel.magnitude() for block in blocks: v = Ball._bounce(nextpos, self.radius, block.rect) if v is not False and v != (0, 0): bounced = True # adjust velocity vec = vector.Vector2D(v[0], v[1]) vec *= speed self.vel = vec # add code here for what happens to the blocks # ... if bounced: # sounds, animations or something pass self.pos = nextpos self.rect.x = self.pos.x self.rect.y = self.pos.y
def __init__(self, position, velocity): if Ball.sprites == None: Ball.__load_graphics() self.radius = 24 / 2 self.animation = Ball.animation self.image = self.animation[0] self.rect = self.image.get_rect() self.pos = vector.Vector2D(position[0], position[1]) self.vel = vector.Vector2D(velocity[0], velocity[1]) super(Ball, self).__init__() # config.sounds['ball.wav'].play() self.lastTime = 0
def create_random_asteroid(width, height): data = AsteroidData(vector.Vector2D(random.randint(1, width), \ random.randint(1, height)), \ np.random.normal(ASTEROID_MASS_MEAN, ASTEROID_MASS_STD)) sprite = arcade.Sprite(ASTEROID_FILE_PATH, MapMassToScale(data.mass)) sprite.center_x = data.center.x sprite.center_y = data.center.y sprite.radians = random.uniform(0, 2 * math.pi) return data, sprite
def on_key_release(self, symbol, modifiers): if arcade.key.W == symbol: self.rocket_acceleration = vector.Vector2D(0, 0) self.ship_fire.remove_from_sprite_lists() self.ship_list.append(self.ship) self.ship_engine_on = False if arcade.key.A == symbol: self.radian_speed = 0 if arcade.key.D == symbol: self.radian_speed = 0
def setup(self): # ship state self.position = self.init_position.copy() self.velocity = vector.Vector2D(0, 0) self.gravity_acceleration = vector.Vector2D(0, 0) self.rocket_acceleration = vector.Vector2D(0, 0) self.ship_engine_on = False self.radians = 0 self.radian_speed = 0 # create the ship off sprite self.ship.radians = 0 self.ship.center_x = self.position.x self.ship.bottom = self.position.y # create the ship on sprite self.ship_fire.center_x = self.position.x self.ship_fire.top = self.position.y # keep the ship sprites in a sprite list which is faster later self.ship_list = arcade.SpriteList() self.ship_list.append(self.ship)
def part1(rows, operation): width = len(rows[0]) - 1 pos = v.Vector2D(0, 0) treecount = 0 while(pos.y != len(rows) - 1): pos = pos + operation pos.x = pos.x % width if(rows[pos.y][pos.x] == "#"): treecount += 1 return treecount
def on_key_press(self, symbol, modifiers): if arcade.key.W == symbol: self.rocket_acceleration = vector.Vector2D(-math.sin(self.radians), math.cos(self.radians)) self.rocket_acceleration = vector.Multipy(self.rocket_acceleration, ACCELERATION_ROCKET) self.ship_list.append(self.ship_fire) self.ship.remove_from_sprite_lists() self.ship_engine_on = True if arcade.key.A == symbol: self.radian_speed = ANGULAR_SPEED if arcade.key.D == symbol: self.radian_speed = -ANGULAR_SPEED
def generate_random_ship(self): self.ship = arcade.Sprite(SHIP_PATH, self.scale) self.ship_fire = arcade.Sprite(SHIP_FIRE_PATH, self.scale) minX = int(self.ship_fire.width / 2) maxX = int(self.width - self.ship_fire.width / 2) minY = int(self.ship_fire.height / 2) maxY = int(self.height - self.ship_fire.height / 2) self.init_position = vector.Vector2D(random.randint(minX, maxX), random.randint(minY, maxY)) self.position = self.init_position.copy() self.ship_fire.center_x = self.position.x self.ship_fire.center_y = self.position.y self.ship.center_x = self.position.x self.ship.center_y = self.position.y
def compute_asteriod_to_point_gravity(self, position): result = vector.Vector2D(0, 0) for dp in self.asteroid_data: direction = vector.Add(result, vector.Subtract(dp.center, position)) length = direction.length() # prevent division by small numebers from blowing stuff up if length > 50: direction.make_unit() else: length = 50 direction = vector.Multipy(direction, 1.0 / length) result = vector.Add( result, vector.Multipy( direction, dp.mass * ACCELERATION_GRAVITY / (length * length))) return result
def on_land(self): self.velocity = vector.Vector2D(0, 0)
def part1(rows, operation): width = len(rows[0]) - 1 pos = v.Vector2D(0, 0) treecount = 0 while(pos.y != len(rows) - 1): pos = pos + operation pos.x = pos.x % width if(rows[pos.y][pos.x] == "#"): treecount += 1 return treecount def part2(rows): slope1 = part1(rows, v.Vector2D(1,1)) slope2 = part1(rows, v.Vector2D(3,1)) slope3 = part1(rows, v.Vector2D(5,1)) slope4 = part1(rows, v.Vector2D(7,1)) slope5 = part1(rows, v.Vector2D(1,2)) return slope1 * slope2 * slope3 * slope4 * slope5 if __name__ == "__main__": with open("input.txt") as f: rows = f.readlines() print(part1(rows, v.Vector2D(3,1))) print(part2(rows))
key2action["RIGHT"] = E key2action["LEFT"] = W # Rotation and vector mappings ROTATION = [None] * (Action.num_actions - 1) VECTOR = [None] * (Action.num_actions - 1) offset_angle = 22.5 current_angle = 0.00001 for act in Action.all_actions: if act != Action.ST: ROTATION[act] = current_angle x = math.cos(coord.Coord.to_radians(-current_angle)) y = math.sin(coord.Coord.to_radians(-current_angle)) VECTOR[act] = vector.Vector2D(x, y) VECTOR[act].normalize() current_angle += offset_angle OBLIQUE_DIR_CLOCKWISE = [None] * (Action.num_actions - 1) OBLIQUE_DIR_ANTI_CLOCKWISE = [None] * (Action.num_actions - 1) for idx in range(Action.num_directions): act = Action.all_directions[idx] jdx = (idx + 4) % Action.num_directions OBLIQUE_DIR_CLOCKWISE[act] = Action.all_directions[jdx] jdx = (idx - 4) if jdx < 0: jdx += Action.num_directions OBLIQUE_DIR_ANTI_CLOCKWISE[act] = Action.all_directions[jdx]