import pygame import sys import random pygame.init() WIDTH = 800 HEIGHT = 800 CELLSIZE =…
- User
import pygame import sys import random
pygame.init()
WIDTH = 800 HEIGHT = 800 CELL_SIZE = 40 GRID_SIZE = WIDTH // CELL_SIZE
WHITE = (255, 255, 255) BLACK = (0, 0, 0) player_colors = [(255, 0, 0), (0, 0, 255)]
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)
codeif 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 return True return False
def change_pion(player, x, y): global grid if grid[y][x]: grid[y][x] = player return True
def expand_pions(player): global grid new_positions = []
codefor 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 check_neighbors(x, y, player): global grid neighbors = [ (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1), ]
codecolors_found = set() for nx, ny in neighbors: if 0 <= nx < GRID_SIZE and 0 <= ny < GRID_SIZE and grid[ny][nx] is not None: colors_found.add(grid[ny][nx]) return colors_founddef pierre_feuille_ciseaux(player1, player2): choices = ["pierre", "feuille", "ciseaux"] choice1 = choices[random.randint(0, 2)] choice2 = choices[random.randint(0, 2)]
- Assistant
It looks like you have an indentation error in your code. Line 129 should be indented the same number of spaces as the line that contains the
elif event.type == pygame.MOUSEBUTTONUP:. You should also remove the extraelif event.key == pygame.K_RETURN:line which is causing the current syntax error.Your corrected code should look like this:
python#... while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONUP: x, y = pygame.mouse.get_pos() grid_x, grid_y = x // CELL_SIZE, y // CELL_SIZE if start_phase: if count_pions(turn) == 0: place_pion(turn, grid_x, grid_y) turn = (turn + 1) % len(player_colors) if count_pions(turn) == 1: start_phase = False turn = 0 elif event.type == pygame.KEYDOWN: #... elif event.key == pygame.K_RETURN: turn = (turn + 1) % len(player_colors) - User
code complet avec indentation
- Assistant
- User
le fait de cliquer sur espace , il ne e passe rien
- Assistant
Je m'excuse pour la confusion. Le problème provient de la condition
if grid[grid_y][grid_x] is None or len(check_neighbors(grid_x, grid_y, grid[grid_y][grid_x])) < 4), qui restreint l'utilisation de l'espace dans des cas où il y a déjà un pion présent. Nous devrions corriger cette condition pour qu'elle autorise également l'utilisation de l'espace lorsque la cellule est occupée par un pion.Voici la partie modifiée du code à l'intérieur de la boucle
while running::python