IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Programmation multimédia/Jeux Python Discussion :

Aide pour le jeu Pentago avec Pygame


Sujet :

Programmation multimédia/Jeux Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 16
    Par défaut Aide pour le jeu Pentago avec Pygame
    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 : 1105
Taille : 789 octets
    Nom : fleche2.png
Affichages : 1105
Taille : 948 octets
    Nom : bloc.png
Affichages : 1109
Taille : 248 octets
    Nom : pion1.png
Affichages : 1116
Taille : 1,3 Ko
    Nom : pion2.png
Affichages : 1114
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()

  2. #2
    Expert confirmé Avatar de BufferBob
    Profil pro
    responsable R&D vidage de truites
    Inscrit en
    Novembre 2010
    Messages
    3 041
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : responsable R&D vidage de truites

    Informations forums :
    Inscription : Novembre 2010
    Messages : 3 041
    Par défaut
    salut,

    il semble que ce soit très à la mode décidément

    je me suis contenté d'essayer d'exécuter ton code dans un premier temps (remplacer "fleche{1,2}.png" par "arrow{1,2}.png" ou inversement)
    sous Windows ça me donne ça :

    Nom : screenshot1.png
Affichages : 1238
Taille : 25,5 Ko

    pour le moins il y a un leger souci d'affichage à droite et la partie basse du plateau voit afficher les pions à droite tandis que le cadran est à gauche
    je pense que tu pourrais également facilement rajouter un test de "quit" avec la croix ou la touche échap, ça coûte pas plus cher

    Edit: après un premier survol du code :
    • ça sert à rien d'utiliser pygame.MOUSEBUTTONDOWN puisque normalement tu as importé tous les pygame.locals, donc uniquement MOUSEBUTTONDOWN ça doit fonctionner
    • ça coutera pas plus cher de déclarer LEFT_BUTTON = 1 tout en haut, ou alors rajouter une ligne de commentaire pour gagner en lisibilité
    • "Au tour du joueur", il y a une typo
    • des commentaires seraient bienvenus, au moins en tete de fonctions pour décrire ce qu'elles font

    au delà c'est de la structure du code, pour le moins tu as des pygame.display.update() un peu partout, et y compris une boucle qu'on qualifiera de "secondaire" (par opposition à la boucle principale, censée être celle où sont gérés tous les évènements) au fond de la fonction rotation()

  3. #3
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 16
    Par défaut
    Bonsoir, merci grandement pour ta réponse donc finalement je me suis diriger vers un tableau de jeu que j'ai dessiner. Pour l'instant mon problème principale est que j'ai un tableau 6x6, pour gagner le joueur doit poser au moins 5 pions verticalement horizontalement ou en diagonal seulement comme tu pourra le constater ça marche à quelques endroits, je sais que le problème viens à l’intérieur de la def winner mais je n'ai aucune idée de comment je peux résoudre ce problème...


    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
    149
    150
    151
    152
    153
    154
    import pygame, sys
    from pygame.locals import *
     
    PERSO = (64, 208, 206)
     
    EMPTY   = 0
    PLAYER1 = 1
    PLAYER2 = 2
     
    player = 1
     
     
    window = pygame.display.set_mode((1200,800))
    window.fill(PERSO)
    pygame.display.set_caption('Pentago')
    FPS = 10
    fpsClock = pygame.time.Clock()
     
     
    def displayEmptyGameboard(window):
     
        rect_un = pygame.draw.rect(window, (102, 0, 0), (46, 50, 300, 300))
        for line in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (46, 100 * line + 50), (345, 100 * line + 50), 5)             
        for column in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (100 * column + 50, 50), (100 * column + 50, 350), 5)
     
        rect_deux = pygame.draw.rect(window, (102, 0, 0), (352, 50, 300, 300))
        for line in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (352, 100 * line + 50), (652, 100 * line + 50), 5)
        for column in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (100 * column + 355 , 50), (100 * column + 355 , 350), 5)
     
        rect_trois = pygame.draw.rect(window, (102, 0, 0), (46, 355, 300, 300))
        for line in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (46, 100 * line + 355), (345, 100 * line + 355), 5)
        for column in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (100 * column + 50, 355), (100 * column + 50, 654), 5)
     
        rect_quattre = pygame.draw.rect(window, (102, 0, 0), (352, 355, 300, 300))
        for line in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (352, 100 * line + 355), (652, 100 * line + 355), 5)
        for column in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (100 * column + 355 , 355), (100 * column + 355 , 652), 5)
     
    def gameBoardIsFull(gameBoard):
        for line in gameBoard:
            for square in line:
                if square == EMPTY:
                    return False
        return True
     
    def play(window, gameBoard, player, line, column):
        if gameBoard[line][column] == 0:
            gameBoard[line][column] = player
            posX = 50 + 100 * column
            posY = 50 + 100 * line
     
            if player == 1:
                pygame.draw.circle(window, (255, 255, 255), (posX + 50, posY + 50), 35)
            else:
                pygame.draw.circle(window, (0, 0, 0), (posX + 50, posY + 50), 35)
            return True
        return False
     
    def checkTile(mouseX, mouseY):
        x = (mouseX - 50) // 100
        y = (mouseY - 50) // 100
        if x >= 0 and x < 6 and y >= 0 and y < 6:
            return y, x
        return -1, -1
     
    def winner(gameBoard, player):
        for line in gameBoard:
            win = True
            for square in line:
                if square != player:
                    win = False
                    break
     
            if win:
                return True
     
        for j in range(5):
            win  = True
            for i in range(5):
                if gameBoard[i][j] != player:
                    win = False
                    break
     
            if win:
                return True
     
        for j in range(5):
            win  = True
            for i in range(5):
                if gameBoard[j][i] != player:
                    win = False
                    break
     
            if win:
                return True
     
        win = True
        for i in range(5):
            if gameBoard[i][i] != player:
                win = False
                break
     
        if win:
            return True
     
        win = True
        for i, j in zip(range(5), range(5)):
                if gameBoard[i][j] != player:
                    win = False
                    break
     
        return win
     
    gameBoard = [[0] * 6 for i in range(6)]
    player = 1
    pygame.init()
    displayEmptyGameboard(window)
    inProgress = True
    inGame = True
     
    while inProgress:
        for event in pygame.event.get():
            if event.type == QUIT:
                inProgress = False
            elif event.type == MOUSEBUTTONUP and event.button == 1:
                if inGame:
                    line, colomn = checkTile(event.pos[0], event.pos[1])
                    if line != -1:
                        if play(window, gameBoard, player, line, colomn):
                            if winner(gameBoard, player):
                                inGame = False
                                posX = 400
                                posY = 20
                                if player == 1:
                                    pygame.draw.circle(window, (255, 255, 255), (posX + 500, posY + 50), 40)
                                else:
                                    pygame.draw.circle(window, (0, 0, 0), (posX + 500, posY + 500), 40)
                            player = 2 if player == 1 else 1
     
        if gameBoardIsFull(gameBoard):
            inGame = False
     
     
        pygame.display.update()
        fpsClock.tick(FPS)
     
    pygame.quit()

  4. #4
    Expert confirmé Avatar de BufferBob
    Profil pro
    responsable R&D vidage de truites
    Inscrit en
    Novembre 2010
    Messages
    3 041
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : responsable R&D vidage de truites

    Informations forums :
    Inscription : Novembre 2010
    Messages : 3 041
    Par défaut
    ton deuxième code n'a plus grand chose à voir avec le premier
    ...ah si, y'a toujours aussi peu de commentaires

    l'idée de stocker dans un tableau est évidemment une bonne idée, t'as une représentation virtuelle de ton plateau de jeu que tu affiches à chaque tour, tu affiches pas directement à l'écran

    pour ce qui est de checker les diagonales, je te propose un truc dans ce style, à adapter et à compléter :
    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
    # maxy
    #    [1 ][2 ][  ][  ][  ][  ]   on cherche a aligner 5 jetons, donc wincond = 5
    #  | [3 ][14][2 ][  ][  ][  ]   du coup on a 4 diagonales 1, 2, 3 et 4
    #    [  ][3 ][14][2 ][  ][  ]   (etonnant pas vrai ?)
    #  | [  ][  ][3 ][14][2 ][  ]
    #    [  ][  ][  ][3 ][14][2 ]
    #  | [  ][  ][  ][  ][3 ][ 4]
    #  0 --  --  --  --  --  --  maxx
     
    # ici on teste toutes les diagonales "descendantes" (haut/gauche -> bas/droite)
    winner = 0
    for dy in range(maxy-wincond):
       for dx in range(maxx-wincond): # il y a plusieurs "points de departs" possibles pour la diagonale, le plateau est grand !
          diagonale = ''
     
          for i in range(wincond):
             diagonale += str(plateau[i+dx][i+dy]) # si a la fin on a '11111' ou '22222' c'est qu'on a un gagnant
     
          if diagonale == str(PLAYER1)*wincond:
             winner = PLAYER1
          elif diagonale == str(PLAYER2)*wincond:
             winner = PLAYER2
     
          if winner: break
       if winner: break

  5. #5
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 16
    Par défaut
    Encore merci pour ton bout de code, j'ai finalement réussi à faire gagner le joueur dans toutes les coins et recoins ! J'ai encore un assez gros problème, je compte afficher 2 flèches pour chaque quadrant afin d’effectuer le choix d'une rotation à chaque tour, mais il faut également que les jetons tourne avec le quadrant... As tu une idée ou une piste de comment je pourrais réaliser ça ?


    Après réflexion je me dis que peut être en plaçant l'axe de rotation au centre du quadrant je pourrais faire tourner les images des pion déjà placé autour de cet axe ?

    (J'ai mis des images pour les pions dans mon jeu qui est "esthétiquement différent" de celui juste en dessous


    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
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    import pygame, sys
    from pygame.locals import *
     
    BLACK = (64, 208, 206)
     
    EMPTY   = 0
    PLAYER1 = 1
    PLAYER2 = 2
     
     
    player = 1
     
     
    window = pygame.display.set_mode((1200,800))
    window.fill(BLACK)
    pygame.display.set_caption('Pentago')
    FPS = 10
    fpsClock = pygame.time.Clock()
     
     
    def displayEmptyGameboard(window):
     
        rect_un = pygame.draw.rect(window, (102, 0, 0), (46, 50, 300, 300))
        for line in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (46, 100 * line + 50), (345, 100 * line + 50), 5)             
        for column in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (100 * column + 50, 50), (100 * column + 50, 350), 5)
     
        rect_deux = pygame.draw.rect(window, (102, 0, 0), (352, 50, 300, 300))
        for line in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (352, 100 * line + 50), (652, 100 * line + 50), 5)
        for column in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (100 * column + 355 , 50), (100 * column + 355 , 350), 5)
     
        rect_trois = pygame.draw.rect(window, (102, 0, 0), (46, 355, 300, 300))
        for line in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (46, 100 * line + 355), (345, 100 * line + 355), 5)
        for column in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (100 * column + 50, 355), (100 * column + 50, 654), 5)
     
        rect_quattre = pygame.draw.rect(window, (102, 0, 0), (352, 355, 300, 300))
        for line in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (352, 100 * line + 355), (652, 100 * line + 355), 5)
        for column in range(1, 3):
            pygame.draw.line(window, (255, 255, 255), (100 * column + 355 , 355), (100 * column + 355 , 652), 5)
     
    def gameBoardIsFull(gameBoard):
        for line in gameBoard:
            for square in line:
                if square == EMPTY:
                    return False
        return True
     
    def play(window, gameBoard, player, line, column):
        if gameBoard[line][column] == 0:
            gameBoard[line][column] = player
            posX = 50 + 100 * column
            posY = 50 + 100 * line
     
            if player == 1:
                pygame.draw.circle(window, (255, 255, 255), (posX + 50, posY + 50), 35)
            else:
                pygame.draw.circle(window, (0, 0, 0), (posX + 50, posY + 50), 35)
            return True
        return False
     
    def checkTile(mouseX, mouseY):
        x = (mouseX - 50) // 100
        y = (mouseY - 50) // 100
        if x >= 0 and x < 6 and y >= 0 and y < 6:
            return y, x
        return -1, -1
     
    def winner(gameBoard, player ):
        for line in gameBoard:
            win = True
        for square in line:
            win = True
     
    #------------------------- COLONNES ------------------------
     
    #COLONNE 1 
        for j in range(1):# Colonne 1 gagnante en partant du BAS
            win  = True
            for i in range(1,6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
        for j in range(1):# Colonne 1 gagnante en partant du HAUT
            win  = True
            for i in range(5):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
    #COLONNE 2
        for j in range(2):# Colonne 2 gagnante en partant du BAS
            win  = True
            for i in range(1,6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
        for j in range(2):# Colonne 2 gagnante en partant du HAUT
            win  = True
            for i in range(5):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
    #COLONNE 3
        for j in range(3):# Colonne 3 gagnante en partant du BAS
            win  = True
            for i in range(1,6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
        for j in range(3):# Colonne 3 gagnante en partant du HAUT
            win  = True
            for i in range(5):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
    #COLONNE 4
        for j in range(4):# Colonne 4 gagnante en partant du BAS
            win  = True
            for i in range(1,6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
        for j in range(4):# Colonne 4 gagnante en partant du HAUT
            win  = True
            for i in range(5):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
    #COLONNE 5
        for j in range(5):# Colonne 5 gagnante en partant du BAS
            win  = True
            for i in range(1,6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
        for j in range(5):# Colonne 5 gagnante en partant du HAUT
            win  = True
            for i in range(5):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
    #COLONNE 6
        for j in range(6):# Colonne 6 gagnante en partant du BAS
            win  = True
            for i in range(1,6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
        for j in range(6):# Colonne 6 gagnante en partant du HAUT
            win  = True
            for i in range(5):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
    #------------------------- COLONNES ------------------------
     
    #------------------------- LIGNES --------------------------
     
    #LIGNE 1
        for i in range(1):# Ligne 1 gagnante en partant de DROITE
            win  = True
            for j in range(1,6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
        for i in range(6):# Ligne 1 gagnante en partant de GAUCHE
            win  = True
            for j in range(5):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
    #LIGNE 2
        for i in range(2):# Ligne 2 gagnante en partant de DROITE
            win  = True
            for j in range(1,6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
        for i in range(2):# Ligne 2 gagnante en partant de GAUCHE
            win  = True
            for j in range(5):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
    #LIGNE 3
        for i in range(3):# Ligne 3 gagnante en partant de DROITE
            win  = True
            for j in range(1,6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
        for i in range(3):# Ligne 3 gagnante en partant de GAUCHE
            win  = True
            for j in range(5):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
    #LIGNE 4
        for i in range(4):# Ligne 4 gagnante en partant de DROITE
            win  = True
            for j in range(1,6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
        for i in range(4):# Ligne 4 gagnante en partant de GAUCHE
            win  = True
            for j in range(5):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
    #LIGNE 5
        for i in range(5):# Ligne 5 gagnante en partant de DROITE
            win  = True
            for j in range(1,6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
        for i in range(5):# Ligne 5 gagnante en partant de GAUCHE
            win  = True
            for j in range(5):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
    #LIGNE 6
        for i in range(6):# Ligne 6 gagnante en partant de DROITE
            win  = True
            for j in range(1,6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
     
        for i in range(6):# Ligne 6 gagnante en partant de GAUCHE
            win  = True
            for j in range(6):
                if gameBoard[i][j] != player:
                    win = False
                    break
            if win:
                return True
    #------------------------- LIGNES --------------------------
     
    #------------------------------------------ DIAGONALES ------------------------------------------
     
    #DIAGONALE 1 
        win = True
        for i, j in zip(range(5), range(5, -1, -1)):# Diagonale de droit à gauche en partant du haut
                if gameBoard[i][j] != player:
                    win = False
                    break
        if win:
            return True
     
        win = True
        for i, j in zip(range(1, 6), range(4, -1, -1)):# Diagonale de droit à gauche en partant du BAS
                if gameBoard[i][j] != player:
                    win = False
                    break
        if win:
            return True
     
     
     
    #DIAGONALE 2 
        win = True
        for i, j in zip(range(5), range(6)):# Diagonale de gauche à droite en partant du HAUT
                if gameBoard[i][j] != player:
                    win = False
                    break
        if win:
            return True
     
        win = True
        for i, j in zip(range(1,6), range(1 ,6, 1)):# Diagonale de gauche à droite en partant du BAS
                if gameBoard[i][j] != player:
                    win = False
                    break
        if win:
            return True
     
    #DIAGONALE PARALELLE DU BAS A LA DIAGONALE 2 
        win = True
        for i, j in zip(range(1,6), range(6)):
                if gameBoard[i][j] != player:
                    win = False
                    break
        if win:
            return True
     
    #DIAGONALE PARALELLE DU BAS A LA DIAGONALE 1
        win = True
        for i, j in zip(range(1,6), range(5, 0, -1)):# Diagonale paralelle à la diagonale 2
                if gameBoard[i][j] != player:
                    win = False
                    break
        if win:
            return True
     
    #DIAGONALE PARALELLE DU HAUT A LA DIAGONALE 2 
        win = True
        for i, j in zip(range(6), range(1,6)):
                if gameBoard[i][j] != player:
                    win = False
                    break
        if win:
            return True
     
        win = True
        for i, j in zip(range(6), range(4,-1,-1)):
                if gameBoard[i][j] != player:
                    win = False
                    break
        if win:
            return True
     
        return win
     
    #------------------------------------------ DIAGONALES ------------------------------------------
     
    gameBoard = [[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]]
     
     
     
     
     
     
     
    player = 1
    pygame.init()
    displayEmptyGameboard(window)
    inProgress = True
    inGame = True
     
    while inProgress:
        for event in pygame.event.get():
            if event.type == QUIT:
                inProgress = False
            elif event.type == MOUSEBUTTONUP and event.button == 1:
                if inGame:
                    line, colomn = checkTile(event.pos[0], event.pos[1])
                    if line != -1:
                        if play(window, gameBoard, player, line, colomn):
                            if winner(gameBoard, player):
                                inGame = False
                                posX = 400
                                posY = 20
                                if player == 1:
                                    pygame.draw.circle(window, (255, 255, 255), (posX + 500, posY + 50), 40)
                                else:
                                    pygame.draw.circle(window, (0, 0, 0), (posX + 500, posY + 500), 40)
                            player = 2 if player == 1 else 1
     
        if gameBoardIsFull(gameBoard):
            inGame = False
     
     
        pygame.display.update()
        fpsClock.tick(FPS)
     
    pygame.quit()

  6. #6
    Expert confirmé Avatar de BufferBob
    Profil pro
    responsable R&D vidage de truites
    Inscrit en
    Novembre 2010
    Messages
    3 041
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : responsable R&D vidage de truites

    Informations forums :
    Inscription : Novembre 2010
    Messages : 3 041
    Par défaut
    pour la rotation tu peux utiliser pygame.transform.rotate()un exemple à adapter/dont s'inspirer librement :
    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
    import pygame
     
    pygame.init()
     
    window = pygame.display.set_mode((150, 150))
     
    s1 = pygame.Surface((50, 50))
    pygame.draw.rect(s1, (0, 0, 255), (5, 5, 35, 35))
     
    s2 = pygame.Surface((50, 50))
    pygame.draw.rect(s2, (255, 0, 0), (10, 10, 40, 40))
    s2.set_colorkey((0,0,0)) # tout ce qui est noir dans la surface sera considere comme transparent (en l'occurrence le fond)
    s2.set_alpha(96) # transparence globale d'affichage de la surface
     
    inProgress, i, fps = True, 0, 60
    clock = pygame.time.Clock()
    while inProgress:
       for event in pygame.event.get():
          if event.type == pygame.QUIT:
             inProgress = False
       # on affiche la scene
       window.fill((64, 208, 206))
       surf1 = pygame.transform.rotate(s1, i) # la rotation agrandit la surface et laisse apparaitre le fond noir pour s1
       surf2 = pygame.transform.rotate(s2, -2 * i) # pour s2 c'est le meme principe, sauf qu'on a defini la colorkey plus haut
       i = (i + 1) % 360
       window.blit(surf1, (20, 20))
       window.blit(surf2, (60, 40))
       pygame.display.update()
       clock.tick(fps)
    pygame.quit()
    finalement faire une fonction rotate_left() qui fait pivoter un cadran sur 90° n'est pas très compliqué

Discussions similaires

  1. [Pygame] Demande d'aide pour coder un jeu
    Par Andromedx dans le forum GUI
    Réponses: 1
    Dernier message: 11/04/2013, 22h27
  2. Besoin d'aide pour jeu de Pinball
    Par amintheone dans le forum Développement 2D, 3D et Jeux
    Réponses: 1
    Dernier message: 08/08/2010, 15h22
  3. besoin d'aide pour jeu d'awalé
    Par Dabech dans le forum Débuter avec Java
    Réponses: 6
    Dernier message: 12/04/2008, 22h41
  4. aide pour jeu de la bataille navale
    Par Jeannot Alpin dans le forum Delphi
    Réponses: 17
    Dernier message: 19/11/2006, 20h33
  5. Besoin d'aide pour un projet de jeu en ligne
    Par FLEO dans le forum Projets
    Réponses: 1
    Dernier message: 21/10/2005, 08h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo