Eng-Mohamed-Essam

Untitled

Aug 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.53 KB | None | 0 0
  1. from game import Maze, Character, Path, blockSize
  2. import time
  3. Collected_Coins = []
  4. endPointCount = 0
  5. ########### WRITE A CODE TO PARSE THE TEST FILE INTO AN ARRAY CALLED "grid" BELOW ###############
  6. grid = [] #Creating an Empty List
  7. Var = open('C:/Users/carniavl/Desktop/MIA/MIA_TASK1/Template/level2.txt', 'r') # Open the file in order to read it
  8. for line in Var.readlines(): # Looping on the lines
  9.     grid.append ([c for c in line]) #turn the line string into list of chars and adding them to the grid list
  10. ###############################################################################################
  11.  
  12.  
  13.  
  14. """
  15. DONOT DELETE OR EDIT THE FOLLOWING LINES
  16. """
  17.  
  18. maze = Maze()
  19. maze.setUpMaze(grid)
  20. startingPos = maze.getStartingPosition()
  21.  
  22. character = Character(startingPos)
  23.  
  24. wall = maze.getWall()
  25.  
  26. finish = maze.getFinish()
  27.  
  28. coins = maze.getCoins()
  29.  
  30. path = Path()
  31.  
  32.  
  33. while(True):
  34.     """
  35.    * You have the coordinates of the walls stored in "wall" list in the form of tuples [(x,y), (i,j)] -- list of tuples
  36.  
  37.    * You have the coordinates of the end point stored in "finish" tuple in the form of (x,y)
  38.  
  39.    * You have the coordinates of the coins stored in "coins" list in the form of tuples [(x,y), (i,j)] -- list of tuples
  40.  
  41.    * At each itteration you get the following:
  42.        - X coordinate of the character stored in "currentX" variable
  43.        - Y coordinate of the character stored in "currentY" variable
  44.        - Current angle of the character stored in "angle" variable
  45.  
  46.    * To move the character forward write character.moveForward()
  47.  
  48.    * To rotate the character to the right write character.rotateRight()
  49.  
  50.    * To rotate the character to the left write character.rotateLeft()
  51.  
  52.    * To update the coordinates after moving write the following lines:
  53.        -   currentX = character.getCurrentX()
  54.        -   currentY = character.getCurrentY()
  55.  
  56.    * To update the angle after rotating write the following line:
  57.        -   angle = character.getAngle()
  58.  
  59.    * To draw the path you can use either of the following functions:
  60.        - path.drawBlock(x,y) to draw a box on x,y coordinates
  61.        - path.drawArray(array) to draw a series of boxes by passing a list that contains a
  62.        tuple of coordinates i.e.
  63.                array = [(10,20), (5, 30)]
  64.                path.drawArray(array) #this will draw two blue boxes one at(10,20) and the other at (5,30)
  65.  
  66.  
  67.    * The angles are illustrated below
  68.  
  69.  
  70.                           angle  = 90
  71.                                |
  72.                                |
  73.                                |
  74.                   _____________|_____________
  75.         angle = 180            |           angle = 0
  76.                                |
  77.                                |
  78.                                |
  79.                           angle = 270
  80.  
  81.    * To end the game break from the while loop
  82.    """
  83.     currentX = character.getCurrentX()
  84.     currentY = character.getCurrentY()
  85.     angle = character.getAngle()
  86.     print("X: " + str(currentX) + " Y: " + str(currentY) + " Angle: " + str(angle))
  87.  
  88.     ######################### GAME LOOP ###############################
  89.     ################## WRITE YOUR CODE BELOW ##########################
  90.     if ((currentX,currentY) == finish and (len(coins) == 0 or endPointCount > 0)):
  91.         print("Coins Collected :", len(Collected_Coins))
  92.         print("Coins Coordinates:", Collected_Coins)
  93.         break
  94.     if ((currentX,currentY) == finish ):
  95.         endPointCount+=1
  96. #Move Right
  97.     if (character.getAngle() == 180):
  98.         currentX = character.getCurrentX()
  99.         currentY = character.getCurrentY()
  100.         path.drawBlock(currentX,currentY)
  101.         if (currentX,currentY) in coins:
  102.             if (currentX,currentY) not in Collected_Coins:
  103.                 Collected_Coins.append((currentX,currentY))
  104.            
  105.         if (currentX, currentY -24) in wall:  # check to see if there are walls on the left
  106.             if (currentX - 24, currentY) not in wall: # check to see if path ahead is clear
  107.                 character.moveForward()
  108.             else:
  109.                 character.rotateRight()
  110.         else:
  111.             character.rotateLeft()
  112.             character.moveForward()
  113.  
  114. #Move Down
  115.     if (character.getAngle() == 270 ):
  116.         currentX = character.getCurrentX()
  117.         currentY = character.getCurrentY()
  118.         path.drawBlock(currentX,currentY)
  119.         if (currentX,currentY) in coins:
  120.             if (currentX,currentY) not in Collected_Coins:
  121.                 Collected_Coins.append((currentX,currentY))
  122.  
  123.         if (currentX + 24, currentY) in wall : #check if there're walls on the left
  124.             if (currentX, currentY -24) not in wall: # check to see if path ahead is clear
  125.                 character.moveForward()
  126.             else:
  127.                 character.rotateRight()
  128.         else:
  129.             character.rotateLeft()
  130.             character.moveForward()
  131.  
  132.  
  133.  
  134. #Move Left
  135.     if (character.getAngle() == 0):
  136.         currentX = character.getCurrentX()
  137.         currentY = character.getCurrentY()
  138.         path.drawBlock(currentX,currentY)
  139.         if (currentX,currentY) in coins:
  140.             if (currentX,currentY) not in Collected_Coins:
  141.                 Collected_Coins.append((currentX,currentY))
  142.  
  143.         if (currentX,currentY + 24) in wall:  #check to see if there are walls on the left
  144.             if (currentX + 24, currentY) not in wall: #check to see if the path ahead is clear
  145.                 character.moveForward()
  146.             else:
  147.                 character.rotateRight()
  148.         else:
  149.             character.rotateLeft()
  150.             character.moveForward()
  151. #Move Up
  152.     if (character.getAngle() == 90):
  153.         currentX = character.getCurrentX()
  154.         currentY = character.getCurrentY()
  155.         path.drawBlock(currentX,currentY)
  156.         if (currentX,currentY) in coins:
  157.             if (currentX,currentY) not in Collected_Coins:
  158.                 Collected_Coins.append((currentX,currentY))
  159.  
  160.         if (currentX - 24, currentY) in wall: # check to see if there are walls on the left
  161.             if (currentX,currentY + 24) not in wall:# check to see if path ahead is clear
  162.                 character.moveForward()
  163.             else:
  164.                 character.rotateRight()
  165.         else:
  166.             character.rotateLeft()
  167.             character.moveForward()
  168.  
  169.  
  170.  
  171.  
  172.      
  173.  
  174.            
  175.    
  176.     ###################################################################
  177.  
  178.  
  179.  
  180.  
  181.  
  182.        
  183.        
  184.  
  185.  
  186.  
  187.  
  188. """
  189. DONOT DELETE THE FOLLOWING LINE
  190. """
  191. maze.endProgram()
Advertisement
Add Comment
Please, Sign In to add comment