Help with two issues, buttons and second class object

Thomas Grops twgrops at googlemail.com
Thu Nov 24 07:42:06 EST 2016


Hi I have created some code, which moves a rectangle around and when it hits the edge it picks a random new direction. It does this by the count function within my class. I am wanting to create a button to randomly change count but I my class seems to be getting errors.

I also wanted to create a second class object tank2 but it doesn't seem to move when I create it with tank2.Tank(x,y,vx,vy) and call tank1.move()

Can anyone help me many thanks.

from tkinter import *
import time
import random

#Properties for the tank
class Tank():
    
    

    #life, speed, starting position, vectors, size of tank
    def __init__(self, x, y, vx, vy):
        self.__life=10
        #self.canvas=canvas
        self.speed = 1
        self.x = x
        self.y = y
        self.vx = vx
        self.vy = vy
        self.count=0
        #self.w = tank.width()
        #self.h = tank.height()
        self.id = canvas.create_rectangle(x,y,vx,vy, fill='green')
        #Create left button and call function to print left
        '''buttonL=Button(canvas,text='Change Direction', command=self.changeDirection())
        buttonL.pack(side=BOTTOM, anchor=SW)'''
        #print(self.x)
    #tank attacked
    def attack(self):
        print('ouch!')
        self.__life -= 1

    #check life
    def checkLife(self):
        if self.__life <= 0:
            print('dead')
        else:
            print(str(self.__life) + " life left")

    #respawn at starting point
    #def respawn(self):
    

    #medic pack
    def medic(self):
        self.__life += 5

    #move directions
    def right(self):
        canvas.move(self.id,+5,0)#move right
        
        #reposition x,vx,y,vy values
        self.x+=5
        self.vx+=5

        #Update canvas
        canvas.update()
        time.sleep(0.1)
        
    def left(self):
        canvas.move(self.id,-5,0)#move left
        
        #reposition x,vx,y,vy values
        self.x+=-5
        self.vx+=-5

        #Update canvas
        canvas.update()
        time.sleep(0.1)

    def up(self):
        canvas.move(self.id,0,-2)#move up
        
        #reposition x,vx,y,vy values
        self.y+=-2
        self.vy+=-2

        #Update canvas
        canvas.update()
        time.sleep(0.1)

    def down(self):
        canvas.move(self.id,0,+2)#move down
        
        #reposition x,vx,y,vy values
        self.y+=2
        self.vy+=2

        #Update canvas
        canvas.update()
        time.sleep(0.1)

    def upLeft(self):
        canvas.move(self.id,-1,-1)#move upLeft
        
        #reposition x,vx,y,vy values
        self.y+=-1
        self.vy+=-1
        self.x+=-1
        self.vx+=-1

        #Update canvas
        canvas.update()
        time.sleep(0.1)

    def upRight(self):
        canvas.move(self.id,+1,-1)#move upRight
        
        #reposition x,vx,y,vy values
        self.y+=-1
        self.vx+=1
        self.vy+=-1
        self.x+=1
        
        #Update canvas
        canvas.update()
        time.sleep(0.1)

    def downLeft(self):
        canvas.move(self.id,-1,+1)#move downLeft

        #reposition x,vx,y,vy values
        self.x+=-1
        self.vx+=-1
        self.y+=1
        self.vy+=1
        
        #Update canvas
        canvas.update()
        time.sleep(0.1)

    def downRight(self):
        #move downRight
        canvas.move(self.id,+1,+1)

        #reposition x,vx,y,vy values
        self.x+=1
        self.vx+=1
        self.y+=1
        self.vy+=1
        
        #Update canvas
        canvas.update()
        time.sleep(0.1)

    

    def count(self,count):
        #Count triggers direction of movement
        self.count = count
        print (count)



    #movement
    def move(self):
        
        # Loop for steps in movement
        for t in range(1, 10000):
            #Move direction depending on count value
            if self.count==0:
                self.left() 
            if self.count==1:
                self.right()
            if self.count==2:
                self.up()
            if self.count==3:
                self.down()
            if self.count==4:
                self.upLeft()
            if self.count==5:
                self.upRight()
            if self.count==6:
                self.downRight()
            if self.count==7:
                self.downLeft()

         # If a boundary has been crossed, pick a direction randomly

            #Left border
            if self.x <= 0:
                #banned directions
                excludedNumbers = [0,4,7]
                #define random integer to be selected
                randomNumber = random.randint(0,8)
                #nested while loop so that the banned directions are not selected
                while randomNumber in excludedNumbers:
                    randomNumber = random.randint(0,8)
                #feed allowed random direction back to the count
                self.count=randomNumber

            #Right border
            elif self.vx >= 1000:
                #banned directions
                excludedNumbers = [1,5,6]
                #define random integer to be selected
                randomNumber = random.randint(0,8)
                #nested while loop so that the banned directions are not selected
                while randomNumber in excludedNumbers:
                    randomNumber = random.randint(0,8)
                #feed allowed random direction back to the count
                self.count=randomNumber
    
            #Top border
            elif self.y <= 0:
                #banned directions
                excludedNumbers = [2,4,5]
                #define random integer to be selected
                randomNumber = random.randint(0,8)
                #nested while loop so that the banned directions are not selected
                while randomNumber in excludedNumbers:
                    randomNumber = random.randint(0,8)
                #feed allowed random direction back to the count
                self.count=randomNumber

            #Bottom border
            elif self.vy >= 700:
                #banned directions
                excludedNumbers = [3,6,7]
                #define random integer to be selected
                randomNumber = random.randint(0,8)
                #nested while loop so that the banned directions are not selected
                while randomNumber in excludedNumbers:
                    randomNumber = random.randint(0,8)
                #feed allowed random direction back to the count
                self.count=randomNumber

    '''def changeDirection():
            randomNumber=random.randint(0,8)
            self.count=randomNumber'''



'class Wall(object):'
    
#Main=======================================================================================================
main = Tk() 

#Canvas size
WIDTH=1000
HEIGHT=700
    
    
'''def changeDirection():
    test=Tank()
    test.count()
    randomNumber=random.randint(0,8)
    tank1.count=randomNumber'''

'''# The velocity, or distance moved per time step
vx = 10.0    # x velocity
vy = 5.0    # y velocity

# Boundaries
x_min = 0.0
y_min = 0.0
x_max = HEIGHT
y_max = WIDTH'''
    
#Show canvas
canvas = Canvas(main,width=WIDTH, height=HEIGHT, bg='khaki')

canvas.pack()

#Create a tank
tank1= Tank(950,650,950+40,650+40)


#testing features
'''tank1.attack()
tank1.attack()
tank1.checkLife()
tank1.medic()
tank1.checkLife()
tank1.move()'''

tank1.move()

   
      
canvas.pack(padx=10,pady=10)

#Complete the GUI
main.mainloop()



More information about the Python-list mailing list