Hey guys,
I am creating a new mini battle tanks game in python.
Not gui based as of now.
What it does so far is show a tank moving around a grid size of -10 to 10
Currently i have set it up as this far, just defined the coordinates method (determineCoords)
in main.
But for some reason it does not work;
the code of the entire program is as follows if anyone can help me, i would appreciate it!
import random
gridSize = 10
class Die(object):
"""The die class, generates the random number for amount of spaces to move"""
def Roll(self):
global move
move = random.randrange(1, 7)
return move
class Direction(object):
"""The direction class, dictionary with values of up, down, left and right. And a Random number generator to assign one of these values"""
def direct_Roll(self):
global way
direction = {1: 'Up',
2: 'Down',
3: 'Left',
4: 'Right'}
ranDir = random.randrange(1, 5)
way = direction[ranDir]
return way
class Tank(object):
"""The tank class, all getters, setters and variables to show a functioning tank in the program."""
def __init__(self, xpos = 0, ypos = 0, armour = 10, firepower = 1):
self._xpos = xpos # Setting The Variables
self._ypos = ypos
self._armour = armour
self._firepower = firepower
# Defining the Getter Methods
def get_xpos(self):
return self._xpos
def get_ypos(self):
return self._ypos
def get_armour(self):
return self._armour
def get_firepower(self):
return self._firepower
# Defining the Setter Methods and property Attributes
def set_xpos(self, Side):
self._xpos = self._xpos + Side
xpos = property(get_xpos, set_xpos)
def set_ypos(self, topBottom):
self._ypos = self._ypos + topBottom
ypos = property(get_ypos, set_ypos)
def set_armour(self, determine):
self._armour = self._armour + determine
armour = property(get_armour, set_armour)
def set_firepower(self, strength):
self._firepower = self._firepower + strength
firepower = property(get_firepower, set_firepower)
# Defining the main program and the while/ if loops
def main():
tankOne = Tank()
def determineCoords(Tank):
if tankOne.get_xpos() > gridSize:
tankOne.set_xpos = (2 * gridSize - tankOne.get_xpos())
elif tankOne.get_xpos() < -gridSize:
tankOne.set_xpos = (-2 * gridSize - tankOne.get_xpos())
elif tankOne.get_ypos() > gridSize:
tankOne.set_ypos = (2 * gridSize - tankOne.get_ypos())
elif tankOne.get_ypos() < -gridSize:
tankOne.set_ypos = (-2 * gridSize - tankOne.get_ypos())
counter = 1 # While loop counter - starting at 1
print '\t\tRoll', '\tDirection', '\tX Pos', '\tY Pos', '\tArmour', '\tFirepower' # Sets up the column names in the table
# Initiating the while loop
determineCoords(tankOne)
while counter <= 50:
toMove = Die() # Calling Die class
whatDirection = Direction() # Calling the Direction Class
toMove.Roll() # Calling the Roll method in the Die class
whatDirection.direct_Roll() # Calling the direct_Roll method in the Direction class
# Initiating the if loop for use with xpos and ypos variables.
if way =='Up':
tankOne.ypos = move
elif way =='Down':
tankOne.ypos = -move
elif way =='Left':
tankOne.xpos = -move
elif way =='Right':
tankOne.xpos = move
print 'Tank 1-', '\t', move, '\t', way, '\t\t', tankOne.xpos, '\t', tankOne.ypos, '\t', tankOne.armour, '\t', tankOne.firepower
counter += 1
main()
A sample out put it as follows:
Tank 1- 4 Down -14 -3 10 1
Tank 1- 1 Up -14 -2 10 1
Tank 1- 4 Down -14 -6 10 1
Tank 1- 5 Down -14 -11 10 1
Tank 1- 3 Down -14 -14 10 1
Tank 1- 4 Right -10 -14 10 1
Tank 1- 2 Left -12 -14 10 1
Tank 1- 3 Left -15 -14 10 1
The two columns after down are xpos and ypos and as you can see they do go above and below 10. I need them to be on ten or below.... with the same sort of code, what am i doing wrong?
Thanks in advance
Shank