-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmazeRandom.py
134 lines (112 loc) · 4.49 KB
/
mazeRandom.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
import PySimpleGUI as sg
import numpy as np
import math
AppFont = 'Any 16'
sg.theme('DarkGrey5')
_VARS = {'cellCount': 6, 'gridSize': 400, 'canvas': False, 'window': False,
'playerPos': [0, 0], 'exit': [5, 5]}
# randomCellMap = np.random.randint(2, size=(_VARS['cellCount'], _VARS['cellCount']))
cellSize = _VARS['gridSize']/_VARS['cellCount']
# cellMAP = np.array([[0, 0, 0, 0, 0, 0],
# [0, 1, 0, 1, 0, 0],
# [0, 0, 0, 0, 0, 0],
# [0, 1, 0, 1, 0, 1],
# [1, 1, 0, 1, 1, 0],
# [0, 0, 0, 0, 0, 0]])
# cellMAP = randomCellMap
cellMAP = np.random.randint(2, size=(_VARS['cellCount'], _VARS['cellCount']))
cellMAP[0][0] = 0
cellMAP[5][5] = 0
# METHODS:
def drawGrid():
cells = _VARS['cellCount']
_VARS['canvas'].TKCanvas.create_rectangle(
1, 1, _VARS['gridSize'], _VARS['gridSize'], outline='BLACK', width=1)
for x in range(cells):
_VARS['canvas'].TKCanvas.create_line(
((cellSize * x), 0), ((cellSize * x), _VARS['gridSize']),
fill='BLACK', width=1)
_VARS['canvas'].TKCanvas.create_line(
(0, (cellSize * x)), (_VARS['gridSize'], (cellSize * x)),
fill='BLACK', width=1)
def drawCell(x, y, color='GREY'):
_VARS['canvas'].TKCanvas.create_rectangle(
x, y, x + cellSize, y + cellSize,
outline='BLACK', fill=color, width=1)
def placeCells():
for row in range(cellMAP.shape[0]):
for column in range(cellMAP.shape[1]):
if(cellMAP[column][row] == 1):
drawCell((cellSize*row), (cellSize*column))
def checkEvents(event):
move = ''
if len(event) == 1:
if ord(event) == 63232: # UP
move = 'Up'
elif ord(event) == 63233: # DOWN
move = 'Down'
elif ord(event) == 63234: # LEFT
move = 'Left'
elif ord(event) == 63235: # RIGHT
move = 'Right'
# Filter key press Windows :
else:
if event.startswith('Up'):
move = 'Up'
elif event.startswith('Down'):
move = 'Down'
elif event.startswith('Left'):
move = 'Left'
elif event.startswith('Right'):
move = 'Right'
return move
# INIT :
layout = [[sg.Canvas(size=(_VARS['gridSize'], _VARS['gridSize']),
background_color='white',
key='canvas')],
[sg.Exit(font=AppFont),
sg.Text('', key='-exit-', font=AppFont, size=(15, 1))]]
_VARS['window'] = sg.Window('GridMaker', layout, resizable=True, finalize=True,
return_keyboard_events=True)
_VARS['canvas'] = _VARS['window']['canvas']
drawGrid()
drawCell(_VARS['playerPos'][0], _VARS['playerPos'][1], 'TOMATO')
drawCell(_VARS['exit'][0]*cellSize, _VARS['exit'][1]*cellSize, 'Black')
placeCells()
while True: # Event Loop
event, values = _VARS['window'].read()
if event in (None, 'Exit'):
break
# Filter key press
# Note the math.ceil
xPos = int(math.ceil(_VARS['playerPos'][0]/cellSize))
yPos = int(math.ceil(_VARS['playerPos'][1]/cellSize))
print(f"prev playerPos: {xPos},{yPos}")
if checkEvents(event) == 'Up':
if int(_VARS['playerPos'][1] - cellSize) >= 0:
if cellMAP[yPos-1][xPos] != 1:
_VARS['playerPos'][1] = _VARS['playerPos'][1] - cellSize
elif checkEvents(event) == 'Down':
if int(_VARS['playerPos'][1] + cellSize) < 400:
if cellMAP[yPos+1][xPos] != 1:
_VARS['playerPos'][1] = _VARS['playerPos'][1] + cellSize
elif checkEvents(event) == 'Left':
if int(_VARS['playerPos'][0] - cellSize) >= 0:
if cellMAP[yPos][xPos-1] != 1:
_VARS['playerPos'][0] = _VARS['playerPos'][0] - cellSize
elif checkEvents(event) == 'Right':
if int(_VARS['playerPos'][0] + cellSize) < 400:
if cellMAP[yPos][xPos+1] != 1:
_VARS['playerPos'][0] = _VARS['playerPos'][0] + cellSize
# Clear canvas, draw grid and cells
_VARS['canvas'].TKCanvas.delete("all")
drawGrid()
drawCell(_VARS['exit'][0]*cellSize, _VARS['exit'][1]*cellSize, 'Black')
drawCell(_VARS['playerPos'][0], _VARS['playerPos'][1], 'TOMATO')
placeCells()
# Check for Exit:
xPos = int(math.ceil(_VARS['playerPos'][0]/cellSize))
yPos = int(math.ceil(_VARS['playerPos'][1]/cellSize))
if [xPos, yPos] == _VARS['exit']:
_VARS['window']['-exit-'].update('Found the exit !')
_VARS['window'].close()