Advertisement
Eng-Mohamed-Essam

Untitled

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