Guest User

Untitled

a guest
Sep 30th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.10 KB | None | 0 0
  1. from tkinter import *
  2. from random import randint, choice
  3. import time
  4.  
  5.  
  6. class Ball:
  7.     def __init__(self):
  8.         self.R = randint(35, 45)
  9.         self.x = randint(self.R, WIDTH - self.R)
  10.         self.y = randint((self.R + 50), HEIGHT - self.R)
  11.         self.colors = ['red', 'orange', 'darkcyan', 'green', 'blue', 'indigo', 'darkmagenta']
  12.         self.dx, self.dy = +6, +7
  13.         self.ball_id = canv.create_oval(self.x - self.R, self.y - self.R,
  14.                                         self.x + self.R, self.y + self.R,
  15.                                         fill=choice(self.colors), width=0)
  16.  
  17.     def move(self):
  18.         self.x += self.dx
  19.         self.y += self.dy
  20.         if self.x + self.R > WIDTH or self.x - self.R <= 0:
  21.             self.dx = -self.dx
  22.         if self.y + self.R > HEIGHT or (self.y - self.R - 50) <= 0:
  23.             self.dy = -self.dy
  24.  
  25.     def show(self):
  26.         canv.move(self.ball_id, self.dx, self.dy)
  27.  
  28.  
  29. class Rhombus:
  30.     def __init__(self):
  31.         self.R = randint(25, 30)
  32.         self.x = randint(self.R, WIDTH - self.R)
  33.         self.y = randint((self.R + 50), HEIGHT - self.R)
  34.         self.dx, self.dy = +8, +9
  35.         self.romb_id = canv.create_polygon(self.x, self.y - self.R, self.x + self.R, self.y,
  36.                                            self.x, self.y + self.R, self.x - self.R, self.y,
  37.                                            fill='yellow', width=0)
  38.  
  39.     def move(self):
  40.         self.x += self.dx
  41.         self.y += self.dy
  42.         if self.x + self.R > WIDTH or self.x - self.R <= 0:
  43.             self.dx = -self.dx
  44.         if self.y + self.R > HEIGHT or (self.y - self.R - 50) <= 0:
  45.             self.dy = -self.dy
  46.         if self.y + self.R > HEIGHT:
  47.             self.dy = self.dy - 2
  48.  
  49.     def show(self):
  50.         canv.move(self.romb_id, self.dx, self.dy)
  51.  
  52.  
  53. def ball_move():
  54.     ball1.move()
  55.     ball1.show()
  56.     ball2.move()
  57.     ball2.show()
  58.     root.after(20, ball_move)
  59.  
  60.  
  61. def romb_move():
  62.     global romb1
  63.     romb1.move()
  64.     romb1.show()
  65.     if romb1.dy >= 27 or romb1.dy <= - 27:
  66.         canv.delete(romb1.romb_id)
  67.         romb1 = Rhombus()
  68.     root.after(20, romb_move)
  69.  
  70.  
  71. def inRhombus(x, y, xp, yp):
  72.     c = 0
  73.     for i in range(len(xp)):
  74.         if (((yp[i] <= y and y < yp[i - 1]) or (yp[i - 1] <= y and y < yp[i])) and \
  75.                 (x > (xp[i - 1] - xp[i]) * (y - yp[i]) / (yp[i - 1] - yp[i]) + xp[i])): c = 1 - c
  76.     return c
  77.  
  78.  
  79. def new_score(event):
  80.     global global_score, ball1, ball2, romb1
  81.     goal = ((event.x - ball1.x) ** 2 + (event.y - ball1.y) ** 2) ** 0.5
  82.     if goal <= ball1.R:
  83.         score_label['text'] = 'Total score: ' + str(global_score + 1)
  84.         global_score += 1
  85.         canv.delete(ball1.ball_id)
  86.         ball1 = Ball()
  87.     goal = ((event.x - ball2.x) ** 2 + (event.y - ball2.y) ** 2) ** 0.5
  88.     if goal <= ball2.R:
  89.         score_label['text'] = 'Total score: ' + str(global_score + 1)
  90.         global_score += 1
  91.         canv.delete(ball2.ball_id)
  92.         ball2 = Ball()
  93.     ok = inRhombus(event.x, event.y, (romb1.x, romb1.x + romb1.R, romb1.x, romb1.x - romb1.R),
  94.                    (romb1.y - romb1.R, romb1.y, romb1.y + romb1.R, romb1.y))
  95.     if ok == 1:
  96.         score_label['text'] = 'Total score: ' + str(global_score + 2)
  97.         global_score += 2
  98.         canv.delete(romb1.romb_id)
  99.         romb1 = Rhombus()
  100.  
  101.  
  102. def main():
  103.     global root, canv, WIDTH, HEIGHT, score_label, global_score, ball1, ball2, romb1
  104.     root = Tk()
  105.     WIDTH = 800
  106.     HEIGHT = 600
  107.     global_score = 0
  108.     root.geometry(str(WIDTH) + 'x' + str(HEIGHT) + '+500+50')
  109.     root.title('Catch the ball')
  110.  
  111.     canv = Canvas(root, bg='white')
  112.     canv.pack(fill=BOTH, expand=1)
  113.     score_label = Label(canv, text='Total score: 0', font='Arial 20', bg='darkslategrey', fg='white', width=90, bd=5)
  114.     score_label.pack()
  115.  
  116.     start_time = time.time()
  117.     while (time.time() - start_time) < 30:
  118.         ball1 = Ball()
  119.         ball2 = Ball()
  120.         ball_move()
  121.  
  122.         romb1 = Rhombus()
  123.         romb_move()
  124.  
  125.     canv.bind('<Button-1>', new_score)
  126.  
  127.     root.mainloop()
  128.  
  129.  
  130. if __name__ == '__main__':
  131.     main()
  132.  
Advertisement
Add Comment
Please, Sign In to add comment