SHARE
TWEET

Untitled

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