Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- wn = turtle.Screen()
- wn.setup(700,700)
- wn.bgcolor("black")
- #create pen
- class Pen(turtle.Turtle):
- def __init__(self):
- turtle.Turtle.__init__(self)
- self.shape("square")
- self.color("blue")
- self.penup()
- self.speed(0)
- class Player(turtle.Turtle):
- def __init__(self):
- turtle.Turtle.__init__(self)
- self.shape("square")
- self.color("Green")
- self.penup()
- self.speed(0)
- self.gold= 0
- def go_up(self):
- move_to_x=player.xcor()
- move_to_y=player.ycor() + 24
- if (move_to_x,move_to_y) not in walls:
- self.goto(move_to_x, move_to_y)
- def go_down(self):
- move_to_x=player.xcor()
- move_to_y=player.ycor() - 24
- if (move_to_x,move_to_y) not in walls:
- self.goto(move_to_x, move_to_y)
- def go_right(self):
- move_to_x=player.xcor() + 24
- move_to_y=player.ycor()
- if (move_to_x,move_to_y) not in walls:
- self.goto(move_to_x, move_to_y)
- def go_left(self):
- move_to_x=player.xcor() - 24
- move_to_y=player.ycor()
- if (move_to_x,move_to_y) not in walls:
- self.goto(move_to_x, move_to_y)
- def is_collision(self, other):
- a = self.xcor()-other.xcor()
- b = self.ycor()-other.ycor()
- distance = math.sqrt((a ** 2) + (b ** 2))
- if distance < 5:
- return True
- else:
- return False
- class Treasure(turtle.Turtle):
- def _init_(self, x, y):
- turtle.Turtle._init_(self)
- self.shape("circle")
- self.color("gold")
- self.penup()
- self.speed(0)
- self.gold = 100
- self.goto(x, y)
- def destroy(self):
- self.goto(2000, 2000)
- #create Levels list
- levels = [""]
- #define first level
- level_1 = [
- "XXXXXXXXXXXXXXXXXXXXXXXXX",
- "XP XXXXXXX TXX",
- "X XXXXXXX XXXXXX XX",
- "X XX XXXXXX XXX",
- "X XX XXX XX",
- "X XXX XX XXX XX",
- "X X XX XXXXXX XXXXX",
- "XXXX X XX XXXX XXXXX",
- "X X X T X",
- "X XX XXXX X X",
- "X XXXXXXXX XXXXXXXXXXX X",
- "X XX X",
- "XXXXXXXXXXXX XXXTXXXX",
- "X TXXXXXXXXXXX XXXXX X",
- "X XXXXXXXXXXXXX X",
- "X X X",
- "X XXXX XXXXXXX",
- "X XXXX XXXXXXXX XXXXXX",
- "X XXXX XX X",
- "X T XXXXX X",
- "XX XXXXXX XX X",
- "XXXXXXXXXXXXXXXXXX XX X",
- "XX X XXXXX X",
- "XXXX X",
- "XXXXXXXXXXXXXXXXXXXXXXXXX"
- ]
- #Add treasure list
- treasures = []
- #Add maze to mazes list
- levels.append(level_1)
- #Create Level Setup Function
- def setup_maze(level):
- turtle.speed(0)
- for y in range(len(level)):
- for x in range(len(level[y])):
- #Get the Character at each x,y coordinate
- character = level[y][x]
- #Calculate the screen x, y coordinates
- screen_x = -288 + (x * 24)
- screen_y = 288 - (y * 24)
- #To check if it is an X (rep a wall)
- if character == "X":
- pen.goto(screen_x, screen_y)
- pen.stamp()
- walls.append((screen_x,screen_y))
- #To check if P (reps the player
- if character == "P":
- player.goto(screen_x, screen_y)
- #To check if T (reps the treasures)
- if character == "T":
- treasures.append(Treasure(screen_x, screen_y))
- #Create class instances
- pen = Pen()
- player = Player()
- # Walls
- walls=[]
- print(walls)
- #Set up the level
- setup_maze(levels[1])
- print(walls)
- #Keyboard Binding
- wn.listen()
- wn.onkey(player.go_left,"Left")
- wn.onkey(player.go_right,"Right")
- wn.onkey(player.go_up,"Up")
- wn.onkey(player.go_down,"Down")
- #Turn off screen upates
- wn.tracer(0)
- #Main Game Loop
- while True:
- #Check collision with treasure
- #Iterate through treasure list
- for treasure in treasures:
- if player.is_collision(treasure):
- #Add treasure gold to plsyer gold
- player.gold += treasure.gold
- print ("Player GOld: {}".format(player.gold))
- #Destroys the treasure
- treasure.destroy()
- #Removes the treasure from the treasures list
- treasure.remove(treasure)
- # Update Screen
- wn.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement