Advertisement
Black_Albatros

Python Jeu Pacman AMIOT Antoine

Jun 7th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.75 KB | Gaming | 0 0
  1. import random
  2. from colorama import Style, Fore
  3.  
  4.  
  5. def affichage(tab):
  6.     for i in range(0,10):
  7.         for j in range(0,10):
  8.             if tab[i][j] == 1:
  9.                 print("\t",'■', end = '')
  10.             elif tab[i][j] == 0:
  11.                 print("\t",'.', end = '')
  12.             elif tab[i][j] == 3:
  13.                 print("\t",'ᗧ', end = '')
  14.             elif tab[i][j] == 2:
  15.                 print("\t",'●', end = '')
  16.             elif tab[i][j] == 4:
  17.                 print("\t",'👻', end = '')
  18.  
  19.         print("\n")
  20.    
  21.     print("\n")
  22.  
  23.  
  24. def fin():
  25.     print("~      PARTIE TERMINEE      ~")
  26.     print("     VOULEZ VOUS REJOUEZ ?    \n")
  27.     rep= input(print ("         OUI/NON            "))
  28.     if rep == 'OUI':
  29.         main
  30.     else :
  31.         print(Style.BRIGHT + Fore.GREEN +"Au revoir!\n")
  32.         print(Style.RESET_ALL)
  33.         is_ended = True
  34.        
  35.    
  36. def menu ():
  37.     print(Style.NORMAL + Fore.GREEN +"\n――――――― MENU ―――――――\n")
  38.     print(Style.BRIGHT + Fore.MAGENTA +"1 ― Lancer une partie ")
  39.     print(Style.BRIGHT + Fore.CYAN +"2 ― Afficher les règles")
  40.     print((Style.BRIGHT + Fore.BLUE) +"3 ― Afficher les crédits")
  41.     print(Style.BRIGHT + Fore.GREEN +"4 ― Quitter\n")
  42.     print(Style.RESET_ALL)
  43.     choix = input("Entrez le numéro de votre choix : ")
  44.     return choix
  45.  
  46.  
  47. def rules():
  48.     print(Style.NORMAL + Fore.CYAN +"\n―――――――――――――――――――― REGLES ――――――――――――――――――――\n")
  49.     print(Style.RESET_ALL)
  50.     print("Le Pac-man est un jeu où il faut accumuler des points pour gagner")
  51.     print("Vous êtes le Pac-man, pour gagner vous devez collecter des super gommes.")
  52.     print("Ces dernieres vous immunisent aux fantomes et vous permet de les manger. ")
  53.     print("Attention si un fantome vous touche sans que vous soyer sous super vous perdez 1 vie ")
  54.     print("Vous disposer d'un total de 3 vies")
  55.     print("Bonne chance !!")
  56.  
  57. def crédits():
  58.     print(Style.BRIGHT + Fore.BLUE +"\n――――――――――――――――― CREDITS ―――――――――――――――――\n")
  59.     print(Style.RESET_ALL)
  60.     print("Développé par AMIOT Antoine et REFFAY Paul")
  61.  
  62.  
  63. def main():
  64.     while True:
  65.         choix = menu()  # affichage du menu principal et récupération du choix de l'utilisateur
  66.         if choix == '1':
  67.             # grid()
  68.             break
  69.         if choix == '2':
  70.             rules()
  71.             main()
  72.         if choix == '3':
  73.             crédits()
  74.             main()
  75.         elif choix == '4':
  76.             return fin()
  77.            
  78.    
  79. def grid():
  80.     bg =[]
  81.     bg_size=[10,10]
  82.     for i in range(bg_size[0]):
  83.         line =[]
  84.         for j in range(bg_size[1]):
  85.             line.append('0')
  86.         bg.append(line)
  87.  
  88.     for i in range(bg_size[0]):
  89.         for j in range(bg_size[1]):
  90.             if (i == 0 or i == 9 or j == 0 or j == 9):
  91.                 bg[i][j] = 1
  92.             else:
  93.                 bg[i][j] = 0
  94.    
  95.     return bg
  96.  
  97.  
  98. bg = grid()
  99.  
  100. bg [2][2] = 1
  101. bg [2][3] = 1
  102. bg [3][2] = 1
  103. bg [4][2] = 1
  104. bg [5][2] = 1
  105. bg [2][5] = 1
  106. bg [2][6] = 1
  107. bg [2][7] = 1
  108. bg [2][8] = 1
  109. bg [4][4] = 1
  110. bg [4][6] = 1
  111. bg [4][7] = 1
  112. bg [5][7] = 1
  113. bg [7][2] = 1
  114. bg [7][3] = 1
  115. bg [7][4] = 1
  116. bg [7][5] = 1
  117. bg [7][7] = 1
  118. bg [6][4] = 1
  119. bg [6][5] = 1
  120.  
  121. #pac gomme
  122. bg [1][8] = 2
  123. bg [8][2] = 2
  124. bg [3][3] = 2
  125.  
  126. #pac man
  127. position = [3, 1]
  128. bg [position[0]][position[1]] = 3
  129.  
  130.  
  131. #fantomes
  132.  
  133. ft1 = [8,8]
  134. ft2 = [6,6]
  135. bg [ft1[0]][ft1[1]] = 4
  136. bg [ft2[0]][ft2[1]] = 4
  137.  
  138. score = 0
  139.  
  140. #Fonction pour Mouvement PACMAN
  141. def le_mouvement_est_valide(direction, position, grid):
  142.     if (direction == "gauche" and grid[position[0]][position[1] - 1] != 1):
  143.         return True
  144.     elif (direction == "droite" and grid[position[0]][position[1] + 1] != 1):
  145.         return True
  146.     elif (direction == "bas" and grid[position[0]+1][ position[1]] != 1):
  147.         return True
  148.     elif (direction == "haut" and grid[position[0]-1][position[1]] != 1):
  149.         return True
  150. #Fonction pour interaction fantome    
  151. def le_mvmt_est_invalide(direction, position,grid):
  152.     if (direction == "gauche" and grid[position[0]][position[1] - 1] == 4):
  153.         return True
  154.     elif (direction == "droite" and grid[position[0]][position[1] + 1] ==4):
  155.         return True
  156.     elif (direction == "bas" and grid[position[0]+1][ position[1]] == 4):
  157.         return True
  158.     elif (direction == "haut" and grid[position[0]-1][position[1]] == 4):
  159.         return True
  160.  
  161. # def manger_super (direction,position,grid):
  162. #     if (direction == "gauche" and grid[position[0]][position[1] - 1] == 2):
  163. #         return True
  164. #     elif (direction == "droite" and grid[position[0]][position[1] + 1]  == 2):
  165. #         return True
  166. #     elif (direction == "bas" and grid[position[0]+1][ position[1]]  == 2):
  167. #         return True
  168. #     elif (direction == "haut" and grid[position[0]-1][position[1]]  == 2):
  169. #         return True
  170.  
  171.  
  172.  
  173. # fonction mouvement des fantomes en aléatoire
  174.  
  175. # def choisir_direction_aleatoire1(direction,ft1):
  176. #     nouvelle_position_ftm1 = goTo(direction_ftm1, ft1)
  177. #     direction = ["gauche", "droite", "haut", "bas"]
  178. #     return random.choice(direction)
  179.  
  180.  
  181. # def choisir_direction_aleatoire2(direction,ft2):
  182. #     nouvelle_position_ftm2 = goTo(direction_ftm2, ft2)
  183. #     direction = ["gauche", "droite", "haut", "bas"]
  184. #     return random.choice(direction)
  185.  
  186.  
  187.  
  188.  
  189. def goTo(direction, position):
  190.     nouvelle_position = position
  191.     if (direction == "bas"):
  192.         nouvelle_position[0] += 1
  193.     elif (direction == "haut"):
  194.         nouvelle_position[0] -= 1
  195.     elif (direction == "gauche"):
  196.         nouvelle_position[1] -= 1
  197.     elif (direction == "droite"):
  198.         nouvelle_position[1] += 1
  199.    
  200.     return nouvelle_position
  201.  
  202.  
  203.  
  204. is_ended = False
  205.  
  206. score = 0
  207. main()
  208.  
  209. while (not is_ended):
  210.    
  211.     affichage(bg)
  212.  
  213.     command = input("Entrez votre déplacement :\nGauche : 4 ; Droite : 6 ; Haut : 8 ; Bas : 2\n")
  214.    
  215.  
  216.     """
  217.    Gestion des touches pour les directions
  218.    """
  219.     if (command == "4"):
  220.         direction = "gauche"
  221.     elif (command == "6"):
  222.         direction = "droite"
  223.     elif (command == "8"):
  224.         direction = "haut"
  225.     elif (command == "2"):
  226.         direction = "bas"
  227.  
  228. #Déplacement PACMAN    
  229.     if (le_mouvement_est_valide(direction, position, bg)):
  230.         print("Déplacement possible...")
  231.         bg[position[0]][position[1]] = 0
  232.         position = goTo(direction, position)
  233.         bg[position[0]][position[1]] = 3
  234.         print("La nouvelle position est", position)
  235.     else :
  236.         print("Déplacement impossible...")
  237.        
  238.    
  239. # Interaction avec les fantômes
  240.  
  241.     if (le_mvmt_est_invalide(direction, position, bg)):
  242.         bg[position[0]][position[1]] = 0
  243.         position = goTo(direction, position)
  244.         is_ended = True
  245.         bg[position[0]][position[1]] = 4
  246.         print("\n――――――――――――――――― TU AS PERDU ―――――――――――――――――\n")
  247.         main()
  248.        
  249. # mouvement des fantomes en aléatoire        
  250.     # if (le_mouvement_est_valide(direction_ftm1, ft1, bg)) and bg !=1:
  251.     #     direction_ftm1 = choisir_direction_aleatoire1()
  252.     #     bg[ft1[0]][ft1[1]] = 0
  253.     #     ft1 = nouvelle_position_ftm1
  254.     #     bg[ft1[0]][ft1[1]] = 4
  255.  
  256.    
  257.    
  258.     # if (le_mouvement_est_valide(direction_ftm2, ft2, bg)) and bg !=1:
  259.     #     direction_ftm2 = choisir_direction_aleatoire2()
  260.     #     bg[ft2[0]][ft2[1]] = 0
  261.     #     ft2 = nouvelle_position_ftm2
  262.     #     bg[ft2[0]][ft2[1]] = 4
  263.     # if (manger_super(direction, position, grid)):
  264.     #     score = score +1
  265.     #     print("score =",score)
  266.        
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement