Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # find_overlapping_game_demo.py
- from Tkinter import*
- import random
- import time
- root=Tk()
- speed=sp=50
- x=0
- y=700
- champ=' YOU HAVE LOST !!! '
- bsp=-30
- def mainfire():
- global bullet
- global c1
- global c2
- c1=canvas.coords(mainchar)[0]+50
- c2=canvas.coords(mainchar)[1]
- canvas.coords(bullet,c1,c2,c1+50,c2+50)
- def mouse(event):
- mainfire()
- def key(event):
- if event.char.lower() == "a":
- canvas.move(mainchar,0,-50)
- elif event.char.lower() == "z":
- canvas.move(mainchar,0,50)
- if event.char.lower() == " ":
- mainfire()
- w,h=660,660
- canvas=Canvas(root,width=w,height=h)
- canvas.bind("<Key>",key)
- canvas.bind("<Button-1>",mouse)
- canvas.focus_set()
- canvas.pack()
- mainchar=canvas.create_rectangle(0,400,40,500,fill="blue",outline="red")
- badguy=canvas.create_rectangle(600,600,640,700,fill="red",outline="blue")
- bullet=canvas.create_oval(0,0,0,0,fill="green")
- bullet2=canvas.create_oval(0,50,0,0,fill="green")
- canvas.move(mainchar,10,0)
- z=123
- def movebullet():
- global champ,z
- root.after(1,movebullet)
- canvas.move(bullet,10,0)
- c1,c2,c3,c4=canvas.coords(bullet)
- if 2 in canvas.find_overlapping(c1,c2,c3,c4):
- root.destroy()
- champ=' YOU HAVE WON !!! '
- movebullet()
- def movebullet2():
- if canvas.coords(bullet2)[0] < 0:
- badguybullet()
- else:
- root.after(100,movebullet2)
- canvas.move(bullet2,bsp,0)
- c1,c2,c3,c4=canvas.coords(bullet2)
- if 1 in canvas.find_overlapping(c1,c2,c3,c4):
- root.destroy()
- movebullet2()
- def badguybullet():
- root.after(500,movebullet2)
- c1=canvas.coords(badguy)[0]-50
- c2=canvas.coords(badguy)[1]
- canvas.coords(bullet2,c1,c2,c1+50,c2+50)
- canvas.move(bullet2,0,0)
- badguybullet()
- root.geometry('%sx%s+0+0' %(w,h))
- def movebadguy():
- global sp
- y=canvas.coords(badguy)[1]
- if y < 100:
- sp=speed
- elif y > 500:
- sp=-speed
- canvas.move(badguy,0,sp)
- root.after(200,movebadguy)
- #
- root.after(200,movebadguy)
- root.mainloop()
- moot=Tk()
- label= Label(moot,text=champ,font=("none",50))
- label.pack()
- moot.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement