-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintro.py
More file actions
164 lines (124 loc) · 3.55 KB
/
Copy pathintro.py
File metadata and controls
164 lines (124 loc) · 3.55 KB
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
import pgzrun
WIDTH = 800/2
HEIGHT = 800/2
TILE_SIZE = 40/2
alien = Actor('alien_resize')
alien.topleft = 0, 40/2
from df_maze import give_maze
import numpy as np
import random
maze = give_maze()
where = np.where(maze == 1)
mazeSet = set((where[0][i],where[1][i]) for i in range(where[0].shape[0]))
def draw():
screen.clear()
for i in range(20):
for j in range(20):
if maze[i,j] == 1:
screen.blit('maze', (i*40/2, j*40/2))
alien.draw()
def get_possible_directions():
#NOTATION [UP, LEFT, DOWN, RIGHT]
#UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3
row = int(alien.y / TILE_SIZE)
column = int(alien.x / TILE_SIZE)
UP = not (column,row - 1) in mazeSet and row > 0
LEFT = not (column-1,row) in mazeSet and column > 0
DOWN = not (column,row+1) in mazeSet and row < 19
RIGHT = not (column+1,row) in mazeSet and column < 19
return [UP,LEFT,DOWN,RIGHT]
past_moves = [3]
def policy(possible_directions):
#YOU WILL IMPLEMENT POLICY
#LEFT, RIGHT
#POLICY OUTPUTS A NUMBER 0,1,2,3 CORRESPONDING TO UP, LEFT, DOWN, RIGHT
UP = possible_directions[0]
LEFT = possible_directions[1]
DOWN = possible_directions[2]
RIGHT = possible_directions[3]
print(possible_directions)
retMe = 0
#THIS IS WRONG
if RIGHT:
return 3
if UP:
return 0
if DOWN:
return 2
if LEFT:
return 1
def execute_policy():
dir = get_possible_directions()
move = policy(dir)
#print(move)
if move == 0:
move_up()
past_moves.append(0)
if move == 1:
move_left()
past_moves.append(1)
if move == 2:
move_down()
past_moves.append(2)
if move == 3:
move_right()
past_moves.append(3)
def on_mouse_down(pos):
execute_policy()
def move_left():
row = int(alien.y / TILE_SIZE)
column = int(alien.x / TILE_SIZE)
new_row = row
new_col = column - 1
if not (new_col,new_row) in mazeSet and column > 0:
alien.x -= 40/2
def move_right():
row = int(alien.y / TILE_SIZE)
column = int(alien.x / TILE_SIZE)
new_row = row
new_col = column + 1
if not (new_col,new_row) in mazeSet and column < 19:
alien.x += 40/2
def move_up():
row = int(alien.y / TILE_SIZE)
column = int(alien.x / TILE_SIZE)
new_row = row - 1
new_col = column
if not (new_col,new_row) in mazeSet and row > 0:
alien.y -= 40/2
def move_down():
row = int(alien.y / TILE_SIZE)
column = int(alien.x / TILE_SIZE)
new_row = row + 1
new_col = column
if not (new_col,new_row) in mazeSet and row < 19:
alien.y += 40/2
'''
def on_mouse_down(pos):
if alien.collidepoint(pos):
set_alien_hurt()
'''
def on_key_down(key):
# player movement
row = int(alien.y / TILE_SIZE)
column = int(alien.x / TILE_SIZE)
new_row = row - (key == keys.UP) + (key == keys.DOWN)
new_col = column - (key == keys.LEFT) + (key == keys.RIGHT)
print("OLD", row, column)
print("NEW", new_row, new_col)
if not (new_col,new_row) in mazeSet:
if key == keys.UP and row > 0:
alien.y -= 40/2
if key == keys.DOWN and row < 19:
alien.y += 40/2
if key == keys.LEFT and column > 0:
alien.x -= 40/2
if key == keys.RIGHT and column < 19:
alien.x += 40/2
def set_alien_hurt():
alien.image = 'alien_hurt'
sounds.eep.play()
clock.schedule_unique(set_alien_normal, 1.0)
def set_alien_normal():
alien.image = 'alien_resize'
pgzrun.go()