Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. from tkinter import *
  2. from random import randrange as rnd, choice
  3. import time
  4.  
  5. root = Tk()
  6. root.geometry('800x600')
  7.  
  8. canv = Canvas(root, bg='#6DF873')
  9. canv.pack(side=LEFT, fill=BOTH, expand=1)
  10.  
  11. colors = ['red', 'orange', 'yellow', 'green', 'blue']
  12.  
  13.  
  14. def new_ball():
  15. '''Функция создает круг из 5 рандомных цветов( red, orange, green, blue).
  16. С случайным значением радиуса, с помощью rnd(randrange).Шарик появляется на 1000мс,
  17. далее выполняется функция вызова прямоугольника.'''
  18. global x, y, r
  19. canv.delete(ALL)
  20. x = rnd(100, 300)
  21. y = rnd(100, 300)
  22. r = rnd(30, 50)
  23. canv.create_oval(x - r, y - r, x + r, y + r, fill=choice(colors), width=0)
  24. root.after(1000, new_ball)
  25. def ball_ball():
  26. global x1, y1, r , m, x2, y2, x3, y3
  27. canv.delete(ALL)
  28. m = rnd(1,4)
  29. x1 = rnd(100, 300)
  30. y1 = rnd(100, 300)
  31. r = rnd(30, 50)
  32. x2 = rnd(100, 300)
  33. y2= rnd(100, 300)
  34. x3 = rnd(100, 300)
  35. y3 = rnd(100, 300)
  36. if m==1:
  37. ball_1 = canv.create_oval(x1 - r, y1 - r, x1 + r, y1 + r, fill=choice(colors), width=0)
  38. if m==2:
  39. ball_1 = canv.create_oval(x1 - r, y1 - r, x1+ r, y1 + r, fill=choice(colors), width=0)
  40. ball_2 = canv.create_oval(x2- r, y2 - r, x2 + r, y2 + r, fill=choice(colors), width=0)
  41. if m==3:
  42. ball_1 = canv.create_oval(x1 - r, y1 - r, x1 + r, y1 + r, fill=choice(colors), width=0)
  43. ball_2 = canv.create_oval(x2 - r, y2 - r, x2 + r, y2 + r, fill=choice(colors), width=0)
  44. ball_3 = canv.create_oval(x3 - r, y3 - r, x3 + r, y3 + r, fill=choice(colors), width=0)
  45. root.after(1000, ball_ball)
  46.  
  47. x1 = rnd(100, 300)
  48. y1 = rnd(100, 300)
  49. r = rnd(30, 50)
  50. x2 = rnd(100, 300)
  51. y2= rnd(100, 300)
  52. x3 = rnd(100, 300)
  53. y3 = rnd(100, 300)
  54. ball_1 = canv.create_oval(x1 - r, y1 - r, x1 + r, y1 + r, fill=choice(colors), width=0)
  55. ball_2 = canv.create_oval(x2 - r, y2 - r, x2 + r, y2 + r, fill=choice(colors), width=0)
  56. ball_3 = canv.create_oval(x3 - r, y3 - r, x3 + r, y3 + r, fill=choice(colors), width=0)
  57. points=0
  58. def motion():
  59. global x, ball_1
  60. canv.move(ball_1, -5, 0)
  61. if canv.coords(ball_1)[0] < 100:
  62. root.after(100, motion)
  63. root.after (100, new_ball)
  64.  
  65.  
  66. def motion2():
  67. global y, ball_1
  68. canv.move(ball_1, 0, -5)
  69. if canv.coords(ball_1)[1] < 100:
  70. root.after(100,motion2)
  71. root.after(100, new_ball)
  72.  
  73.  
  74. def click(event):
  75. global x1, y1, r , x2, y2, x3, y, points
  76. if event.x <=(x1 + r) and event.x >= (x1 - r):
  77. if event.y >= (y1 - r) and event.y <= (y1 + r):
  78. points+=1
  79. print('Попал')
  80. canv1.delete(ALL)
  81. if event.x <=(x2 + r) and event.x >= (x2 - r):
  82. if event.y >= (y2 - r) and event.y <= (y2 + r):
  83. points+=1
  84. print('Попал')
  85. canv1.delete(ALL)
  86. if event.x <=(x3 + r) and event.x >= (x3 - r):
  87. if event.y >= (y3 - r) and event.y <= (y3 + r):
  88. points+=1
  89. print('Попал')
  90. canv1.delete(ALL)
  91. text2 = canv1.create_text(60, 60, text=(points), font='Arial 20')
  92. canv2 = Canvas(width=120, height=60, bg='white')
  93. canv1 = Canvas(width=400, height=400, bg='white')
  94. text = canv2.create_text(60,20,text='Points', font = 'Arial 20')
  95. canv.bind('<Button-1>', click)
  96. canv2.pack(side=TOP)
  97. canv1.pack(side=RIGHT)
  98. motion()
  99. motion2()
  100. ball_ball()
  101. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement