[python] Turtle Escape- TDEJ

Viewer

copydownloadembedprintName: Turtle Escape- TDEJ
  1. import turtle as trtl
  2. import random as rand
  3.  
  4. wall_color = "darkgreen"
  5. num_sides = 23
  6. path_width = 24 
  7.  
  8.  
  9. def draw_door(pos):
  10.   maze_painter.forward(pos)
  11.   maze_painter.penup()
  12.   maze_painter.forward(path_width*2)
  13.   maze_painter.pendown()
  14.  
  15. def draw_barrier(pos):
  16.   maze_painter.forward(pos)
  17.   maze_painter.left(90)
  18.   maze_painter.forward(path_width*2)
  19.   maze_painter.backward(path_width*2)
  20.   maze_painter.right(90)
  21.  
  22. def move_forward():
  23.   player.forward(5)
  24.  
  25. def move_up():
  26.   player.setheading(90)
  27.   move_forward()
  28.  
  29. def move_down():
  30.   player.setheading(270)
  31.   move_forward()
  32.   
  33. def move_left():
  34.   player.setheading(180)
  35.   move_forward()
  36.  
  37. def move_right():
  38.   player.setheading(0)
  39.   move_forward()
  40.  
  41.  
  42.  
  43. maze_painter = trtl.Turtle()
  44. maze_painter.pencolor(wall_color)
  45. maze_painter.speed(0)
  46. maze_painter.pensize(3)
  47. maze_painter.penup()
  48. maze_painter.goto(path_width*2, -path_width*2)
  49. maze_painter.pendown()
  50.  
  51. player = trtl.Turtle(shape="turtle")
  52. player.fillcolor("red")
  53. player.pensize(5)
  54. player.penup()
  55. player.goto(-path_width*2, path_width*2)
  56. #player.pendown()
  57.  
  58. wall_len = path_width
  59. for w in range(num_sides)
  60.   wall_len += path_width
  61.  
  62.   if (> 4)
  63.     maze_painter.left(90)
  64.     #value a should be less than b
  65.     
  66.     door = rand.randint(path_width*2, (wall_len - path_width*2))
  67.     barrier = rand.randint(path_width*2, (wall_len - path_width*2))
  68.     while abs(door - barrier) < path_width:
  69.       door = rand.randint(path_width*2, (wall_len - path_width*2))
  70.  
  71.     if (door < barrier):
  72.       draw_door(door)
  73.       draw_barrier(barrier - door - path_width*2)
  74.       maze_painter.forward(wall_len - barrier)
  75.     else
  76.       draw_barrier(barrier)
  77.       draw_door(door - barrier)
  78.       maze_painter.forward(wall_len - door - path_width*2)
  79.  
  80. maze_painter.hideturtle()
  81.  
  82. wn = trtl.Screen()
  83. wn.onkeypress(move_up, 'w')
  84. wn.onkeypress(move_down, 's')
  85. wn.onkeypress(move_left, 'a')
  86. wn.onkeypress(move_right, 'd')
  87.  
  88.  
  89. wn.listen()
  90.  
  91. wn.bgcolor("lightBlue")
  92.  
  93. wn.mainloop()

Editor

You can edit this paste and save as new:


File Description
  • Turtle Escape- TDEJ
  • Paste Code
  • 01 Mar-2021
  • 1.99 Kb
You can Share it: