コード例 #1
0
def gasgraph_loop(limit_mass):
    done = False
    data = {}
    text_mass = 'Mass: N/A'
    text_radius = 'Radius: N/A'
    text_density = 'Density: N/A'
    invalid = True

    fondo = display.set_mode((ANCHO, ALTO), SCALED)
    fondo.fill(COLOR_BOX)

    numbers = WidgetGroup()
    for i in [i for i in range(len(radius_keys[:4]))]:
        n = Number(radius_imgs[i], x=i * 28 + 30, y=3)
        numbers.add(n)
        exes.append(n.rect.centerx)

    for i in [i + 4 for i in range(len(radius_keys[4:14]))]:
        n = Number(radius_imgs[i], x=i * 27 + 26, y=3)
        numbers.add(n)
        exes.append(n.rect.centerx)

    for i in [i + 14 for i in range(len(radius_keys[14:]))]:
        n = Number(radius_imgs[i], x=i * 22 + 90, y=3)
        numbers.add(n)
        exes.append(n.rect.centerx)

    for i in [i for i in range(len(mass_keys))]:
        n = Number(mass_imgs[i], right=30, centery=i * 20 + 21)
        numbers.add(n)
        yes.append(n.rect.centery)

    x = exes[radius_keys.index(1.02)]
    y = yes[mass_keys.index(2)]

    rect_super = Rect(31, y, x - 3, (img_rect.h / 2) - 60)
    rect_puffy = Rect(x + 28, 16, (img_rect.w / 2) + 100, y - 16)
    rect_giant = Rect(31, 16, x - 3, y - 16)

    lim_y = find_and_interpolate(limit_mass, mass_keys, yes)
    lim_rect = Rect(31, lim_y, img_rect.w, img_rect.h - lim_y + img_rect.y)
    lim_img = Surface(lim_rect.size)
    lim_img.set_alpha(150)

    lineas = WidgetGroup()
    linea_h = Linea(img_rect, img_rect.x, img_rect.centery, img_rect.w, 1,
                    lineas)
    linea_v = Linea(img_rect, img_rect.centerx, img_rect.y, 1, img_rect.h,
                    lineas)
    punto = Punto(img_rect, img_rect.centerx, img_rect.centery, lineas)

    move_x, move_y = True, True
    while not done:
        for e in event.get(
            [KEYDOWN, KEYUP, QUIT, MOUSEBUTTONDOWN, MOUSEMOTION]):
            if (e.type == KEYDOWN and e.key == K_ESCAPE) or e.type == QUIT:
                quit()
                exit()

            elif e.type == MOUSEMOTION:
                px, py = e.pos

                if move_y:
                    linea_h.move_y(py)
                    punto.move_y(py)
                if move_x:
                    linea_v.move_x(px)
                    punto.move_x(px)

                dx, dy = punto.rect.center
                valid = [
                    rect_puffy.collidepoint(dx, dy),
                    rect_giant.collidepoint(dx, dy),
                    rect_super.collidepoint(dx, dy)
                ]
                off_limit = lim_rect.collidepoint(dx, dy)

                if img_rect.collidepoint(px,
                                         py) and any(valid) and not off_limit:
                    invalid = False
                    mass = round(
                        find_and_interpolate(linea_h.rect.y + 1, yes,
                                             mass_keys), 5)
                    radius = round(
                        find_and_interpolate(linea_v.rect.x, exes,
                                             radius_keys), 3)
                    clase = 'Puffy Giant' if valid[0] else ''
                    clase = 'Gas Giant' if valid[1] else clase
                    clase = 'Super Jupiter' if valid[2] else clase
                    data.update({
                        'mass': mass,
                        'radius': radius,
                        'clase': clase,
                        'albedo': 42.25
                    })

                    d = round(density(mass, radius), 5)
                    text_mass = 'Mass: {}'.format(mass)
                    text_radius = 'Radius: {}'.format(radius)
                    text_density = 'Density: {}'.format(d)

                else:
                    invalid = True
                    text_mass = 'Mass: N/A'
                    text_radius = 'Radius: N/A'
                    text_density = 'Density: N/A'

            elif e.type == MOUSEBUTTONDOWN:
                if e.button == 1:
                    done = True

            elif e.type == KEYDOWN and not invalid:
                if e.key == K_SPACE:
                    done = True

                elif e.key == K_LSHIFT:
                    move_x = False

                elif e.key == K_LCTRL:
                    move_y = False

            elif e.type == KEYUP:
                if e.key == K_LSHIFT:
                    move_x = True

                elif e.key == K_LCTRL:
                    move_y = True

        render_mass = fuente2.render(text_mass, True, COLOR_TEXTO, COLOR_BOX)
        render_radius = fuente2.render(text_radius, True, COLOR_TEXTO,
                                       COLOR_BOX)
        render_density = fuente.render(text_density, True, COLOR_TEXTO,
                                       COLOR_BOX)

        fondo.fill(COLOR_BOX)
        fondo.blit(render_mass, (3, ALTO - 20))
        fondo.blit(render_radius, (150, ALTO - 20))
        fondo.blit(render_density, (300, ALTO - 20))

        fondo.blit(img, img_rect)
        fondo.blit(lim_img, lim_rect)
        numbers.draw(fondo)
        lineas.update()
        lineas.draw(fondo)
        display.update()

    display.quit()
    return data
コード例 #2
0
def dwarfgraph_loop(limit_mass=None):
    fondo = display.set_mode((ANCHO, ALTO), SCALED)
    fondo.fill(COLOR_BOX)

    fuente2 = font.SysFont('Verdana', 13, bold=True)

    render = fuente2.render('Mass', True, COLOR_TEXTO, COLOR_BOX)
    render = transform.rotate(render, -90)
    render_rect = render.get_rect(right=ANCHO - 53, centery=ALTO / 2)
    fondo.blit(render, render_rect)

    render = fuente2.render('Radius', True, COLOR_TEXTO, COLOR_BOX)
    render_rect = render.get_rect(x=3, y=3)
    fondo.blit(render, render_rect)

    text_mass = 'Mass: N/A'
    text_radius = 'Radius: N/A'
    text_density = 'Density: N/A'

    done = False
    data = {}

    lineas = WidgetGroup()
    linea_h = Linea(bg_rect, bg_rect.x, bg_rect.centery, bg_rect.w, 1, lineas)
    linea_v = Linea(bg_rect, bg_rect.centerx, bg_rect.y, 1, bg_rect.h, lineas)
    punto = Punto(bg_rect, bg_rect.centerx, bg_rect.centery, lineas)

    lim_img, lim_rect = None, None
    if limit_mass is not None:
        lim_y = find_and_interpolate(limit_mass, mass_keys, yes)
        lim_rect = Rect(54, lim_y + 26, bg_rect.w,
                        bg_rect.h - lim_y - 26 + bg_rect.y)
        lim_img = Surface(lim_rect.size)
        lim_img.set_alpha(150)

    move_x, move_y = True, True
    while not done:
        for e in event.get(
            [KEYDOWN, QUIT, MOUSEMOTION, MOUSEBUTTONDOWN, KEYUP]):
            if (e.type == KEYDOWN and e.key == K_ESCAPE) or e.type == QUIT:
                quit()
                exit()

            elif e.type == MOUSEMOTION:
                x, y = e.pos

                if move_y:
                    linea_h.move_y(y)
                    punto.move_y(y)

                if move_x:
                    linea_v.move_x(x)
                    punto.move_x(x)

                dx, dy = punto.rect.center
                if bg_rect.collidepoint(dx, dy):
                    mass = round(
                        find_and_interpolate(linea_h.rect.y - 26, yes,
                                             mass_keys), 5)
                    radius = round(
                        find_and_interpolate(linea_v.rect.x, exes,
                                             radius_keys), 3)

                    data.update({
                        'mass': mass,
                        'radius': radius,
                        'clase': 'Dwarf Planet',
                        'albedo': 40
                    })
                    d = density(mass, radius)
                    text_mass = 'Mass: {}'.format(mass)
                    text_radius = 'Radius: {}'.format(radius)
                    text_density = 'Density: {}'.format(d)
                    color = bg.get_at((dx - 54, dy - 24))
                    composition = {}
                    s, i, r = 0, 0, 0
                    if tuple(color) == (63, 223, 0, 255):
                        i = 100
                    elif tuple(color) == (31, 255, 0,
                                          255):  # 10% silicates, 90% water ice
                        i = round(roll(60, 90), 2)
                        s = round(100 - i, 2)
                    elif tuple(color) == (95, 191, 0,
                                          255):  # 60% silicates, 40% water ice
                        s = round(roll(50, 60), 2)
                        i = round(100 - s, 2)
                    elif tuple(color) == (127, 159, 0,
                                          255):  # 90% silicates, 10% water ice
                        s = round(roll(60, 90), 2)
                        i = round(100 - s, 2)
                    elif tuple(color) == (159, 127, 0,
                                          255):  # 90% silicates, 10% iron
                        s = round(roll(60, 90), 2)
                        r = round(100 - s, 2)
                    elif tuple(color) == (191, 95, 0,
                                          255):  # 60% silicates, 40% iron
                        s = round(roll(50, 60), 2)
                        r = round(100 - s, 2)
                    elif tuple(color) == (223, 63, 0,
                                          255):  # 10% silicates, 90% iron
                        r = round(roll(60, 90), 2)
                        s = round(100 - r, 2)
                    elif tuple(color) == (255, 31, 0, 255):
                        r = 100

                    if s:
                        composition['silicates'] = s
                    if i:
                        composition['water ice'] = i
                    if r:
                        composition['iron'] = r

                    data['composition'] = composition
                else:
                    text_mass = 'Mass: N/A'
                    text_radius = 'Radius: N/A'
                    text_density = 'Density: N/A'

            elif e.type == MOUSEBUTTONDOWN:
                if e.button == 1:
                    done = True

            elif e.type == KEYDOWN:
                if e.key == K_SPACE:
                    done = True

                elif e.key == K_LSHIFT:
                    move_x = False

                elif e.key == K_LCTRL:
                    move_y = False

            elif e.type == KEYUP:
                if e.key == K_LSHIFT:
                    move_x = True

                elif e.key == K_LCTRL:
                    move_y = True

        render_mass = fuente.render(text_mass, True, COLOR_TEXTO, COLOR_BOX)
        render_radius = fuente.render(text_radius, True, COLOR_TEXTO,
                                      COLOR_BOX)
        render_density = fuente.render(text_density, True, COLOR_TEXTO,
                                       COLOR_BOX)

        fondo.fill(COLOR_BOX)
        fondo.blit(render_mass, (3, ALTO - 20))
        fondo.blit(render_radius, (150, ALTO - 20))
        fondo.blit(render_density, (300, ALTO - 20))
        fondo.blit(bg, bg_rect)
        if limit_mass is not None:
            fondo.blit(lim_img, lim_rect)
        numbers.draw(fondo)
        lineas.update()
        lineas.draw(fondo)
        display.update()

    display.quit()
    return data