|
| 1 | +from random import choice |
| 2 | +from turtle import * |
| 3 | +from freegames import floor, vector |
| 4 | + |
| 5 | +state = {'score': 0} |
| 6 | +path = Turtle(visible=False) |
| 7 | +writer = Turtle(visible=False) |
| 8 | +aim = vector(5, 0) |
| 9 | +pacman = vector(-40, -80) |
| 10 | +ghosts = [ |
| 11 | + [vector(-180, 160), vector(5, 0)], |
| 12 | + [vector(-180, -160), vector(0, 5)], |
| 13 | + [vector(100, 160), vector(0, -5)], |
| 14 | + [vector(100, -160), vector(-5, 0)], |
| 15 | +] |
| 16 | +tiles = [ |
| 17 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 18 | + 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, |
| 19 | + 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, |
| 20 | + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, |
| 21 | + 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, |
| 22 | + 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, |
| 23 | + 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, |
| 24 | + 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, |
| 25 | + 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, |
| 26 | + 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, |
| 27 | + 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, |
| 28 | + 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, |
| 29 | + 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, |
| 30 | + 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, |
| 31 | + 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, |
| 32 | + 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, |
| 33 | + 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, |
| 34 | + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, |
| 35 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 36 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 37 | +] |
| 38 | + |
| 39 | +def square(x, y): |
| 40 | + "Draw square using path at (x, y)." |
| 41 | + path.up() |
| 42 | + path.goto(x, y) |
| 43 | + path.down() |
| 44 | + path.begin_fill() |
| 45 | + |
| 46 | + for count in range(4): |
| 47 | + path.forward(20) |
| 48 | + path.left(90) |
| 49 | + |
| 50 | + path.end_fill() |
| 51 | + |
| 52 | +def offset(point): |
| 53 | + "Return offset of point in tiles." |
| 54 | + x = (floor(point.x, 20) + 200) / 20 |
| 55 | + y = (180 - floor(point.y, 20)) / 20 |
| 56 | + index = int(x + y * 20) |
| 57 | + return index |
| 58 | + |
| 59 | +def valid(point): |
| 60 | + "Return True if point is valid in tiles." |
| 61 | + index = offset(point) |
| 62 | + |
| 63 | + if tiles[index] == 0: |
| 64 | + return False |
| 65 | + |
| 66 | + index = offset(point + 19) |
| 67 | + |
| 68 | + if tiles[index] == 0: |
| 69 | + return False |
| 70 | + |
| 71 | + return point.x % 20 == 0 or point.y % 20 == 0 |
| 72 | + |
| 73 | +def world(): |
| 74 | + "Draw world using path." |
| 75 | + bgcolor('black') |
| 76 | + path.color('blue') |
| 77 | + |
| 78 | + for index in range(len(tiles)): |
| 79 | + tile = tiles[index] |
| 80 | + |
| 81 | + if tile > 0: |
| 82 | + x = (index % 20) * 20 - 200 |
| 83 | + y = 180 - (index // 20) * 20 |
| 84 | + square(x, y) |
| 85 | + |
| 86 | + if tile == 1: |
| 87 | + path.up() |
| 88 | + path.goto(x + 10, y + 10) |
| 89 | + path.dot(2, 'white') |
| 90 | + |
| 91 | +def move(): |
| 92 | + "Move pacman and all ghosts." |
| 93 | + writer.undo() |
| 94 | + writer.write(state['score']) |
| 95 | + |
| 96 | + clear() |
| 97 | + |
| 98 | + if valid(pacman + aim): |
| 99 | + pacman.move(aim) |
| 100 | + |
| 101 | + index = offset(pacman) |
| 102 | + |
| 103 | + if tiles[index] == 1: |
| 104 | + tiles[index] = 2 |
| 105 | + state['score'] += 1 |
| 106 | + x = (index % 20) * 20 - 200 |
| 107 | + y = 180 - (index // 20) * 20 |
| 108 | + square(x, y) |
| 109 | + |
| 110 | + up() |
| 111 | + goto(pacman.x + 10, pacman.y + 10) |
| 112 | + dot(20, 'yellow') |
| 113 | + |
| 114 | + for point, course in ghosts: |
| 115 | + if valid(point + course): |
| 116 | + point.move(course) |
| 117 | + else: |
| 118 | + options = [ |
| 119 | + vector(5, 0), |
| 120 | + vector(-5, 0), |
| 121 | + vector(0, 5), |
| 122 | + vector(0, -5), |
| 123 | + ] |
| 124 | + plan = choice(options) |
| 125 | + course.x = plan.x |
| 126 | + course.y = plan.y |
| 127 | + |
| 128 | + up() |
| 129 | + goto(point.x + 10, point.y + 10) |
| 130 | + dot(20, 'red') |
| 131 | + |
| 132 | + update() |
| 133 | + |
| 134 | + for point, course in ghosts: |
| 135 | + if abs(pacman - point) < 20: |
| 136 | + return |
| 137 | + |
| 138 | + ontimer(move, 100) |
| 139 | + |
| 140 | +def change(x, y): |
| 141 | + "Change pacman aim if valid." |
| 142 | + if valid(pacman + vector(x, y)): |
| 143 | + aim.x = x |
| 144 | + aim.y = y |
| 145 | + |
| 146 | +setup(420, 420, 370, 0) |
| 147 | +hideturtle() |
| 148 | +tracer(False) |
| 149 | +writer.goto(160, 160) |
| 150 | +writer.color('white') |
| 151 | +writer.write(state['score']) |
| 152 | +listen() |
| 153 | +onkey(lambda: change(5, 0), 'Right') |
| 154 | +onkey(lambda: change(-5, 0), 'Left') |
| 155 | +onkey(lambda: change(0, 5), 'Up') |
| 156 | +onkey(lambda: change(0, -5), 'Down') |
| 157 | +world() |
| 158 | +move() |
| 159 | +done() |
0 commit comments