12
def collide(self, ball):
19
def __init__(self, x, y, w, h, c):
24
self.fmt = curses.A_BOLD | curses.color_pair(c)
26
for i in range(self.x, self.x + self.w):
27
for j in range(self.y, self.y + self.h):
28
grid[j + 1][i + 1] = self
31
def collide(self, ball):
33
for i in range(self.x, self.x + self.w):
34
for j in range(self.y, self.y + self.h):
35
grid[j + 1][i + 1] = None
41
for i in range(self.x, self.x + self.w):
42
for j in range(self.y, self.y + self.h):
43
win.addch(j, i, curses.ACS_BLOCK, self.fmt)
50
def __init__(self, x, y, vx, vy):
57
def collide(self, ball):
60
def encounter(self, dx, dy):
61
ent = grid[self.y + dy + 1][self.x + dx + 1]
62
if ent and not ent.collide(self):
68
while self.y < ship.y:
69
if self.encounter((self.vx + self.vy) / 2, (self.vy - self.vx) / 2):
71
if self.encounter((self.vx - self.vy) / 2, (self.vy + self.vx) / 2):
73
if self.encounter(self.vx, self.vy):
79
win.addch(self.y, self.x, 'O')
86
def __init__(self, x, y):
96
[ None ] * (self.x - self.hw + 1) +
97
[ self ] * (self.hw * 2 + 1) +
98
[ None ] * (width - self.x - self.hw)
101
def collide(self, ball):
103
if ball.x > self.x + self.hw / 2:
105
elif ball.x < self.x - self.hw / 2:
112
if self.x - self.hw < 0:
114
elif self.x + self.hw >= width:
115
self.x = width - self.hw - 1
120
entities.append(Ball(self.x, self.y - 1, self.last, -1))
124
win.addch(self.y - 1, self.x, 'O')
125
win.addch(self.y, self.x - self.hw, curses.ACS_LTEE)
126
for i in range(-self.hw + 1, self.hw):
127
win.addch(curses.ACS_HLINE)
128
win.addch(curses.ACS_RTEE)
131
class PowerOverwhelmingException(Exception):
135
global height, width, ship
137
for i in range(1, 8):
138
curses.init_pair(i, i, 0)
142
height, width = stdscr.getmaxyx()
144
if height < 15 or width < 30:
145
raise PowerOverwhelmingException(
146
"Your computer is not powerful enough to run 'arc anoid'. "
147
"It must support at least 30 columns and 15 rows of next-gen "
148
"full-color 3D graphics.")
150
status = curses.newwin(1, width, 0, 0)
152
game = curses.newwin(height, width, 1, 0)
156
grid[:] = [ [ None for x in range(width + 2) ] for y in range(height + 2) ]
158
for x in range(width + 2):
160
for y in range(height + 2):
161
grid[y][0] = grid[y][-1] = wall
162
ship = Ship(width / 2, height - 5)
163
entities.append(ship)
165
colors = [ 1, 3, 2, 6, 4, 5 ]
167
for x in range(1, width / 7 - 1):
168
for y in range(1, 7):
169
entities.append(Block(x * 7, y * h + x / 2 % 2, 7, h, colors[y - 1]))
172
while select.select([ sys.stdin ], [], [], 0)[0]:
174
if key == curses.KEY_LEFT or key == ord('a') or key == ord('A'):
176
elif key == curses.KEY_RIGHT or key == ord('d') or key == ord('D'):
178
elif key == ord(' '):
180
elif key == 0x1b or key == 3 or key == ord('q') or key == ord('Q'):
183
game.resize(height, width)
185
entities[:] = [ ent for ent in entities if ent.tick(game) ]
187
status.hline(0, 0, curses.ACS_HLINE, width)
188
status.addch(0, 2, curses.ACS_RTEE)
189
status.addstr(' SCORE: ', curses.A_BOLD | curses.color_pair(4))
190
status.addstr('%s/%s ' % (Block.killed, Block.total), curses.A_BOLD)
191
status.addch(curses.ACS_VLINE)
192
status.addstr(' DEATHS: ', curses.A_BOLD | curses.color_pair(4))
193
status.addstr('%s ' % Ball.killed, curses.A_BOLD)
194
status.addch(curses.ACS_LTEE)
196
if Block.killed == Block.total:
197
message = ' A WINNER IS YOU!! '
198
i = int(time.time() / 0.8)
199
for x in range(width):
201
game.addch(height / 2 + y - 3 + (x / 8 + i) % 2, x,
203
curses.A_BOLD | curses.color_pair(colors[y]))
204
game.addstr(height / 2, (width - len(message)) / 2, message,
205
curses.A_BOLD | curses.color_pair(7))
213
print ('You destroyed %s blocks out of %s with %s deaths.' %
214
(Block.killed, Block.total, Ball.killed))
215
except PowerOverwhelmingException as e: