Bonjour, depuis peu je me suis lancée dans la création d'un petit reversi aussi appelé "othello" en python avec la librairie graphique pygame.
pour ce qui connaissent pas le jeu voici:
https://www.lecomptoirdesjeux.com/regle-reversi.htm#:~:text=Chacun%20%C3%A0%20son%20tour%2C%20les,'il%20vient%20d'encadrer.
Au début du programme ça fonctionne mais plus on avance dans la partie plus le programme commence à faire des siennes et je sais pas pourquoi voici mon code:
import pygame
from CONSTS import ROWS, COLS, CELL_SIZE
pygame.init()
board = []
board_bool = []
for row in range(ROWS):
board.append([])
board_bool.append([])
for col in range(COLS):
board[row].append(0)
board_bool[row].append(True)
board[3][3] = board[4][4] = 2
board[4][3] = board[3][4] = 1
screen = pygame.display.set_mode((COLS * CELL_SIZE, ROWS * CELL_SIZE))
def draw_board():
for y in range(ROWS):
for x in range(COLS):
pygame.draw.rect(screen, "black", (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE), 2)
all_pieces = []
def darw_piece_on_the_board():
global click, turn, last_turn, can_pose
for y in range(ROWS):
for x in range(COLS):
if x * CELL_SIZE < pygame.mouse.get_pos()[0] < (x + 1) * CELL_SIZE and y * CELL_SIZE < \
pygame.mouse.get_pos()[1] < (y + 1) * CELL_SIZE:
pygame.draw.circle(screen, "blue", [x * CELL_SIZE + 45, y * CELL_SIZE + 45], 25)
if click and not board_bool[y][x]:
click = False
if click and board_bool[y][x]:
board[y][x] = turn
if turn == 1:
turn = 2
last_turn = 1
else:
turn = 1
last_turn = 2
try:
if board[y + 1][x] == turn:
for i in range(6):
if board[y + (i + 2)][x] == 0:
break
if board[y + (i + 2)][x] == last_turn:
for v in range(i + 1):
board[y + (v + 1)][x] = last_turn
can_pose = True
break
except IndexError:
pass
try:
if board[y - 1][x] == turn:
for i in range(6):
if board[y - (i + 2)][x] == 0:
break
if board[y - (i + 2)][x] == last_turn:
for v in range(i + 1):
board[y - (v + 1)][x] = last_turn
can_pose = True
break
except IndexError:
pass
try:
if board[y][x + 1] == turn:
for i in range(6):
if board[y][x + (i + 2)] == 0:
break
if board[y][x + (i + 2)] == last_turn:
for v in range(i + 1):
board[y][x + (v + 1)] = last_turn
can_pose = True
break
except IndexError:
pass
try:
if board[y][x - 1] == turn:
for i in range(6):
if board[y][x - (i + 2)] == 0:
break
if board[y][x - (i + 2)] == last_turn:
for v in range(i + 1):
board[y][x - (v + 1)] = last_turn
can_pose = True
break
except IndexError:
pass
try:
if board[y - 1][x - 1] == turn:
for i in range(6):
if board[y - (i + 2)][x - (i + 2)] == 0:
break
if board[y - (i + 2)][x - (i + 2)] == last_turn:
for v in range(i + 1):
board[y - (v + 1)][x - (v + 1)] = last_turn
can_pose = True
break
except IndexError:
pass
try:
if board[y + 1][x + 1] == turn:
for i in range(6):
if board[y + (i + 2)][x + (i + 2)] == 0:
break
if board[y + (i + 2)][x + (i + 2)] == last_turn:
for v in range(i + 1):
board[y + (v + 1)][x + (v + 1)] = last_turn
can_pose = True
break
except IndexError:
pass
try:
if board[y - 1][x + 1] == turn:
for i in range(6):
if not board[y - (i + 2)][x + (i + 2)] == 0:
if board[y - (i + 2)][x + (i + 2)] == last_turn:
for v in range(i + 1):
board[y - (v + 1)][x + (v + 1)] = last_turn
can_pose = True
break
except IndexError:
pass
try:
if board[y + 1][x - 1] == turn:
for i in range(6):
if not board[y + (i + 2)][x - (i + 2)] == 0:
if board[y + (i + 2)][x - (i + 2)] == last_turn:
for v in range(i + 1):
board[y + (v + 1)][x - (v + 1)] = last_turn
can_pose = True
break
except IndexError:
pass
if not can_pose:
board[y][x] = 0
turn = last_turn
click = False
if board[y][x] == 1:
pygame.draw.circle(screen, "white", [x * CELL_SIZE + 45, y * CELL_SIZE + 45], 30)
if board[y][x] == 2:
pygame.draw.circle(screen, "black", [x * CELL_SIZE + 45, y * CELL_SIZE + 45], 30)
if not board[y][x] == 0:
board_bool[y][x] = False
turn = 1
last_turn = 2
can_pose = False
click = False
running = True
while running:
can_pose = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
click = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if turn == 1:
turn = 2
else:
turn = 1
screen.fill((10, 60, 10))
draw_board()
darw_piece_on_the_board()
pygame.display.flip()
pygame.time.Clock().tick(120)
pygame.quit()
il faut savoir que je suis débutant donc si vous voyez des grosses erreurs dîtes le moi,
merci énormément à ceux qui m'aideront.