Advertisement
IsakViste

ISN: The Maze

May 30th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 26.64 KB | None | 0 0
  1. #The Maze : Projet ISN réalisé par Bryan A., Martin C. et Isak V., élèves de TS6.
  2.  
  3. import sys, os, time, pygame
  4. import tkinter as tk
  5. import time
  6. from tkinter import filedialog
  7. from random import randint
  8.  
  9. # -------- TKINTER --------
  10. # Fenetre pour les sauvegardes/chargement de carte
  11. root = tk.Tk()
  12. root.withdraw()
  13.  
  14. # -------- PYGAME --------
  15. # Fenetre pour le jeu et ses menus
  16. pygame.init()
  17. myfont = pygame.font.SysFont("monospace", 50)
  18. creditfont = pygame.font.SysFont("monospace", 40, italic=True)
  19.  
  20. # -------- § Variables § --------
  21. # Couleurs
  22. BLACK = (0, 0, 0)
  23. GRAY = (150, 150, 150)
  24. WHITE = (255, 255, 255)
  25.  
  26. # Dossier = "C:/Users/Isak Viste/Documents/Programming/TheMaze/"
  27.  
  28. # Carte de jeu
  29. mapSize = 100
  30. Map = []
  31. startPos = (0, 0)
  32.  
  33. # Joueur (position et rotation (statistiques de départ))
  34. x, y = 0, 0
  35. prevRotation = 0
  36. morts = 0
  37. start = 0
  38. end = 0
  39. duree = 0
  40.  
  41. # Taille et Nom de la fenetre
  42. screen = pygame.display.set_mode((700, 700))
  43. temps = 0
  44.  
  45. # Textures - Terrain
  46. rawTextures = ["Ground", "Wall", "Wasteland", "Start", "Goal", "Trap", "Decor"]
  47. finTextures = []
  48. imageSize = (0, 0)
  49.  
  50. # Textures - Pieges et Decors
  51. xTraps = 3
  52. xDecors = 3
  53. trapTextures = []
  54. decorTextures = []
  55.  
  56. # Texture - Joueur
  57. playerTexture = pygame.image.load(os.path.join("Player.gif")).convert_alpha()
  58. playerTexture = pygame.transform.scale(playerTexture, (100, 100))
  59.  
  60. playerTextureDead = pygame.image.load(os.path.join("PlayerDead.gif")).convert_alpha()
  61. playerTextureDead = pygame.transform.scale(playerTextureDead, (100, 100))
  62.  
  63. # Texture - Banners
  64. winBannerTexture = pygame.image.load(os.path.join("WinBanner.jpg")).convert()
  65. tryAgainBannerTexture = pygame.image.load(os.path.join("TryAgainBanner.jpg")).convert()
  66.  
  67. # Texture - Boutton
  68. butTextures = []
  69.  
  70. for i in range(6):
  71.     texture = pygame.image.load(os.path.join('Button' + str(i) + '.gif')).convert_alpha()
  72.     butTextures.append(pygame.transform.scale(texture, (300, 50)))
  73.     texture = pygame.image.load(os.path.join('Button' + str(i) + 'i' + '.gif')).convert_alpha()
  74.     butTextures.append(pygame.transform.scale(texture, (300, 50)))
  75.  
  76. level01Button = pygame.image.load(os.path.join("Level01.gif")).convert_alpha()
  77. level01Button = pygame.transform.scale(level01Button, (300, 50))
  78. level01Buttoni = pygame.image.load(os.path.join("Level01i.gif")).convert_alpha()
  79. level01Buttoni = pygame.transform.scale(level01Buttoni, (300, 50))
  80.  
  81. loadButton = pygame.image.load(os.path.join("Load.gif")).convert_alpha()
  82. loadButton = pygame.transform.scale(loadButton, (300, 50))
  83. loadButtoni = pygame.image.load(os.path.join("Loadi.gif")).convert_alpha()
  84. loadButtoni = pygame.transform.scale(loadButtoni, (300, 50))
  85.  
  86. # Pygame - Conteur
  87. clock = pygame.time.Clock()
  88.  
  89. # -------- MENU PRINCIPAL --------
  90. def MainMenu():
  91.     # ------ § Variables § ------
  92.     global playerTexture
  93.     global temps
  94.    
  95.     isInMainMenu = True
  96.  
  97.     while isInMainMenu:
  98.         # Nom de la fenetre
  99.         if(pygame.display.get_caption() != "The Maze | Main Menu"):
  100.             pygame.display.set_caption("The Maze | Main Menu")
  101.        
  102.         # ---- BOUCLE D'EVENEMENT ----
  103.         for event in pygame.event.get():
  104.             if (event.type == pygame.QUIT):
  105.                 isInMainMenu = False
  106.             if (event.type == pygame.MOUSEBUTTONUP):
  107.                 # Position de la souris ([0] = x et [1] = y)
  108.                 mp = pygame.mouse.get_pos()
  109.                 if(200 < mp[0] < 500 and 250 < mp[1] < 300):
  110.                     LevelMenu()
  111.                 if(200 < mp[0] < 500 and 350 < mp[1] < 400):
  112.                     if(len(Map) == 0):
  113.                         BlankWorld()
  114.                     LevelEditor()
  115.                 if(200 < mp[0] < 500 and 450 < mp[1] < 500):
  116.                     InstructionsMenu()
  117.                 if(200 < mp[0] < 500 and 550 < mp[1] < 600):
  118.                     isInMainMenu = False
  119.        
  120.         # -- EFFACER L'ECRAN --
  121.         # Ne pas mettre de commande pour dessiner au dessus!
  122.         screen.fill(BLACK)
  123.        
  124.         Background = pygame.image.load(os.path.join("Background.jpg")).convert_alpha()
  125.         Background = pygame.transform.scale(Background, (700, 700))
  126.         screen.blit(Background,(0,0))
  127.        
  128.         Title = pygame.image.load(os.path.join("Title.gif")).convert_alpha()
  129.         Title = pygame.transform.scale(Title, (700, 135))
  130.         screen.blit(Title,(0,75))
  131.        
  132.         # -- DESSINER SUR L'ECRAN --
  133.         # Buttons
  134.         Button(0, 200, 250, 300, 50)
  135.         Button(1, 200, 350, 300, 50)
  136.         Button(4, 200, 450, 300, 50)
  137.         Button(2, 200, 550, 300, 50)
  138.        
  139.         # Credits
  140.         label = myfont.render("Created by Bryan ARTHAUD, Martin CORSON et Isak VISTE", 1, (200,200,67))
  141.         if (700 - temps > -label.get_rect().width):
  142.             screen.blit(label, (700 - temps, 620))
  143.             temps += 10
  144.         else:
  145.             temps = 0
  146.            
  147.        
  148.         # -- METTRE A JOUR LA FENETRE --
  149.         pygame.display.flip()
  150.        
  151.         # ---- LIMITER A 60 IMAGES PAR SECONDE ----
  152.         clock.tick(60)
  153.  
  154. def Button(id, x, y, length, height):
  155.     # § Variables §
  156.     mouse = pygame.mouse.get_pos()
  157.    
  158.     if(x+length > mouse[0] > x and y+height > mouse[1] > y):
  159.         screen.blit(butTextures[id*2+1],(x,y))
  160.     else:
  161.         screen.blit(butTextures[id*2],(x,y))
  162.        
  163. # -------- MENU NIVEAUX --------
  164. def LevelMenu():
  165.     # ------ § Variables § ------
  166.     global playerTexture
  167.     global morts
  168.     global start
  169.    
  170.     level01 = os.path.join("Mazes/Level01.maze")
  171.    
  172.     isInLevelMenu = True
  173.  
  174.     while isInLevelMenu:
  175.         # Nom de la fenetre
  176.         if(pygame.display.get_caption() != "The Maze | Level Menu"):
  177.             pygame.display.set_caption("The Maze | Level Menu")
  178.        
  179.         # ---- BOUCLE D'EVENEMENT ----
  180.         for event in pygame.event.get():
  181.             if (event.type == pygame.QUIT):
  182.                 isInLevelMenu = False
  183.             if (event.type == pygame.MOUSEBUTTONUP):
  184.                 # Position de la souris ([0] = x et [1] = y)
  185.                 mp = pygame.mouse.get_pos()
  186.                 if(200 < mp[0] < 500 and 300 < mp[1] < 350):
  187.                     if(len(Map) == 0):
  188.                         BlankWorld()
  189.                     LoadLevel(level01)
  190.                     playerTexture = pygame.transform.rotate(playerTexture, 0)
  191.                     morts = 0
  192.                     start = pygame.time.get_ticks()
  193.                     PlayGame()
  194.                     isInLevelMenu = False
  195.                 if(200 < mp[0] < 500 and 400 < mp[1] < 450):
  196.                     if(len(Map) == 0):
  197.                         BlankWorld()
  198.                     LoadWorld()
  199.                     playerTexture = pygame.transform.rotate(playerTexture, 0)
  200.                     morts = 0
  201.                     start = pygame.time.get_ticks()
  202.                     PlayGame()
  203.                     isInLevelMenu = False
  204.                 if(200 < mp[0] < 500 and 500 < mp[1] < 550):
  205.                     isInLevelMenu = False
  206.                
  207.        
  208.         # -- EFFACER L'ECRAN --
  209.         # Ne pas mettre de commande pour dessiner au dessus!
  210.         screen.fill(BLACK)
  211.        
  212.         Background = pygame.image.load(os.path.join("Background.jpg")).convert_alpha()
  213.         Background = pygame.transform.scale(Background, (700, 700))
  214.         screen.blit(Background,(0,0))
  215.        
  216.         Title = pygame.image.load(os.path.join("Title.gif")).convert_alpha()
  217.         Title = pygame.transform.scale(Title, (700, 135))
  218.         screen.blit(Title,(0,125))
  219.        
  220.         # -- DESSINER SUR L'ECRAN --
  221.         # Buttons
  222.         mp = pygame.mouse.get_pos()
  223.         if(200+300 > mp[0] > 200 and 300+50 > mp[1] > 300):
  224.             screen.blit(level01Buttoni, (200,300))
  225.         else:
  226.             screen.blit(level01Button, (200,300))
  227.        
  228.         if(200+300 > mp[0] > 200 and 400+50 > mp[1] > 400):
  229.             screen.blit(loadButtoni, (200,400))
  230.         else:
  231.             screen.blit(loadButton, (200,400))
  232.        
  233.         Button(2, 200, 500, 300, 50)
  234.        
  235.         # -- METTRE A JOUR LA FENETRE --
  236.         pygame.display.flip()
  237.        
  238.         # ---- LIMITER A 60 IMAGES PAR SECONDE ----
  239.         clock.tick(60)
  240.  
  241. #Menu d'instructions        
  242. def InstructionsMenu():
  243.     isInInstructionsMenu = True
  244.     page = 1
  245.     while isInInstructionsMenu:
  246.         # Nom de la fenetre
  247.         if(pygame.display.get_caption() != "The Maze | Instructions"):
  248.             pygame.display.set_caption("The Maze | Instructions")
  249.            
  250.         # ---- BOUCLE D'EVENEMENT ----
  251.         for event in pygame.event.get():
  252.             if (event.type == pygame.QUIT):
  253.                 isInInstructionsMenu = False
  254.             if (event.type == pygame.MOUSEBUTTONUP):
  255.                 # Position de la souris ([0] = x et [1] = y)
  256.                 mp = pygame.mouse.get_pos()
  257.                 if(50 < mp[0] < 350 and 625 < mp[1] < 675):
  258.                     isInInstructionsMenu = False
  259.                 #Pour changer de page d'instructions si on clique sur le bouton placé aux coordonnées suivantes.
  260.                 if(350 < mp[0] < 650 and 625 < mp[1] < 675):
  261.                     if page == 1:
  262.                         page = 2
  263.                     else:
  264.                         page = 1
  265.                
  266.        
  267.         # -- EFFACER L'ECRAN --
  268.         # Ne pas mettre de commande pour dessiner au dessus!
  269.         screen.fill(BLACK)
  270.        
  271.         Background = pygame.image.load(os.path.join("Background.jpg")).convert_alpha()
  272.         Background = pygame.transform.scale(Background, (700, 700))
  273.         screen.blit(Background,(0,0))
  274.        
  275.         Title = pygame.image.load(os.path.join("Title.gif")).convert_alpha()
  276.         Title = pygame.transform.scale(Title, (700, 135))
  277.         screen.blit(Title,(0,35))
  278.        
  279.         #Pour afficher une page d'instructions ou l'autre
  280.         if page == 2:
  281.             ParchemInstructions = pygame.image.load(os.path.join("ParchemInstructions2.gif")).convert_alpha()
  282.             ParchemInstructions = pygame.transform.scale(ParchemInstructions, (700, 413))
  283.             screen.blit(ParchemInstructions,(0,200))
  284.         else:
  285.             ParchemInstructions = pygame.image.load(os.path.join("ParchemInstructions1.gif")).convert_alpha()
  286.             ParchemInstructions = pygame.transform.scale(ParchemInstructions, (700, 413))
  287.             screen.blit(ParchemInstructions,(0,200))
  288.        
  289.         # -- DESSINER SUR L'ECRAN --
  290.         # Buttons
  291.         Button(2, 50, 625, 300, 50)
  292.         Button(5, 350, 625, 300, 50)
  293.        
  294.         # -- METTRE A JOUR LA FENETRE --
  295.         pygame.display.flip()
  296.        
  297.         # ---- LIMITER A 60 IMAGES PAR SECONDE ----
  298.         clock.tick(60)
  299.  
  300. # -------- GENERATION DE LA CARTE --------
  301. def BlankWorld():
  302.     # ------ § Variables § ------
  303.     global Map
  304.     Map = []
  305.    
  306.     for i in range (mapSize):
  307.         Map.append([])
  308.         for j in range (mapSize):
  309.             Map[i].append(0)
  310.  
  311. # -------- IMPORTER LES TEXTURES --------
  312. def ImportTextures(imgS):
  313.     global imageSize
  314.     imageSize = (imgS, imgS)
  315.    
  316.     for val in rawTextures:
  317.         ImportImage(finTextures, val, "jpg", imageSize, rawTextures.index(val))
  318.    
  319.     for i in range (xTraps):
  320.         ImportImage(trapTextures, "Trap" + str(i), "jpg", imageSize, i)
  321.     for i in range (xDecors):
  322.         ImportImage(decorTextures, "Decor" + str(i), "jpg", imageSize, i)
  323.        
  324.  
  325. # -------- IMPORTER DES IMAGES --------
  326. def ImportImage(liste, image, ext, size, pos):
  327.     texture = pygame.image.load(os.path.join(image + '.' + ext)).convert()
  328.     try:
  329.         liste[pos] = pygame.transform.scale(texture, size)
  330.     except:
  331.         liste.append(pygame.transform.scale(texture, size))
  332.  
  333. # -------- JEU PRINCIPAL --------
  334. def PlayGame():
  335.     pygame.display.set_caption("The Maze | Play")
  336.  
  337.     # ------ § Variables § ------
  338.     global x, y
  339.     global prevRotation
  340.     global playerTexture
  341.     global morts
  342.     global imageSize
  343.     global start
  344.     global duree
  345.     global end
  346.  
  347.     x, y = startPos
  348.    
  349.     # Importer des images
  350.     ImportTextures(100)
  351.    
  352.     # Diversifier les Pieges
  353.     for i in range (mapSize):
  354.         for j in range (mapSize):
  355.             if (Map[i][j] == 5):
  356.                 Map[i][j] = 10 + randint(0, xTraps-1)
  357.             elif (Map[i][j] == 6):
  358.                 Map[i][j] = 50 + randint(0, xDecors-1)
  359.                
  360.     timerActive = True
  361.     isDead = False
  362.     gameOver = False
  363.     isGameOver = True
  364.     isRunning = True
  365.     playerMoved = True
  366.      
  367.     # ------ BOUCLE PRINCIPAL ------
  368.     while isRunning:
  369.         # ---- BOUCLE D'EVENEMENT ----
  370.         for event in pygame.event.get():
  371.             if (event.type == pygame.QUIT):
  372.                 isRunning = False
  373.             if (event.type == pygame.KEYDOWN):
  374.                 if (event.key == pygame.K_UP and playerMoved == False):
  375.                     y = MovePlayer(90, 0, -1, y, -1)
  376.                     playerMoved = True
  377.                 if (event.key == pygame.K_DOWN and playerMoved == False):
  378.                     y = MovePlayer(270, 0, 1, y, 1)
  379.                     playerMoved = True
  380.                 if (event.key == pygame.K_RIGHT and playerMoved == False):
  381.                     x = MovePlayer(0, 1, 0, x, 1)
  382.                     playerMoved = True
  383.                 if (event.key == pygame.K_LEFT and playerMoved == False):
  384.                     x = MovePlayer(180, -1, 0, x, -1)
  385.                     playerMoved = True
  386.      
  387.         # ---- LOGIQUE DU JEU ----
  388.         # Si le joueur a bougé
  389.         if (playerMoved == True):
  390.             # Si le joueur est sur la case de fin
  391.             if (Map[x][y] == 4):
  392.                 gameOver = True
  393.                 isRunning = False
  394.            
  395.             if (Map[x][y] >= 10 and Map[x][y] < 50):
  396.                 isDead = True
  397.                 gameOver = True
  398.                 isRunning = False
  399.                 morts += 1
  400.                
  401.             # -- EFFACER L'ECRAN --
  402.             # Ne pas mettre de commande pour dessiner au dessus!
  403.             screen.fill(BLACK)
  404.  
  405.             # -- DESSINER SUR L'ECRAN --
  406.             DrawScreen(imageSize, 3, 4)
  407.             screen.blit(playerTexture, (300, 300))
  408.            
  409.             # -- METTRE A JOUR LA FENETRE --
  410.             pygame.display.flip()
  411.             playerMoved = False
  412.        
  413.         # ---- LIMITER A 60 IMAGES PAR SECONDE ----
  414.         clock.tick(60)
  415.    
  416.     if (gameOver == True):
  417.         # ------ BOUCLE PRINCIPAL ------
  418.         while isGameOver:
  419.             # ---- BOUCLE D'EVENEMENT ----
  420.             for event in pygame.event.get():
  421.                 if (event.type == pygame.QUIT):
  422.                     isGameOver = False
  423.                 if (event.type == pygame.KEYDOWN):
  424.                     if (event.key == pygame.K_SPACE):
  425.                         isGameOver = False
  426.                         if (isDead == False):
  427.                             morts = 0
  428.                             start = pygame.time.get_ticks()
  429.                         PlayGame()
  430.                 if (event.type == pygame.MOUSEBUTTONUP):
  431.                     mp = pygame.mouse.get_pos()
  432.                     if(200 < mp[0] < 500 and 500 < mp[1] < 550):
  433.                         isGameOver = False
  434.                         if (isDead == False):
  435.                             morts = 0
  436.                             start = pygame.time.get_ticks()
  437.                         PlayGame()
  438.                     if(200 < mp[0] < 500 and 600 < mp[1] < 650):
  439.                         isGameOver = False
  440.                        
  441.             # -- EFFACER L'ECRAN --
  442.             # Ne pas mettre de commande pour dessiner au dessus!
  443.             screen.fill(BLACK)
  444.    
  445.             # -- DESSINER SUR L'ECRAN --
  446.             DrawScreen(imageSize, 3, 4)
  447.            
  448.             if (isDead == True):
  449.                 screen.blit(playerTextureDead, (300, 300))
  450.                 screen.blit(winBannerTexture, ((700 / 2 - 310, 40)))
  451.                 label = myfont.render("YOU LOSE", 1, (255, 255, 255))
  452.                 screen.blit(label, (700 / 2 - label.get_rect().width / 2, 50))
  453.                 end = pygame.time.get_ticks()
  454.                
  455.             else:
  456.                 Finishline = pygame.image.load(os.path.join("Finishline.gif")).convert_alpha()
  457.                 screen.blit(Finishline,(0,0))
  458.                 screen.blit(winBannerTexture, ((700 / 2 - 310, 40)))
  459.                 label = myfont.render("YOU WIN", 1, (255,255,255))
  460.                 screen.blit(label, (700/2-label.get_rect().width/2, 50))
  461.                 #Pour arrêter le chronomètre si le joueur a fini le niveau
  462.                 while timerActive:
  463.                     end = pygame.time.get_ticks()
  464.                     timerActive = False
  465.                
  466.             duree = int((end - start)/1000)
  467.             label = myfont.render("Nombre de Morts : " + str(morts), 1, (255,0,0))
  468.             screen.blit(label, (700/2-label.get_rect().width/2, 90))
  469.             label = myfont.render("Temps : " + str(duree) + " secondes", 1, (255,0,0))
  470.             screen.blit(label, (700/2-label.get_rect().width/2, 130))
  471.  
  472.             screen.blit(tryAgainBannerTexture, ((700/2-170, 484)))
  473.             Button(3, 200, 500, 300, 50)
  474.             Button(2, 200, 600, 300, 50)
  475.            
  476.             # -- METTRE A JOUR LA FENETRE --
  477.             pygame.display.flip()
  478.            
  479.             # ---- LIMITER A 60 IMAGES PAR SECONDE ----
  480.             clock.tick(60)
  481.    
  482. # -------- FAIRE BOUGER LE JOUEUR --------
  483. def MovePlayer(rot, tempX, tempY, move, moveDir):
  484.     # ------ § Variables § ------
  485.     global x, y
  486.     global prevRotation
  487.     global playerTexture
  488.    
  489.     playerTexture = pygame.transform.rotate(playerTexture, rot-prevRotation)
  490.     prevRotation = rot
  491.     if (0 <= x+tempX < mapSize and 0 <= y+tempY < mapSize):
  492.         if(Map[x+tempX][y+tempY] != 1):
  493.             return(move + moveDir)
  494.         else:
  495.             return(move)
  496.     else:
  497.         return(move)
  498.  
  499. # -------- EDITEUR DE CARTE --------
  500. def LevelEditor():
  501.     pygame.display.set_caption("The Maze | Level Editor")
  502.  
  503.     # ------ § Variables § ------
  504.     global x, y
  505.     global startPos
  506.     global imageSize
  507.  
  508.     # Importer des images
  509.     ImportTextures(50)
  510.    
  511.     # Uniformiser les Pieges
  512.     for i in range (mapSize):
  513.         for j in range (mapSize):
  514.             if (Map[i][j] >= 10 and Map[i][j] < 50):
  515.                 Map[i][j] = 5
  516.             elif (Map[i][j] >= 50 and Map[i][j] < 90):
  517.                 Map[i][j] = 6
  518.    
  519.     isRunning = True
  520.     actionExectued = True
  521.      
  522.     # ------ BOUCLE PRINCIPAL ------
  523.     while isRunning:
  524.         # ---- BOUCLE D'EVENEMENT ----
  525.         for event in pygame.event.get():
  526.             # Coordonee du click
  527.             mousePosition = pygame.mouse.get_pos()
  528.             pressed = pygame.mouse.get_pressed()
  529.             mpx, mpy = int(mousePosition[0]/50), int(mousePosition[1]/50)
  530.            
  531.             if (event.type == pygame.QUIT):
  532.                 isRunning = False
  533.             if (event.type == pygame.KEYDOWN):
  534.                 if (event.key == pygame.K_UP):
  535.                     y -= 1
  536.                     actionExectued = True
  537.                 if (event.key == pygame.K_DOWN):
  538.                     y += 1
  539.                     actionExectued = True                    
  540.                 if (event.key == pygame.K_RIGHT):
  541.                     x += 1                                                
  542.                     actionExectued = True
  543.                 if (event.key == pygame.K_LEFT):
  544.                     x -= 1
  545.                     actionExectued = True
  546.                 if (event.key == pygame.K_s):
  547.                     SaveWorld()
  548.                 if (event.key == pygame.K_l):
  549.                     LoadWorld()
  550.                     actionExecuted = True
  551.                 if (event.key == pygame.K_n):
  552.                     BlankWorld()
  553.                     actionExecuted = True
  554.                 if (event.key == pygame.K_m):
  555.                     ShowMap()
  556.                     ImportTextures(50)
  557.                 if (event.key == pygame.K_p):
  558.                     PlayGame()
  559.                     ImportTextures(50)
  560.                     # Uniformiser les Pieges
  561.                     for i in range (mapSize):
  562.                         for j in range (mapSize):
  563.                             if (Map[i][j] >= 10 and Map[i][j] < 50):
  564.                                 Map[i][j] = 5
  565.                             elif (Map[i][j] >= 50 and Map[i][j] < 90):
  566.                                 Map[i][j] = 6
  567.                     actionExecuted = True
  568.                 if (event.key == pygame.K_g):
  569.                     if(startPos != (0, 0)):
  570.                         Map[startPos[0]][startPos[1]] = 0
  571.                     Map[x-7+mpx][y-7+mpy] = 3
  572.                     startPos = (x-7+mpx, y-7+mpy)
  573.                 if (event.key == pygame.K_h):
  574.                     Map[x-7+mpx][y-7+mpy] = 4
  575.             if (event.type == pygame.MOUSEBUTTONUP):
  576.                 if(0 <= x-7+mpx < mapSize and 0 <= y-7+mpy < mapSize):
  577.                     if(event.button == 1):
  578.                         if(Map[x-7+mpx][y-7+mpy] == 1):
  579.                             Map[x-7+mpx][y-7+mpy] = 0
  580.                         else:
  581.                             Map[x-7+mpx][y-7+mpy] = 1
  582.                     if(event.button == 3):
  583.                         if(Map[x-7+mpx][y-7+mpy] == 5):
  584.                             Map[x-7+mpx][y-7+mpy] = 6
  585.                         elif(Map[x-7+mpx][y-7+mpy] == 6):
  586.                             Map[x-7+mpx][y-7+mpy] = 0
  587.                         else:
  588.                             Map[x-7+mpx][y-7+mpy] = 5
  589.                     actionExecuted = True
  590.                    
  591.      
  592.         # ---- LOGIQUE DU JEU ----
  593.         # Si une action a ete executer
  594.         if (actionExectued == True):
  595.             # -- EFFACER L'ECRAN --
  596.             # Ne pas mettre de commande pour dessiner au dessus!
  597.             screen.fill(BLACK)
  598.  
  599.             DrawScreen(imageSize, 7, 7)
  600.            
  601.             # -- METTRE A JOUR LA FENETRE --
  602.             pygame.display.flip()
  603.             actionExecuted = False
  604.        
  605.         # ---- LIMITER A 60 IMAGES PAR SECONDE ----
  606.         clock.tick(60)
  607.  
  608. def ShowMap():
  609.     pygame.display.set_caption("The Maze | Map")
  610.    
  611.     # ------ § Variables § ------
  612.     global x, y
  613.     global imageSize
  614.    
  615.     x = mapSize//2
  616.     y = mapSize//2
  617.    
  618.     # Importer des images
  619.     ImportTextures(700//mapSize)
  620.    
  621.     isShowingMap = True
  622.    
  623.     # ------ BOUCLE PRINCIPAL ------
  624.     while isShowingMap:
  625.         # ---- BOUCLE D'EVENEMENT ----
  626.         for event in pygame.event.get():
  627.             if (event.type == pygame.QUIT):
  628.                 isShowingMap = False
  629.             if (event.type == pygame.KEYDOWN):
  630.                 if (event.key == pygame.K_m):
  631.                     isShowingMap = False
  632.                
  633.         # ---- LOGIQUE DU JEU ----
  634.         # -- EFFACER L'ECRAN --
  635.         # Ne pas mettre de commande pour dessiner au dessus!
  636.         screen.fill(BLACK)
  637.  
  638.         DrawScreen(imageSize, mapSize//2, mapSize//2)
  639.        
  640.         # -- METTRE A JOUR LA FENETRE --
  641.         pygame.display.flip()
  642.        
  643.         # ---- LIMITER A 60 IMAGES PAR SECONDE ----
  644.         clock.tick(60)
  645.        
  646.  
  647. # -------- DESSINER LE TERRAIN --------
  648. def DrawScreen(imgS, tempM, tempP):
  649.     # ------ § Variables § ------
  650.     k = 0
  651.     l = 0
  652.    
  653.     for i in range (y-tempM, y+tempP):
  654.         for j in range (x-tempM, x+tempP):
  655.             if (0 <= i < mapSize and 0 <= j < mapSize):
  656.                 texture = finTextures[0] #GROUND
  657.                 if(Map[j][i] == 1):
  658.                     texture = finTextures[1] #WALL
  659.                 elif(Map[j][i] == 3):
  660.                     texture = finTextures[3] #START
  661.                 elif(Map[j][i] == 4):
  662.                     texture = finTextures[4] #GOAL
  663.                 elif(Map[j][i] == 5):
  664.                     texture = finTextures[5] #TRAP
  665.                 elif(Map[j][i] == 6):
  666.                     texture = finTextures[6] #DECOR
  667.                 # TRAPS
  668.                 elif(Map[j][i] == 10):
  669.                     texture = trapTextures[0]
  670.                 elif(Map[j][i] == 11):
  671.                     texture = trapTextures[1]
  672.                 elif(Map[j][i] == 12):
  673.                     texture = trapTextures[2]
  674.                 #DECORS
  675.                 elif(Map[j][i] == 50):
  676.                     texture = decorTextures[0]
  677.                 elif(Map[j][i] == 51):
  678.                     texture = decorTextures[1]
  679.                 elif(Map[j][i] == 52):
  680.                     texture = decorTextures[2]
  681.             else:
  682.                 texture = finTextures[2] #WASTELAND
  683.             screen.blit(texture, (l*imgS[0], k*imgS[1]))
  684.             l += 1
  685.         l = 0
  686.         k += 1
  687.  
  688. # -------- SAUVEGARDE DE LA CARTE --------
  689. def SaveWorld():
  690.     print("SAVE START")
  691.     try:
  692.         file_path = filedialog.asksaveasfilename(defaultextension=".maze", filetypes=(("Text Files", "*.maze"),("All Files", "*.*")))
  693.         levelText = open(file_path, "w+") #Write and Overwrite
  694.    
  695.         for i in range (mapSize):
  696.             for j in range (mapSize):
  697.                 levelText.write(str(Map[i][j]))
  698.             levelText.write("\n")
  699.         levelText.close()
  700.         print("(Y) SAVE COMPLETE", end="\n\n")
  701.     except Exception as e:
  702.         print("(N)SAVE FAILURE:\n -->", e, end="\n\n")
  703.         pass
  704.  
  705. # -------- CHARGEMENT DE LA CARTE --------
  706. def LoadWorld():
  707.     # ------ § Variables § ------
  708.     global startPos
  709.    
  710.     print("LOAD START")
  711.     try:
  712.         file_path = filedialog.askopenfilename(defaultextension=".maze", filetypes=(("Text Files", "*.maze"),("All Files", "*.*")))
  713.         levelText = open(file_path, "r") #Read Only
  714.  
  715.         for i in range(mapSize):
  716.             read = levelText.read(mapSize)
  717.             for j in range(mapSize):
  718.                 Map[i][j] = int(read[j])
  719.                 if(int(read[j]) == 3):
  720.                     startPos = (i, j)
  721.             levelText.read(1)
  722.         print("(Y) LOAD COMPLETE", end="\n\n")
  723.     except Exception as e:
  724.         print("(N) LOAD FAILURE:\n -->", e, end="\n\n")
  725.        
  726. # -------- CHARGEMENT D'UN LEVEL --------
  727. def LoadLevel(level):
  728.     # ------ § Variables § ------
  729.     global startPos
  730.    
  731.     print("LEVEL LOAD START")
  732.     try:
  733.         file_path = level
  734.         levelText = open(file_path, "r") #Read Only
  735.  
  736.         for i in range(mapSize):
  737.             read = levelText.read(mapSize)
  738.             for j in range(mapSize):
  739.                 Map[i][j] = int(read[j])
  740.                 if(int(read[j]) == 3):
  741.                     startPos = (i, j)
  742.             levelText.read(1)
  743.         print("(Y) LOAD COMPLETE", end="\n\n")
  744.     except Exception as e:
  745.         print("(N) LOAD FAILURE:\n -->", e, end="\n\n")
  746.        
  747. MainMenu()
  748. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement