Advertisement
snowden_web

Untitled

May 26th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import tkinter
  3. import random
  4.  
  5. # constants
  6. WIDTH = 640
  7. HEIGHT = 480
  8. BG_COLOR = 'white'
  9. ZERO = 0
  10.  
  11.  
  12. class Ball():
  13.     def __init__(self, x, y, r, color, dx = 0, dy = 0):
  14.         self.x = x
  15.         self.y = y
  16.         self.r = r
  17.         self.color = color
  18.         self.dx = dx
  19.         self.dy = dy
  20.    
  21.     def draw(self):
  22.         canvas.create_oval(self.x - self.r, self.y - self.r,  self.x + self.r, self.y + self.r, fill = self.color, outline = self.color)
  23.     def hide(self):
  24.         canvas.create_oval(self.x - self.r, self.y - self.r,  self.x + self.r, self.y + self.r, fill = BG_COLOR, outline = BG_COLOR)
  25.     def move(self):
  26.         #отскакивание от стенок
  27.         if (self.x + self.r + self.dx >= WIDTH) or (self.x - self.r + self.dx <= ZERO):
  28.             self.dx = - self.dx
  29.        
  30.         if (self.y + self.r + self.dy >= HEIGHT) or (self.y - self.r + self.dy <= ZERO):
  31.             self.dy = - self.dy
  32.        
  33.        
  34.         #столкновение с шариками
  35.         for ball in balls:
  36.             if self.is_collision(ball):
  37.                 if ball.color != "red":   #not a bad ball
  38.                     ball.hide()
  39.                     balls.remove(ball)
  40.                     self.dx = -self.dx
  41.                     self.dy = -self.dy
  42.                 else:  #bad ball
  43.                     self.dx = self.dy = 0
  44.        
  45.         #основное движение
  46.         self.hide()
  47.         self.x += self.dx
  48.         self.y += self.dy
  49.         self.draw()        
  50.            
  51.     def is_collision(self, ball):
  52.         a = abs(self.x + self.dx - ball.x)
  53.         b = abs(self.y + self.dy - ball.y)
  54.         return (a*a + b*b) ** 0.5 <= self.r + ball.r
  55.        
  56.  
  57. # mouse_events
  58. def mouse_click(event):
  59.     global main_ball
  60.     if event.num == 1:
  61.         if 'main_ball' not in globals():
  62.             main_ball = Ball(event.x, event.y, 30, "blue", 1, 1)
  63.             if main_ball.x > WIDTH / 2:
  64.                 main_ball.dx = -main_ball.dx
  65.             if main_ball.y > HEIGHT / 2:
  66.                 main_ball.dy = -main_ball.dy
  67.             main_ball.draw()
  68.         # повернуть налево
  69.         else:        
  70.             if main_ball.dy * main_ball.dx > 0:
  71.                 main_ball.dy = -main_ball.dy
  72.             else:
  73.                 main_ball.dx = -main_ball.dx
  74.  
  75. #еще шары
  76. def create_list_of_balls(number):
  77.     lst = []
  78.     while len(lst) < number:
  79.         next_ball = Ball(random.choice(range(0,WIDTH)), random.choice(range(0,HEIGHT)), random.choice(range(20,25)), random.choice(['aqua', 'red', 'pink', 'yellow', 'gold', 'black', 'fuchsia', 'chartreuse', 'green']))
  80.         lst.append(next_ball)
  81.         next_ball.draw()
  82.     return lst
  83.  
  84. # количество красных
  85. def count_bad_balls(balls):
  86.     result = 0
  87.     for ball in balls:
  88.         if ball.color == 'red':
  89.             result += 1
  90.     return result
  91.  
  92. def main():
  93.     if 'main_ball' in globals():
  94.         main_ball.move()
  95.         if len(balls) - bad_balls == 0:
  96.             canvas.create_text(WIDTH / 2, HEIGHT / 2, text="YOU WON!", font = "Arial 20", fill = "lime")
  97.             main_ball.dx = main_ball.dy = 0
  98.         elif main_ball.dx == 0 and main_ball.dy == 0:
  99.             canvas.create_text(WIDTH / 2, HEIGHT / 2, text="YOU LOSE!", font = "Arial 20", fill = "red")
  100.        
  101.     root.after(10, main)
  102.    
  103.    
  104. #Окно игры
  105. root = tkinter.Tk()
  106. root.title("Война пончиков") #имя окошка
  107. canvas = tkinter.Canvas(root, width = WIDTH, height = HEIGHT, bg = BG_COLOR)
  108. canvas.bind('<Button-1>', mouse_click)
  109. canvas.bind('<Button-2>', mouse_click, "+")
  110. canvas.bind('<Button-3>', mouse_click, "+")
  111. canvas.pack()
  112. balls = create_list_of_balls(5)
  113. bad_balls = count_bad_balls(balls)
  114.  
  115. main()
  116. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement