Advertisement
boris-vlasenko

Волейбол1

May 18th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.84 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.        
  29.         self.vx = 0
  30.         self.vy = 0
  31.         self.g = 0
  32.         self.gamer = 'man1'
  33.    
  34.     def move(self):
  35.         self.vy += self.g
  36.         self.y += self.vy
  37.         self.x += self.vx
  38.         self.vx *= 0.99
  39.          
  40.         if self.y > 550:
  41.             self.vy *= -0.7
  42.             self.y = 550
  43.             self.vx *= 0.75
  44.        
  45.         if self.x > 750:
  46.             self.vx *= -0.75
  47.             self.x = 750
  48.        
  49.         if self.x < 50:
  50.             self.vx *= -0.75
  51.             self.x = 50
  52.        
  53.         if self.x-self.r < 400 < self.x+self.r and  self.y - self.r > 400:
  54.             if self.x > 400:
  55.                 self.gamer = 'man1'
  56.                 man1.points += 1
  57.                 init()
  58.             else:
  59.                 self.gamer = 'man2'
  60.                 man2.points += 1
  61.                 init()
  62.        
  63.         if self.y > 520:
  64.                 if ball.gamer == 'man1':
  65.                     ball.gamer = 'man2'
  66.                     init()
  67.                 else:
  68.                     ball.gamer = 'man1'
  69.                     init()
  70.            
  71.         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:
  72.             man1.lives += 1
  73.             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:
  74.                 man1.lives = 0
  75.        
  76.         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:
  77.             man2.lives += 1
  78.             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:
  79.                 man2.lives = 0
  80.        
  81.         if man2.lives == 3:
  82.             man2.points += 1
  83.             self.gamer = 'man2'
  84.             init()
  85.        
  86.         if man1.lives == 3:
  87.             man1.points += 1
  88.             self.gamer = 'man1'
  89.             init()
  90.        
  91.        
  92.                
  93.         canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
  94.  
  95.        
  96.        
  97.  
  98. class SportsMan():
  99.     def __init__(self,left,right):
  100.         self.x = (left+right)/2
  101.         self.left = left
  102.         self.right = right
  103.         self.y = 550
  104.         self.w = 40
  105.         self.h = 60
  106.         self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill='green',width=0)
  107.         #self.img = ImageTk.PhotoImage(Image.open('man.png'))
  108.         #self.image = canv.create_image(self.x,self.y,image=self.img)
  109.        
  110.         self.vx = 0
  111.         self.ax = 0
  112.         self.vy = 0
  113.         self.g = 1
  114.         self.points = 0
  115.         self.lives = 0
  116.         self.can_jump = True
  117.        
  118.    
  119.     def move(self):
  120.         self.vy += self.g
  121.         self.y += self.vy
  122.         self.vx += self.ax
  123.         self.x += self.vx
  124.         self.vx *= 0.8
  125.        
  126.         if self.x < self.left:
  127.             self.ax = 0
  128.             self.vx = 0
  129.             self.x = self.left
  130.        
  131.         if self.x + self.w > self.right:
  132.             self.ax = 0
  133.             self.vx = 0
  134.             self.x = self.right-self.w
  135.        
  136.         if self.y > 550-self.h:
  137.             self.y = 550-self.h
  138.             self.vy = -0.7*self.vy
  139.             self.can_jump = True
  140.        
  141.         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:
  142.             x = self.x + self.w/2
  143.             y = self.y + self.h/2
  144.             ball.vx = (ball.x - x)/2
  145.             ball.vy = (ball.y - y)/2
  146.             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:
  147.                 ball.x += ball.vx
  148.                 ball.y += ball.vy
  149.             ball.g = 0.7
  150.        
  151.        
  152.        
  153.         canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
  154.         #canv.coords(self.image,self.x+self.w/2,self.y+self.h/2)
  155.  
  156.  
  157. def init():
  158.     if ball.gamer == 'man1':
  159.         ball.x = 200
  160.     else:
  161.         ball.x = 600
  162.     canv.itemconfig(points1,text=man1.points)
  163.     canv.itemconfig(points2,text=man2.points)
  164.     man1.lives = 0
  165.     man2.lives = 0
  166.     ball.y = 450
  167.     ball.vx = 0
  168.     ball.vy = 0
  169.     ball.g = 0
  170.  
  171. def new_game():
  172.     global man1, man2, ball
  173.     man2 = SportsMan(50,380)
  174.     man1 = SportsMan(420,750)
  175.     ball = Ball()
  176.     canv.create_rectangle(50,50,750,600, width = 3)
  177.     canv.create_line(400,400,400,600, width = 5)
  178.     init()
  179.  
  180. def keyDown(event):
  181.     print(event.keycode)
  182.     keys.add(event.keycode)
  183.  
  184. def keyUp(event):
  185.     if event.keycode in keys:
  186.         keys.remove(event.keycode)
  187.  
  188. points1 = canv.create_text(200,300,text=0, font = "Arial 240", fill = 'lightgray')
  189. points2 = canv.create_text(600,300,text=0, font = "Arial 240", fill = 'lightgray')
  190.  
  191. new_game()
  192. root.bind('<Key>',keyDown)
  193. root.bind('<KeyRelease>',keyUp)
  194. keys = set()
  195. while 1:
  196.     ball.move()
  197.     man1.move()
  198.     man2.move()
  199.     man1.ax = 0
  200.     man2.ax = 0
  201.     if 114 in keys:
  202.         man1.ax = 4
  203.     if 113 in keys:
  204.         man1.ax = -4
  205.     if 111 in keys:
  206.         if man1.can_jump:
  207.             man1.vy = -15
  208.             man1.can_jump = False
  209.     if 40 in keys:
  210.         man2.ax = 4
  211.     if 38 in keys:
  212.         man2.ax = -4
  213.     if 25 in keys:
  214.         if man2.can_jump:
  215.             man2.vy = -15
  216.             man2.can_jump = False
  217.    
  218.     canv.update()
  219.     time.sleep(0.03)
  220.  
  221. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement