Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. from tkinter import *
  2. from random import *
  3.  
  4. root = Tk()
  5. canvas = Canvas(root, height=500, width=500, bg='black')
  6. canvas.pack()
  7.  
  8. p1 = canvas.create_rectangle(10, 100, 0, 00, fill='white')
  9. canvas.move(p1, 10, 190)
  10.  
  11. p2 = canvas.create_rectangle(10, 100, 0, 00, fill='white')
  12. canvas.move(p2, 485, 190)
  13.  
  14. b = canvas.create_rectangle(10, 20, 0, 10, fill='white')
  15. canvas.move(b, 250, 250)
  16.  
  17. def movementup(event):
  18. canvas.move(p1, 0, -20)
  19.  
  20. def movementdown(event):
  21. canvas.move(p1, 0, 20)
  22.  
  23. def bchangedirection():
  24. global directionx, directiony, aiturn, moves
  25. if directionx == 10:
  26. directionx = -10
  27. directiony = randint(-5, 5)
  28. aiturn = False
  29. print(directiony)
  30. moves = 0
  31. else:
  32. directionx = 10
  33. directiony = randint(-5, 5)
  34. aiturn = True
  35. print(directiony)
  36. moves = 0
  37.  
  38.  
  39. timebetween = 5
  40. timetaken = 0
  41.  
  42. aiturn = True
  43.  
  44. def check_loss():
  45. if timebetween < timetaken:
  46. print("LOSE")
  47. quit()
  48.  
  49. def check_touching():
  50. global timetaken
  51. x1, y1, x2, y2 = canvas.coords(b)
  52. btouch = canvas.find_overlapping(x1, y1, x2, y2)
  53. check_loss()
  54. if p1 in btouch:
  55. bchangedirection()
  56. timetaken = 0
  57. if p2 in btouch:
  58. bchangedirection()
  59. timetaken = 0
  60. root.after(10, check_touching)
  61. touching = False
  62.  
  63. moves = 0
  64.  
  65. def bconstant():
  66. global touching, timetaken, moves
  67. if touching:
  68. print("T")
  69. else:
  70. canvas.move(b, directionx, directiony)
  71. timetaken += 0.10
  72. moves += 1
  73. root.after(10, bconstant)
  74.  
  75.  
  76. directionx = 10
  77. directiony = 0
  78.  
  79. check_touching()
  80. bconstant()
  81.  
  82. def ai():
  83. print("")
  84.  
  85. def bmoverespond():
  86. if aiturn:
  87. if goup:
  88. canvas.move(p2, 0, 5)
  89. if godown:
  90. canvas.move(p2, 0, -5)
  91. root.after(10, bmoverespond)
  92.  
  93. maptop = [0, 0, 0, 0]
  94. mapbottom = [500, 500, 500, 500]
  95.  
  96. topstop = False
  97. bottomstop = False
  98.  
  99. godown = False
  100. goup = False
  101.  
  102. firstgo = True
  103.  
  104. def bmove():
  105. global directiony, topstop, bottomstop, goup, godown, firstgo
  106. if canvas.coords(p2) == maptop:
  107. topstop = True
  108. if canvas.coords(p2) == mapbottom:
  109. bottomstop = True
  110. if aiturn:
  111. trajectorycoords = [directionx*46, directiony*46, directionx*46, directiony*46]
  112. if canvas.coords(b) > trajectorycoords:
  113. print("GO UP")
  114. godown = False
  115. goup = True
  116. if maptop == False:
  117. canvas.move(p2, 0, -10)
  118. if canvas.coords(b) < trajectorycoords:
  119. print("GO DOWN")
  120. goup = False
  121. godown = True
  122. if mapbottom == False:
  123. print("t")
  124. canvas.move(p2, 0, 10)
  125. root.after(10, bmove)
  126.  
  127. bmoverespond()
  128.  
  129. root.after(1000, bmove)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. root.bind("<Up>", movementup)
  142. root.bind("<Down>", movementdown)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement