pythons games

Solutions on MaxInterview for pythons games by the best coders in the world

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