Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- wn = turtle.Screen()
- wn.bgcolor("black")
- wn.title("A Maze Game")
- wn.setup(700, 700)
- # Create Pen
- class Pen(turtle.Turtle):
- def __init__(self):
- turtle.Turtle.__init__(self)
- self.shape("square")
- self.color("white")
- self.penup()
- self.speed(0)
- class Player(turtle.Turtle):
- def __init__(self):
- turtle.Turtle.__init__(self)
- self.shape("square")
- self.color("blue")
- self.penup()
- self.speed(0)
- def go_up(self):
- self.goto(self.xcor(), self.ycor() + 24)
- def go_down(self):
- self.goto(self.xcor(), self.ycor() - 24)
- def go_right(self):
- self.goto(self.xcor() + 24, self.ycor())
- def go_left(self):
- self.goto(self.xcor() - 24, self.ycor())
- # Create level lists
- levels = [""]
- # Define first level
- level_1 = [
- "XXXXXXXXXXXXXXXXXXXXXXXXX",
- "XP XXXXXXX XXXXXXX X",
- "X XXXXXXX XXXXXXX X",
- "X XXXXXXX X",
- "X XXXXXXX X",
- "XXXXXXXXXXXX XXX X",
- "XXXXXXXXXXXX XXX X",
- "XXXXXXXXXXXX XXXX",
- "XXXXXXXXXXXX XXXX",
- "XX XXXXX XXXX",
- "XX XXXXX XXXX",
- "XX XXXXXXXXXXXXXXX XXXX",
- "XX XXXXXXXXXXXXXXX XXXX",
- "XX XXXXXXXXXXXX X",
- "XX XXXXXXXXXXXX X",
- "XX XXXXXXXXXXXX XXXXXXX",
- "X XXXXXXXXXXX XXXXXXX",
- "X XXXXXXX",
- "X XXXXXXX",
- "XXXXXXXXXXXXXXXX XXXXXXX",
- "XXXXXXXXXXXXX XXXX",
- "XXXXXXXXXXXXX XXXX",
- "X XXXX",
- "X XXXX",
- "XXXXXXXXX XXXXXXXXXXXX",
- "XXXXXXXXXXXXXXXXXXXXXXXXX"
- ]
- # Add maze to mazes list
- levels.append(level_1)
- # Create Level Setup Function
- def setup_maze(level):
- for y in range(len(level)):
- for x in range(len(level[y])):
- # Get the character at each x,y coordinate
- # NOTE the order of y and x in the next line
- character = level[y][x]
- # Calculate the screen x, y coordinates
- screen_x = -288 + (x * 24)
- screen_y = 288 - (y * 24)
- # Check if it is an X (representing a wall)
- if character == "X":
- pen.goto(screen_x, screen_y)
- pen.stamp()
- # Check if it is a P (representing a player)
- if character == "P":
- player.goto(screen_x, screen_y)
- # Create class instances
- pen = Pen()
- player = Player()
- # Set up the level
- setup_maze(levels[1])
- # Keyboard bindings
- wn.onkey(player.go_up, "w")
- wn.onkey(player.go_down, "s")
- wn.onkey(player.go_right, "d")
- wn.onkey(player.go_left, "a")
- wn.listen()
- # Turn off screen updates
- wn.tracer(0)
- # Game Main Loop
- while True:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement