Advertisement
boris-vlasenko

ball_game2

Apr 19th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. from tkinter import *
  2. import time, sys
  3. from random import randrange as rnd
  4.  
  5.  
  6. root = Tk()
  7. root.geometry('800x600')
  8.  
  9. canv = Canvas(bg='white')
  10. canv.pack(fill=BOTH,expand=1)
  11.  
  12. x = 400
  13. y = 300
  14. r = 20
  15. vx = 0
  16. vy = 0
  17.  
  18. w = 20
  19. h = 120
  20. x1 = 10
  21. y1 = 300-h/2
  22. p1 = canv.create_rectangle(x1,y1,x1+w,y1+h,fill='green')
  23. x2 = 770
  24. y2 = y1
  25. p2 = canv.create_rectangle(x2,y2,x2+w,y2+h,fill='green')
  26.  
  27. vy1 = 0
  28. vy2 = 0
  29.  
  30. leftpoints = 0
  31. rightpoints = 0
  32.  
  33. lp = canv.create_text(200,300,text = leftpoints, font='Arial 240',fill='lightgray')
  34. rp = canv.create_text(600,300,text = rightpoints, font='Arial 240',fill='lightgray')
  35. canv.create_text(400,273,text = ':', font='Arial 240',fill='lightgray')
  36.  
  37. ball = canv.create_oval(x-r,y-r,x+r,y+r,fill='orange')
  38.  
  39.  
  40. def endgame(text):
  41.     root.unbind('<Key>')   
  42.     canv.create_text(400,100,text = text+' is win', font='Arial 140')
  43.    
  44.  
  45. def stopgame(text):
  46.     global x,y,vx,vy,y1,y2,vy1,vy2,leftpoints,rightpoints
  47.     if text == 'left fail':
  48.         rightpoints += 1
  49.         if rightpoints > 5:
  50.             endgame('right')
  51.     elif text == 'right fail':
  52.         leftpoints += 1
  53.         if leftpoints > 5:
  54.             endgame('left')
  55.    
  56.     canv.itemconfig(lp,text=leftpoints)
  57.     canv.itemconfig(rp,text=rightpoints)
  58.    
  59.     x = 400
  60.     y = 300
  61.     vx = 0
  62.     vy = 0
  63.  
  64.     y1 = 300-h/2
  65.     y2 = y1
  66.  
  67.     vy1 = 0
  68.     vy2 = 0
  69.    
  70.  
  71. def tick():
  72.     global x,y,vx,vy,y2,y1,vy1,vy2
  73.     # ---ball----vvvvv
  74.     x += vx
  75.     y += vy
  76.     if y > 590-r/2-vy:
  77.         y = 590-r/2
  78.         vy *= -1
  79.        
  80.     if y < 10+vy+r/2:
  81.         y = 10+r/2
  82.         vy *= -1
  83.  
  84.     if x > 800:
  85.         stopgame('right fail')
  86.        
  87.     if x < 0:
  88.         stopgame('left fail')
  89.     # ---ball----^^^^^^
  90.    
  91.     y1 += vy1
  92.     vy1 *= 0.9
  93.     y2 += vy2
  94.     vy2 *= 0.9
  95.     if y2 < 0:
  96.         y2 = 0
  97.         vy2 = 0
  98.     if y1 < 0:
  99.         y1 = 0
  100.         vy1 = 0
  101.  
  102.     if y2+h > 600:
  103.         y2 = 600-h
  104.         vy2 = 0
  105.  
  106.     if y1+h > 600:
  107.         y1 = 600-h
  108.         vy1 = 0
  109.  
  110.     if x-r/2 < x1+w and y1 < y < y1+h:
  111.         vx=-vx
  112.        
  113.     if x+r/2 > x2 and y2 < y < y2+h:
  114.         vx=-vx
  115.    
  116.  
  117.     canv.coords(ball,x-r,y-r,x+r,y+r)
  118.     canv.coords(p1,x1,y1,x1+w,y1+h)
  119.     canv.coords(p2,x2,y2,x2+w,y2+h)
  120.     canv.update()
  121.     root.after(30,tick)
  122.  
  123.  
  124. def keyPressed(event):
  125.     global vy2,vy1,vx,vy
  126.     if event.keycode == 38:
  127.         vy2 -= 12
  128.     elif event.keycode == 40:
  129.         vy2 += 12
  130.     elif event.keycode == 87:
  131.         vy1 -= 12
  132.     elif event.keycode == 83:
  133.         vy1 += 12
  134.     elif event.keycode == 32:
  135.         vx = 7
  136.         if rnd(2) == 0:
  137.             vx = -vx
  138.         vy = rnd(-3,3)
  139.        
  140.    
  141.    
  142. root.bind('<Key>',keyPressed)  
  143.  
  144. tick()
  145.  
  146. mainloop()
  147.  
  148.  
  149. #~ 38 up
  150. #~ 40 down
  151. #~ 87 w
  152. #~ 83 s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement