Main Page

Anomaly

*Main file:

*Imported modules:


import asciiGridEngine as AGE
	

More entries will appear here as I complete more modules. This line imports the grid drawing module and gives it the abreviated name 'AGE'.

*Ship class:


class Ship:
  def __init__(self, x, y):
    self.x = x
    self.y = y
    self.energy = 100
    self.hull = 100
    self.desc = ''        

usr = Ship(1, 1)
	

The ship class defines the statistics of a vessel. The energy and hull variables are currently not fuctional. Eventually this class will also contain weapon info, crew list, crew skill modifiers, and inventory. x and y determine the location of the vessel. Desc (for description) contains a description of the current location, passed from the Pos class.

*Position class:


class Pos:
  def __init__(self, x, y, desc):
    self.x = x
    self.y = y
    self.desc = desc
  def message(self):
    if self.x == usr.x and self.y == usr.y:
      print(self.desc)

pa = Pos(5, 5, 'Star System- Test')
pb = Pos(0, 0, '')
pc = Pos(0, 0, '')
pd = Pos(0, 0, '')
pe = Pos(0, 0, '')
pf = Pos(0, 0, '')
  

The Pos class defines the attributes of world objects such as starports and star-systems. There are a maximum of 6 world objects for each sector map. Pos.desc contains a short description of the location. The Pos.message() method will pass the description to the Ship class if the player's x/y equals the locations x/y. Later this class will contain missions, resources, characters, and combat encounters.

Each of the 6 world objects are defined just below the class. A position of 0, 0 makes the location inactive. The location 'pa' has an x/y position of 5, 5 and a description reading 'Star System- Test'.

*Main functions:


def message():
  pa.message()
  pb.message()
  pc.message()
  pd.message()
  pe.message()
  pf.message()

def getMove():
  key = input("Orders, Captain?  ")
  if key == "w":
    usr.y = usr.y + 1
  elif key == "d":
    usr.x = usr.x + 1
  elif key == "s":
    usr.y = usr.y - 1
  elif key == "a":
    usr.x = usr.x - 1
  else:
    print("Invalid input.")
    getMove()
	

The message() function opens the Pos.message() method for all 6 location slots. This updates the current message that will be passed to the Ship class. This function must be run each time the players ship moves to a new position.

The getMove() function promts the player to enter an indicated key. The w, a, s, d keys move the ship by adding or subtracting from it's x/y variables. usr is the name of the players Ship class instance. If the players input is not a valid option the function will print 'Invalid input.' and restart with getMove(). Later I will add input options for keys 1-7 as are indicated in the screenshots.

*Main loop:


def main():
  print("===========================")
  print("Alert: " + "Rest  ", "Shields: " + "Down")
  AGE.drawMap(usr.x, usr.y, pa.x, pa.y, pb.x, pb.y, pc.x, pc.y, pd.x, pd.y, pe.x, pe.y, pf.x, pf.y)
  print("Energy:", usr.energy, "% ", "Hull:", usr.hull, "%")
  print("===========================")
  message()
  getMove()
  main()
	

In the main() function we have the main game loop. The game-board is printed with rows of '=' to help separate each screen output in testing. Later I will import os commands to clear the screen before drawing a new one. I have made this work on Windows but the Linux version will require a little research. Next we also print the ships statistics, (several of these are just placeholders) using the Ship class.

Using AGE we can call upon the grid drawing module. By using .drawMap() we specify that we want to call that function. In this case that function contains the entire file, the reason for putting the file into the function is to allow us to pass the x/y variables for the ship and world objects to the module. Now we use the message() function to update the all the Pos class instances comparisons to the players x/y variables and print their messages if they are equal. Next the getMove() function gets input from the player and uses it to modify the ships location. Finally we use main() to repeat this process, creating the main loop.