Advertisement
Guest User

Maze problem

a guest
Dec 21st, 2019
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.98 KB | None | 0 0
  1. from __future__ import division
  2. import turtle
  3. import math
  4. from math import *
  5. from turtle import *
  6.  
  7. wn = turtle.Screen()
  8. wn.bgcolor('black')
  9. wn.title('The maze game!')
  10. wn.setup(700,700)
  11.  
  12. #Create pen
  13. class Pen(turtle.Turtle):
  14.     def __init__(self):
  15.         turtle.Turtle.__init__(self)
  16.         self.shape('square')
  17.         self.color('white')
  18.         self.penup()
  19.         self.speed(0)
  20.  
  21.  
  22. class Player(turtle.Turtle):
  23.     def __init__(self):
  24.         turtle.Turtle.__init__(self)
  25.         self.shape('square')
  26.         self.color('blue')
  27.         self.penup()
  28.         self.speed(0)
  29.         self.gold = 0
  30.  
  31.     def go_up(self):
  32.         #Calculate the spot to move to
  33.         move_to_x = player.xcor()
  34.         move_to_y = player.ycor() + 24
  35.  
  36.         #Check if the space has a wall
  37.         if (move_to_x, move_to_y) not in walls:
  38.             self.goto(move_to_x, move_to_y)
  39.  
  40.     def go_down(self):
  41.         #Calculate the spot to move to
  42.         move_to_x = player.xcor()
  43.         move_to_y = player.ycor() - 24
  44.  
  45.         #Check if space has a wall
  46.         if (move_to_x, move_to_y) not in walls:
  47.             self.goto(move_to_x, move_to_y)
  48.  
  49.     def go_left(self):
  50.         #Calculate the spot to move to
  51.         move_to_x = player.xcor() - 24
  52.         move_to_y = player.ycor()
  53.  
  54.     #Check if space has a wall
  55.         if (move_to_x, move_to_y) not in walls:
  56.             self.goto(move_to_x, move_to_y)
  57.  
  58.  
  59.     def go_right(self):
  60.         #Calculate the spot to move to
  61.         move_to_x = player.xcor() + 24
  62.         move_to_y = player.ycor()
  63.  
  64.         #Check if space has a wall
  65.         if (move_to_x, move_to_y) not in walls:
  66.             self.goto(move_to_x, move_to_y)
  67.  
  68.     def is_collision(self, other):
  69.         a = self.xcor()-other.xcor()
  70.         b = self.ycor()-other.ycor()
  71.         distance = math.sqrt((a ** 2) + (b ** 2) )
  72.  
  73.         if distance < 5:
  74.             return True
  75.         else:
  76.             return False
  77.  
  78.  
  79. class Treasure(turtle.Turtle):
  80.     def __init__(self, x, y):
  81.         turtle.Turtle.__init__(self)
  82.         self.shape('circle')
  83.         self.color('gold')
  84.         self.penup()
  85.         self.speed(0)
  86.         self.gold = 100
  87.         self.goto(x, y)
  88.  
  89.     def destroy(self):
  90.         self.goto(2000,2000)
  91.         self.hideturtle()
  92.  
  93. #Create levels list
  94. levels =['']
  95.  
  96. #Define first level '''' COUNTING IS....
  97. # ONLY THERE MOMENTARILY'''
  98. level_1 = [
  99. 'XXXXXXXXXXXXXXXXXXXXXXXXX',
  100. 'XP  XXXXXXX         XXXXX',
  101. 'X  XXXXXXX  XXXXXX  XXXXX',
  102. 'X       XX  XXXXXX  XXXXX',
  103. 'X       XX  XXX        XX',
  104. 'XXXXXX  XX  XXX        XX',
  105. 'XXXXXX  XX  XXXXXX  XXXXX',
  106. 'XXXXXX  XX    XXXX  XXXXX',
  107. 'X  XXX        XXXT  XXXXX',
  108. 'X  XXX  XXXXXXXXXXXXXXXXX',
  109. 'X         XXXXXXXXXXXXXXX',
  110. 'X                XXXXXXXX',
  111. 'XXXXXXXXXXXXXX   XXXX   X',
  112. 'XXX  XXXXXXX     XXXX   X',
  113. 'XXX                     X',
  114. 'XXX                     X',
  115. 'XXXXXXXXXX  XXXXXXXXXXXXX',
  116. 'XXXXXXXXXX  XXXXXXXXXXXXX',
  117. 'XXXXXXXXXX              X',
  118. 'XX   XXXXX              X',
  119. 'XX   XXXXXXXXXXXXX  XXXXX',
  120. 'XX    YXXXXXXXXXXX  XXXXX',
  121. 'XX          XXXX        X',
  122. 'XXXX                    X',
  123. 'XXXXXXXXXXXXXXXXXXXXXXXXX',
  124. ]
  125.  
  126.  
  127. #Add a list of treasures
  128. treasures = []
  129.  
  130. #Add maze to mazes list
  131. levels.append(level_1)
  132.  
  133. #Create level setup function
  134. def setup_maze(level):
  135.     for y in range(len(level)):
  136.         for x in range(len(level[y])):
  137.             #get the character at each x,y coordinate
  138.             #Note the order of y and x in the next line
  139.             character = level[y][x]
  140.             #calculate the screen x, y coordinates
  141.             screen_x = -288 + (x * 24)
  142.             screen_y = 288 - (y * 24)
  143.  
  144.             #Check if it is an x (representing a wall)
  145.             if character == 'X':
  146.                 pen.goto(screen_x, screen_y)
  147.                 pen.stamp()
  148.                 walls.append((screen_x,screen_y))
  149.             #Check if it is a player
  150.             if character == 'P':
  151.                 player.goto(screen_x, screen_y)
  152.  
  153.             #Check if it is a T (representing a treasure)
  154.             if character == 'T':
  155.                 treasures.append(Treasure(screen_x, screen_y))
  156.  
  157. #Create class instances
  158. pen = Pen()
  159. player = Player()
  160.  
  161. #Create wall coordinate list
  162. walls = []
  163.  
  164. #Set up the level
  165. setup_maze(levels[1])
  166.  
  167.  
  168.  
  169. #Keyboard Bindings
  170. wn.listen()
  171. wn.onkey(player.go_up,'w')
  172. wn.onkey(player.go_down,'s')
  173. wn.onkey(player.go_left,'a')
  174. wn.onkey(player.go_right,'d')
  175. wn.mainloop()
  176.  
  177. #Turn off screen updates
  178. wn.tracer(0)
  179.  
  180. #Main game loop
  181. while True:
  182.     #Check for player collision with treasure
  183.     #Iterate through treasure list
  184.     for treasure in treasures:
  185.         if player.is_collision(treasure):
  186.             #Add the treasure gold to the player gold
  187.             player.gold += treasure.gold
  188.             print ('Player Gold: {}'.format(player.gold))
  189.             #Destroy the treasure
  190.             treasure.destroy()
  191.             #Remove the treasure
  192.             treasures.remove(treasure)
  193.     #Update screen
  194.     wn.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement