I suspect this component is inefficient but it works and I can understand it so it will have to do for now. The main gameboard is a 9x9 grid. Beside the grid there are instruction prompts while above and below the grid there is ship data that is drawn in the main-loop component.
*x/y position class:
def drawMap(x, y, ax, ay, bx, by, cx, cy, dx, dy, ex, ey, fx, fy):
shipx = x
shipy = y
class Grid:
def __init__(self, x, y):
self.x = x
self.y = y
def d(self):
if shipx == self.x and shipy == self.y:
return "@"
elif ax == self.x and ay == self.y or ax == self.x and ay == self.y or
ax == self.x and ay == self.y or ax == self.x and ay == self or
ax == self.x and ay == self.y or ax == self.x and ay == self.y:
return "*"
else:
return " "
The entire module is contained within the drawMap() function. When calling this function from the main file the x and y positions of the ship from the main file are passed through to the shipx and shipy variables, followed by x and y values for up to 6 objects, labled a to f.
The 'd' (standing for draw) method determines the character that will be printed for the grid position. If the x/y variables for the ship equal that of the class instance, '@' is returned. If the instance x/y equal those of an object, '*' is returned. Otherwise a blank space is returned. Thus, whenever the Grid.d method is called, the class will check for changes before returning a character.
*Assigning class instances:
#row 1
a1 = Grid(1, 1)
a2 = Grid(1, 2)
a3 = Grid(1, 3)
a4 = Grid(1, 4)
a5 = Grid(1, 5)
a6 = Grid(1, 6)
a7 = Grid(1, 7)
a8 = Grid(1, 8)
a9 = Grid(1, 9)
#row 2
b1 = Grid(2, 1)
b2 = Grid(2, 2)
b3 = Grid(2, 3)
b4 = Grid(2, 4)
b5 = Grid(2, 5)
b6 = Grid(2, 6)
b7 = Grid(2, 7)
b8 = Grid(2, 8)
b9 = Grid(2, 9)
#row 3
c1 = Grid(3, 1)
c2 = Grid(3, 2)
c3 = Grid(3, 3)
c4 = Grid(3, 4)
c5 = Grid(3, 5)
c6 = Grid(3, 6)
c7 = Grid(3, 7)
c8 = Grid(3, 8)
c9 = Grid(3, 9)
#row 4
d1 = Grid(4, 1)
d2 = Grid(4, 2)
d3 = Grid(4, 3)
d4 = Grid(4, 4)
d5 = Grid(4, 5)
d6 = Grid(4, 6)
d7 = Grid(4, 7)
d8 = Grid(4, 8)
d9 = Grid(4, 9)
#row 5
e1 = Grid(5, 1)
e2 = Grid(5, 2)
e3 = Grid(5, 3)
e4 = Grid(5, 4)
e5 = Grid(5, 5)
e6 = Grid(5, 6)
e7 = Grid(5, 7)
e8 = Grid(5, 8)
e9 = Grid(5, 9)
#row 6
f1 = Grid(6, 1)
f2 = Grid(6, 2)
f3 = Grid(6, 3)
f4 = Grid(6, 4)
f5 = Grid(6, 5)
f6 = Grid(6, 6)
f7 = Grid(6, 7)
f8 = Grid(6, 8)
f9 = Grid(6, 9)
#row 7
g1 = Grid(7, 1)
g2 = Grid(7, 2)
g3 = Grid(7, 3)
g4 = Grid(7, 4)
g5 = Grid(7, 5)
g6 = Grid(7, 6)
g7 = Grid(7, 7)
g8 = Grid(7, 8)
g9 = Grid(7, 9)
#row 8
h1 = Grid(8, 1)
h2 = Grid(8, 2)
h3 = Grid(8, 3)
h4 = Grid(8, 4)
h5 = Grid(8, 5)
h6 = Grid(8, 6)
h7 = Grid(8, 7)
h8 = Grid(8, 8)
h9 = Grid(8, 9)
#row 9
i1 = Grid(9, 1)
i2 = Grid(9, 2)
i3 = Grid(9, 3)
i4 = Grid(9, 4)
i5 = Grid(9, 5)
i6 = Grid(9, 6)
i7 = Grid(9, 7)
i8 = Grid(9, 8)
i9 = Grid(9, 9)
The largest downside with this system is that I had to define all 81 x/y positions on the gameboard. There is probably a way to reduce the bulk of this code but I haven't found it.
As you can see, this adds around 100 lines to the file, so I will be looking into alternative solutions.
*Drawing the screen:
print("+---------+")
print("|" + a9.d() + b9.d() + c9.d() + d9.d() + e9.d() + f9.d() + g9.d() + h9.d() + i9.d() + "|"
" [1] Raise/Lower Shields")
print("|" + a8.d() + b8.d() + c8.d() + d8.d() + e8.d() + f8.d() + g8.d() + h8.d() + i8.d() + "|"
" [2] Raise/Lower Alert")
print("|" + a7.d() + b7.d() + c7.d() + d7.d() + e7.d() + f7.d() + g7.d() + h7.d() + i7.d() + "|"
" [3] Investigate Area")
print("|" + a6.d() + b6.d() + c6.d() + d6.d() + e6.d() + f6.d() + g6.d() + h6.d() + i6.d() + "|"
" [4] Ship Diagnostic")
print("|" + a5.d() + b5.d() + c5.d() + d5.d() + e5.d() + f5.d() + g5.d() + h5.d() + i5.d() + "|"
" [5] Senior Staff")
print("|" + a4.d() + b4.d() + c4.d() + d4.d() + e4.d() + f4.d() + g4.d() + h4.d() + i4.d() + "|"
+ " [6] Crew Roster")
print("|" + a3.d() + b3.d() + c3.d() + d3.d() + e3.d() + f3.d() + g3.d() + h3.d() + i3.d() + "|"
+ " [7] Access Databank")
print("|" + a2.d() + b2.d() + c2.d() + d2.d() + e2.d() + f2.d() + g2.d() + h2.d() + i2.d() + "|"
+ " [W][S]")
print("|" + a1.d() + b1.d() + c1.d() + d1.d() + e1.d() + f1.d() + g1.d() + h1.d() + i1.d() + "|"
+ " [A] <--- ^ |")
print("+---------+ [D] ---> | v")
Each class instance is printed in its indicated position. After each of the nine positions of each row, instructional prompts are printed.