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

when you set your x and y positions you need to check to if the new postion will be >10 or <1 if so it is an invalid move.

Please use [code] tags this will keep indenting and make it possible to actually read our code.

You need to then report this and take actions based around this.

Chris

when you set your x and y positions you need to check to if the new postion will be >10 or <1 if so it is an invalid move.

Please use [code] tags this will keep indenting and make it possible to actually read our code.

You need to then report this and take actions based around this.

Chris

Hi mate, im not 100% sure of what you mean, how would you do what you said ?

Cheers for the reply

I think he means, check if the tank's cords are less than -10 or greater than 10, and if so take action, like set it to the 10 or -10, maybe like so.

#positive numbers
if get_ypos > 10:
    set_ypos(10)
if get_xpos > 10:
    set_xpos(10)

#negitive numbers
if get_ypos < -10:
    set_ypos(-10)
if get_xpos < -10:
    set_xpos(-10)

Ah ok i get you, thanks for the reply.

But what i need to do is if it gets 5 right and its at say x pos = 9,

It goes to 10 and comes back to 6.... so it bounces back. Do you get what i mean?

again that is just a case of doing a simple check,

if x+z > 10:
   x += z
   x -= 10
   x = 10 - x
   set_xpos(x)

im sure there is a cleaner way but i can't see it right now.

Chris

Ok thanks mate i will try your way and see how it goes

Thanks for the quick replies mate, but that still does not seem to be working.

I used some print statements to make sure that the method is actually being accessed, and it is. So i dont know wht is going wrong.

put all of your code here in [ /code] tags so we can have a proper look

Chris[code=python][ /code] tags so we can have a proper look

Chris

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(tankOne):
        if tankOne.xpos > gridSize:
            tankOne.xpos = 2 * gridSize - tankOne.xpos
        
        elif tankOne.xpos < -gridSize:
            tankOne.xpos = -2 * gridSize - tankOne.xpos
        
        elif tankOne.ypos > gridSize:
            tankOne.ypos = 2 * gridSize - tankOne.ypos

        elif tankOne.ypos < -gridSize:
            tankOne.ypos = -2 * gridSize - tankOne.ypos

    determineCoords(tankOne)    

    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
    

    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()

I reset it to the original in case i messed it up more. I hope this is the way you intended the output, still new to this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.