def process_bullet(bullet, dt, screen_size):
    """Update bullet position for a delta_t."""
    new_bullet = lib.clone_struct(bullet)
    screen_width, screen_height = screen_size
    k = dt * bullet.speed
    new_bullet.x = bullet.x + k * math.cos(geometry.torad(bullet.angle))
    new_bullet.y = bullet.y - k * math.sin(geometry.torad(bullet.angle))
    if (new_bullet.x >= 0 and new_bullet.x < screen_width and
            new_bullet.y >= 0 and new_bullet.y < screen_height):
        return new_bullet
def fire_bullet(robot, field):
    """Create a bullet for robot with a given speed and add it field.bullets."""
    if robot.time_to_fire:
        return
    absolute_angle = robot.angle + robot.turret_angle
    turret_length = robot.height / 1.5
    x = robot.x + turret_length * math.cos(geometry.torad(absolute_angle))
    y = robot.y - turret_length * math.sin(geometry.torad(absolute_angle))
    bullet = Bullet(x=x, y=y, angle=absolute_angle, origin=robot.name,
        speed=field.config["robot"]["bullet_speed"])
    return bullet
def process_robot(robot, dt, screen_size):
    """Get new robot position and rotation attributes for a time-delta."""
    new_robot = lib.clone_struct(robot)
    screen_width, screen_height = screen_size            
    body_width, body_height = robot.width, robot.height
    new_robot.x = robot.x + dt * robot.speed * math.cos(geometry.torad(robot.angle))
    if new_robot.x - body_width/2.0 < 0:
        new_robot.x = body_width/2.0
    elif new_robot.x + body_width/2.0 >= screen_width:
        new_robot.x = screen_width - body_width/2.0
    new_robot.y = robot.y - dt * robot.speed * math.sin(geometry.torad(robot.angle))
    if new_robot.y < body_height/2.0:
        new_robot.y = body_height/2.0
    elif new_robot.y + body_height/2.0 >= screen_height:
        new_robot.y = screen_height - body_height/2.0                
    new_robot.angle = geometry.normalize_angle(robot.angle + dt * robot.rotation)    
    return new_robot
def get_polygon_for_robot(robot):
    """Get exact polygon (points) of robot with rotation.""" 
    w2 = robot.width / 2.0
    h2 = robot.height / 2.0
    alpha = geometry.torad(-robot.angle)
    dx2 = (w2*math.cos(alpha), -w2*math.sin(-alpha)) 
    dy2 = (h2*math.sin(-alpha), h2*math.cos(alpha))
    center = (robot.x, robot.y)
    p1 = geometry.sum_vectors([center, geometry.invert_vector(dx2), 
        geometry.invert_vector(dy2)])
    p2 = geometry.sum_vectors([center, geometry.invert_vector(dx2), dy2])
    p3 = geometry.sum_vectors([center, dx2, dy2])
    p4 = geometry.sum_vectors([center, dx2, geometry.invert_vector(dy2)])
    return [p1, p2, p3, p4]