Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from random import *
- root = Tk()
- canvas = Canvas(root, height=500, width=500, bg='black')
- canvas.pack()
- p1 = canvas.create_rectangle(10, 100, 0, 00, fill='white')
- canvas.move(p1, 10, 190)
- p2 = canvas.create_rectangle(10, 100, 0, 00, fill='white')
- canvas.move(p2, 485, 190)
- b = canvas.create_rectangle(10, 20, 0, 10, fill='white')
- canvas.move(b, 250, 250)
- def movementup(event):
- canvas.move(p1, 0, -20)
- def movementdown(event):
- canvas.move(p1, 0, 20)
- def bchangedirection():
- global directionx, directiony, aiturn, moves
- if directionx == 10:
- directionx = -10
- directiony = randint(-5, 5)
- aiturn = False
- print(directiony)
- moves = 0
- else:
- directionx = 10
- directiony = randint(-5, 5)
- aiturn = True
- print(directiony)
- moves = 0
- timebetween = 5
- timetaken = 0
- aiturn = True
- def check_loss():
- if timebetween < timetaken:
- print("LOSE")
- quit()
- def check_touching():
- global timetaken
- x1, y1, x2, y2 = canvas.coords(b)
- btouch = canvas.find_overlapping(x1, y1, x2, y2)
- check_loss()
- if p1 in btouch:
- bchangedirection()
- timetaken = 0
- if p2 in btouch:
- bchangedirection()
- timetaken = 0
- root.after(10, check_touching)
- touching = False
- moves = 0
- def bconstant():
- global touching, timetaken, moves
- if touching:
- print("T")
- else:
- canvas.move(b, directionx, directiony)
- timetaken += 0.10
- moves += 1
- root.after(10, bconstant)
- directionx = 10
- directiony = 0
- check_touching()
- bconstant()
- def ai():
- print("")
- def bmoverespond():
- if aiturn:
- if goup:
- canvas.move(p2, 0, 5)
- if godown:
- canvas.move(p2, 0, -5)
- root.after(10, bmoverespond)
- maptop = [0, 0, 0, 0]
- mapbottom = [500, 500, 500, 500]
- topstop = False
- bottomstop = False
- godown = False
- goup = False
- firstgo = True
- def bmove():
- global directiony, topstop, bottomstop, goup, godown, firstgo
- if canvas.coords(p2) == maptop:
- topstop = True
- if canvas.coords(p2) == mapbottom:
- bottomstop = True
- if aiturn:
- trajectorycoords = [directionx*46, directiony*46, directionx*46, directiony*46]
- if canvas.coords(b) > trajectorycoords:
- print("GO UP")
- godown = False
- goup = True
- if maptop == False:
- canvas.move(p2, 0, -10)
- if canvas.coords(b) < trajectorycoords:
- print("GO DOWN")
- goup = False
- godown = True
- if mapbottom == False:
- print("t")
- canvas.move(p2, 0, 10)
- root.after(10, bmove)
- bmoverespond()
- root.after(1000, bmove)
- root.bind("<Up>", movementup)
- root.bind("<Down>", movementdown)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement