Bonsoir, je suis dans l'optique de mettre en place un petit Jeu pour mon apprentissage, je viens faire appel à vous car j’aimerais savoir si il est possible de réduire ce code. J'aimerais aussi pouvoir implémenter une fonction permettant de gagner une partie (Car le jeu ne gagne jamais et ne se réinitialise pas) Et également pouvoir faire tourner les quadrants avec les flèches que j'ai placé. Je vous remercie à tous pour avoir pris la peine de lire,pour information le jeu est le : Pentago A bientôt

Nom : fleche1.png
Affichages : 1091
Taille : 789 octets
Nom : fleche2.png
Affichages : 1091
Taille : 948 octets
Nom : bloc.png
Affichages : 1095
Taille : 248 octets
Nom : pion1.png
Affichages : 1102
Taille : 1,3 Ko
Nom : pion2.png
Affichages : 1100
Taille : 683 octets


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import pygame, sys
from pygame.locals import *
 
pygame.init()
fpsClock = pygame.time.Clock()
maSurface = pygame.display.set_mode((800, 400))
pygame.display.set_caption('Pentago')
 
 
# IMAGES
 
carre_jeton = pygame.image.load('bloc.png')
BLANC = pygame.image.load('pion1.png')
NOIR = pygame.image.load('pion2.png')
fleche1 = pygame.image.load('arrow1.png')
fleche2 = pygame.image.load('arrow2.png')
 
infoJeu = {'marge1': 70, 'marge2': 60, 'nbcarre': 6, 'espCarre': 40}
 
# FONCTIONS
 
def createTable(n): 
    return [[0 for i in range(n)] for k in range(n)]
 
def GameBoard(arr, surface):
    for i in range(1, 5):
        Grille1234(arr, surface, i)
        PionGrille(arr, surface, i)
 
def Grille1234(arr, surface, quadrant):
    Rang = int((quadrant - 1) / 2) + 1
    Col = (quadrant - (Rang - 1) * 3)
    Info = [arr[i][(Col - 1) * int(len(arr)/2):
                   (Col) * int(len(arr)/2)] for i in range((Rang - 1) * int(len(arr)/2), (Rang) * int(len(arr)/2))]
    Col, Rang, tailleQuadrant = Col - 1, Rang - 1, (len(arr) / 2) + 1
    baseX, baseY = Col * tailleQuadrant * infoJeu['espCarre'] + infoJeu['marge2'], Rang  * tailleQuadrant * infoJeu['espCarre'] + infoJeu['marge1']
 
    for i, k in enumerate(Info):
        for ii, kk in enumerate(k):
            surface.blit(carre_jeton, ( baseX + ii * infoJeu['espCarre'], baseY + i * infoJeu['espCarre'] ))
 
 
def PionGrille(arr, surface, quadrant):
    Rang = int((quadrant - 1) / 2) + 1
    Col = (quadrant - (Rang - 1) * 2)
    Info = [arr[i][(Col - 1) * int(len(arr) / 2):
                   (Col) * int(len(arr) / 2)] for i in range((Rang - 1) * int(len(arr) / 2), (Rang) * int(len(arr) / 2))]
    Col, Rang, tailleQuadrant = Col - 1, Rang - 1, (len(arr) / 2) + 1
    baseX, baseY = Col * tailleQuadrant * infoJeu['espCarre'] + infoJeu['marge2'], Rang  * tailleQuadrant * infoJeu['espCarre'] + infoJeu['marge1']
 
    for i, k in enumerate(Info):
        for ii, kk in enumerate(k):
            if (kk == 0): continue
            if (kk == 1):
                surface.blit(BLANC, ( baseX + ii * infoJeu['espCarre'], baseY + i * infoJeu['espCarre'] ))
            if (kk == -1):
                surface.blit(NOIR, ( baseX + ii * infoJeu['espCarre'], baseY + i * infoJeu['espCarre'] ))
 
def PoserUnPion(table,maSurface,players):
    global player
    global state
    x,y = pygame.mouse.get_pos()
    caseX, caseY = int( ( x -infoJeu['marge2'] ) / infoJeu['espCarre'] ), int( ( y -infoJeu['marge1'] ) / infoJeu['espCarre'] )
 
    if caseX <= int(len(table)) and caseY <= int(len(table)):
        if caseX >= int(len(table) / 2):
            caseX, caseY = int(caseX - 1), int(caseY)
        if caseY >= int(len(table) / 2):
            caseX, caseY = int(caseX + 1), int(caseY)
        else:
            caseX, caseY = int( caseX + 1 ), int( caseY + 1 )
 
        if table[caseY-1][caseX-1] == 0:
            table[caseY-1][caseX-1] = player
            player *= -1
            state = 1
 
 
def write(text,y,c):
    myfont = pygame.font.SysFont("Comic Sans MS", 30)
    label = myfont.render(text, 1, c)
    maSurface.fill(pygame.Color("Lightblue"))
    maSurface.blit(label, (infoJeu['marge2']+(infoJeu['nbcarre']*infoJeu['espCarre'])+200, y))
 
def rotation(table,maSurface):
    x = (infoJeu['marge2'] + (infoJeu['espCarre']*-1))
    y = (infoJeu['marge1'] + (infoJeu['espCarre']*0))
    maSurface.blit(pygame.transform.flip(fleche2,1,0), (x,y))#Q1-l
 
    x = (infoJeu['marge2'] + (infoJeu['espCarre']*0))
    y = (infoJeu['marge1'] + (infoJeu['espCarre']*-1))
    maSurface.blit(fleche1, (x, y))
 
    x = (infoJeu['marge2'] + (infoJeu['espCarre']*infoJeu['nbcarre']))
    y = (infoJeu['marge1'] + (infoJeu['espCarre']*-1))
    maSurface.blit(pygame.transform.flip(fleche1,1,0), (x,y))
 
    x = (infoJeu['marge2'] + (infoJeu['espCarre']*(infoJeu['nbcarre']+1)))
    y = (infoJeu['marge1'] + (infoJeu['espCarre']*0))
    maSurface.blit(fleche2, (x,y))
 
    x = (infoJeu['marge2'] + (infoJeu['espCarre']*-1))
    y = (infoJeu['marge1'] + (infoJeu['espCarre']*infoJeu['nbcarre']))
    maSurface.blit(pygame.transform.flip(fleche2,1,1), (x,y))
 
    x = (infoJeu['marge2'] + (infoJeu['espCarre']*0))
    y = (infoJeu['marge1'] + (infoJeu['espCarre']*(infoJeu['nbcarre']+1)))
    maSurface.blit(pygame.transform.flip(fleche1,0,1), (x,y))
 
    x = (infoJeu['marge2'] + (infoJeu['espCarre']*infoJeu['nbcarre']))
    y = (infoJeu['marge1'] + (infoJeu['espCarre']*(infoJeu['nbcarre']+1)))
    maSurface.blit(pygame.transform.flip(fleche1,1,1), (x,y))
 
    x = infoJeu['marge2'] + (infoJeu['espCarre']*(infoJeu['nbcarre']+1))
    y = (infoJeu['marge1'] + (infoJeu['espCarre']*infoJeu['nbcarre']))
    maSurface.blit(pygame.transform.flip(fleche2,0,1), (x,y))
 
    pygame.display.update()
    x,y = 0, 0
    while x == 0:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                x, y = pygame.mouse.get_pos()
 
    caseX, caseY = int( ( x -infoJeu['marge2'] ) / infoJeu['espCarre'] ), int( ( x -infoJeu['marge1'] ) / infoJeu['espCarre'] )
 
player, table = 1, createTable(infoJeu['nbcarre'])
inProgress = True
# BOUCLE PRINCIPALE
 
while inProgress:
    for event in pygame.event.get():
        if event.type == QUIT:
            inProgress = False
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            PoserUnPion(table, maSurface, player)
            write('Faire tourner un cadrant',70,(0,0,0))
            GameBoard(table, maSurface)
            pygame.display.update()
            rotation(table, maSurface)
    if player == 1: playerName = 'Blanc'
    else: playerName = 'Noir'
    write('Au tout du Joueur : '+str(playerName),70,(255,0,0))
    GameBoard(table, maSurface)
    pygame.display.update()
 
    fpsClock.tick(20)
pygame.quit()