Un lucchetto in Python [Mini-gioco]

Creiamo un semplice lucchetto in Python, usando solo la libreria standard.

Voglio mantenere il codice il più semplice possibile, quindi non userò classi.

Il lucchetto avrà scorrimento orizzontale e sarà rappresentato così:

<| 10 | 11 | 12 |>

import os
import random
import shutil

dial = [i for i in range(100)]
combination = [random.randint(0, 99) for i in range(5)]
original_comb = combination.copy()
current = 50
found = list()
win = False
direction = ""
n_guess = 0

# ANSI colors
BLACK = "\033[0;30m"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
BROWN = "\033[0;33m"
BLUE = "\033[0;34m"
PURPLE = "\033[0;35m"
CYAN = "\033[0;36m"
LIGHT_GRAY = "\033[0;37m"
DARK_GRAY = "\033[1;30m"
LIGHT_RED = "\033[1;31m"
LIGHT_GREEN = "\033[1;32m"
YELLOW = "\033[1;33m"
LIGHT_BLUE = "\033[1;34m"
LIGHT_PURPLE = "\033[1;35m"
LIGHT_CYAN = "\033[1;36m"
LIGHT_WHITE = "\033[1;37m"
BOLD = "\033[1m"
FAINT = "\033[2m"
ITALIC = "\033[3m"
UNDERLINE = "\033[4m"
BLINK = "\033[5m"
NEGATIVE = "\033[7m"
CROSSED = "\033[9m"
END = "\033[0m"

def get_term_width():
    return shutil.get_terminal_size().columns

def left(n):
    global current
    current = (current -n)% 100
    return dial[current]

def right(n):
    global current
    current = (current + n) % 100
    return dial[current]

def clear_screen():
    os.system('cls' if os.name == 'nt' else 'clear')
 

def get_mov():
    while True:
        action = input('> ')
        if len(action.split()) == 2:
            pos, num = action.lower().split()
        elif len(action.split()) == 1 and action =='q':
            pos = 'q'
            num = -1
            break
        else:
            continue
        
        if not pos or not num:
            continue
        if not num.isdigit():
            print("Devi inserire un valore numerico per il movimento")
            continue
        if pos not in ['d', 's']:
            continue
        else:
            break

        
    return pos, int(num)

def check_number(n, m):
    return n == m

def print_center(stringa: str, color=None, selector=""):

    t_width = get_term_width()
    padding = max(0, (t_width - len(stringa)) // 2) - len(selector)
    if color:
        stringa = color + stringa + END
        
    if selector != "":
        print(' ' * padding + selector +  stringa)
    else:
        print(' ' * padding + stringa)

    
def print_dial(lista):
    spaces = 0
    t_width = get_term_width()
    tmp_list = ['*'] * len(lista)
    line = str()
    cleanline = str()
    for i in enumerate(found):
        tmp_list[i[0]] = i[1]
    for i in tmp_list:

        if i != '*':
            cleanline += str(i)
            i = GREEN + str(i) + END
        line = line + i + ' '
        spaces += 1
    print()
    padding = max(0, (t_width - len(cleanline)) // 2)
    print(' '* (padding - spaces) + line)

def print_dials(curr, choice):
    n = len(original_comb)
    for i in range(0,n):
        if i == curr:
            current = choice
            color = BROWN
            selector = "-> "
        elif i < curr:
            current = found[i]
            color = DARK_GRAY
            selector = "[v] "
        else:
            current = 50
            color = CYAN
            selector = ""
        print_center(f"<|{(current - 1) % 100} [{current}] {(current + 1) % 100}|>", color, selector)
    
if __name__ == '__main__':
    curr = 0
    while True:
        t_width = get_term_width()
        clear_screen()
        print_center('Indovina la combinazione', BROWN)
        print()
        print_dial(original_comb)
        if len(combination) <= 0:
            win = True
            break
        
        if combination and current == combination[0]:
            print(f"Numero {current} trovato (eri già sulla posizione!)")
            found.append(current)
            combination.pop(0)
            current = 50
            curr += 1
            continue

            
        print(f"Tentativi: {n_guess}")
        print_dials(curr, current)
        #print_center(f"<|{(current - 1) % 100} [{current}] {(current + 1) % 100}|>", PURPLE)
        print(f"{direction}")
        where, much = get_mov()
        if where == 'q':
            break
        
        if where == 'd':
            right(much)
            n_guess += 1
        elif where == 's':
            left(much)
            n_guess += 1
        else:
            print("Scelta non consentita")

        if check_number(current, combination[0]):
            found.append(current)
            combination.pop(0)
            direction = " "
            current = 50
            curr += 1
        else:
            diff = (combination[0] - current + 100) % 100
            if diff == 0:
                direction = ""
            elif diff <= 50:
                direction = "->"
                
            else:
                direction = "<-"


    if win:
        print(f"Hai vinto!!! in {n_guess} tentativi.")

        print("Il lucchetto è stato sbloccato.")
        print("Hai ottenuto l'accesso ai segreti.")

    print("Combinazione originale:")
    for i in original_comb:
        print(i, end=" ")
    print()
    print("\nArrivederci e grazie per aver giocato!")
    

Ecco come appare il nostro gioco finito.

Scarica il mini gioco : mini_gioco_lucchetto.py

This article was updated on aprile 2, 2026