fork download
  1.  
  2. # your code goes here
  3.  
  4.  
  5. Open this file in GitHub Desktop You must be signed in to make or propose changes You must be signed in to make or propose changes
  6.  
  7.  
  8. """Pacman, classic arcade game.
  9.  
  10. Exercises
  11.  
  12. 1. Change the board.
  13. 6 2. Change the number of ghosts.
  14. 7 3. Change where pacman starts.
  15. 8 4. Make the ghosts faster/slower.
  16. 9 5. Make the ghosts smarter.
  17. 10
  18. 11 """
  19. 12
  20.  
  21. 13 from random import choice
  22. 14 from turtle import *
  23. 15 from freegames import floor, vector
  24. 16
  25.  
  26. 17 state = {'score': 0}
  27. 18 path = Turtle(visible=False)
  28. 19 writer = Turtle(visible=False)
  29. 20 aim = vector(5, 0)
  30. 21 pacman = vector(-40, -80)
  31. 22 ghosts = [
  32. 23 [vector(-180, 160), vector(5, 0)],
  33. 24 [vector(-180, -160), vector(0, 5)],
  34. 25 [vector(100, 160), vector(0, -5)],
  35. 26 [vector(100, -160), vector(-5, 0)],
  36. 27 ]
  37. 28 tiles = [
  38. 29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  39. 30 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  40. 31 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
  41. 32 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  42. 33 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0,
  43. 34 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  44. 35 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
  45. 36 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
  46. 37 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  47. 38 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0,
  48. 39 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0,
  49. 40 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  50. 41 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
  51. 42 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0,
  52. 43 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0,
  53. 44 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  54. 45 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
  55. 46 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  56. 47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  57. 48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 49 ]
  59. 50
  60.  
  61. 51 def square(x, y):
  62. 52 "Draw square using path at (x, y)."
  63. 53 path.up()
  64. 54 path.goto(x, y)
  65. 55 path.down()
  66. 56 path.begin_fill()
  67. 57
  68.  
  69. 58 for count in range(4):
  70. 59 path.forward(20)
  71. 60 path.left(90)
  72. 61
  73.  
  74. 62 path.end_fill()
  75. 63
  76.  
  77. 64 def offset(point):
  78. 65 "Return offset of point in tiles."
  79. 66 x = (floor(point.x, 20) + 200) / 20
  80. 67 y = (180 - floor(point.y, 20)) / 20
  81. 68 index = int(x + y * 20)
  82. 69 return index
  83. 70
  84.  
  85. 71 def valid(point):
  86. 72 "Return True if point is valid in tiles."
  87. 73 index = offset(point)
  88. 74
  89.  
  90. 75 if tiles[index] == 0:
  91. 76 return False
  92. 77
  93.  
  94. 78 index = offset(point + 19)
  95. 79
  96.  
  97. 80 if tiles[index] == 0:
  98. 81 return False
  99. 82
  100.  
  101. 83 return point.x % 20 == 0 or point.y % 20 == 0
  102. 84
  103.  
  104. 85 def world():
  105. 86 "Draw world using path."
  106. 87 bgcolor('black')
  107. 88 path.color('blue')
  108. 89
  109.  
  110. 90 for index in range(len(tiles)):
  111. 91 tile = tiles[index]
  112. 92
  113.  
  114. 93 if tile > 0:
  115. 94 x = (index % 20) * 20 - 200
  116. 95 y = 180 - (index // 20) * 20
  117. 96 square(x, y)
  118. 97
  119.  
  120. 98 if tile == 1:
  121. 99 path.up()
  122. 100 path.goto(x + 10, y + 10)
  123. 101 path.dot(2, 'white')
  124. 102
  125.  
  126. 103 def move():
  127. 104 "Move pacman and all ghosts."
  128. 105 writer.undo()
  129. 106 writer.write(state['score'])
  130. 107
  131.  
  132. 108 clear()
  133. 109
  134.  
  135. 110 if valid(pacman + aim):
  136. 111 pacman.move(aim)
  137. 112
  138.  
  139. 113 index = offset(pacman)
  140. 114
  141.  
  142. 115 if tiles[index] == 1:
  143. 116 tiles[index] = 2
  144. 117 state['score'] += 1
  145. 118 x = (index % 20) * 20 - 200
  146. 119 y = 180 - (index // 20) * 20
  147. 120 square(x, y)
  148. 121
  149.  
  150. 122 up()
  151. 123 goto(pacman.x + 10, pacman.y + 10)
  152. 124 dot(20, 'yellow')
  153. 125
  154.  
  155. 126 for point, course in ghosts:
  156. 127 if valid(point + course):
  157. 128 point.move(course)
  158. 129 else:
  159. 130 options = [
  160. 131 vector(5, 0),
  161. 132 vector(-5, 0),
  162. 133 vector(0, 5),
  163. 134 vector(0, -5),
  164. 135 ]
  165. 136 plan = choice(options)
  166. 137 course.x = plan.x
  167. 138 course.y = plan.y
  168. 139
  169.  
  170. 140 up()
  171. 141 goto(point.x + 10, point.y + 10)
  172. 142 dot(20, 'red')
  173. 143
  174.  
  175. 144 update()
  176. 145
  177.  
  178. 146 for point, course in ghosts:
  179. 147 if abs(pacman - point) < 20:
  180. 148 return
  181. 149
  182.  
  183. 150 ontimer(move, 100)
  184. 151
  185.  
  186. 152 def change(x, y):
  187. 153 "Change pacman aim if valid."
  188. 154 if valid(pacman + vector(x, y)):
  189. 155 aim.x = x
  190. 156 aim.y = y
  191. 157
  192.  
  193. 158 setup(420, 420, 370, 0)
  194. 159 hideturtle()
  195. 160 tracer(False)
  196. 161 writer.goto(160, 160)
  197. 162 writer.color('white')
  198. 163 writer.write(state['score'])
  199. 164 listen()
  200. 165 onkey(lambda: change(5, 0), 'Right')
  201. 166 onkey(lambda: change(-5, 0), 'Left')
  202. 167 onkey(lambda: change(0, 5), 'Up')
  203. 168 onkey(lambda: change(0, -5), 'Down')
  204. 169 world()
  205. 170 move()
  206. 171 done()
  207.  
Compilation error #stdin compilation error #stdout 0.03s 9240KB
stdin
Standard input is empty
compilation info
Traceback (most recent call last):
  File "/usr/lib/python3.7/py_compile.py", line 143, in compile
    _optimize=optimize)
  File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./prog.py", line 5
    Open this file in GitHub Desktop  You must be signed in to make or propose changes  You must be signed in to make or propose changes 
    ^
IndentationError: unexpected indent

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.7/py_compile.py", line 147, in compile
    raise py_exc
py_compile.PyCompileError: Sorry: IndentationError: unexpected indent (prog.py, line 5)
stdout
Standard output is empty