################################################### ## Game.py by Miles Naberhaus and Maxwell Conser ## ## Move an arrow through a maze to reach a box ## ## February 8, 2016. Needs Maze.py to run corect ## ################################################### import turtle import random from Maze import maze_create # Uses other python file to draw maze. moveCounter = 0 screen = turtle.Screen() screen.setup(1000, 1000) screen.setworldcoordinates(-500, -500, 500, 500) roberto = turtle.Turtle() roberto.penup() roberto.goto(-465, 465) win = turtle.Turtle() win.hideturtle() win.up() win.shape('square') win.shapesize(2.45, 2.45, 1) mazeT = turtle.Turtle() listCoord, xGoal, yGoal = maze_create(mazeT) def right(): global moveCounter x = roberto.xcor() # The init(round()) is to fix a floating point error with turtles coordinates. if -475 <= x <= 475 and [int(round((x + 50))),int(round(roberto.ycor()))] not in listCoord: roberto.setheading(0) roberto.forward(50) moveCounter += 1 checkWin(roberto.xcor(), roberto.ycor()) def up(): global moveCounter y = roberto.ycor() if -475 <= y <= 475 and [int(round(roberto.xcor())),int(round((y + 50)))] not in listCoord: roberto.setheading(90) roberto.forward(50) moveCounter += 1 checkWin(roberto.xcor(), roberto.ycor()) def left(): global moveCounter x = roberto.xcor() # The init(round()) is to fix a floating point error with turtles coordinates. if -475 <= roberto.xcor() <= 475 and [int(round((x - 50))),int(round(roberto.ycor()))] not in listCoord: roberto.setheading(180) roberto.forward(50) moveCounter += 1 checkWin(roberto.xcor(), roberto.ycor()) def down(): global moveCounter y = roberto.ycor() # The init(round()) is to fix a floating point error with turtles coordinates. if -475 <= roberto.ycor() <= 475 and [int(round(roberto.xcor())), int(round((y - 50)))] not in listCoord: roberto.setheading(270) roberto.forward(50) moveCounter += 1 checkWin(roberto.xcor(), roberto.ycor()) def winGame(): #Follows procedures after the game has been won win.clearstamps() #Clears the maze, as well as the goal roberto.ht() win.goto(0, 0) win.write("You Win!", align = "center", font = ("Arial", 50, "normal")) win.goto(0, -200) win.write("Only " + str(moveCounter) + " moves!", align = "center", font = ("Arial", 24, "normal")) def checkWin(x, y): # Checks to see if turtle is in black box if (xGoal - 25) < x < (xGoal + 25) and (yGoal - 25) < y < (yGoal + 25): winGame() #The next five lines just assign move functions to keys screen.onkey(right, "Right") screen.onkey(up, "Up") screen.onkey(left, "Left") screen.onkey(down, "Down") screen.listen()