1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-22 06:42:41 +01:00

Fix serious problems with 'arc anoid'

Summary: https://github.com/facebook/arcanist/issues/48

Test Plan: hmm

Reviewers: alanh, tuomaspelkonen

Reviewed By: tuomaspelkonen

CC: aran

Differential Revision: https://secure.phabricator.com/D3244
This commit is contained in:
epriestley 2012-08-10 15:16:02 -07:00
parent a31d88ee82
commit 6b0af26b94

View file

@ -15,7 +15,7 @@ class Wall:
class Block: class Block:
killed = 0 killed = 0
total = 0 total = 0
def __init__(self, x, y, w, h, c): def __init__(self, x, y, w, h, c):
self.x = x self.x = x
self.y = y self.y = y
@ -27,7 +27,7 @@ class Block:
for j in range(self.y, self.y + self.h): for j in range(self.y, self.y + self.h):
grid[j + 1][i + 1] = self grid[j + 1][i + 1] = self
Block.total += 1 Block.total += 1
def collide(self, ball): def collide(self, ball):
self.alive = False self.alive = False
for i in range(self.x, self.x + self.w): for i in range(self.x, self.x + self.w):
@ -35,7 +35,7 @@ class Block:
grid[j + 1][i + 1] = None grid[j + 1][i + 1] = None
Block.killed += 1 Block.killed += 1
return False return False
def tick(self, win): def tick(self, win):
if self.alive: if self.alive:
for i in range(self.x, self.x + self.w): for i in range(self.x, self.x + self.w):
@ -46,24 +46,24 @@ class Block:
class Ball: class Ball:
alive = False alive = False
killed = 0 killed = 0
def __init__(self, x, y, vx, vy): def __init__(self, x, y, vx, vy):
self.x = x self.x = x
self.y = y self.y = y
self.vx = vx self.vx = vx
self.vy = vy self.vy = vy
Ball.alive = True Ball.alive = True
def collide(self, ball): def collide(self, ball):
return True return True
def encounter(self, dx, dy): def encounter(self, dx, dy):
ent = grid[self.y + dy + 1][self.x + dx + 1] ent = grid[self.y + dy + 1][self.x + dx + 1]
if ent and not ent.collide(self): if ent and not ent.collide(self):
self.vx -= 2 * dx self.vx -= 2 * dx
self.vy -= 2 * dy self.vy -= 2 * dy
return ent return ent
def tick(self, win): def tick(self, win):
while self.y < ship.y: while self.y < ship.y:
if self.encounter((self.vx + self.vy) / 2, (self.vy - self.vx) / 2): if self.encounter((self.vx + self.vy) / 2, (self.vy - self.vx) / 2):
@ -81,7 +81,7 @@ class Ball:
Ball.alive = False Ball.alive = False
Ball.killed += 1 Ball.killed += 1
return Ball.alive return Ball.alive
class Ship: class Ship:
def __init__(self, x, y): def __init__(self, x, y):
self.x = x self.x = x
@ -90,14 +90,14 @@ class Ship:
self.v = 4 self.v = 4
self.last = 1 self.last = 1
self.update() self.update()
def update(self): def update(self):
grid[self.y + 1] = ( grid[self.y + 1] = (
[ None ] * (self.x - self.hw + 1) + [ None ] * (self.x - self.hw + 1) +
[ self ] * (self.hw * 2 + 1) + [ self ] * (self.hw * 2 + 1) +
[ None ] * (width - self.x - self.hw) [ None ] * (width - self.x - self.hw)
) )
def collide(self, ball): def collide(self, ball):
ball.vy = -1 ball.vy = -1
if ball.x > self.x + self.hw / 2: if ball.x > self.x + self.hw / 2:
@ -105,7 +105,7 @@ class Ship:
elif ball.x < self.x - self.hw / 2: elif ball.x < self.x - self.hw / 2:
ball.vx = -1 ball.vx = -1
return True return True
def shift(self, i): def shift(self, i):
self.last = i self.last = i
self.x += self.v * i self.x += self.v * i
@ -114,11 +114,11 @@ class Ship:
elif self.x + self.hw >= width: elif self.x + self.hw >= width:
self.x = width - self.hw - 1 self.x = width - self.hw - 1
self.update() self.update()
def spawn(self): def spawn(self):
if not Ball.alive: if not Ball.alive:
entities.append(Ball(self.x, self.y - 1, self.last, -1)) entities.append(Ball(self.x, self.y - 1, self.last, -1))
def tick(self, win): def tick(self, win):
if not Ball.alive: if not Ball.alive:
win.addch(self.y - 1, self.x, 'O') win.addch(self.y - 1, self.x, 'O')
@ -127,21 +127,26 @@ class Ship:
win.addch(curses.ACS_HLINE) win.addch(curses.ACS_HLINE)
win.addch(curses.ACS_RTEE) win.addch(curses.ACS_RTEE)
return True return True
def main(stdscr): def main(stdscr):
global height, width, ship global height, width, ship
for i in range(1, 8): for i in range(1, 8):
curses.init_pair(i, i, 0) curses.init_pair(i, i, 0)
curses.curs_set(0) curses.curs_set(0)
curses.raw() curses.raw()
height, width = stdscr.getmaxyx() height, width = stdscr.getmaxyx()
if height < 15 or width < 30:
return 1
status = curses.newwin(1, width, 0, 0) status = curses.newwin(1, width, 0, 0)
height -= 1 height -= 1
game = curses.newwin(height, width, 1, 0) game = curses.newwin(height, width, 1, 0)
game.nodelay(1) game.nodelay(1)
game.keypad(1) game.keypad(1)
grid[:] = [ [ None for x in range(width + 2) ] for y in range(height + 2) ] grid[:] = [ [ None for x in range(width + 2) ] for y in range(height + 2) ]
wall = Wall() wall = Wall()
for x in range(width + 2): for x in range(width + 2):
@ -150,13 +155,13 @@ def main(stdscr):
grid[y][0] = grid[y][-1] = wall grid[y][0] = grid[y][-1] = wall
ship = Ship(width / 2, height - 5) ship = Ship(width / 2, height - 5)
entities.append(ship) entities.append(ship)
colors = [ 1, 3, 2, 6, 4, 5 ] colors = [ 1, 3, 2, 6, 4, 5 ]
h = height / 10 h = height / 10
for x in range(1, width / 7 - 1): for x in range(1, width / 7 - 1):
for y in range(1, 7): for y in range(1, 7):
entities.append(Block(x * 7, y * h + x / 2 % 2, 7, h, colors[y - 1])) entities.append(Block(x * 7, y * h + x / 2 % 2, 7, h, colors[y - 1]))
while True: while True:
while select.select([ sys.stdin ], [], [], 0)[0]: while select.select([ sys.stdin ], [], [], 0)[0]:
key = game.getch() key = game.getch()
@ -168,11 +173,11 @@ def main(stdscr):
ship.spawn() ship.spawn()
elif key == 0x1b or key == 3 or key == ord('q') or key == ord('Q'): elif key == 0x1b or key == 3 or key == ord('q') or key == ord('Q'):
return return
game.resize(height, width) game.resize(height, width)
game.erase() game.erase()
entities[:] = [ ent for ent in entities if ent.tick(game) ] entities[:] = [ ent for ent in entities if ent.tick(game) ]
status.hline(0, 0, curses.ACS_HLINE, width) status.hline(0, 0, curses.ACS_HLINE, width)
status.addch(0, 2, curses.ACS_RTEE) status.addch(0, 2, curses.ACS_RTEE)
status.addstr(' SCORE: ', curses.A_BOLD | curses.color_pair(4)) status.addstr(' SCORE: ', curses.A_BOLD | curses.color_pair(4))
@ -181,7 +186,7 @@ def main(stdscr):
status.addstr(' DEATHS: ', curses.A_BOLD | curses.color_pair(4)) status.addstr(' DEATHS: ', curses.A_BOLD | curses.color_pair(4))
status.addstr('%s ' % Ball.killed, curses.A_BOLD) status.addstr('%s ' % Ball.killed, curses.A_BOLD)
status.addch(curses.ACS_LTEE) status.addch(curses.ACS_LTEE)
if Block.killed == Block.total: if Block.killed == Block.total:
message = ' A WINNER IS YOU!! ' message = ' A WINNER IS YOU!! '
i = int(time.time() / 0.8) i = int(time.time() / 0.8)
@ -192,11 +197,16 @@ def main(stdscr):
curses.A_BOLD | curses.color_pair(colors[y])) curses.A_BOLD | curses.color_pair(colors[y]))
game.addstr(height / 2, (width - len(message)) / 2, message, game.addstr(height / 2, (width - len(message)) / 2, message,
curses.A_BOLD | curses.color_pair(7)) curses.A_BOLD | curses.color_pair(7))
game.refresh() game.refresh()
status.refresh() status.refresh()
time.sleep(0.05) time.sleep(0.05)
curses.wrapper(main) res = curses.wrapper(main)
print ('You destroyed %s blocks out of %s with %s deaths.' % if res == 1:
(Block.killed, Block.total, Ball.killed)) print ("Your computer is not powerful enough to run 'arc anoid'. "
"It must support at least 30 columns and 15 rows of next-gen "
"full-color 3D graphics.")
else:
print ('You destroyed %s blocks out of %s with %s deaths.' %
(Block.killed, Block.total, Ball.killed))