SHARE
TWEET

Python Maze Game Treasure

a guest Apr 27th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import turtle
  2. wn = turtle.Screen()
  3. wn.setup(700,700)
  4. wn.bgcolor("black")
  5.  
  6. #create pen
  7. class Pen(turtle.Turtle):
  8.   def __init__(self):
  9.     turtle.Turtle.__init__(self)
  10.     self.shape("square")
  11.     self.color("blue")
  12.     self.penup()
  13.     self.speed(0)
  14.    
  15. class Player(turtle.Turtle):
  16.   def __init__(self):
  17.     turtle.Turtle.__init__(self)
  18.     self.shape("square")
  19.     self.color("Green")
  20.     self.penup()
  21.     self.speed(0)
  22.     self.gold= 0
  23.  
  24.   def go_up(self):
  25.     move_to_x=player.xcor()
  26.     move_to_y=player.ycor() + 24
  27.    
  28.     if (move_to_x,move_to_y) not in walls:
  29.       self.goto(move_to_x, move_to_y)
  30.  
  31.   def go_down(self):
  32.     move_to_x=player.xcor()
  33.     move_to_y=player.ycor() - 24
  34.    
  35.     if (move_to_x,move_to_y) not in walls:
  36.       self.goto(move_to_x, move_to_y)  
  37.        
  38.   def go_right(self):
  39.     move_to_x=player.xcor() + 24
  40.     move_to_y=player.ycor()
  41.    
  42.     if (move_to_x,move_to_y) not in walls:
  43.       self.goto(move_to_x, move_to_y)
  44.        
  45.   def go_left(self):
  46.     move_to_x=player.xcor() - 24
  47.     move_to_y=player.ycor()
  48.    
  49.     if (move_to_x,move_to_y) not in walls:
  50.       self.goto(move_to_x, move_to_y)
  51.  
  52.   def is_collision(self, other):
  53.     a = self.xcor()-other.xcor()
  54.     b = self.ycor()-other.ycor()
  55.     distance = math.sqrt((a ** 2) + (b ** 2))
  56.    
  57.     if distance < 5:
  58.       return True
  59.     else:
  60.       return False
  61.      
  62. class Treasure(turtle.Turtle):
  63.   def _init_(self, x, y):
  64.     turtle.Turtle._init_(self)
  65.     self.shape("circle")
  66.     self.color("gold")
  67.     self.penup()
  68.     self.speed(0)
  69.     self.gold = 100
  70.     self.goto(x, y)
  71.  
  72. def destroy(self):
  73.   self.goto(2000, 2000)
  74. #create Levels list
  75. levels = [""]
  76.  
  77. #define first level
  78. level_1 = [
  79. "XXXXXXXXXXXXXXXXXXXXXXXXX",
  80. "XP  XXXXXXX           TXX",
  81. "X  XXXXXXX  XXXXXX     XX",
  82. "X       XX  XXXXXX    XXX",
  83. "X       XX  XXX        XX",
  84. "X  XXX  XX  XXX        XX",
  85. "X    X  XX  XXXXXX  XXXXX",
  86. "XXXX X  XX    XXXX  XXXXX",
  87. "X  X X          T       X",
  88. "X       XX  XXXX      X X",
  89. "X XXXXXXXX  XXXXXXXXXXX X",
  90. "X                XX     X",
  91. "XXXXXXXXXXXX     XXXTXXXX",
  92. "X  TXXXXXXXXXXX  XXXXX  X",
  93. "X XXXXXXXXXXXXX         X",
  94. "X X                     X",
  95. "X           XXXX  XXXXXXX",
  96. "X XXXX  XXXXXXXX   XXXXXX",
  97. "X XXXX  XX              X",
  98. "X  T XXXXX              X",
  99. "XX          XXXXXX  XX  X",
  100. "XXXXXXXXXXXXXXXXXX  XX  X",
  101. "XX   X       XXXXX      X",
  102. "XXXX                    X",
  103. "XXXXXXXXXXXXXXXXXXXXXXXXX"
  104. ]
  105. #Add treasure list
  106. treasures = []
  107. #Add maze to mazes list
  108. levels.append(level_1)
  109.  
  110. #Create Level Setup Function
  111.  
  112. def setup_maze(level):
  113.   turtle.speed(0)
  114.   for y in range(len(level)):
  115.     for x in range(len(level[y])):
  116.       #Get the Character at each x,y coordinate
  117.       character = level[y][x]
  118.       #Calculate the screen x, y coordinates
  119.       screen_x = -288 + (x * 24)
  120.       screen_y = 288 - (y * 24)
  121.      
  122.       #To check if it is an X (rep a wall)
  123.       if character == "X":
  124.         pen.goto(screen_x, screen_y)
  125.         pen.stamp()
  126.         walls.append((screen_x,screen_y))
  127.        #To check if P (reps the player
  128.       if character == "P":
  129.         player.goto(screen_x, screen_y)
  130.         #To check if T (reps the treasures)
  131.       if character == "T":
  132.         treasures.append(Treasure(screen_x, screen_y))
  133.  
  134. #Create class instances
  135. pen = Pen()
  136. player = Player()
  137. # Walls
  138. walls=[]
  139. print(walls)
  140.  
  141. #Set up the level
  142. setup_maze(levels[1])
  143. print(walls)
  144.  
  145.  
  146. #Keyboard Binding
  147. wn.listen()
  148. wn.onkey(player.go_left,"Left")
  149. wn.onkey(player.go_right,"Right")
  150. wn.onkey(player.go_up,"Up")
  151. wn.onkey(player.go_down,"Down")
  152.  
  153.  
  154. #Turn off screen upates
  155. wn.tracer(0)
  156.  
  157.  
  158. #Main Game Loop
  159. while True:
  160.     #Check collision with treasure
  161.     #Iterate through treasure list                      
  162.     for treasure in treasures:
  163.       if player.is_collision(treasure):
  164.            #Add treasure gold to plsyer gold
  165.            player.gold += treasure.gold
  166.            print ("Player GOld: {}".format(player.gold))
  167.            #Destroys the treasure
  168.            treasure.destroy()
  169.            #Removes the treasure from the treasures list
  170.            treasure.remove(treasure)
  171. # Update Screen
  172.     wn.update()
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top