Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from game import Maze, Character, Path, blockSize
- import time
- Coins = []
- ########### WRITE A CODE TO PARSE THE TEST FILE INTO AN ARRAY CALLED "grid" BELOW ###############
- grid = [] #Creating an Empty List
- Var = open('C:/Users/carniavl/Desktop/MIA/MIA_TASK1/Template/level1.txt', 'r') # Open the file in order to read it
- for line in Var.readlines(): # Looping on the lines
- grid.append ([c for c in line]) #turn the line string into list of chars and adding them to the grid list
- ###############################################################################################
- """
- DONOT DELETE OR EDIT THE FOLLOWING LINES
- """
- maze = Maze()
- maze.setUpMaze(grid)
- startingPos = maze.getStartingPosition()
- character = Character(startingPos)
- wall = maze.getWall()
- finish = maze.getFinish()
- coins = maze.getCoins()
- path = Path()
- while(True):
- """
- * You have the coordinates of the walls stored in "wall" list in the form of tuples [(x,y), (i,j)] -- list of tuples
- * You have the coordinates of the end point stored in "finish" tuple in the form of (x,y)
- * You have the coordinates of the coins stored in "coins" list in the form of tuples [(x,y), (i,j)] -- list of tuples
- * At each itteration you get the following:
- - X coordinate of the character stored in "currentX" variable
- - Y coordinate of the character stored in "currentY" variable
- - Current angle of the character stored in "angle" variable
- * To move the character forward write character.moveForward()
- * To rotate the character to the right write character.rotateRight()
- * To rotate the character to the left write character.rotateLeft()
- * To update the coordinates after moving write the following lines:
- - currentX = character.getCurrentX()
- - currentY = character.getCurrentY()
- * To update the angle after rotating write the following line:
- - angle = character.getAngle()
- * To draw the path you can use either of the following functions:
- - path.drawBlock(x,y) to draw a box on x,y coordinates
- - path.drawArray(array) to draw a series of boxes by passing a list that contains a
- tuple of coordinates i.e.
- array = [(10,20), (5, 30)]
- path.drawArray(array) #this will draw two blue boxes one at(10,20) and the other at (5,30)
- * The angles are illustrated below
- angle = 90
- |
- |
- |
- _____________|_____________
- angle = 180 | angle = 0
- |
- |
- |
- angle = 270
- * To end the game break from the while loop
- """
- currentX = character.getCurrentX()
- currentY = character.getCurrentY()
- angle = character.getAngle()
- print("X: " + str(currentX) + " Y: " + str(currentY) + " Angle: " + str(angle))
- ######################### GAME LOOP ###############################
- ################## WRITE YOUR CODE BELOW ##########################
- #Move Right
- if (character.getAngle() == 180):
- wallX = round(character.xcor(),0)
- wallY = round(character.ycor(),0)
- currentX = character.getCurrentX()
- currentY = character.getCurrentY()
- path.drawBlock(currentX,currentY)
- if ((currentX,currentY) == finish):
- break
- if (wallX, wallY -24) in wall: # check to see if there are walls on the left
- if (wallX - 24, wallY) not in wall: # check to see if path ahead is clear
- character.moveForward()
- else:
- character.rotateRight()
- else:
- character.rotateLeft()
- character.moveForward()
- #Move Down
- if (character.getAngle() == 270 ):
- wallX = round(character.xcor(),0)
- wallY = round(character.ycor(),0)
- currentX = character.getCurrentX()
- currentY = character.getCurrentY()
- path.drawBlock(currentX,currentY)
- if ((currentX,currentY) == finish):
- break
- if (wallX + 24, wallY) in wall : #check if there're walls on the left
- if (wallX, wallY -24) not in wall: # check to see if path ahead is clear
- character.moveForward()
- else:
- character.rotateRight()
- else:
- character.rotateLeft()
- character.moveForward()
- #Move Left
- if (character.getAngle() == 0):
- wallX = round(character.xcor(),0)
- wallY = round(character.ycor(),0)
- currentX = character.getCurrentX()
- currentY = character.getCurrentY()
- path.drawBlock(currentX,currentY)
- if ((currentX,currentY) == finish):
- break
- if (wallX,wallY + 24) in wall: #check to see if there are walls on the left
- if (wallX + 24, wallY) not in wall: #check to see if the path ahead is clear
- character.moveForward()
- else:
- character.rotateRight()
- else:
- character.rotateLeft()
- character.moveForward()
- #Move Up
- if (character.getAngle() == 90):
- currentX = character.getCurrentX()
- currentY = character.getCurrentY()
- wallX = round(character.xcor(),0)
- wallY = round(character.ycor(),0)
- path.drawBlock(currentX,currentY)
- if ((currentX,currentY) == finish):
- break
- if (wallX - 24, wallY) in wall: # check to see if there are walls on the left
- if (wallX,wallY + 24) not in wall:# check to see if path ahead is clear
- character.moveForward()
- else:
- character.rotateRight()
- else:
- character.rotateLeft()
- character.moveForward()
- ###################################################################
- """
- DONOT DELETE THE FOLLOWING LINE
- """
- maze.endProgram()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement