Il mio primo progetto Python: un nerdissimo impiccato in ASCII

🌐 Anche in: English · Deutsch · Français · Español · Português · Nederlands · Polski
Allora, da qualche giorno sto smanettando con Python — per lo più teoria, ripassando variabili, cicli, condizioni e i soliti sospetti. E come ogni nuovo programmatore sano di mente, ho deciso che il mio primo progetto “vero” doveva essere qualcosa di epico.
Ovviamente, questo significava… l’impiccato.
È la sfida perfetta per un principiante:
- Ti eserciti con le variabili tenendo traccia delle lettere indovinate e dei tentativi sbagliati.
- Usi i cicli per il flusso del gioco.
- Aggiungi un pizzico di condizioni per decidere chi vive e chi muore (beh, in ASCII).
Dopo qualche tentativo ed errore (e un bel po’ di “Ah già, Python distingue maiuscole e minuscole”), la mia versione funzionava. Lo ammetto, ho lasciato che ChatGPT “pompasse” qualche parte — ma ehi, è così che si impara più in fretta, no?
Cosa fa
- Sceglie una parola a caso da una lista predefinita
- Mostra un omino ASCII a ogni tentativo sbagliato
- Ti fa indovinare lettere o la parola intera
- Gestisce maiuscole, minuscole e tentativi ripetuti
- Sfoggia i miei crescenti superpoteri in Python
Perché lo adoro
Non è solo un gioco — è una piccola capsula del tempo di dove mi trovo adesso nel mio percorso di programmazione.
Tra qualche mese lo guarderò e penserò: “Wow… ne ho fatta di strada.”
Ma per ora è pura gioia guardare il terminale riempirsi di trattini bassi, ASCII art e messaggi di vittoria (o sconfitte tragiche).
Considerazioni finali
Se stai iniziando con Python, fai un gioco dell’impiccato. Sul serio. È il parco giochi perfetto per sperimentare con cicli, logica e la magia dell’ASCII. E se riesci a far sembrare il tuo codice pulito e a farlo girare senza bug, hai già vinto.
library.py
words = ["Apfel", "Banane", "Mango", "Kirsche", "Pfirsich", "Linux", "Kernel", "Docker", "Ansible", "Python"]
logo = ['''
_
| |
| |__ __ _ _ __ __ _ _ __ ___ __ _ _ __
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
__/ |
|___/''']
HANGMANPICS = ['''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
/|\ |
|
|
=========''', '''
+---+
| |
O |
/|\ |
/ |
|
=========''', '''
+---+
| |
O |
/|\ |
/ \ |
|
=========''']
main.py
# =============================================================================
# Hangman Game
#
# File: main.py
# Author: Raphael Münch
# Email: you@example.com
# Website: https://www.apt-upgrade.me
#
# Created: 2025-08-15
# Last Updated: 2025-08-15
#
# Description:
# A simple Hangman game played in the console.
# Includes ASCII art, word guessing, and configurable max attempts.
#
# Copyright (c) 2025 Raphael Münch. All rights reserved.
#
# License:
# This software is licensed under the MIT License.
# You may use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, provided the copyright notice
# above appears in all copies.
#
# =============================================================================
import library
import os
import random
DEBUG = False
while True:
# Clear the console (Windows = 'cls', others = 'clear')
os.system('cls' if os.name == 'nt' else 'clear')
print(library.logo[0])
chosen_word = random.choice(library.words)
word_lower = chosen_word.lower()
# Create a mask: "_" for each letter in the word
mask = ["_"] * len(chosen_word)
guessed_letters = "" # Letters guessed in the current round
wrong_attempts = 0 # Count of wrong guesses
max_attempts = len(library.HANGMANPICS) - 1 # e.g., 6 wrong guesses allowed
found_letter = False
while True:
print(f"Your guesses: {guessed_letters}")
print(f"Current word: {' '.join(mask)}")
if DEBUG:
print(f"Chosen word: {chosen_word}")
print(f"Attempt: {wrong_attempts}/{max_attempts}")
choice = input("Enter a letter or guess the word: ").strip()
# Whole word guess
if len(choice) > 1:
if choice.lower() == word_lower:
mask = list(chosen_word) # Reveal the whole word
print("You win!!!")
break
else:
wrong_attempts += 1
print(f"\n\n{library.HANGMANPICS[min(wrong_attempts, max_attempts)]}")
if wrong_attempts >= max_attempts:
print("\n\aYou've lost!!!")
print(f"The word was: {chosen_word}")
break
continue
# Single letter guess
if not choice:
continue
guess = choice.lower()
# Skip if letter already guessed
if guess in guessed_letters:
print("Already guessed. Try another letter.")
continue
guessed_letters += guess
# Check if guess matches any letter in the word
found_letter = False
for i, ch in enumerate(word_lower):
if guess == ch:
mask[i] = chosen_word[i] # Preserve original case
found_letter = True
if not found_letter:
wrong_attempts += 1
print(f"\n\n{library.HANGMANPICS[min(wrong_attempts, max_attempts)]}")
if wrong_attempts >= max_attempts:
print("\n\aYou've lost!!!")
print(f"The word was: {chosen_word}")
break
if "".join(mask).lower() == word_lower:
print("You win!!!")
break
answer = input("Do you want to play again (y/n)? ").strip().lower()
if answer != "y":
break

