boris-vlasenko

Волейбол с анимированным мячом

May 19th, 2016
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.70 KB | None | 0 0
  1. from tkinter import *
  2. import time
  3. from random import randrange as rnd, random, choice
  4. from PIL import Image, ImageTk
  5.  
  6.  
  7. root = Tk()
  8. fr = Frame(root)
  9. root.geometry('800x600')
  10. canv = Canvas(root, bg = 'white')
  11. canv.pack(fill=BOTH,expand=1)
  12. colors = ['red', 'orange', 'yellow', 'green', 'blue']
  13.  
  14. def jump2(event):
  15.     global ball
  16.     ball.vy = (event.y - ball.y)/15
  17.     ball.vx = (event.x - ball.x)/15
  18.  
  19. canv.bind('<1>',jump2)
  20.  
  21. class Ball():
  22.     def __init__(self):
  23.         self.x = 200
  24.         self.y = 400
  25.         self.r = 20
  26.         self.m = self.x
  27.         #self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill='green')
  28.         self.images = []
  29.         self.images.append(ImageTk.PhotoImage(Image.open('ball1.png')))
  30.         self.images.append(ImageTk.PhotoImage(Image.open('ball2.png')))
  31.         self.images.append(ImageTk.PhotoImage(Image.open('ball3.png')))
  32.  
  33.         self.image_number= 0
  34.         self.rotate_frame = 20
  35.         self.image = canv.create_image(self.x,self.y,image=self.images[self.image_number])
  36.         self.count_from_to_rotate = 6
  37.        
  38.         self.vx = 0
  39.         self.vy = 0
  40.         self.g = 0
  41.         self.gamer = 'man1'
  42.    
  43.     def move(self):
  44.         if abs(self.vx)+abs(self.vy) > 0:
  45.             self.rotate_frame -= 1
  46.         if self.rotate_frame <= 0:
  47.             self.rotate_frame = self.count_from_to_rotate
  48.             self.image_number += 1
  49.             if self.image_number >= len(self.images):
  50.                 self.image_number = 0
  51.             canv.delete(self.image)
  52.             self.image = canv.create_image(self.x,self.y,image=self.images[self.image_number])
  53.  
  54.            
  55.         self.vy += self.g
  56.         self.y += self.vy
  57.         self.x += self.vx
  58.         self.vx *= 0.99
  59.          
  60.         if self.y > 550:
  61.             self.vy *= -0.7
  62.             self.y = 550
  63.             self.vx *= 0.75
  64.        
  65.         if self.x > 750:
  66.             self.vx *= -0.75
  67.             self.x = 750
  68.        
  69.         if self.x < 50:
  70.             self.vx *= -0.75
  71.             self.x = 50
  72.        
  73.         if self.x-self.r < 400 < self.x+self.r and  self.y - self.r > 400:
  74.             if self.x > 400:
  75.                 self.gamer = 'man1'
  76.                 man1.points += 1
  77.                 init()
  78.             else:
  79.                 self.gamer = 'man2'
  80.                 man2.points += 1
  81.                 init()
  82.        
  83.         if self.y > 520:
  84.                 if ball.gamer == 'man1':
  85.                     ball.gamer = 'man2'
  86.                     init()
  87.                 else:
  88.                     ball.gamer = 'man1'
  89.                     init()
  90.            
  91.         if man1.x - self.r < self.x < man1.x+man1.w+self.r and man1.y-self.r < self.y < man1.y+man1.h+self.r:
  92.             man1.lives += 1
  93.             if man2.x - self.r < self.x < man2.x+man2.w+self.r and man2.y-self.r < self.y < man2.y+man2.h+self.r:
  94.                 man1.lives = 0
  95.        
  96.         if man2.x - self.r < self.x < man2.x+man2.w+self.r and man2.y-self.r < self.y < man2.y+man2.h+self.r:
  97.             man2.lives += 1
  98.             if man1.x - self.r < self.x < man1.x+man1.w+self.r and man1.y-self.r < self.y < man1.y+man1.h+self.r:
  99.                 man2.lives = 0
  100.        
  101.         if man2.lives == 3:
  102.             man2.points += 1
  103.             self.gamer = 'man2'
  104.             init()
  105.        
  106.         if man1.lives == 3:
  107.             man1.points += 1
  108.             self.gamer = 'man1'
  109.             init()
  110.        
  111.        
  112.                
  113.         #canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
  114.         canv.coords(self.image,self.x+self.r/2,self.y+self.r/2)
  115.  
  116.        
  117.        
  118.  
  119. class SportsMan():
  120.     def __init__(self,left,right):
  121.         self.x = (left+right)/2
  122.         self.left = left
  123.         self.right = right
  124.         self.y = 550
  125.         self.w = 40
  126.         self.h = 60
  127.         #self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill='green',width=0)
  128.         self.img = ImageTk.PhotoImage(Image.open('man.png'))
  129.         self.image = canv.create_image(self.x,self.y,image=self.img)
  130.        
  131.         self.vx = 0
  132.         self.ax = 0
  133.         self.vy = 0
  134.         self.g = 1
  135.         self.points = 0
  136.         self.lives = 0
  137.         self.can_jump = True
  138.        
  139.    
  140.     def move(self):
  141.         self.vy += self.g
  142.         self.y += self.vy
  143.         self.vx += self.ax
  144.         self.x += self.vx
  145.         self.vx *= 0.8
  146.        
  147.         if self.x < self.left:
  148.             self.ax = 0
  149.             self.vx = 0
  150.             self.x = self.left
  151.        
  152.         if self.x + self.w > self.right:
  153.             self.ax = 0
  154.             self.vx = 0
  155.             self.x = self.right-self.w
  156.        
  157.         if self.y > 550-self.h:
  158.             self.y = 550-self.h
  159.             self.vy = -0.7*self.vy
  160.             self.can_jump = True
  161.        
  162.         if self.x - ball.r < ball.x < self.x+self.w+ball.r and self.y-ball.r < ball.y < self.y+self.h+ball.r:
  163.             x = self.x + self.w/2
  164.             y = self.y + self.h/2
  165.             ball.vx = (ball.x - x)/2
  166.             ball.vy = (ball.y - y)/2
  167.             ball.count_from_to_rotate = (abs(ball.vx) + abs(ball.vx))/5
  168.             while self.x - ball.r < ball.x < self.x+self.w+ball.r and self.y-ball.r < ball.y < self.y+self.h+ball.r:
  169.                 ball.x += ball.vx
  170.                 ball.y += ball.vy
  171.             ball.g = 0.7
  172.        
  173.        
  174.        
  175.         #canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
  176.         canv.coords(self.image,self.x+self.w/2,self.y+self.h/2)
  177.  
  178.  
  179. def init():
  180.     if ball.gamer == 'man1':
  181.         ball.x = 200
  182.     else:
  183.         ball.x = 600
  184.     canv.itemconfig(points1,text=man1.points)
  185.     canv.itemconfig(points2,text=man2.points)
  186.     man1.lives = 0
  187.     man2.lives = 0
  188.     ball.y = 450
  189.     ball.vx = 0
  190.     ball.vy = 0
  191.     ball.g = 0
  192.  
  193. def new_game():
  194.     global man1, man2, ball
  195.     man2 = SportsMan(50,380)
  196.     man1 = SportsMan(420,750)
  197.     ball = Ball()
  198.     canv.create_rectangle(50,50,750,600, width = 3)
  199.     canv.create_line(400,400,400,600, width = 5)
  200.     init()
  201.  
  202. def keyDown(event):
  203.     print(event.keycode)
  204.     keys.add(event.keycode)
  205.  
  206. def keyUp(event):
  207.     if event.keycode in keys:
  208.         keys.remove(event.keycode)
  209.  
  210. points1 = canv.create_text(200,300,text=0, font = "Arial 240", fill = 'lightgray')
  211. points2 = canv.create_text(600,300,text=0, font = "Arial 240", fill = 'lightgray')
  212.  
  213. new_game()
  214. root.bind('<Key>',keyDown)
  215. root.bind('<KeyRelease>',keyUp)
  216. keys = set()
  217. while 1:
  218.     ball.move()
  219.     man1.move()
  220.     man2.move()
  221.     man1.ax = 0
  222.     man2.ax = 0
  223.     if 114 in keys:
  224.         man1.ax = 4
  225.     if 113 in keys:
  226.         man1.ax = -4
  227.     if 111 in keys:
  228.         if man1.can_jump:
  229.             man1.vy = -15
  230.             man1.can_jump = False
  231.     if 40 in keys:
  232.         man2.ax = 4
  233.     if 38 in keys:
  234.         man2.ax = -4
  235.     if 25 in keys:
  236.         if man2.can_jump:
  237.             man2.vy = -15
  238.             man2.can_jump = False
  239.    
  240.     canv.update()
  241.     time.sleep(0.03)
  242.  
  243. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment