By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,400 Members | 1,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,400 IT Pros & Developers. It's quick & easy.

int object has no attribute 'destroy'...why is this happening? Please help

P: 1
Expand|Select|Wrap|Line Numbers
  1. from tkinter import *
  2. import time
  3. import random
  4.  
  5. # Set up Root
  6. root=Tk()
  7. root.geometry("1200x1000")
  8. root.title("Aliens Attack!!")
  9.  
  10. # Set up Global variables
  11.  
  12. global score
  13. score=0
  14.  
  15. global lives
  16. lives=3
  17.  
  18. global myRandom1
  19. myRandom1=150
  20.  
  21. global myRandom2
  22. myRandom2=300
  23.  
  24. global alienNumber
  25. alienNumber=2
  26.  
  27.  
  28. # Set up canvases.
  29.  
  30. top=Canvas(root,width=1200,height=50, bg="black")
  31. top.grid(column=0,row=0)
  32.  
  33. playground=Canvas(root,width=1200,height=800,bg='white')
  34. playground.grid(column=0,row=1)
  35.  
  36. userInterface=Canvas(root,width=1200,height=150, bg="black")
  37. userInterface.grid(column=0,row=2)
  38.  
  39. # top Canvas Parameters
  40.  
  41. scoreLabel=Label(top,text="Score: " + str(score), bg="green", fg="white",font=(50))
  42. scoreLabel.place(x=50,y=15)
  43.  
  44. livesLabel=Label(top, text="Lives Left: " + str(lives), bg="red", fg="white",font=(50))
  45. livesLabel.place(x=1040, y=15)
  46.  
  47. # playground Canvas Parameters
  48.  
  49. avoidLine=playground.create_line(0,800,1200,800,fill="red")
  50.  
  51. shipImage=PhotoImage(file="Ship.png")
  52. myShip=playground.create_image(600,530,image=shipImage)
  53.  
  54. alienImage=PhotoImage(file="MyAku2.png")
  55. Alien=playground.create_image(600,100,image=alienImage)
  56.  
  57. Alien2=playground.create_image(400,100,image=alienImage)
  58.  
  59. #   Bottom Canvas Parameters
  60.  
  61. #   Functions programmed here....
  62.  
  63. # Death of the Ship
  64.  
  65. def shipDies():
  66.     global lives
  67.     lives -=1
  68.     livesLabel.config(text="Lives Left: " + str(lives))
  69.     playground(myShip.destroy)
  70.  
  71.  
  72.  
  73.  
  74. #   Detects Collision between the ship and alien
  75.  
  76. def collisionDetection():
  77.     sb=playground.bbox(myShip)
  78.     ab=playground.bbox(Alien)
  79.     # coming from Right Hand Side
  80.     if ab[3]>sb[1] and ab[0]>sb[0]< ab[2]:
  81.         shipDies()
  82.     # coming from left hand side
  83.     if ab[0]<sb[0]<ab[2] and ab[1] <sb[1]<ab[3]:
  84.         shipDies()
  85.     # coming from left below
  86.     if ab[1]<sb[2]<ab[3] and ab[0] <sb[3]<ab[2]:
  87.         shipDies()
  88.     # coming from right below
  89.     if ab[1]<sb[0]<ab[3] and ab[0] <sb[3]<ab[2]:
  90.         shipDies()
  91.  
  92. #   Aliens Move:
  93.  
  94.  
  95.  
  96.  
  97. def AlienMove2():
  98.     global myRandom1
  99.     global myRandom2
  100.     global score
  101.     myMove=random.randint(myRandom1,myRandom2)
  102.     playground.move(Alien2,10,5)
  103.     playground.move(Alien2,-10,5)
  104.     myX=random.randint(-20,20)
  105.     myY=random.randint(5,15)
  106.     playground.move(Alien2,myX,myY)
  107.     MyCoords=playground.bbox(Alien2)
  108.     if MyCoords[1]>800:
  109.         score +=1
  110.         scoreLabel.config(text= "Score: " + str(score))
  111.         playground.coords(Alien2,600,50)
  112.     root.after(myMove,AlienMove2)
  113.  
  114. def AlienMove():
  115.     global myRandom1
  116.     global myRandom2
  117.     global score
  118.     myMove=random.randint(myRandom1,myRandom2)
  119.     playground.move(Alien,10,5)
  120.     playground.move(Alien,-10,5)
  121.     myX=random.randint(-20,20)
  122.     myY=random.randint(5,15)
  123.     playground.move(Alien,myX,myY)
  124.     MyCoords=playground.bbox(Alien)
  125.     if MyCoords[1]>800:
  126.         score +=1
  127.         scoreLabel.config(text= "Score: " + str(score))
  128.         playground.coords(Alien,600,50)
  129.     root.after(myMove,AlienMove)
  130.  
  131.  
  132. #   Key Ship Movements:
  133.  
  134. #   Right
  135.  
  136. def moveRight(event):
  137.  
  138.     randRight=random.randint(1,8)
  139.     playground.move(myShip,10,0)
  140.     playground.move(Alien,randRight,0)
  141.     collisionDetection()
  142.  
  143. #   Left
  144.  
  145. def moveLeft(event):
  146.     playground.move(myShip,-10,0)
  147.     randLeft=random.randint(1,8)
  148.     playground.move(Alien,-randLeft,0)
  149.     collisionDetection()
  150.  
  151. #   Up
  152.  
  153. def moveUp(event):
  154.     playground.move(myShip,0,-10)
  155.     collisionDetection()
  156.  
  157. #   Down
  158.  
  159. def moveDown(event):
  160.     playground.move(myShip,0,10)
  161.     randToward=random.randint(1,5)
  162.     playground.move(Alien,0,randToward)
  163.     collisionDetection()
  164.  
  165.  
  166.  
  167. #   Key Bindings
  168.  
  169. playground.bind_all("<Right>", moveRight)
  170. playground.bind_all("<Left>", moveLeft)
  171. playground.bind_all("<Up>", moveUp)       
  172. playground.bind_all("<Down>", moveDown)
  173.  
  174.  
  175. AlienMove()
  176. timing=random.randint(1000,3000)
  177. root.after(timing, AlienMove2)
  178. root.mainloop()
Attached Images
File Type: png Ship.png (3.6 KB, 15 views)
File Type: png MyAku2.png (2.4 KB, 14 views)
Oct 6 '20 #1
Share this Question
Share on Google+
1 Reply

Rabbit
Expert Mod 10K+
P: 12,441
It's all in the error message. You're trying to access something called destroy on an object. But that object has nothing called destroy.
Oct 6 '20 #2

Post your reply

Sign in to post your reply or Sign up for a free account.