Précédent   Forum du club des développeurs et IT Pro > Autres langages > Python & Zope > Programmation multimédia/Jeux
Programmation multimédia/Jeux Forum d'entraide sur la 2D, 3D, video, son, ...
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 24/03/2007, 17h50   #1
Bibi88
Invité de passage
 
Étudiant
Inscription : mars 2007
Messages : 1
Détails du profil
Informations personnelles :
Localisation : France

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : mars 2007
Messages : 1
Points : 0
Points : 0
Par défaut [Pygame] puissance 4

Salut à tous,

Je suis lancée dans la réalisation d'un puissance 4 en python avec Pygame.
J'ai utilisé une image de grille ainsi que deux images pour les pions (rouge et jaune). Le programme fonctionne globalement. Il reste un probleme car mon test pour stopper le remplissage d'une colonne une fois que celle ci est déjà remplie ne fonctionne pas.
Cependant, j'aimerais simuler une chute de pions plutôt que d'afficher ceux ci comme mon programme le permet pour l'instant. Cela fait longtemps que je planche sur le probleme mais je n'arrive à rien.
Est ce quelqu'un pourrait m'aider à sortir de l'impasse dans laquelle je me trouve car je dois absolument avancer pour créer une intelligence artificielle.
Voici mon programme

Code :
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: cp1252 -*-
 
 
#####################
# Variables globales
 
M = [[0, 0, 0, 0, 0, 0, 0], \
     [0, 0, 0, 0, 0, 0, 0], \
     [0, 0, 0, 0, 0, 0, 0], \
     [0, 0, 0, 0, 0, 0, 0], \
     [0, 0, 0, 0, 0, 0, 0], \
     [0, 0, 0, 0, 0, 0, 0]]
 
joueur = 1
JetonsJoues = 0
P4 = False
 
#####################
# Fonctions
 
def quel_joueur():
# Cette fonction retourne le numero du joueur qui doit jouer
    if (JetonsJoues % 2 == 0):
        jou = 1
    else:
        jou = 2
    return jou
 
def choisir_colonne(x,y):
# Cette fonction retourne la colonne demandee au joueur1
# Tant que la valeur n'est pas acceptable, on demande la colonne a jouer
    col=x-16
    col=col/97
    if col in range(0,7):
        if (M[5][col]==0):
            test=False
    return col
 
def ligne():
# Cette fonction retourne la ligne vide correspondant a la colonne demandee
    lig = 0
    for i in range (1,6):
        if ( M[i][colonne] == 0 and M[i-1][colonne] != 0 ):
            lig = i
    return lig
 
def verification_P4():
    test2=False
 
    # test d'un P4 horizontal
    i = j = 0
    while(not(i==5 and j==3)):
        if (M[i][j]==M[i][j+1] and M[i][j]==M[i][j+2] \
            and M[i][j]==M[i][j+3] and M[i][j]==joueur):
            test2=True
        if (j==3):
            i=i+1
            j=0
        else:
            j=j+1
 
    # test d'un P4 vertical
    i = j = 0
    while(not(i==2 and j==6)):
        if (M[i][j]==M[i+1][j] and M[i][j]==M[i+2][j] \
            and M[i][j]==M[i+3][j] and M[i][j]==joueur):
            test2=True
        if (j==6):
            i=i+1
            j=0
        else:
            j=j+1
 
    # test d'un P4 diagonal vers la droite
    i=j=0
    while(not(i==2 and j==3)):
        if (M[i][j]==M[i+1][j+1] and M[i][j]==M[i+2][j+2] \
            and M[i][j]==M[i+3][j+3] and M[i][j]==joueur):
            test2=True
        if (j==3):
            i=i+1
            j=0
        else:
            j=j+1
 
    # test d'un P4 diagonal vers la gauche 
    i=0
    j=3
    while(not(i==2 and j==6)):
        if (M[i][j]==M[i+1][j-1] and M[i][j]==M[i+2][j-2] \
            and M[i][j]==M[i+3][j-3] and M[i][j]==joueur):
            test2=True
        if (j==6):
            i=i+1
            j=3
        else:
            j=j+1    
 
    return test2
 
 
 
 
 
 
 
 
 
# -*- coding: cp1252 -*-
import pygame
import sys
 
 
pygame.init ()
image = pygame.image.load ("Grille.png")
sizeim = image.get_size ()
size = (sizeim[0]*1, sizeim[1])
screen = pygame.display.set_mode (size)
screen.blit (image, (0,0))
pygame.display.flip ()
 
 
 
 
 
 
def affichage(matrice):
    screen.fill((0,0,0))
    screen.blit(image,(0,0))
    for i in range(len(matrice)):
        for j in range(len(matrice[i])):
            if matrice[i][j]==1:
                    screen.blit(pionrouge,(16+97*j,13-97.5*i+486))
                pygame.display.flip()
            if matrice[i][j]==2:
                    screen.blit(pionjaune,(16+97*j,13-97.5*i+486))
                pygame.display.flip()
 
 
pionjaune = pygame.image.load ("PionJaune.png")
pionrouge = pygame.image.load ("PionRouge.png")
font = pygame.font.Font ("freesansbold.ttf", 15)
 
 
 
 
import time
 
while (not P4 and JetonsJoues < 42):
    time.sleep (0.1)
    # Le joueur joue
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONUP :
            x,y = pygame.mouse.get_pos()
            joueur = quel_joueur()
            colonne = choisir_colonne(x,y)
     # On modifie les variables pour tenir compte du jeton depose.
            M[ligne()][colonne] = joueur
            JetonsJoues = JetonsJoues + 1
            P4 = verification_P4()
            affichage(M)
            pygame.display.flip()
 
 
        if event.type == pygame.QUIT:
            sys.exit()


D'avance merci

Sabrina
Images attachées
Type de fichier : png Grille2.PNG (130,6 Ko, 25 affichages)
Type de fichier : png PionJaune.png (4,5 Ko, 59 affichages)
Type de fichier : png PionRouge.png (4,9 Ko, 55 affichages)
Bibi88 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/03/2007, 19h03   #2
see++
Membre du Club
 
Inscription : février 2006
Messages : 52
Détails du profil
Informations personnelles :
Âge : 35
Localisation : France

Informations forums :
Inscription : février 2006
Messages : 52
Points : 56
Points : 56
Salut,

Est ce que tu pourrais mettre ton code en utilisant les balises code (bouton #), ca rend le code beaucoup plus lisible.

Sinon dans la méthode choix de colonne, je trouve la ligne très bizarre, je crois que j'aurais mis un
Code :
1
2
3
 
for lig in range(8) :
    if M[lig][col] == 0 : return col
See++
see++ est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/03/2007, 20h20   #3
see++
Membre du Club
 
Inscription : février 2006
Messages : 52
Détails du profil
Informations personnelles :
Âge : 35
Localisation : France

Informations forums :
Inscription : février 2006
Messages : 52
Points : 56
Points : 56
J'ai vu que t'avais créé un nouveau post et que tu ne comprenais pas ma remarque.
Donc juste une question :
pourquoi

Dans la fonction choix de colonne ? Pourquoi ce 5 ?

See++

see++ est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 19h04.


 
 
 
 
Partenaires

Hébergement Web