Skip to content

Instantly share code, notes, and snippets.

@crawsible
Created May 24, 2014 15:11
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 crawsible/5a90126006591a2201ec to your computer and use it in GitHub Desktop.
Save crawsible/5a90126006591a2201ec to your computer and use it in GitHub Desktop.
A command-line game written without any libraries or frameworks beyond those included in the python install.
#!/usr/bin/python3
import time
from random import randint
from subprocess import call
DIRECTION_ICONS = ["∩", "⊂", "∪", "⊃"]
DIRECTION_ICONS_ACTIVE = ["▲", "◀︎", "▼", "▶︎"]
SIZE = 19
class Ship(object):
def __init__(self, location, direction):
self.location = location
self.direction = direction
self.repaired = False
def turn(self, direction_input):
if direction_input == "right":
self.direction -= 1
elif direction_input == "left":
self.direction += 1
if self.direction < 0:
self.direction += 4
elif self.direction > 3:
self.direction -= 4
def advance(self, distance):
_distance = distance
if self.direction < 2:
_distance = -_distance
if (self.direction == 0) or (self.direction == 2):
self.location[0] += _distance
else:
self.location[1] += _distance
self.location = self.location_check(self.location)
def location_check(self, location):
clean_location = location
for i in range(2):
if clean_location[i] > SIZE - 1:
clean_location[i] -= SIZE
elif clean_location[i] < 0:
clean_location[i] += SIZE
return clean_location
class Fleet(object):
def __init__(self, fleet_size):
self.ships = []
ship_properties = []
while len(ship_properties) < fleet_size:
location = [randint(1, SIZE) - 1, randint(1, SIZE) - 1]
direction = randint(0, 3)
property_pair = [location, direction]
if not property_pair in ship_properties:
ship_properties.append(property_pair)
for property_pair in ship_properties:
self.ships.append(Ship(property_pair[0], property_pair[1]))
class Game(object):
def __init__(self, fleet_size):
self.fleet = Fleet(fleet_size)
self.active_ship = self.fleet.ships[0]
intro = input("Watch intro? (Y/n): ").lower()
if not ((intro == "n") or (intro == "no")):
self.intro()
self.update()
def intro(self):
fleet_size = len(self.fleet.ships)
fleet_left = int(fleet_size / 2)
fleet_right = fleet_size - fleet_left - 1
print("You are the PILOT of a STARSHIP.")
print((fleet_left * " ") + "▲︎ ")
time.sleep(6)
print("The FLEET you are a part of has barely survived an ATTACK.")
print((fleet_left * "∩ ") + "▲︎ " + (fleet_right * "∩ "))
time.sleep(6)
print("The aggressors are returning.")
print(SIZE * "+ ")
time.sleep(6)
print("YOUR ship has the only functioning WARP DRIVE.")
print((fleet_left * " ") + "▲︎ ")
time.sleep(6)
print("Position your ship next to and parallel to a FLEETMATE.")
print(((fleet_left - 1) * " ") + "︎︎︎︎︎︎︎▶︎ " + ". " "∩ ")
time.sleep(4)
print(((fleet_left) * " ") + "▶︎︎ " + "∩ ")
time.sleep(2)
print(((fleet_left) * " ") + "︎▲︎ " + "∩ ")
time.sleep(4)
print("WARPSHARE will put your friend out of harm's way.")
print(((fleet_left) * " ") + "︎▲︎ ")
time.sleep(6)
print("Save as many of your friends' lives as possible.")
time.sleep(3)
print("Your own safety is not important to you.")
time.sleep(6)
def refresh_gamespace(self):
self.gamespace = [["." for i in range(SIZE)] for j in range(SIZE)]
def ship_icon(self, ship):
if ship == self.active_ship:
return DIRECTION_ICONS_ACTIVE[ship.direction]
else:
return DIRECTION_ICONS[ship.direction]
def update(self):
self.refresh_gamespace()
for ship in self.fleet.ships:
loc = ship.location
self.gamespace[loc[0]][loc[1]] = self.ship_icon(ship)
game = Game(9)
fleet = game.fleet
active_ship = 0
# The "Game Loop"
while True:
call("clear")
for row in game.gamespace:
print(" ".join(row))
ship = fleet.ships[active_ship]
action = input("TURN, ADVANCE, or WARPSHARE: ").lower()
if action == "turn":
direction = input("RIGHT or LEFT: ")
ship.turn(direction)
elif action == "advance":
distance = int(input("Distance (1-5): "))
if (distance > 0) and (distance <= 5):
ship.advance(distance)
game.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment