/
minerRL.py
151 lines (127 loc) · 3.13 KB
/
minerRL.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
#!/usr/bin/env python
#import actor
import mapper
import curses
global y
global x
global py
#global px
global moving
y = py = 1
x = 1
moving = True
global mapper
mapper = mapper.mapper()
#from mapper.py generate a map
global map
map = mapper.map
#Initialization of the curses window stdscr
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
stdscr.keypad(1)
def QuitGame():
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.curs_set(1)
curses.endwin()
def update():
global py
#global px
global moving
stdscr.clear()
mapy = mapper.mmapy
#For the TOP rendering
if py <= 10:
moving = True
for yy in xrange(20):
for xx in xrange(mapper.mmapx):
stdscr.addstr(yy, xx, map[yy][xx])
#For the BOTTOM rendering
elif py >= mapy - 10:
startY = 0
moving = True
for yy in xrange(mapy - 20, mapy):
startY += 1
for xx in xrange(mapper.mmapx):
stdscr.addstr(startY-1, xx, map[yy][xx])
#For the MIDDLE rendering
else:
startY = 0
moving = False
for yy in xrange(py - 10, py + 10):
startY += 1
for xx in xrange(mapper.mmapx):
stdscr.addstr(startY-1, xx, map[yy][xx])
stdscr.addstr(1, 16, str(py))
stdscr.addstr(1, 19, str(y))
stdscr.refresh()
def walkable(y, x):
if map[y][x] == ".":
return True
return False
def digable(y, x):
if map[y][x] == "#":
return True
return False
def dig():
global py
global x
y = py
c = stdscr.getch()
if c == ord('w'):
if not walkable(y-1, x) and digable(y-1, x):
map[y-1][x] = "."
elif c == ord('s'):
if not walkable(y+1, x) and digable(y+1, x):
map[y+1][x] = "."
elif c == ord('a'):
if not walkable(y, x-1) and digable(y, x-1):
map[y][x-1] = "."
elif c == ord('d'):
if not walkable(y, x+1) and digable(y, x+1):
map[y][x+1] = "."
else:
stdscr.addstr(21, 1, "Wrong direction.(try wasd)")
stdscr.refresh()
stdscr.getch()
def main():
global y
global x
global py
update()
stdscr.addstr(y,x, "@")
stdscr.refresh()
while 1:
c = stdscr.getch()
if c == ord('w'):
if walkable(py - 1, x):
py -= 1
elif c == ord('s'):
if walkable(py + 1, x):
py += 1
elif c == ord('a'):
if walkable(py, x - 1):
x -= 1
elif c == ord('d'):
if walkable(py, x + 1):
x += 1
#Shift + q to quit
if c == ord('Q'):
break
if c == ord('f'):
stdscr.addstr(21, 1, "Choose direction.")
stdscr.refresh()
dig()
if moving and (py<=10 or py>=mapper.mmapy - 10):
y = py
update()
if y <= 10:
stdscr.addstr(y,x, "@")
elif y >= mapper.mmapy-10:
stdscr.addstr(y - (mapper.mmapy - 20), x, "@")
QuitGame()
if __name__ == '__main__':
main()