-
Notifications
You must be signed in to change notification settings - Fork 1
/
kara.py
382 lines (311 loc) · 10.2 KB
/
kara.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import copy
import tkinter
import time
player = '@'
player_on_storage = '+'
box = '$'
box_on_storage = '*'
storage = '.'
wall = '#'
empty = ' '
if 'levels' not in globals(): levels = [
"""
###
#.#
# ####
###$ $.#
#. $@###
####$#
#.#
###
"""
,
[
[' ', ' ', '#', '#', '#'],
[' ', ' ', '#', '.', '#'],
[' ', ' ', '#', ' ', '#', '#', '#', '#'],
['#', '#', '#', '$', ' ', '$', '.', '#'],
['#', '.', ' ', '$', '@', '#', '#', '#'],
['#', '#', '#', '#', '$', '#'],
[' ', ' ', ' ', '#', '.', '#'],
[' ', ' ', ' ', '#', '#', '#'],
],
]
CELL_SIZE = 32
HEIGHT = CELL_SIZE * 16
WIDTH = CELL_SIZE * 16
if 'TIME_S' not in globals(): TIME_S = 0.5
if 'STRICT' not in globals(): STRICT = True
level = []
if 'current_level' not in globals(): current_level = 0
player_rotation = 'up'
offset_x = 0
offset_y = 0
def load_level():
global level, offset_x, offset_y
level = copy.deepcopy(levels[current_level])
if(str == type(level)):
level = level.splitlines()
for index, line in enumerate(level):
level[index] = list(line)
level = level[1:len(level)-1]
#find max
offset_y = len(level)-1
offset_x = 0
for line in level:
if len(line)-1 > offset_x: offset_x = len(line)-1
#add missing empty fields if needed
for index, line in enumerate(level):
diff = int(offset_x - (len(line)-1))
if(diff > 0):
for i in range(diff):
level[index].append(' ')
#cell to pixel
offset_y = (HEIGHT - offset_y * CELL_SIZE) / 2
offset_x = (WIDTH - offset_x * CELL_SIZE) / 2
load_level()
def find_player():
for test_y, row in enumerate(level):
for test_x, cell in enumerate(row):
if cell == player or cell == player_on_storage:
player_x = test_x
player_y = test_y
return player_x, player_y
def turnRight():
global player_rotation
if player_rotation == 'right':
player_rotation = 'down'
elif player_rotation == 'down':
player_rotation = 'left'
elif player_rotation == 'left':
player_rotation = 'up'
elif player_rotation == 'up':
player_rotation = 'right'
draw()
def turnLeft():
global player_rotation
if player_rotation == 'right':
player_rotation = 'up'
elif player_rotation == 'up':
player_rotation = 'left'
elif player_rotation == 'left':
player_rotation = 'down'
elif player_rotation == 'down':
player_rotation = 'right'
draw()
def move():
global current_level
player_x, player_y = find_player()
dx = 0
dy = 0
if player_rotation == 'right':
dx = 1
elif player_rotation == 'up':
dy = -1
elif player_rotation == 'left':
dx = -1
elif player_rotation == 'down':
dy = 1
_move(player_x, player_y, dx, dy)
def _move(player_x, player_y, dx, dy):
global level, STRICT
if STRICT:
if player_y + dy < 0 or player_x + dx < 0:
raise Exception('Kara can not leave the world.')
current = level[player_y][player_x] #kara or kara on kara on berry
adjacent = level[player_y + dy][player_x + dx] # next field
beyond = ''
if (
0 <= player_y + dy + dy < len(level)
and 0 <= player_x + dx + dx < len(level[player_y + dy + dy])
):
beyond = level[player_y + dy + dy][player_x + dx + dx]
#define next states
next_adjacent = {
empty: player,
storage: player_on_storage,
}
next_current = {
player: empty,
player_on_storage: storage,
}
next_beyond = {
empty: box,
storage: box_on_storage,
}
next_adjacent_push = {
box: player,
box_on_storage: player_on_storage,
}
if adjacent in next_adjacent: # next field
level[player_y][player_x] = next_current[current]
level[player_y + dy][player_x + dx] = next_adjacent[adjacent]
elif beyond in next_beyond and adjacent in next_adjacent_push: # field after next field
level[player_y][player_x] = next_current[current]
level[player_y + dy][player_x + dx] = next_adjacent_push[adjacent]
level[player_y + dy + dy][player_x + dx + dx] = next_beyond[beyond]
elif STRICT: #can't move
raise Exception('Kara bumped into a tree.')
is_level_complete()
draw()
def putLeaf():
for test_y, row in enumerate(level):
for test_x, cell in enumerate(row):
if cell == player:
level[test_y][test_x] = player_on_storage
draw()
def putBerry():
putLeaf()
def removeLeaf():
for test_y, row in enumerate(level):
for test_x, cell in enumerate(row):
if cell == player_on_storage:
level[test_y][test_x] = player
draw()
def removeBerry():
removeLeaf();
def onLeaf():
for test_y, row in enumerate(level):
for test_x, cell in enumerate(row):
if cell == player_on_storage:
return True
return False
def onBerry():
return onLeaf();
def treeFront():
if player_rotation == 'right':
return isTreeFromKara(1, 0)
elif player_rotation == 'up':
return isTreeFromKara(0, -1)
elif player_rotation == 'left':
return isTreeFromKara(-1, 0)
elif player_rotation == 'down':
return isTreeFromKara(0, 1)
def treeLeft():
if player_rotation == 'right':
return isTreeFromKara(0, -1)
elif player_rotation == 'up':
return isTreeFromKara(-1, 0)
elif player_rotation == 'left':
return isTreeFromKara(0, 1)
elif player_rotation == 'down':
return isTreeFromKara(1, 0)
def treeRight():
if player_rotation == 'right':
return isTreeFromKara(0, 1)
elif player_rotation == 'up':
return isTreeFromKara(1, 0)
elif player_rotation == 'left':
return isTreeFromKara(0, -1)
elif player_rotation == 'down':
return isTreeFromKara(-1, 0)
def isTreeFromKara(x, y):
player_x, player_y = find_player()
return isTree(player_x + x, player_y + y)
def isTree(x, y):
if len(level) > y and len(level[y]) > x:
return level[y][x] == wall
else:
return False; #no field
def mushroomFront():
if player_rotation == 'right':
return isMushroomFromKara(1, 0)
if player_rotation == 'up':
return isMushroomFromKara(0, -1)
if player_rotation == 'left':
return isMushroomFromKara(-1, 0)
if player_rotation == 'down':
return isMushroomFromKara(0, 1)
def isMushroomFromKara(x, y):
player_x, player_y = find_player()
return isMushroom(player_x + x, player_y+ y)
def isMushroom(x, y):
if len(level) > y and len(level[y]) > x:
return level[y][x] == box or level[y][x] == box_on_storage
else:
return False; #no field
class Kara:
def move(self):
move()
def turnRight(self):
turnRight()
def turnLeft(self):
turnLeft()
def putLeaf(self):
putLeaf()
def putBerry(self):
putBerry()
def removeLeaf(self):
removeLeaf()
def removeBerry(self):
removeBerry()
def onLeaf(self):
onLeaf()
def onBerry(self):
onBerry()
def treeFront(self):
treeFront()
def treeLeft(self):
treeLeft()
def treeRight(self):
treeRight()
def mushroomFront(self):
mushroomFront()
kara = Kara()
def is_level_complete():
global current_level
complete = True
for y, row in enumerate(level):
for x, cell in enumerate(row):
if cell == box:
complete = False
return complete
def draw():
global window, canvas, objs
#grid
cells_y = len(level)-1
cells_x = 0
for line in level:
if len(line)-1 > cells_x: cells_x = len(line)-1
for y in range(cells_y+2):
canvas.create_line(
(offset_x - CELL_SIZE/2, offset_y + y * CELL_SIZE - CELL_SIZE/2), #start
(offset_x + (cells_x+1) * CELL_SIZE - CELL_SIZE/2, offset_y + y * CELL_SIZE - CELL_SIZE/2), #end
fill="#90b890" #rgb(144,184,144)
)
for x in range(cells_x+2):
canvas.create_line(
(offset_x + x * CELL_SIZE - CELL_SIZE/2, offset_y - CELL_SIZE/2), #start
(offset_x + x * CELL_SIZE - CELL_SIZE/2, offset_y + (cells_y+1) * CELL_SIZE - CELL_SIZE/2), #end
fill="#90b890" #rgb(144,184,144)
)
#figures
objs = {}
i = 0
for y, row in enumerate(level):
for x, cell in enumerate(row):
if cell != empty:
if cell == player_on_storage or cell == box_on_storage:
objs[str(i)+'storage'] = tkinter.PhotoImage(file=r'images/strawberry.png')
canvas.create_image(offset_x + x * CELL_SIZE, offset_y + y * CELL_SIZE, image=objs[str(i)+'storage'], anchor="center")
if cell == player or cell == player_on_storage:
objs[i] = tkinter.PhotoImage(file=r'images/ladybug-' + player_rotation + '.png')
elif cell == box or cell == box_on_storage:
objs[i] = tkinter.PhotoImage(file=r'images/mushroom-single.png')
elif cell == storage:
objs[i] = tkinter.PhotoImage(file=r'images/strawberry.png')
elif cell == wall:
objs[i] = tkinter.PhotoImage(file=r'images/forest.png')
canvas.create_image(offset_x + x * CELL_SIZE, offset_y + y * CELL_SIZE, image=objs[i], anchor="center")
i += 1
canvas.update()
time.sleep(TIME_S)
window = tkinter.Tk()
window.title("PythonKara")
window.geometry(str(WIDTH) + 'x' + str(HEIGHT))
# window and background
canvas = tkinter.Canvas(window, width=WIDTH, height=HEIGHT)
canvas.pack()
canvas.create_rectangle(0, 0, WIDTH, HEIGHT, fill="#B4E6B4")
objs = {}
draw() # initial setup