Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import tkinter
- import random
- # constants
- WIDTH = 640
- HEIGHT = 480
- BG_COLOR = 'white'
- ZERO = 0
- class Ball():
- def __init__(self, x, y, r, color, dx = 0, dy = 0):
- self.x = x
- self.y = y
- self.r = r
- self.color = color
- self.dx = dx
- self.dy = dy
- def draw(self):
- 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)
- def hide(self):
- 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)
- def move(self):
- #отскакивание от стенок
- if (self.x + self.r + self.dx >= WIDTH) or (self.x - self.r + self.dx <= ZERO):
- self.dx = - self.dx
- if (self.y + self.r + self.dy >= HEIGHT) or (self.y - self.r + self.dy <= ZERO):
- self.dy = - self.dy
- #основное движение
- self.hide()
- self.x += self.dx
- self.y += self.dy
- self.draw()
- # mouse_events
- def mouse_click(event):
- global main_ball
- if event.num == 1:
- if 'main_ball' not in globals():
- main_ball = Ball(event.x, event.y, 30, "blue", 1, 1)
- if main_ball.x > WIDTH / 2:
- main_ball.dx = -main_ball.dx
- if main_ball.y > HEIGHT / 2:
- main_ball.dy = -main_ball.dy
- main_ball.draw()
- # повернуть налево
- else:
- if main_ball.dy * main_ball.dx > 0:
- main_ball.dy = -main_ball.dy
- else:
- main_ball.dx = -main_ball.dx
- main_ball.move()
- else:
- main_ball.hide()
- #еще шары
- def create_list_of_balls(number):
- lst = []
- while len(lst) < number:
- 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']))
- lst.append(next_ball)
- next_ball.draw()
- return lst
- def main():
- if 'main_ball' in globals():
- main_ball.move()
- root.after(10, main)
- #Окно игры
- root = tkinter.Tk()
- root.title("Война пончиков") #имя окошка
- canvas = tkinter.Canvas(root, width = WIDTH, height = HEIGHT, bg = BG_COLOR)
- canvas.bind('<Button-1>', mouse_click)
- canvas.bind('<Button-2>', mouse_click, "+")
- canvas.bind('<Button-3>', mouse_click, "+")
- canvas.pack()
- balls = create_list_of_balls(7)
- main()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement