-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.py
210 lines (192 loc) · 7.28 KB
/
world.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
from random import choice, randint, uniform, shuffle
import curses
from util import clamp, Point
from tank import Tank
import blockgraphics, colorpairs
GROUNDFG = 101
GROUNDBG = 102
SKYFG = 103
SKYBG = 104
waves =[[1,1,1,1,1,0,0,0,0,0],
[0,0,0,0,1,1,1,1,1,0]]
class World():
def __init__(self, win, conf):
h, w = win.getmaxyx()
ground = [[False]*h for x in range(w)]
changes = [1, -1] * int(w*conf['ground_steepness']/2)
changes += [0] * (w - len(changes))
assert len(changes) == w
shuffle(changes)
min_y = int(h*conf['sky_min'])
max_y = int(h*(1-conf['ground_min']))
if(min_y >= max_y):
raise RuntimeError('Your sky_min and/or ground_min are too large for this terminal')
levels = [randint(min_y, max_y)]
for x in range(w-1):
assert(len(changes) == w - x)
next_y = levels[-1] + changes[0]
while(next_y < min_y or next_y > max_y):
shuffle(changes)
next_y = levels[-1] + changes[0]
levels += [next_y]
changes = changes[1:]
for x in range(w):
for y in range(h):
ground[x][y] = levels[x] < y
self.ground = ground
self.groundchars = [['█'] *h for x in range(w)]
self.style = conf['world_style']
self.border = conf['world_border']
for x in range(w):
for y in range(h):
self.paint(x, y)
gameobjects = [self]
players = []
for i in range(conf['players_number']):
curses.init_pair(i+1, conf['players_colors'][i], SKYBG)
x = int(w*(i+1.0)/(conf['players_number']+1))
newplayer = Tank((x, min(levels[x-2:x+3])), "Player {:d}".format(i+1), i+1, self, conf)
players += [newplayer]
gameobjects += [newplayer]
self.wind = randint(-conf['wind_max'], conf['wind_max'])
# init particles
partcount = int(h*w*conf['particles_max']/200.0)
self.particles = [[uniform(0,w-1),uniform(0,h-1),uniform(20,80)]
for i in range(partcount)]
self.conf = conf
self.players = players
self.gameobjects = gameobjects
if(self.style == 'Dirt'):
# green
curses.init_color(GROUNDFG, 400, 700, 100)
# brown
curses.init_color(GROUNDBG, 300, 200, 100)
# dark blue
curses.init_color(SKYBG, 0, 0, 150)
elif(self.style == 'Candy'):
# pink
curses.init_color(GROUNDFG, 1000, 300, 800)
# purple
curses.init_color(GROUNDBG, 800, 0, 600)
# black
curses.init_color(SKYBG, 0, 0, 0)
else:
# white
curses.init_color(GROUNDFG, 1000, 1000, 1000)
# black
curses.init_color(GROUNDBG, 0, 0, 0)
# black
curses.init_color(SKYBG, 0, 0, 0)
curses.init_color(SKYFG, 1000, 1000, 1000)
curses.init_pair(colorpairs.GROUNDPAIR, GROUNDFG, GROUNDBG)
self.groundcolor = curses.color_pair(colorpairs.GROUNDPAIR)
curses.init_pair(colorpairs.SKYPAIR, SKYFG, SKYBG)
self.skycolor = curses.color_pair(colorpairs.SKYPAIR)
def update(self, win):
h, w = win.getmaxyx()
for part in self.particles:
if(self.conf['particles_type'] == 'Snow'):
part[0] += self.wind * self.conf['wind_force'] * part[2]
part[1] += self.conf['gravity'] * part[2] * 0.0002
if(self.conf['particles_type'] == 'Rain'):
part[0] += self.wind * self.conf['wind_force'] * part[2]
part[1] += self.conf['gravity'] * part[2] * 0.001
def draw(self, win):
h, w = win.getmaxyx()
win.bkgdset(' ', self.skycolor)
# Draw Particles
parttype = self.conf['particles_type']
for part in self.particles:
part[0] = part[0]%w
part[1] = part[1]%h
try:
if(parttype == 'Snow'):
if(part[2] > 60):
win.addstr(int(part[1]), int(part[0]), '∗')
else:
win.addstr(int(part[1]), int(part[0]), '·')
elif(parttype == 'Rain'):
win.addstr(int(part[1]), int(part[0]), '|', curses.color_pair(2))
elif(parttype == 'Stars'):
if(randint(0, 200) == 0):
if(part[2] > 60):
win.addstr(int(part[1]), int(part[0]), '◆')
else:
win.addstr(int(part[1]), int(part[0]), '✦')
else:
if(part[2] > 60):
win.addstr(int(part[1]), int(part[0]), '٭')
elif(part[2] > 40):
win.addstr(int(part[1]), int(part[0]), '·')
else:
win.addstr(int(part[1]), int(part[0]), '・')
except curses.error:
pass
# Draw Ground
try:
for x, col in enumerate(self.ground):
for y, cell in enumerate(col):
if(cell):
win.addstr(y, x, self.groundchars[x][y], self.groundcolor)
except curses.error:
# The very last character will error so except only once
pass
def destroy_ground(self, x, y):
self.ground[x][y] = False
if(self.style in ['Silhouette', 'Pipes']):
for xi in range(max(0, x-1), min(len(self.ground), x+2)):
for yi in range(max(0, y-1), min(len(self.ground[0]), y+2)):
self.paint(xi, yi)
def paint(self, x, y):
if(self.ground[x][y]):
h, w = len(self.ground[0]), len(self.ground)
if(self.style == 'Block'):
c = '█'
elif(self.style == 'Silhouette'):
neighbors = (self.ground[x-1][y],
self.ground[(x+1)%w][y],
self.ground[x][y-1],
self.ground[x][min(y+1,h-1)])
diags =(self.ground[x-1][y-1],
self.ground[(x+1)%w][y-1],
self.ground[x-1][min(y+1,h-1)],
self.ground[(x+1)%w][min(y+1,h-1)])
block = ( not(neighbors[0] and neighbors[2] and diags[0]),
not(neighbors[1] and neighbors[2] and diags[1]),
not(neighbors[0] and neighbors[3] and diags[2]),
not(neighbors[1] and neighbors[3] and diags[3]))
c = blockgraphics.blocks[block]
elif(self.style == 'Dirt'):
grass = clamp(max([0]+[y-yi for yi in range(0, y) if self.ground[x][yi]]), 0, 4)
c = ['█', '▓', '▒', '░', ' '][grass]
elif(self.style == 'Candy'):
block = (waves[0][(x*2 +2*y)%10],
waves[0][(x*2+1+2*y)%10],
waves[1][(x*2 +2*y)%10],
waves[1][(x*2+1+2*y)%10])
c = blockgraphics.blocks[block]
elif(self.style == 'Pipes'):
neighbors = (self.ground[x][y-1] or y % 4 == 0,
self.ground[x-1][y] or y % 4 == 0,
self.ground[(x+1)%w][y] or x % 4 == 0,
self.ground[x][(y+1)%h] or x % 4 == 0)
c = blockgraphics.pipes[neighbors]
self.groundchars[x][y] = c
def check_collision(self, p):
if (p.y >= len(self.ground[0])):
return True
elif(p.x < 0 or p.x >= len(self.ground)):
if(self.border == 'Loop'):
return self.check_collision(Point(p.x%len(self.ground), p.y, self))
else:
return self.border == 'Wall'
elif(p.y < 0):
return False
else:
return self.ground[int(p.x)][int(p.y)]
# point moved by x_off, y_off according to this world's rules
def moveby(self, p, x_off, y_off):
if(self.border == 'Loop'):
return Point((p.x+x_off)%len(self.ground) , p.y + y_off, self)
else:
return Point(p.x + x_off, p.y + y_off, self)