April 01, 2026

Mülltrennung als Computerspiel

Hier ist ein Lernspiel für Jung und Alt bei dem es um Mülltrennung geht. Der Spieler muss mittels drag&drop mögliche Items in die korrekte Tonne werfen. Viel Vergnügen.

import pygame
import random

# Initialisierung
pygame.init()

# Fenster-Einstellungen
WIDTH, HEIGHT = 900, 650 # Etwas breiter für die 5. Tonne
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Müll-Profi: Jetzt auch mit Glas!")

# Farben
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 102, 204)    # Papier
YELLOW = (255, 204, 0)  # Verpackung
BROWN = (102, 51, 0)    # Bio
GRAY = (50, 50, 50)     # Restmüll
DARK_GREEN = (0, 100, 0) # Glas
LIGHT_GREEN = (0, 255, 0) # Feedback Richtig
RED = (255, 0, 0)       # Feedback Falsch

# Schriftarten
font = pygame.font.SysFont("Arial", 22, bold=True)
title_font = pygame.font.SysFont("Arial", 36, bold=True)

# Erweiterte Müll-Daten (Insgesamt 22 Items)
# Erweiterte Müll-Daten (Jetzt insgesamt 42 Items!)
WASTE_ITEMS = {
    # PAPIER (Blau) - Nur sauberes Papier!
    "Zeitungen": BLUE, "Karton": BLUE, "Schulheft": BLUE, "Prospekte": BLUE,
    "Briefumschlag": BLUE, "Eierkarton": BLUE, "Mehltüte (leer)": BLUE, "Geschenkpapier": BLUE,
    
    # GELBER SACK / TONNE (Gelb) - Verpackungen aus Plastik, Metall, Verbundstoff
    "Getränkedose": YELLOW, "Plastetüte": YELLOW, "Joghurtbecher": YELLOW, 
    "Milchtüte": YELLOW, "Alufolie": YELLOW, "Shampooflasche": YELLOW,
    "Konservendose": YELLOW, "Butterfolie": YELLOW, "Kronkorken": YELLOW,
    "Styropor": YELLOW, "Chipsdose": YELLOW,
    
    # BIO (Braun) - Organisches
    "Apfelrest": BROWN, "Bananenschale": BROWN, "Kaffeesatz": BROWN, 
    "Eierschalen": BROWN, "Rasenschnitt": BROWN, "Teebeutel": BROWN,
    "Orangenschale": BROWN, "Kartoffelschalen": BROWN, "Welke Blumen": BROWN,
    
    # RESTMÜLL (Grau) - Alles Verschmutzte oder Nicht-Verwertbare
    "Zahnbürste": GRAY, "Windel": GRAY, "Staubsaugerbeutel": GRAY, 
    "Asche": GRAY, "Zigarette": GRAY, "Pizzakarton (fettig)": GRAY,
    "Backpapier": GRAY, "Kaugummi": GRAY, "Putzlappen": GRAY,
    "Katzenstreu": GRAY, "Alte Fotos": GRAY,
    
    # GLAS (Grün) - Behälterglas (kein Trinkglas/Fensterglas!)
    "Weinflasche": DARK_GREEN, "Marmeladenglas": DARK_GREEN, "Senfglas": DARK_GREEN,
    "Ölflasche (Glas)": DARK_GREEN, "Parfümflakon": DARK_GREEN
}

class Bin:
    def __init__(self, color, x, label):
        self.rect = pygame.Rect(x, HEIGHT - 160, 140, 140)
        self.color = color
        self.label = label

class DraggableItem:
    def __init__(self, text, target_color):
        self.text = text
        self.target_color = target_color
        self.reset_position()
        self.dragging = False
        
    def reset_position(self):
        self.rect = pygame.Rect(WIDTH // 2 - 75, 180, 150, 45)

# 5 Tonnen erstellen
bins = [
    Bin(BLUE, 30, "Papier"),
    Bin(YELLOW, 200, "Gelber Sack"),
    Bin(BROWN, 375, "Bio"),
    Bin(DARK_GREEN, 550, "Glas"),
    Bin(GRAY, 725, "Restmüll")
]

# Spiel-Variablen
score = 0
feedback_text = "Zieh das Wort in die richtige Tonne!"
feedback_color = BLACK

def get_new_item():
    name = random.choice(list(WASTE_ITEMS.keys()))
    return DraggableItem(name, WASTE_ITEMS[name])

current_item = get_new_item()

running = True
while running:
    screen.fill((240, 240, 240)) # Hellgrauer Hintergrund
    
    # UI Zeichnen
    title = title_font.render("Müll-Sortier-Station", True, BLACK)
    screen.blit(title, (WIDTH//2 - title.get_width()//2, 30))
    
    score_display = font.render(f"Punkte: {score}", True, BLACK)
    screen.blit(score_display, (30, 30))

    # Tonnen zeichnen
    for b in bins:
        pygame.draw.rect(screen, b.color, b.rect, border_radius=8)
        # Beschriftung der Tonne
        txt_color = WHITE if b.color != YELLOW else BLACK
        txt = font.render(b.label, True, txt_color)
        screen.blit(txt, (b.rect.centerx - txt.get_width()//2, b.rect.y + 55))

    # Aktuelles Item zeichnen
    if not current_item.dragging: # Schatten-Effekt wenn nicht gezogen
        pygame.draw.rect(screen, (200, 200, 200), current_item.rect.move(3, 3), border_radius=10)
    
    pygame.draw.rect(screen, WHITE, current_item.rect, border_radius=10)
    pygame.draw.rect(screen, BLACK, current_item.rect, 3, border_radius=10)
    item_txt = font.render(current_item.text, True, BLACK)
    screen.blit(item_txt, (current_item.rect.centerx - item_txt.get_width()//2, current_item.rect.y + 10))

    # Feedback
    f_txt = font.render(feedback_text, True, feedback_color)
    screen.blit(f_txt, (WIDTH//2 - f_txt.get_width()//2, 120))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if current_item.rect.collidepoint(event.pos):
                current_item.dragging = True
                mouse_x, mouse_y = event.pos
                offset_x = current_item.rect.x - mouse_x
                offset_y = current_item.rect.y - mouse_y

        elif event.type == pygame.MOUSEBUTTONUP:
            if current_item.dragging:
                current_item.dragging = False
                hit_bin = False
                for b in bins:
                    if current_item.rect.colliderect(b.rect):
                        if b.color == current_item.target_color:
                            score += 10
                            feedback_text = f"Richtig! {current_item.text} gehört in {b.label}."
                            feedback_color = LIGHT_GREEN
                        else:
                            score -= 5
                            feedback_text = f"Falsch! {current_item.text} ist kein {b.label}!"
                            feedback_color = RED
                        
                        current_item = get_new_item()
                        hit_bin = True
                        break
                
                if not hit_bin:
                    current_item.reset_position()

        elif event.type == pygame.MOUSEMOTION:
            if current_item.dragging:
                mouse_x, mouse_y = event.pos
                current_item.rect.x = mouse_x + offset_x
                current_item.rect.y = mouse_y + offset_y

    pygame.display.flip()

pygame.quit()
 

No comments:

Post a Comment