Advertisement
boris-vlasenko

ball_game

Apr 19th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. from tkinter import *
  2. import time
  3. from random import randrange as rnd, random, choice
  4.  
  5. root = Tk()
  6. fr = Frame(root)
  7. root.geometry('800x600')
  8. canv = Canvas(root, bg = 'white')
  9. canv.pack(fill=BOTH,expand=1)
  10. colors = ['red', 'orange', 'yellow', 'green', 'blue']
  11.  
  12.  
  13. def click(event):
  14.     j.vx = (event.x - j.x)/10
  15.     j.vy = (event.y - j.y)/10
  16.      
  17. canv.bind('<1>',click)
  18.  
  19.  
  20.    
  21.  
  22. class Jumper():
  23.     def __init__(self):
  24.         self.x = 40
  25.         self.y = 150
  26.         self.r = 20
  27.         self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill='orange')
  28.         self.vy = 5
  29.         self.vx = 6
  30.  
  31.        
  32.  
  33.  
  34.     def move(self):
  35.        
  36.         self.y += self.vy
  37.         self.x += self.vx
  38.  
  39.         if self.x > 750:
  40.             self.x = 750
  41.             self.vx *= -1
  42.         if self.x < 50:
  43.             self.x = 50
  44.             self.vx *= -1
  45.            
  46.        
  47.  
  48.         canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
  49.          
  50. class Platform():
  51.     def __init__(self):
  52.         self.x = 300
  53.         self.y = 550
  54.         self.ax = 0
  55.         self.w = 120
  56.         self.h = 20
  57.         self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill='green',width=0)
  58.         self.vx = 0
  59.         self.vy = 0
  60.         if self.x < 50:
  61.             self.ax = 0
  62.             self.vx = 0
  63.         if self.x > 750:
  64.             self.ax = 0
  65.             self.vx = 0
  66.         def plat(self):
  67.             self.vx -= 30
  68.    
  69.     def auto(self,target):
  70.         print('a')
  71.         if target.x > self.x+self.w/2:
  72.             self.ax = 1.2
  73.         else:
  74.             self.ax = -1.2
  75.         self.move()
  76.        
  77.     def move(self):
  78.         self.vx += self.ax
  79.         self.x += self.vx
  80.         self.vx *= 0.9
  81.         if self.x+self.w > 750:
  82.             self.x = 750-self.w
  83.         if self.x < 50:
  84.             self.x = 50
  85.         if self.x <= j.x+j.r and j.x-j.r < self.x +self.w and self.y <= j.y+j.r and j.y-j.r <= self.y+self.h:      
  86.             j.vy *= -1
  87.             j.vx += (j.x-(self.x+self.w/2))/7
  88.            
  89.         canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
  90.  
  91. j = Jumper()
  92. pp1 = Platform()
  93. pp2 = Platform()
  94.  
  95.  
  96.  
  97. pp2.y = 50
  98. pp1.y = 550
  99.  
  100.  
  101. def keyDown(event):
  102.     print(event.keycode)
  103.     keys.add(event.keycode)
  104.  
  105. def keyUp(event):
  106.     keys.remove(event.keycode)
  107.  
  108. root.bind('<Key>',keyDown)
  109. root.bind('<KeyRelease>',keyUp)
  110.  
  111. keys = set()
  112.  
  113.  
  114. while 1:
  115.     pp1.ax = 0
  116.     #pp2.ax = 0
  117.    
  118.     if 113 in keys:
  119.         pp1.ax = -1
  120.     if 114 in keys:
  121.         pp1.ax = 1
  122.     #if 38 in keys:
  123.     #   pp2.ax = -1
  124.     #if 40 in keys:
  125.     #   pp2.ax = 1
  126.        
  127.    
  128.    
  129.    
  130.     j.move()
  131.    
  132.     pp1.move()
  133.     pp2.auto(j)
  134.    
  135.        
  136.     canv.update()
  137.     time.sleep(0.03)
  138. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement