Skip to content

Instantly share code, notes, and snippets.

@knutae
Created October 26, 2014 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knutae/5b19b11b29ca31da5931 to your computer and use it in GitHub Desktop.
Save knutae/5b19b11b29ca31da5931 to your computer and use it in GitHub Desktop.
GAME
#!/usr/bin/env python
SCREEN_HEIGHT = 10
SCREEN_WIDTH = 10
WALL='##'
PLAYER='@@'
GOAL='<>'
BOULDER='()'
EMPTY=' '
class Level:
def __init__(self):
self.objects = [[EMPTY for x in range(SCREEN_WIDTH)] for y in range(SCREEN_HEIGHT)]
self.solved = False
def draw(self):
print('')
print(WALL * (SCREEN_WIDTH+2))
for y in range(SCREEN_HEIGHT):
line = WALL
for x in range(SCREEN_WIDTH):
line += self.objects[y][x]
line += WALL
print(line)
print(WALL * (SCREEN_WIDTH+2))
print('')
def move_left(self):
for row in self.objects:
for x in range(SCREEN_WIDTH):
if row[x] in [PLAYER, BOULDER]:
obj = row[x]
dest = x
for c in range(x-1,-1,-1):
self.solved = (obj == PLAYER and row[c] == GOAL)
if row[c] in [EMPTY]:
dest = c
else:
break
row[x] = EMPTY
row[dest] = obj
def move_right(self):
for row in self.objects:
for x in range(SCREEN_WIDTH-1,-1,-1):
if row[x] in [PLAYER, BOULDER]:
obj = row[x]
dest = x
for c in range(x+1,SCREEN_WIDTH):
self.solved = (obj == PLAYER and row[c] == GOAL)
if row[c] in [EMPTY]:
dest = c
else:
break
row[x] = EMPTY
row[dest] = obj
def move_up(self):
for x in range(SCREEN_WIDTH):
for y in range(SCREEN_HEIGHT):
if self.objects[y][x] in [PLAYER,BOULDER]:
obj = self.objects[y][x]
dest = y
for c in range(y-1,-1,-1):
self.solved = (obj == PLAYER and self.objects[c][x] == GOAL)
if self.objects[c][x] in [EMPTY]:
dest = c
else:
break
self.objects[y][x] = EMPTY
self.objects[dest][x] = obj
def move_down(self):
for x in range(SCREEN_WIDTH):
for y in range(SCREEN_HEIGHT-1,-1,-1):
if self.objects[y][x] in [PLAYER,BOULDER]:
obj = self.objects[y][x]
dest = y
for c in range(y+1,SCREEN_HEIGHT):
self.solved = (obj == PLAYER and self.objects[c][x] == GOAL)
if self.objects[c][x] in [EMPTY]:
dest = c
else:
break
self.objects[y][x] = EMPTY
self.objects[dest][x] = obj
def loop(level):
level.draw()
print('Guide player ' + PLAYER + ' to goal ' + GOAL + '. WASD (+ enter) to move.')
while not level.solved:
c = raw_input().strip().lower()
if c == 'a':
level.move_left()
elif c == 'w':
level.move_up()
elif c == 's':
level.move_down()
elif c == 'd':
level.move_right()
else:
print('No! WASD!')
level.draw()
print('SOLVED!!!')
def level1():
level = Level()
level.objects[0][0] = GOAL
level.objects[5][5] = PLAYER
return level
def level2():
level = Level()
level.objects[1][1] = GOAL
level.objects[0][0] = PLAYER
level.objects[0][9] = WALL
return level
def level3():
level = Level()
level.objects[1][1] = GOAL
level.objects[5][5] = PLAYER
level.objects[3][3] = BOULDER
return level
def level4():
level = Level()
level.objects[1][1] = GOAL
level.objects[0][0] = PLAYER
level.objects[0][5] = WALL
level.objects[0][7] = WALL
level.objects[1][5] = WALL
level.objects[3][5] = WALL
level.objects[4][9] = WALL
level.objects[5][6] = WALL
level.objects[6][6] = WALL
level.objects[8][0] = WALL
level.objects[9][8] = WALL
return level
def main_loop():
loop(level1())
loop(level2())
loop(level3())
loop(level4())
print('Yay, you won in 0-ish hours!')
if __name__ == '__main__':
main_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment