SHARE
TWEET

Untitled

a guest Nov 18th, 2018 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time, math
  2. from tkinter import *
  3. from random import randint
  4. width = 700
  5. height = 700
  6. r = 5
  7.  
  8. class Game:
  9.     def __init__(self, width = 500, height = 500):
  10.         self.root = Tk()
  11.         self.root.title("Meteorits")
  12.         self.root.resizable(False,False)
  13.         self.canvas = Canvas(self.root, width = width, height = height, highlightthickness = 0)
  14.         self.canvas.pack()
  15.         self.root.update()
  16.         self.sprites = []
  17.         self.width = self.canvas.winfo_width()
  18.         self.height = self.canvas.winfo_height()
  19.  
  20.     def mainloop(self):
  21.         while True:
  22.             for z in self.sprites:
  23.                 z.move()
  24.             self.colision_with_borders()
  25.             self.colision_with_sprites()
  26.             self.root.update_idletasks()
  27.             self.root.update()
  28.             time.sleep(1/100)
  29.  
  30.     def get_center(self, obj1):
  31.         obj1_x1 = self.canvas.coords(obj1.ball)[0]
  32.         obj1_y1 = self.canvas.coords(obj1.ball)[1]
  33.         obj1_x2 = self.canvas.coords(obj1.ball)[2]
  34.         obj1_y2 = self.canvas.coords(obj1.ball)[3]
  35.         obj1_center = ((obj1_x2 + obj1_x1) / 2, (obj1_y2 + obj1_y1) / 2)
  36.         return obj1_center
  37.  
  38.     def get_dist(self, obj1_center, obj2_center):
  39.         kx = obj1_center[0] - obj2_center[0]
  40.         ky = obj1_center[1] - obj2_center[1]
  41.         g = math.sqrt(kx ** 2 + ky ** 2)
  42.         return g
  43.  
  44.     def colision_with_borders(self):
  45.         for i in self.sprites:
  46.             if self.canvas.coords(i.ball)[0] < 0: self.canvas.move(i.ball, self.width, 0)
  47.             if self.canvas.coords(i.ball)[2] > self.width: self.canvas.move(i.ball, -self.width, 0)
  48.             if self.canvas.coords(i.ball)[1] < 0: self.canvas.move(i.ball,  0, self.height)
  49.             if self.canvas.coords(i.ball)[3] > self.height: self.canvas.move(i.ball, 0, -self.height)
  50.  
  51.     def colision_with_sprites(self):
  52.         remove_sprites = []
  53.         for z in range(len(self.sprites) - 1):
  54.             for zz in range(z + 1, len(self.sprites)):
  55.                 zcenter = self.get_center(self.sprites[z])
  56.                 zzcenter = self.get_center(self.sprites[zz])
  57.                 dist = self.get_dist(zcenter, zzcenter)
  58.                 #print(zcenter, zzcenter, dist)
  59.                 if dist <= (self.sprites[z].r + self.sprites[zz].r):
  60.                     remove_sprites.append(self.sprites[z])
  61.                     remove_sprites.append(self.sprites[zz])
  62.         new_sprites = [i for i in self.sprites if i not in remove_sprites]
  63.         for h in remove_sprites:
  64.             self.canvas.delete(h.ball)
  65.         self.sprites = new_sprites
  66.  
  67. class Sprite:
  68.     def __init__(self, game, x, y, r, color):
  69.         self.deltax = 0
  70.         self.deltay = 0
  71.         self.x1 = x-r
  72.         self.y1 = y-r
  73.         self.x2 = x+r
  74.         self.y2 = y+r
  75.         self.r = r
  76.         self.game = game
  77.         self.ball = self.game.canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill=color)
  78.  
  79.     def move(self):
  80.         self.game.canvas.move(self.ball, self.deltax, self.deltay)
  81. class Meteorit(Sprite):
  82.     def __init__(self, game, x, y, r, color):
  83.         Sprite.__init__(self, game, x, y, r, color)
  84.         self.deltax = randint(-5, 5)
  85.         self.deltay = randint(-5, 5)
  86. class Spaceship(Sprite):
  87.     def __init__(self, game, x, y, r, color):
  88.         Sprite.__init__(self, game, x, y, r, color)
  89.         game.canvas.bind_all('<KeyPress-Left>', self.turn_left)
  90.         game.canvas.bind_all('<KeyPress-Right>', self.turn_right)
  91.         game.canvas.bind_all('<KeyPress-Up>', self.turn_up)
  92.         game.canvas.bind_all('<KeyPress-Down>', self.turn_down)
  93.         game.canvas.bind_all('<KeyPress-Control_L>', self.stop)
  94.  
  95.     def turn_left(self, event):
  96.         self.deltax = self.deltax - 1
  97.     def turn_right(self, event):
  98.         self.deltax = self.deltax + 1
  99.     def turn_up(self, event):
  100.         self.deltay = self.deltay - 1  
  101.     def turn_down(self, event):
  102.         self.deltay = self.deltay + 1  
  103.     def stop(self, event):
  104.         self.deltax = 0
  105.         self.deltay = 0
  106. g = Game(width, height)
  107. for i in range(21):
  108.     x=randint(r,width-r)
  109.     y=randint(r,height-r)
  110.     g.sprites.append(Meteorit(g, x, y, r, "gray50"))
  111. g.sprites.append(Spaceship(g, width / 2, height / 2, r, "deep pink"))
  112. g.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top