SHARE
TWEET

Maze Game Part 3

a guest Nov 14th, 2019 122 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import turtle
  2.  
  3. wn = turtle.Screen()
  4. wn.bgcolor("black")
  5. wn.title("A Maze Game")
  6. wn.setup(700, 700)
  7.  
  8. # Create Pen
  9. class Pen(turtle.Turtle):
  10.     def __init__(self):
  11.         turtle.Turtle.__init__(self)
  12.         self.shape("square")
  13.         self.color("white")
  14.         self.penup()
  15.         self.speed(0)
  16.  
  17.  
  18. class Player(turtle.Turtle):
  19.     def __init__(self):
  20.         turtle.Turtle.__init__(self)
  21.         self.shape("square")
  22.         self.color("blue")
  23.         self.penup()
  24.         self.speed(0)
  25.  
  26.     def go_up(self):
  27.         self.goto(self.xcor(), self.ycor() + 24)
  28.  
  29.     def go_down(self):
  30.         self.goto(self.xcor(), self.ycor() - 24)
  31.  
  32.     def go_right(self):
  33.         self.goto(self.xcor() + 24, self.ycor())
  34.  
  35.     def go_left(self):
  36.         self.goto(self.xcor() - 24, self.ycor())
  37.  
  38. # Create level lists
  39. levels = [""]
  40. # Define first level
  41. level_1 = [
  42.     "XXXXXXXXXXXXXXXXXXXXXXXXX",
  43.     "XP XXXXXXX     XXXXXXX  X",
  44.     "X  XXXXXXX     XXXXXXX  X",
  45.     "X              XXXXXXX  X",
  46.     "X              XXXXXXX  X",
  47.     "XXXXXXXXXXXX  XXX       X",
  48.     "XXXXXXXXXXXX  XXX       X",
  49.     "XXXXXXXXXXXX         XXXX",
  50.     "XXXXXXXXXXXX         XXXX",
  51.     "XX            XXXXX  XXXX",
  52.     "XX            XXXXX  XXXX",
  53.     "XX  XXXXXXXXXXXXXXX  XXXX",
  54.     "XX  XXXXXXXXXXXXXXX  XXXX",
  55.     "XX  XXXXXXXXXXXX        X",
  56.     "XX  XXXXXXXXXXXX        X",
  57.     "XX  XXXXXXXXXXXX  XXXXXXX",
  58.     "X    XXXXXXXXXXX  XXXXXXX",
  59.     "X                 XXXXXXX",
  60.     "X                 XXXXXXX",
  61.     "XXXXXXXXXXXXXXXX  XXXXXXX",
  62.     "XXXXXXXXXXXXX        XXXX",
  63.     "XXXXXXXXXXXXX        XXXX",
  64.     "X                    XXXX",
  65.     "X                    XXXX",
  66.     "XXXXXXXXX    XXXXXXXXXXXX",
  67.     "XXXXXXXXXXXXXXXXXXXXXXXXX"
  68.  
  69. ]
  70. # Add maze to mazes list
  71. levels.append(level_1)
  72.  
  73. # Create Level Setup Function
  74. def setup_maze(level):
  75.     for y in range(len(level)):
  76.         for x in range(len(level[y])):
  77.             # Get the character at each x,y coordinate
  78.             # NOTE the order of y and x in the next line
  79.             character = level[y][x]
  80.             # Calculate the screen x, y coordinates
  81.             screen_x = -288 + (x * 24)
  82.             screen_y = 288 - (y * 24)
  83.  
  84.             # Check if it is an X (representing a wall)
  85.             if character == "X":
  86.                 pen.goto(screen_x, screen_y)
  87.                 pen.stamp()
  88.             # Check if it is a P (representing a player)
  89.             if character == "P":
  90.                 player.goto(screen_x, screen_y)
  91.  
  92. # Create class instances
  93. pen = Pen()
  94. player = Player()
  95. # Set up the level
  96. setup_maze(levels[1])
  97. # Keyboard bindings
  98. wn.onkey(player.go_up, "w")
  99. wn.onkey(player.go_down, "s")
  100. wn.onkey(player.go_right, "d")
  101. wn.onkey(player.go_left, "a")
  102. wn.listen()
  103. # Turn off screen updates
  104. wn.tracer(0)
  105. # Game Main Loop
  106. while True:
  107.     pass
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
 
Top