Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. import tkinter, math, random
  2.  
  3. canvas = tkinter.Canvas(width=600, height=600, bg='white')
  4. canvas.pack()
  5.  
  6. class lopta:
  7. """class for lopta"""
  8.  
  9. def __init__(self,x=100,y=100,r=20,color='blue',rot=45,speed=5,objType='ball'):
  10. self.x=x
  11. self.y=y
  12. self.smerx=0
  13. self.smery=0
  14. self.r=r
  15. self.color=color
  16. self.objType=objType
  17.  
  18. if self.objType == 'ball':
  19. self.ball = canvas.create_oval(x-r, y-r, x+r, y+r, fill=self.color)
  20. elif self.objType == 'sqr':
  21. self.ball = canvas.create_rectangle(x-r, y-r, x+r, y+r, fill=self.color)
  22.  
  23. self.rot=rot
  24. self.speed=speed
  25.  
  26. def __del__(self):
  27. canvas.delete(self.ball)
  28.  
  29. def move(self):
  30. self.smerx=math.sin(math.radians(self.rot))*self.speed
  31. self.smery=math.cos(math.radians(self.rot))*self.speed
  32.  
  33. self.x+=self.smerx
  34. self.y+=self.smery
  35.  
  36. if self.x > 600-self.r or self.x < 0+self.r:
  37. self.rot=-self.rot
  38.  
  39. if self.y > 600-self.r or self.y < 0+self.r:
  40. print(self.rot)
  41. self.rot=-self.rot+180
  42. print('B'+str(self.rot))
  43.  
  44.  
  45. canvas.move(self.ball,self.smerx,self.smery)
  46.  
  47. def changeSpeed(self,speed):
  48. self.speed+=speed
  49.  
  50. def changeDirection(self,rot):
  51. self.rot+=rot
  52.  
  53. def checkBall(self,other):
  54. if math.sqrt(math.pow(self.x-other.x,2)+math.pow(self.y-other.y,2)) < self.r+other.r:
  55. return True
  56. else:
  57. return False
  58.  
  59. def checkSqr(self,other):
  60. if self.x > other.x-other.r and self.x < other.x+other.r and self.y > other.y-other.r and self.y < other.y+other.r:
  61. return True
  62. else:
  63. return False
  64.  
  65. def checkBallSqr(self,other):
  66. if math.sqrt(math.pow(self.x-other.x,2)+math.pow(self.y-other.y,2)) < self.r + math.sqrt(math.pow(other.r,2)*2):
  67. return True
  68. else:
  69. return False
  70.  
  71. def press(tuk):
  72. #print(tuk.keysym)
  73. if tuk.keysym == 'Up':
  74. jano.changeSpeed(5)
  75. elif tuk.keysym == 'Down':
  76. jano.changeSpeed(-5)
  77. if tuk.keysym == 'Left':
  78. jano.changeDirection(+15)
  79. elif tuk.keysym == 'Right':
  80. jano.changeDirection(-15)
  81.  
  82. jano = lopta()
  83. palo = lopta(250,100,50,'green', 70, 10,'ball')
  84.  
  85. myList = []
  86. for i in range(10):
  87. x = lopta(random.randint(100,500), random.randint(100,500), random.randint(5,100), random.choice(['red','blue','green']), random.randint(0,360), random.randint(1,10),'ball')
  88. myList.append(x)
  89.  
  90. canvas.bind_all('<Key>',press)
  91.  
  92.  
  93. def loop():
  94. jano.move()
  95. palo.move()
  96. canvas.update()
  97.  
  98. for i in range(len(myList)):
  99.  
  100. if jano.checkBall(adam) or jano.checkBall(adam):
  101. canvas.after(500,loop)
  102. else:
  103. canvas.after(5,loop)
  104.  
  105. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement