Advertisement
here2share

# find_overlapping_game_demo.py

Dec 13th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. # find_overlapping_game_demo.py
  2.  
  3. from Tkinter import*
  4. import random
  5. import time
  6.  
  7. root=Tk()
  8.  
  9. speed=sp=50
  10. x=0
  11. y=700
  12. champ=' YOU HAVE LOST !!! '
  13. bsp=-30
  14. def mainfire():
  15.     global bullet
  16.     global c1
  17.     global c2
  18.     c1=canvas.coords(mainchar)[0]+50
  19.     c2=canvas.coords(mainchar)[1]
  20.     canvas.coords(bullet,c1,c2,c1+50,c2+50)
  21. def mouse(event):
  22.     mainfire()  
  23. def key(event):
  24.     if event.char.lower() == "a":
  25.         canvas.move(mainchar,0,-50)
  26.     elif event.char.lower() == "z":
  27.         canvas.move(mainchar,0,50)
  28.     if event.char.lower() == " ":
  29.         mainfire()
  30. w,h=660,660
  31. canvas=Canvas(root,width=w,height=h)
  32. canvas.bind("<Key>",key)
  33. canvas.bind("<Button-1>",mouse)
  34. canvas.focus_set()
  35. canvas.pack()
  36. mainchar=canvas.create_rectangle(0,400,40,500,fill="blue",outline="red")
  37. badguy=canvas.create_rectangle(600,600,640,700,fill="red",outline="blue")
  38. bullet=canvas.create_oval(0,0,0,0,fill="green")
  39. bullet2=canvas.create_oval(0,50,0,0,fill="green")
  40. canvas.move(mainchar,10,0)
  41. z=123
  42. def movebullet():
  43.     global champ,z
  44.     root.after(1,movebullet)
  45.     canvas.move(bullet,10,0)
  46.     c1,c2,c3,c4=canvas.coords(bullet)
  47.     if 2 in canvas.find_overlapping(c1,c2,c3,c4):
  48.         root.destroy()
  49.         champ=' YOU HAVE WON !!! '
  50. movebullet()
  51. def movebullet2():
  52.     if canvas.coords(bullet2)[0] < 0:
  53.         badguybullet()
  54.     else:
  55.         root.after(100,movebullet2)
  56.         canvas.move(bullet2,bsp,0)
  57.         c1,c2,c3,c4=canvas.coords(bullet2)
  58.         if 1 in canvas.find_overlapping(c1,c2,c3,c4):
  59.             root.destroy()
  60. movebullet2()  
  61. def badguybullet():
  62.     root.after(500,movebullet2)
  63.     c1=canvas.coords(badguy)[0]-50
  64.     c2=canvas.coords(badguy)[1]
  65.     canvas.coords(bullet2,c1,c2,c1+50,c2+50)
  66.     canvas.move(bullet2,0,0)
  67. badguybullet() 
  68. root.geometry('%sx%s+0+0' %(w,h))
  69. def movebadguy():
  70.     global sp
  71.     y=canvas.coords(badguy)[1]
  72.     if y < 100:
  73.         sp=speed
  74.     elif y > 500:
  75.         sp=-speed
  76.     canvas.move(badguy,0,sp)
  77.     root.after(200,movebadguy)
  78. #
  79.  
  80. root.after(200,movebadguy)
  81. root.mainloop()
  82.  
  83. moot=Tk()
  84. label= Label(moot,text=champ,font=("none",50))
  85. label.pack()
  86. moot.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement