Advertisement
snowden_web

Untitled

May 19th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 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.         self.hide()
  34.         self.x += self.dx
  35.         self.y += self.dy
  36.         self.draw()
  37.        
  38.  
  39. # mouse_events
  40. def mouse_click(event):
  41.     global main_ball
  42.     if event.num == 1:
  43.         if 'main_ball' not in globals():
  44.             main_ball = Ball(event.x, event.y, 30, "blue", 1, 1)
  45.             if main_ball.x > WIDTH / 2:
  46.                 main_ball.dx = -main_ball.dx
  47.             if main_ball.y > HEIGHT / 2:
  48.                 main_ball.dy = -main_ball.dy
  49.             main_ball.draw()
  50.         # повернуть налево
  51.         else:        
  52.             if main_ball.dy * main_ball.dx > 0:
  53.                 main_ball.dy = -main_ball.dy
  54.             else:
  55.                 main_ball.dx = -main_ball.dx
  56.         main_ball.move()
  57.     else:
  58.         main_ball.hide()
  59. #еще шары
  60. def create_list_of_balls(number):
  61.     lst = []
  62.     while len(lst) < number:
  63.         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']))
  64.         lst.append(next_ball)
  65.         next_ball.draw()
  66.     return lst
  67.  
  68. def main():
  69.     if 'main_ball' in globals():
  70.         main_ball.move()
  71.     root.after(10, main)
  72.    
  73.    
  74. #Окно игры
  75. root = tkinter.Tk()
  76. root.title("Война пончиков") #имя окошка
  77. canvas = tkinter.Canvas(root, width = WIDTH, height = HEIGHT, bg = BG_COLOR)
  78. canvas.bind('<Button-1>', mouse_click)
  79. canvas.bind('<Button-2>', mouse_click, "+")
  80. canvas.bind('<Button-3>', mouse_click, "+")
  81. canvas.pack()
  82. balls = create_list_of_balls(7)
  83. main()
  84. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement