Advertisement
Black_Albatros

Problème mouvement fantomes

Jun 11th, 2023 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.79 KB | Gaming | 0 0
  1.  
  2. import sys
  3. import random
  4.  
  5. from colorama import Style, Fore
  6.  
  7.  
  8.  
  9.  
  10. def affichage(tab):
  11.  
  12.     for i in range(0,10):
  13.  
  14.         for j in range(0,10):
  15.  
  16.             if tab[i][j] == 1:
  17.  
  18.                 print("\t",'■', end = '')
  19.  
  20.             elif tab[i][j] == 0:
  21.  
  22.                 print("\t",'.', end = '')
  23.  
  24.             elif tab[i][j] == 3:
  25.  
  26.                 print("\t",'ᗧ', end = '')
  27.  
  28.             elif tab[i][j] == 2:
  29.  
  30.                 print("\t",'●', end = '')
  31.  
  32.             elif tab[i][j] == 4:
  33.  
  34.                 print("\t",'👻', end = '')
  35.  
  36.         print("\n")
  37.  
  38.    
  39.  
  40.     print("\n")
  41.  
  42.  
  43.  
  44.  
  45.  
  46. def fin():
  47.  
  48.     while True :
  49.         print("~      PARTIE TERMINEE      ~")
  50.  
  51.         print("     VOULEZ VOUS REJOUEZ ?    ")
  52.  
  53.         rep= input(str(print ("         OUI/NON            ")))
  54.  
  55.         if rep == 'OUI':
  56.             return menu()
  57.             break
  58.         elif rep == 'NON':
  59.             print(Style.BRIGHT + Fore.GREEN +"Au revoir!\n")
  60.  
  61.             print(Style.RESET_ALL)
  62.             sys.exit()
  63.        
  64.         elif rep != 'OUI' or 'NON':
  65.             print("Erreur veuillez saisir une réponse valide")
  66.             return fin()
  67.  
  68.  
  69.  
  70. def menu ():
  71.  
  72.    
  73.     for line in m:
  74.         print(Style.BRIGHT + Fore.GREEN + line)
  75.  
  76.     print(Style.BRIGHT + Fore.YELLOW +"1 ― Lancer une partie ")
  77.     print(Style.BRIGHT + Fore.RED +"2 ― Règles du jeu")
  78.     print(Style.NORMAL + Fore.WHITE + "3 - Crédits")
  79.     print(Style.BRIGHT + Fore.GREEN +"4 ― Quitter")
  80.     print(Style.RESET_ALL)
  81.  
  82.     choix = input("Entrez le numéro de votre choix : ")
  83.  
  84.     return choix
  85.  
  86.  
  87.  
  88. def rules():
  89.  
  90.     print(Style.NORMAL + Fore.CYAN +"\n―――――――――――――――――――― REGLES ――――――――――――――――――――\n")
  91.  
  92.     print(Style.RESET_ALL)
  93.  
  94.     print("Le Pac-man est un jeu où il faut accumuler des points pour gagner")
  95.  
  96.     print("Vous êtes le Pac-man, pour gagner vous devez collecter des super gommes.")
  97.  
  98.     print("Ces dernieres vous immunisent aux fantomes et vous permet de les manger. ")
  99.  
  100.     print("Attention si un fantome vous touche sans que vous soyer sous super vous perdez 1 vie ")
  101.  
  102.     print("Vous disposer d'un total de 3 vies")
  103.  
  104.     print("Bonne chance !!")
  105.  
  106. def crédits():
  107.  
  108.     print(Style.BRIGHT + Fore.BLUE +"\n――――――――――――――――― CREDITS ―――――――――――――――――\n")
  109.  
  110.     print(Style.RESET_ALL)
  111.  
  112.     print("Développé par AMIOT Antoine et REFFAY Paul")
  113.  
  114.  
  115.  
  116. def main():
  117.  
  118.     while True:
  119.  
  120.         choix = menu()  # affichage du menu principal et récupération du choix de l'utilisateur
  121.  
  122.         if choix == '1':
  123.  
  124.             # grid()
  125.  
  126.             break
  127.  
  128.         if choix == '2':
  129.  
  130.             rules()
  131.  
  132.             main()
  133.  
  134.         if choix == '3':
  135.  
  136.             crédits()
  137.  
  138.             main()
  139.  
  140.         elif choix == '4':
  141.  
  142.             return fin()
  143.  
  144.            
  145.  
  146.    
  147.  
  148. def grid():
  149.  
  150.     bg =[]
  151.  
  152.     bg_size=[10,10]
  153.  
  154.     for i in range(bg_size[0]):
  155.  
  156.         line =[]
  157.  
  158.         for j in range(bg_size[1]):
  159.  
  160.             line.append('0')
  161.  
  162.         bg.append(line)
  163.  
  164.     for i in range(bg_size[0]):
  165.  
  166.         for j in range(bg_size[1]):
  167.  
  168.             if (i == 0 or i == 9 or j == 0 or j == 9):
  169.  
  170.                 bg[i][j] = 1
  171.  
  172.             else:
  173.  
  174.                 bg[i][j] = 0
  175.  
  176.    
  177.  
  178.     return bg
  179.  
  180.  
  181.  
  182.  
  183.  
  184. bg = grid()
  185.  
  186. bg [2][2] = 1
  187.  
  188. bg [2][3] = 1
  189.  
  190. bg [3][2] = 1
  191.  
  192. bg [4][2] = 1
  193.  
  194. bg [5][2] = 1
  195.  
  196. bg [2][5] = 1
  197.  
  198. bg [2][6] = 1
  199.  
  200. bg [2][7] = 1
  201.  
  202. bg [2][8] = 1
  203.  
  204. bg [4][4] = 1
  205.  
  206. bg [4][6] = 1
  207.  
  208. bg [4][7] = 1
  209.  
  210. bg [5][7] = 1
  211.  
  212. bg [7][2] = 1
  213.  
  214. bg [7][3] = 1
  215.  
  216. bg [7][4] = 1
  217.  
  218. bg [7][5] = 1
  219.  
  220. bg [7][7] = 1
  221.  
  222. bg [6][4] = 1
  223.  
  224. bg [6][5] = 1
  225.  
  226. bg [4][3] = 1
  227.  
  228. #pac gomme
  229.  
  230. bg [1][8] = 2
  231.  
  232. bg [8][2] = 2
  233.  
  234. bg [3][3] = 2
  235.  
  236.  
  237.  
  238. #pac man
  239.  
  240. position = [3, 1]
  241.  
  242. bg [position[0]][position[1]] = 3
  243.  
  244.  
  245.  
  246.  
  247.  
  248. #fantomes
  249.  
  250.  
  251.  
  252. ft1 = [8,1]
  253.  
  254. ft2 = [6,6]
  255.  
  256. bg [ft1[0]][ft1[1]] = 4
  257.  
  258. bg [ft2[0]][ft2[1]] = 4
  259.  
  260.  
  261.  
  262. score = 0
  263. m= [    
  264.     "  _________________________________________________ ",
  265.     " |                      _                          |",
  266.     " |                |\/| |_ |\| | | |                |",
  267.     " |                |  | |_ | | |_| .                |" ,
  268.     " |_________________________________________________|",
  269.     ]      
  270. game_over = [
  271.     "   _____          __  __ ______    ______      ________ _____      _    ",
  272.     "  / ____|   /\  |  \/  |  ____|  / __ \ \   / /  ____|  __ \   | |  ",
  273.     " | |  __   /  \ | \ / | |__    | |  | \ \ / /| |__  | |__) |   | |  ",
  274.     " | | |_ | / /\ \ | |\/| |  __|   | |  | |\ \/ / |  __| |  _  /    |_|  ",
  275.     " | |__| |/ ____ \| |  | | |____  | |__| | \ /  | |____| | \ \    _  ",
  276.     "  \_____/_/    \_\_|  |_|______|  \____/   \/   |______|_|  \_\  |_| ",
  277. ]
  278.  
  279. win = [
  280. "__      _ _  ____ _____  ____  _  _____   _____     _ ",
  281. "\ \   / | |/ __/__   __\ __ \| ||  __  \|  ___|   | |",
  282. " \ \ / /| | |     | | | |  | | || |__) || |__     | |",
  283. "  \ \/ / | | |     | | | |  | | ||  _  / |  __|    |_|",
  284. "   \ /  | | |__   | | | |__| | || | \ \ | |___     _ ",
  285. "    \/   |_| ___\ |_|  \____/|_||_|  \_\|_____|   |_|",
  286. ]
  287.  
  288. #Fonction pour Mouvement PACMAN
  289.  
  290. def le_mouvement_est_valide(direction, position, grid):
  291.     if (direction == "gauche" and grid[position[0]][position[1] - 1] != 1 and grid[position[0]][position[1] - 1] != 4 and grid[position[0]][position[1] - 1] != 2):
  292.         return 0
  293.     elif (direction == "droite" and grid[position[0]][position[1] + 1] != 1 and grid[position[0]][position[1] + 1] != 4 and grid[position[0]][position[1] + 1] != 2):
  294.         return 0
  295.     elif (direction == "bas" and grid[position[0]+1][ position[1]] != 1 and grid[position[0]+1][position[1]] != 4 and grid[position[0]+1][position[1]] != 2):
  296.         return 0
  297.     elif (direction == "haut" and grid[position[0]-1][position[1]] != 1 and grid[position[0]-1][position[1]] != 4 and grid[position[0]-1][position[1]] != 2):
  298.         return 0
  299.     else:
  300.         if (direction == "gauche" and grid[position[0]][position[1] - 1] == 1) or (direction == "droite"  and grid[position[0]][position[1] + 1] == 1) or  (direction == "bas" and grid[position[0]+1][ position[1]] == 1) or (direction == "haut" and grid[position[0]-1][position[1]] != 1):
  301.             return 1
  302.         elif (direction == "droite" and grid[position[0]][position[1] + 1] == 2) or (direction == "gauche" and grid[position[0]][position[1] - 1] == 2 ) or (direction == "haut" and grid[position[0]-1][position[1]] ==2) or (direction == "bas" and grid[position[0]+1][ position[1]] == 2):
  303.             return 2
  304.         elif (direction == "droite" and grid[position[0]][position[1] + 1] == 4) or (direction == "gauche" and grid[position[0]][position[1] - 1] == 4 ) or (direction == "haut" and grid[position[0]-1][position[1]] ==4) or (direction == "bas" and grid[position[0]+1][ position[1]] == 4):
  305.            return 4
  306.  
  307.  
  308.  
  309. def choisir_direction_aleatoire1(direction,ft1):
  310.     direction_ftm1 = choisir_direction_aleatoire1()
  311.     nouvelle_position_ftm1 = goTo(direction_ftm1, ft1)
  312.     direction = ["gauche", "droite", "haut", "bas"]
  313.     return random.choice(direction)
  314.  
  315.  
  316. def choisir_direction_aleatoire2(direction,ft2):
  317.     direction_ftm2 = choisir_direction_aleatoire2()
  318.     nouvelle_position_ftm2 = goTo(direction_ftm2, ft2)
  319.     direction = ["gauche", "droite", "haut", "bas"]
  320.     return random.choice(direction)
  321.  
  322.  
  323.  
  324. def goTo(direction, position):
  325.  
  326.     nouvelle_position = position.copy()
  327.  
  328.     if (direction == "bas"):
  329.  
  330.         nouvelle_position[0] += 1
  331.  
  332.     elif (direction == "haut"):
  333.  
  334.         nouvelle_position[0] -= 1
  335.  
  336.     elif (direction == "gauche"):
  337.  
  338.         nouvelle_position[1] -= 1
  339.  
  340.     elif (direction == "droite"):
  341.  
  342.         nouvelle_position[1] += 1
  343.  
  344.    
  345.  
  346.     return nouvelle_position
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354. is_ended = False
  355.  
  356.  
  357.  
  358. main()
  359. joueur = input(Style.BRIGHT + Fore.LIGHTBLACK_EX +"Entre ton nom :")
  360. score = 0
  361.  
  362. while (not is_ended):
  363.  
  364.    
  365.     print("Partie de :", Style.BRIGHT + Fore.GREEN + joueur)
  366.     print(Style.RESET_ALL)
  367.     affichage(bg)
  368.    
  369.  
  370.  
  371.     command = input("Entrez votre déplacement :\nGauche : 4 ; Droite : 6 ; Haut : 8 ; Bas : 2\n")
  372.     print("score =",score)
  373.    
  374.  
  375.  
  376.  
  377.     """
  378.  
  379.    Gestion des touches pour les directions
  380.  
  381.    """
  382.  
  383.     if (command == "4"):
  384.  
  385.         direction = "gauche"
  386.  
  387.     elif (command == "6"):
  388.  
  389.         direction = "droite"
  390.  
  391.     elif (command == "8"):
  392.  
  393.         direction = "haut"
  394.  
  395.     elif (command == "2"):
  396.  
  397.         direction = "bas"
  398.  
  399.  
  400.  
  401. #Déplacement PACMAN  
  402.  
  403.     valide = le_mouvement_est_valide(direction, position, bg)
  404.  
  405.     if valide == 0:
  406.  
  407.         print("Déplacement possible...")
  408.         bg[position[0]][position[1]] = 0
  409.         position = goTo(direction, position)
  410.         bg[position[0]][position[1]] = 3
  411.         print("La nouvelle position est", position)
  412.  
  413.        
  414.        
  415.     elif valide == 2 :
  416.         print("Déplacement possible...")
  417.         bg[position[0]][position[1]] = 0
  418.         position = goTo(direction, position)
  419.         bg[position[0]][position[1]] = 3
  420.         score = score + 1
  421.         print("score = ", score)
  422.              
  423.  
  424.     elif valide == 1 :
  425.         print("Déplacement impossible... mur")
  426.        
  427.  
  428.     elif valide == 4 :
  429.         bg[position[0]][position[1]] = 0
  430.        
  431.         # position = goTo(direction, position)
  432.  
  433.         # bg[position[0]][position[1]] = 4
  434.  
  435.         print(joueur,"a perdu la partie")
  436.    
  437.         for line in game_over:
  438.             print(Style.BRIGHT + Fore.RED + line)
  439.             print(Style.RESET_ALL)
  440.         fin()
  441.        
  442.     if  score == 3 :
  443.         print(joueur,"a gagné la partie")
  444.         for line in win:
  445.             print(Style.BRIGHT + Fore.YELLOW + line)
  446.             print(Style.RESET_ALL)
  447.         break
  448.  
  449.    
  450.  
  451.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement