fork download
  1.  
Success #stdin #stdout 0.1s 14072KB
stdin
import pygame
import sys

# Initialisierung von Pygame
pygame.init()

# Fenstergröße
WIDTH = 700
HEIGHT = 600

# Farben
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

# Spielbrettgröße
ROWS = 6
COLS = 7

# Spielbrett erstellen
def create_board():
    return [[0 for _ in range(COLS)] for _ in range(ROWS)]

# Spielstein einfügen
def drop_piece(board, row, col, piece):
    board[row][col] = piece

# Gültiger Zug?
def is_valid_location(board, col):
    return board[ROWS-1][col] == 0

# Nächste freie Zeile
def get_next_open_row(board, col):
    for r in range(ROWS):
        if board[r][col] == 0:
            return r

# Gewinner überprüfen
def winning_move(board, piece):
    # Horizontal
    for c in range(COLS-3):
        for r in range(ROWS):
            if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece:
                return True

    # Vertikal
    for c in range(COLS):
        for r in range(ROWS-3):
            if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece:
                return True

    # Diagonal (positiv)
    for c in range(COLS-3):
        for r in range(ROWS-3):
            if board[r][c] == piece and board[r+1][c+1] == piece and board[r+2][c+2] == piece and board[r+3][c+3] == piece:
                return True

    # Diagonal (negativ)
    for c in range(COLS-3):
        for r in range(3, ROWS):
            if board[r][c] == piece and board[r-1][c+1] == piece and board[r-2][c+2] == piece and board[r-3][c+3] == piece:
                return True

    return False

# Spielbrett zeichnen
def draw_board(board):
    for c in range(COLS):
        for r in range(ROWS):
            pygame.draw.rect(screen, BLUE, (c*SQUARESIZE, r*SQUARESIZE+SQUARESIZE, SQUARESIZE, SQUARESIZE))
            pygame.draw.circle(screen, BLACK, (int(c*SQUARESIZE+SQUARESIZE/2), int(r*SQUARESIZE+SQUARESIZE+SQUARESIZE/2)), RADIUS)
    
    for c in range(COLS):
        for r in range(ROWS):        
            if board[r][c] == 1:
                pygame.draw.circle(screen, RED, (int(c*SQUARESIZE+SQUARESIZE/2), HEIGHT-int(r*SQUARESIZE+SQUARESIZE/2)), RADIUS)
            elif board[r][c] == 2: 
                pygame.draw.circle(screen, YELLOW, (int(c*SQUARESIZE+SQUARESIZE/2), HEIGHT-int(r*SQUARESIZE+SQUARESIZE/2)), RADIUS)
    pygame.display.update()

# Musik laden und abspielen
def play_music():
    pygame.mixer.music.load("funny_music.mp3")  # Lade deine alberne Musikdatei
    pygame.mixer.music.play(-1)  # -1 bedeutet, die Musik in einer Endlosschleife abzuspielen

# Hauptprogramm
board = create_board()
game_over = False
turn = 0

SQUARESIZE = 100
RADIUS = int(SQUARESIZE/2 - 5)

size = (WIDTH, HEIGHT)
screen = pygame.display.set_mode(size)
draw_board(board)
pygame.display.set_caption("Vier gewinnt: Der Käsekrieg 🧀")

# Schriftart für den Gewinnertext
myfont = pygame.font.SysFont("comicsans", 75)

# Alberne Musik abspielen
play_music()

while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == pygame.MOUSEMOTION:
            pygame.draw.rect(screen, BLACK, (0,0, WIDTH, SQUARESIZE))
            posx = event.pos[0]
            if turn == 0:
                pygame.draw.circle(screen, RED, (posx, int(SQUARESIZE/2)), RADIUS)
            else:
                pygame.draw.circle(screen, YELLOW, (posx, int(SQUARESIZE/2)), RADIUS)
        pygame.display.update()

        if event.type == pygame.MOUSEBUTTONDOWN:
            pygame.draw.rect(screen, BLACK, (0,0, WIDTH, SQUARESIZE))
            # Spieler 1
            if turn == 0:
                posx = event.pos[0]
                col = int(posx // SQUARESIZE)

                if is_valid_location(board, col):
                    row = get_next_open_row(board, col)
                    drop_piece(board, row, col, 1)

                    if winning_move(board, 1):
                        label = myfont.render("Käse-König gewinnt!! 🧀👑", 1, RED)
                        screen.blit(label, (40,10))
                        game_over = True

            # Spieler 2
            else:
                posx = event.pos[0]
                col = int(posx // SQUARESIZE)

                if is_valid_location(board, col):
                    row = get_next_open_row(board, col)
                    drop_piece(board, row, col, 2)

                    if winning_move(board, 2):
                        label = myfont.render("Käse-Rebell gewinnt!! 🧀🎉", 1, YELLOW)
                        screen.blit(label, (40,10))
                        game_over = True

            draw_board(board)
            turn += 1
            turn = turn % 2

            if game_over:
                pygame.time.wait(3000)
stdout
Standard output is empty