import pygame import sys import random pygame.init() Taille de la fenêtre WIDTH = 800…
- User
import pygame import sys import random
pygame.init()
Taille de la fenêtre
WIDTH = 800 HEIGHT = 800
Taille des cases et grille
CELL_SIZE = 40 GRID_SIZE = WIDTH // CELL_SIZE
Couleurs
WHITE = (255, 255, 255) BLACK = (0, 0, 0)
Joueurs
player_colors = ["red", "blue"]
Grille
grid = []
def initialize_grid(): global grid grid = [[None] * GRID_SIZE for _ in range(GRID_SIZE)]
def draw_pion(screen, x, y, color): position = (x * CELL_SIZE + CELL_SIZE // 2, y * CELL_SIZE + CELL_SIZE // 2) pygame.draw.circle(screen, color, position, CELL_SIZE // 2 - 2)
def draw_grid(screen): for y in range(GRID_SIZE): for x in range(GRID_SIZE): rect = pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE) pygame.draw.rect(screen, BLACK, rect, 1)
if grid[y][x] is not None: color = pygame.Color(player_colors[grid[y][x]]) draw_pion(screen, x, y, color)def place_pion(player, x, y): global grid if grid[y][x] is None: grid[y][x] = player
def expand_pions(player): global grid new_positions = []
for y in range(GRID_SIZE): for x in range(GRID_SIZE): if grid[y][x] == player: for dy in (-1, 0, 1): for dx in (-1, 0, 1): ny, nx = y + dy, x + dx if 0 <= nx < GRID_SIZE and 0 <= ny < GRID_SIZE and grid[ny][nx] is None: new_positions.append((nx, ny)) for position in new_positions: grid[position[1]][position[0]] = playerdef pierre_feuille_ciseaux(player1, player2): choices = ["pierre", "feuille", "ciseaux"] choice1 = choices[random.randint(0, 2)] choice2 = choices[random.randint(0, 2)]
if (choice1 == "pierre" and choice2 == "ciseaux") or (choice1 == "feuille" and choice2 == "pierre") or (choice1 == "ciseaux" and choice2 == "feuille"): return player1 if choice1 == choice2: return None return player2def is_game_over(): for row in grid: if None in row: return False return True
def count_pions(player): count = 0 for row in grid: for cell in row: if cell == player: count += 1 return count
def play_game(): screen = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() pygame.display.set_caption("Jeu de connexion")
initialize_grid() running = True turn = 0 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: expand_pions(turn) turn = (turn + 1) % len(player_colors) elif event.key == pygame.K_RETURN: # Changer de joueur pour le prochain tour turn = (turn + 1) % len(player_colors) elif event.type == pygame.MOUSEBUTTONUP: x, y = pygame.mouse.get_pos() grid_x, grid_y = x // CELL_SIZE, y // CELL_SIZE place_pion(turn, grid_x, grid_y) screen.fill(WHITE) draw_grid(screen) pygame.display.flip() clock.tick(60) if is_game_over(): winner = 0 if count_pions(0) > count_pions(1) else 1 print("Le joueur", winner + 1, "a gagné!") break pygame.quit() sys.exit() - Assistant
Il semble qu'il y ait un problème avec la couleur des pions sur le plateau. Voici le code mis à jour et simplifié pour gérer correctement la couleur des joueurs :
- User
apres que chaque joueur est posé un pion commencer le jeu
- Assistant
- User
- Assistant
- User
- Assistant