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

Python Discussion :

Comment transformé ce code en C en python ?


Sujet :

Python

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Août 2005
    Messages
    67
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2005
    Messages : 67
    Par défaut Comment transformé ce code en C en python ?
    Voilà, c'est un petit jeu en mode console, j'aimerai le transformé en python ?
    Merci de m'aider


    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
    #include <stdio.h>
    #include <stdlib.h>                                   
    int DeterminerDirection(char ToucheAppuyee) //On associe la touche appuyée au nombre qu'il faut ajouter à la position
            {
            switch(ToucheAppuyee)
                    {
                    case 'z':
                            return -10;
                            break;
                    case 'q':
                            return -1;
                            break;
                    case 's':
                            return 10;
                            break;
                    case 'd':
                            return 1;
                            break;
                    }
            return 0;
            }
    int TypeMouvement(char PositionNext,char PositionNextNext) //Qu'y a-t-il devant et peut-on avancer ?
            {
            if (PositionNext == ' ' ) //S'il y a rien devant
                    {
                    return 1; //Cas numéro 1
                    }
            if (PositionNext == '#' ) //S'il y a un bloc devant
                    {
                    if (PositionNextNext == ' ' ) //Et qu'après c'est libre
                            {
                            return 2;
                            }
                    if ( PositionNextNext == 'O' ) 
                            {
                            return 3;
                            }
                    }
            return 0;
            }
    int main(int argc, char *argv[])
            {
            //Déclaration et initialisataion des variables
            char Matrice[100] = {'X','X','X','X','X','X','X','X','X','X','X',' ',' ',' ',' ',' ',' ',' ',' ','X','X',' ','J',' ',' ',' ',' ',' ',' ','X','X',' ',' ',' ',' ',' ',' ',' ',' ','X','X',' ',' ',' ',' ','#',' ',' ',' ','X','X',' ',' ',' ',' ',' ',' ',' ',' ','X','X',' ',' ',' ',' ',' ',' ',' ',' ','X','X',' ',' ',' ',' ',' ',' ',' ','O','X','X',' ',' ',' ',' ',' ',' ',' ',' ','X','X','X','X','X','X','X','X','X','X','X'}; //On initialise le niveau
            long PositionDuJoueur;
            int NombreDeCubesRestants,Quitter,Direction,Mouvement;
            char ToucheAppuyee,i,j;
            NombreDeCubesRestants = 1;
            Quitter = 1;
            PositionDuJoueur = 22;//On initialise la position du joueur
            //Présentation
            printf("                                                             \n");
            printf("                              _____________________          \n");
            printf("                             |                     |         \n");
            printf("                             |   PushBlocConsole   |         \n");
            printf("                             |_____________________|         \n");
            printf("                                                             \n\n");
     
            printf("          Appuyer sur 'q' pour aller a gauche.\n");
            printf("                 -sur 'z' pour aller en haut.\n");
            printf("                 -sur 's' pour aller en bas.\n");
            printf("                 -sur 'd' pour aller a droite.\n\n");
            printf("Poussez les Blocs dans le trou.\n\n");
     
            printf("Le Joueur    -> 'J'\n");
            printf("Les Blocs    -> '#'\n");
            printf("Le Trou      -> 'O'\n\n");
     
            printf("Appuyez sur n'importe quelle touche pour jouer");
            getch();
            //InitialiserJeu();
            //Début de la boucle de jeu
            while (NombreDeCubesRestants != 0 && Quitter == 1)
                    {
                    system("CLS");//On efface l'écran
                    //On affiche le niveau
                    printf("%c%c%c%c%c%c%c%c%c%c \n",Matrice[0],Matrice[1],Matrice[2],Matrice[3],Matrice[4],Matrice[5],Matrice[6],Matrice[7],Matrice[8],Matrice[9]);
                    printf("%c%c%c%c%c%c%c%c%c%c \n",Matrice[10],Matrice[11],Matrice[12],Matrice[13],Matrice[14],Matrice[15],Matrice[16],Matrice[17],Matrice[18],Matrice[19]);
                    printf("%c%c%c%c%c%c%c%c%c%c \n",Matrice[20],Matrice[21],Matrice[22],Matrice[23],Matrice[24],Matrice[25],Matrice[26],Matrice[27],Matrice[28],Matrice[29]);
                    printf("%c%c%c%c%c%c%c%c%c%c \n",Matrice[30],Matrice[31],Matrice[32],Matrice[33],Matrice[34],Matrice[35],Matrice[36],Matrice[37],Matrice[38],Matrice[39]);
                    printf("%c%c%c%c%c%c%c%c%c%c \n",Matrice[40],Matrice[41],Matrice[42],Matrice[43],Matrice[44],Matrice[45],Matrice[46],Matrice[47],Matrice[48],Matrice[49]);
                    printf("%c%c%c%c%c%c%c%c%c%c \n",Matrice[50],Matrice[51],Matrice[52],Matrice[53],Matrice[54],Matrice[55],Matrice[56],Matrice[57],Matrice[58],Matrice[59]);
                    printf("%c%c%c%c%c%c%c%c%c%c \n",Matrice[60],Matrice[61],Matrice[62],Matrice[63],Matrice[64],Matrice[65],Matrice[66],Matrice[67],Matrice[68],Matrice[69]);
                    printf("%c%c%c%c%c%c%c%c%c%c \n",Matrice[70],Matrice[71],Matrice[72],Matrice[73],Matrice[74],Matrice[75],Matrice[76],Matrice[77],Matrice[78],Matrice[79]);
                    printf("%c%c%c%c%c%c%c%c%c%c \n",Matrice[80],Matrice[81],Matrice[82],Matrice[83],Matrice[84],Matrice[85],Matrice[86],Matrice[87],Matrice[88],Matrice[89]);
                    printf("%c%c%c%c%c%c%c%c%c%c \n",Matrice[90],Matrice[91],Matrice[92],Matrice[93],Matrice[94],Matrice[95],Matrice[96],Matrice[97],Matrice[98],Matrice[99]);
                    ToucheAppuyee = getch();//Détermination de la touche appuyée
                    if (ToucheAppuyee != '0') //Si la touche appuyée n'est pas celle qui permet de quitter le jeu
                            {
                            Direction = DeterminerDirection(ToucheAppuyee);
                            i = PositionDuJoueur + Direction;
                            j = PositionDuJoueur + Direction * 2;
                            Mouvement = TypeMouvement(Matrice[i],Matrice[j]);
                            if (Mouvement != 0)
                                    {
                                    if (Mouvement == 1) //Y a rien devant, le chemin est libre
                                            {
                                            Matrice[PositionDuJoueur] = ' ';
                                            Matrice[PositionDuJoueur + Direction] = 'J';
                                            PositionDuJoueur = i;
                                            }
                                    if (Mouvement == 2) //On pousse le bloc
                                            {
                                            Matrice[PositionDuJoueur + Direction * 2] = '#';
                                            Matrice[PositionDuJoueur + Direction] = 'J';
                                            Matrice[PositionDuJoueur] = ' ';
                                            PositionDuJoueur = i;
                                            }
                                    if (Mouvement == 3) //On pousse le bloc dans le trou
                                            {
                                            Matrice[PositionDuJoueur] = ' ';
                                            Matrice[PositionDuJoueur + Direction] = 'J';
                                            PositionDuJoueur = i;
                                            NombreDeCubesRestants--;
                                            }
                                    }
                            }
                    else //Si tu ne veux pas ne pas quitter, c'est que tu veux quitter !!!
                            {
                            Quitter = 0;
                            }
                    }
            system("CLS");// On fait le champ libre
            if (NombreDeCubesRestants == 0) printf("Bravo tu a reussi !");
            else printf("Bye Bye !");
            getch();
            return 0;
            }

  2. #2
    Membre expérimenté
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    222
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 222
    Par défaut
    Python étant un langage orienté objet, voilà comment je ferais:
    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
     
    import msvcrt, os
     
    class Jeu:
        def __init__(self, matrice):
            self.matrice = matrice
            self.nombreDeCubesRestants = self.matrice.count('O')
            self.positionJoueur = self.matrice.index("J")
            self.determinerDirection = {'z': -10, 'q': -1, 's': 10, 'd': 1}
            self.showCommande()
            msvcrt.getch()
            self.loop()
     
        def showCommande(self):
            listPrint = ("'q' => gauche.", "'z' => haut", "'s' => bas", "'d' => droite.", "'k' => Quitter", "",
    			"Le Joueur -> 'J'", "Les Blocs -> '#'", "Le Trou -> 'O'", "",
                "Appuyez sur n'importe quelle touche pour jouer")
            for p in listPrint:
                print p
     
        def move(self, toucheAppuyee):
            if not self.determinerDirection.has_key(toucheAppuyee):
                return
            direction = self.determinerDirection[toucheAppuyee]
            newPos = self.positionJoueur + direction
            newPosValue = self.matrice[newPos]     
     
            if newPosValue == ' ':
                self.matrice[self.positionJoueur] = ' '
                self.matrice[newPos] = 'J'
                self.positionJoueur = newPos
     
            elif newPosValue == '#':
                newPosNext = newPos + direction
                if self.matrice[newPosNext] == 'X':
                    return
     
                if self.matrice[newPosNext] == 'O':
                    self.nombreDeCubesRestants -= 1;
                self.matrice[newPosNext] = '#';
                self.matrice[newPos] = 'J';
                self.matrice[self.positionJoueur] = ' ';
                self.positionJoueur = newPos
     
        def loop(self):
            while self.nombreDeCubesRestants:
                os.system("cls")
                for m in range(0, len(self.matrice), 10):
                    print "".join(self.matrice[m:m+10])
                touche = msvcrt.getch()
                if touche == "k":
                    break
                self.move(touche)
     
            os.system("cls");
            if not self.nombreDeCubesRestants: 
                print "Bravo tu a reussi !";
            else: 
                print "Bye Bye !";
            msvcrt.getch()
     
    matrice =  ['X','X','X','X','X','X','X','X','X','X',
                'X',' ',' ',' ',' ',' ',' ',' ',' ','X',
                'X',' ','J',' ',' ',' ',' ',' ',' ','X',
                'X',' ',' ',' ',' ',' ',' ',' ',' ','X',
                'X',' ',' ',' ',' ','#',' ',' ',' ','X',
                'X',' ',' ',' ',' ',' ',' ',' ',' ','X',
                'X',' ',' ',' ',' ',' ',' ',' ',' ','X',
                'X',' ',' ',' ',' ',' ',' ',' ','O','X',
                'X',' ',' ',' ',' ',' ',' ',' ',' ','X',
                'X','X','X','X','X','X','X','X','X','X']
     
    Jeu(matrice)
    Le module msvcrt fonctionne uniquement pour windows.
    Si tu es sur linux il faut procéder un peu différemment:

    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
     
    import sys, tty, termios, os
     
     
    class Jeu:
        def __init__(self, matrice):
            self.matrice = matrice
            self.nombreDeCubesRestants = self.matrice.count('O')
            self.positionJoueur = self.matrice.index("J")
            self.determinerDirection = {'z': -10, 'q': -1, 's': 10, 'd': 1}
            self.showCommande()
            self.getch()
            self.loop()
     
        def showCommande(self):
            listPrint = ("'q' => gauche.", "'z' => haut", "'s' => bas", "'d' => droite.", "'k' => Quitter", "",
    			"Le Joueur -> 'J'", "Les Blocs -> '#'", "Le Trou -> 'O'", "",
                "Appuyez sur n'importe quelle touche pour jouer")
            for p in listPrint:
                print p
     
        def getch(self):
            fd = sys.stdin.fileno()
            old_settings = termios.tcgetattr(fd)
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
            return ch
     
        def move(self, toucheAppuyee):
            if not self.determinerDirection.has_key(toucheAppuyee):
                return
            direction = self.determinerDirection[toucheAppuyee]
            newPos = self.positionJoueur + direction
            newPosValue = self.matrice[newPos]     
     
            if newPosValue == ' ':
                self.matrice[self.positionJoueur] = ' '
                self.matrice[newPos] = 'J'
                self.positionJoueur = newPos
     
            elif newPosValue == '#':
                newPosNext = newPos + direction
                if self.matrice[newPosNext] == 'X':
                    return
     
                if self.matrice[newPosNext] == 'O':
                    self.nombreDeCubesRestants -= 1;
                self.matrice[newPosNext] = '#';
                self.matrice[newPos] = 'J';
                self.matrice[self.positionJoueur] = ' ';
                self.positionJoueur = newPos
     
        def loop(self):
            while self.nombreDeCubesRestants:
                os.system("clear")
                for m in range(0, len(self.matrice), 10):
                    print "".join(self.matrice[m:m+10])
                touche = self.getch()
                if touche == "k":
                    break
                self.move(touche)
     
            os.system("clear");
            if not self.nombreDeCubesRestants: 
                print "Bravo tu a reussi !";
            else: 
                print "Bye Bye !";
            self.getch();
     
    matrice = ['X','X','X','X','X','X','X','X','X','X',
                'X',' ',' ',' ',' ',' ',' ',' ',' ','X',
                'X',' ','J',' ',' ',' ',' ',' ',' ','X',
                'X',' ',' ',' ',' ',' ',' ',' ',' ','X',
                'X',' ',' ',' ',' ','#',' ',' ',' ','X',
                'X',' ',' ',' ',' ',' ',' ',' ',' ','X',
                'X',' ',' ',' ',' ',' ',' ',' ',' ','X',
                'X',' ',' ',' ',' ',' ',' ',' ','O','X',
                'X',' ',' ',' ',' ',' ',' ',' ',' ','X',
                'X','X','X','X','X','X','X','X','X','X']
     
    Jeu(matrice)

Discussions similaires

  1. Réponses: 1
    Dernier message: 16/05/2013, 16h31
  2. Réponses: 7
    Dernier message: 03/03/2010, 18h33
  3. Comment transformer un code Vb en Dll ( treeview ,treenode )
    Par lunik dans le forum Windows Forms
    Réponses: 3
    Dernier message: 16/03/2009, 00h42
  4. Comment écrire un shell qui transforme un code ascii en caractère?
    Par jack-ft dans le forum Shell et commandes GNU
    Réponses: 3
    Dernier message: 21/04/2008, 17h51
  5. [Outils] Comment transformer du vieux code VB en .Net ?
    Par linux dans le forum EDI/Outils
    Réponses: 10
    Dernier message: 17/02/2006, 10h29

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