calpy.py

Created by maelg0000

Created on February 08, 2026

2.68 KB


from kandinsky import draw_string, fill_rect
from ion import keydown, KEY_OK, KEY_UP, KEY_DOWN

def display_zone(digits, scroll_y):
    # On garde les 2 premieres lignes intactes (Titre + Compteur)
    fill_rect(0, 50, 320, 172, (255, 255, 255))
    curr_x, curr_y = 10, 50 - scroll_y
    for d in digits:
        if curr_y >= 50 and curr_y <= 200:
            draw_string(d, curr_x, curr_y, (0, 0, 0))
        curr_x += 10
        if curr_x > 300:
            curr_x = 10
            curr_y += 18

def pi_calc():
    # Setup
    fill_rect(0, 0, 320, 222, (255, 255, 255))
    draw_string("PI CALCULATOR", 100, 5, (0, 0, 255))
    
    digits = []
    # Parametres Spigot pour un bloc
    n = 1000 # On calcule par blocs de 1000 pour la stabilite
    len_a = int(10 * n / 3) + 1
    a = [2] * len_a
    
    x, y = 10, 50
    scroll_offset = 0
    
    for j in range(n):
        q = 0
        for i in range(len_a - 1, -1, -1):
            x_val = 10 * a[i] + q * (i + 1)
            a[i] = x_val % (2 * i + 1)
            q = x_val // (2 * i + 1)
        
        a[0] = q % 10
        val = q // 10
        
        # Formatage du chiffre
        char = str(val)
        if len(digits) == 1: digits[0] = digits[0] + "."
        digits.append(char)
        
        # Affichage direct
        draw_string("Decimales: " + str(len(digits)-1), 10, 25, (255, 0, 0))
        
        # On affiche le "3." correctement au debut
        to_print = char
        if len(digits) == 1: to_print = char + "."
        
        draw_string(to_print, x, y - scroll_offset, (0,0,0))
        
        # Avancement curseur
        x += 10
        if x > 300:
            x = 10
            y += 18
            if y - scroll_offset > 200:
                scroll_offset += 18
                display_zone(digits, scroll_offset)
        
        # Pause / Navigation
        if keydown(KEY_OK):
            draw_string("PAUSE", 135, 205, (255, 0, 0))
            while keydown(KEY_OK): pass
            paused = True
            while paused:
                if keydown(KEY_UP) and scroll_offset >= 18:
                    scroll_offset -= 18
                    display_zone(digits, scroll_offset)
                    while keydown(KEY_UP): pass
                if keydown(KEY_DOWN):
                    scroll_offset += 18
                    display_zone(digits, scroll_offset)
                    while keydown(KEY_DOWN): pass
                if keydown(KEY_OK):
                    paused = False
                    fill_rect(0, 205, 320, 18, (255, 255, 255))
                    display_zone(digits, scroll_offset)
                    while keydown(KEY_OK): pass

    draw_string("FIN DU BLOC", 110, 205, (0, 150, 0))

# Lancement
pi_calc()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.