CONNEXION
  • RetourJeux
    • Sorties
    • Hit Parade
    • Les + populaires
    • Les + attendus
    • Soluces
    • Tous les Jeux
    • Gaming
  • RetourActu Gaming
    • News
    • Astuces
    • Tests
    • Previews
    • Toute l'actu gaming
  • RetourBons plans
    • Bons plans
    • Bons plans Smartphone
    • Bons plans Hardware
    • Bons plans Image et Son
    • Bons plans Amazon
    • Bons plans Cdiscount
    • Bons plans Decathlon
    • Bons plans Fnac
    • Tous les Bons plans
  • RetourJVTech
    • Actus High-Tech
    • Intelligence Artificielle
    • Smartphones
    • Mobilité urbaine
    • Hardware
    • Image et son
    • Tutoriels
    • Tests produits High-Tech
    • Guides d'achat High-Tech
    • JVTech
  • RetourCulture
    • Actus Culture
    • Culture
  • RetourVidéos
    • A la une
    • Gaming Live
    • Vidéos Tests
    • Vidéos Previews
    • Gameplay
    • Trailers
    • Chroniques
    • Replay Web TV
    • Toutes les vidéos
  • RetourForums
    • Hardware PC
    • PS5
    • Switch 2
    • Xbox Series
    • Switch
    • Pokemon pocket
    • FC 25 Ultimate Team
    • League of Legends
    • Tous les Forums
  • PC
  • PS5
  • Xbox Series
  • Switch 2
  • PS4
  • One
  • Switch
  • iOS
  • Android
  • MMO
  • RPG
  • FPS
En ce moment Genshin Impact Valhalla Breath of the wild Animal Crossing GTA 5 Red dead 2
Liste des sujets

Probleme Pygame

strinpontin
strinpontin
Niveau 3
02 juillet 2021 à 14:48:57

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.

Sous forums
  • Aide à l'achat Mac
  • Internet
  • Macintosh
  • Création de sites web
  • Création de Jeux
  • Linux
  • Programmation
  • Steam Deck
  • Hardware
La vidéo du moment