pteacher

Untitled

Feb 20th, 2022
7,599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. from tkinter import *
  2. from time import sleep
  3. from random import randint
  4.  
  5. class Player:
  6.     def __init__(self, c, x, y, size, color="RED"):
  7.         self.x = x
  8.         self.y = y
  9.         self.size = size
  10.         self.color = color
  11.         self.c = c
  12.         self.body = self.c.create_oval(self.x - self.size / 2,
  13.         self.y - self.size / 2,
  14.         self.x + self.size / 2,
  15.         self.y + self.size / 2,
  16.         fill=self.color)
  17.  
  18.     def moveto(self, x, y):
  19.         self.mx = x
  20.         self.my = y
  21.         self.dx = (self.mx - self.x) / 50
  22.         self.dy = (self.my - self.y) / 50
  23.         self.draw()
  24.  
  25.     def draw(self):
  26.  
  27.         self.x += self.dx
  28.         self.y += self.dy
  29.         self.c.move(self.body, self.dx, self.dy)
  30.  
  31.         print(abs(self.x))
  32.         if abs(self.mx - self.x) > 2:
  33.             self.c.after(100, self.draw)
  34.  
  35.  
  36.     def distance(self, x, y):
  37.         return ((self.x - x)**2 + (self.y - y)**2) ** 0.5
  38.  
  39. root = Tk()
  40. root.geometry("400x400")
  41. c = Canvas(root, width=400, height=400)
  42. c.pack()
  43.  
  44. p1 = Player(c, 25, 25, 20, "GREEN")
  45. p2 = Player(c, 375, 25, 20, "RED")
  46.  
  47. p1.moveto(200, 200)
  48. p2.moveto(200, 200)
  49.  
  50. root.mainloop()
  51. # c.create_oval(p.x + 5,p.y + 5,p.x+p.size + 5,p.y+p.size + 5)
Advertisement
Add Comment
Please, Sign In to add comment