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
- #столкновение с шариками
- for ball in balls:
- if self.is_collision(ball):
- if ball.color != "red": #not a bad ball
- ball.hide()
- balls.remove(ball)
- self.dx = -self.dx
- self.dy = -self.dy
- else: #bad ball
- self.dx = self.dy = 0
- #основное движение
- self.hide()
- self.x += self.dx
- self.y += self.dy
- self.draw()
- def is_collision(self, ball):
- a = abs(self.x + self.dx - ball.x)
- b = abs(self.y + self.dy - ball.y)
- return (a*a + b*b) ** 0.5 <= self.r + ball.r
- # 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
- #еще шары
- 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 count_bad_balls(balls):
- result = 0
- for ball in balls:
- if ball.color == 'red':
- result += 1
- return result
- def main():
- if 'main_ball' in globals():
- main_ball.move()
- if len(balls) - bad_balls == 0:
- canvas.create_text(WIDTH / 2, HEIGHT / 2, text="YOU WON!", font = "Arial 20", fill = "lime")
- main_ball.dx = main_ball.dy = 0
- elif main_ball.dx == 0 and main_ball.dy == 0:
- canvas.create_text(WIDTH / 2, HEIGHT / 2, text="YOU LOSE!", font = "Arial 20", fill = "red")
- 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(5)
- bad_balls = count_bad_balls(balls)
- main()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement