boris-vlasenko

аэрохоккей

Dec 6th, 2016
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.58 KB | None | 0 0
  1. from tkinter import *
  2. from random import randrange as rnd, choice
  3. import sys
  4.  
  5. root = Tk()
  6. root.geometry('600x600+100+100')
  7. canv = Canvas(bg='white')
  8. canv.pack(fill=BOTH,expand=1)
  9.  
  10.  
  11. class Ball():
  12.     def __init__(self,x,y):
  13.         self.x = x
  14.         self.y = y
  15.         self.r = 12
  16.         self.vx = 0
  17.         self.vy = 5
  18.         self.color = 'orange'
  19.         self.id = canv.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r, fill= self.color, width = 0)
  20.         self.update()
  21.  
  22.        
  23.     def update(self):
  24.         if mode != 'pause':
  25.             canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
  26.             self.x += self.vx
  27.             self.y += self.vy
  28.             if self.x >= 550 or self.x <= 50:
  29.                 self.vx *= -1
  30.            
  31.             if self.y > 600:
  32.                 goal('down')
  33.                
  34.             if self.y < 0:
  35.                 goal('up')
  36.        
  37.         root.after(30,self.update)
  38.            
  39. class Platform():
  40.     def __init__(self,y):
  41.         self.y = y
  42.         self.h = 20
  43.         self.w = 80
  44.         self.x = 300-self.w/2
  45.         self.vx = 0
  46.         self.points = 0
  47.         self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill = 'green')
  48.         self.update()
  49.    
  50.     def update(self):
  51.         if mode != 'pause':
  52.             self.x += self.vx
  53.             self.vx *= 0.8
  54.             canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
  55.         root.after(30,self.update)
  56.        
  57.     def auto(self):
  58.         if j.x < self.x:
  59.             self.vx -= 3
  60.         if j.x > self.x+self.w:
  61.             self.vx += 3
  62.         root.after(40,self.auto)
  63.        
  64. def goal(side):
  65.     global mode
  66.    
  67.     if side == 'up':
  68.         p_down.points += 1
  69.     else:
  70.         p_up.points += 1
  71.     mode = 'goal_'+side
  72.     j.vx = 0
  73.     j.vy = 0
  74.     j.x = 300
  75.     j.y = 300
  76.    
  77.    
  78.     canv.delete('points')
  79.     canv.create_text(300,200,text=p_up.points, font = 'Tahoma 40', tag = 'points')
  80.     canv.create_text(300,400,text=p_down.points, font = 'Tahoma 40', tag = 'points')
  81.    
  82. def keyDown(event):
  83.     global mode
  84.     #print(event.keycode)
  85.     keys.add(event.keycode)
  86.     if mode == 'pause' and event.keycode == 88:
  87.         showMenu()
  88.     if event.keycode == 27:
  89.         if mode != 'game':
  90.             mode = 'game'
  91.             canv.delete('pause')
  92.         elif mode == 'game':
  93.             mode = 'pause'
  94.             canv.create_text(300,300,text='PAUSE', font = 'Tahoma 40', tag = 'pause')
  95.    
  96. def keyUp(event):
  97.     keys.remove(event.keycode)     
  98.        
  99.        
  100. def update():
  101.     global mode
  102.     if 65 in keys:
  103.         p_up.vx = -5
  104.     if 68 in keys:
  105.         p_up.vx = 5
  106.  
  107.     if 37 in keys:
  108.         p_down.vx = -5
  109.     if 39 in keys:
  110.         p_down.vx = 5  
  111.        
  112.     if p_down.x <= j.x <= p_down.x + p_down.w and p_down.y <= j.y+j.r <= p_down.y + p_down.h:
  113.         j.vy *= -1
  114.         j.vx = (j.x - (p_down.x + p_down.w/2))/3
  115.  
  116.     if p_up.x <= j.x <= p_up.x + p_up.w and p_up.y <= j.y-j.r <= p_up.y + p_up.h:
  117.         j.vy *= -1
  118.         j.vx = (j.x-(p_up.x + p_up.w/2))/3
  119.                
  120.        
  121.        
  122.     if 32 in keys and 'goal' in mode:
  123.         if 'up' in mode:
  124.             j.vy = 5
  125.         if 'down' in mode:
  126.             j.vy = -5
  127.         mode = 'game'
  128.        
  129.            
  130.  
  131.     root.after(30,update)  
  132.    
  133. def startGame(event=0):    
  134.     global mode,keys,p_down,p_up,j
  135.     canv.delete(ALL)
  136.     mode = 'game'
  137.     keys = set()   
  138.     j = Ball(300,300)
  139.     p_up = Platform(50)
  140.     p_up.auto()
  141.     p_down = Platform(550)
  142.     update()
  143.  
  144.     root.bind('<Key>',keyDown)
  145.     root.bind('<KeyRelease>',keyUp)
  146.     canv.bind('<1>','')
  147.  
  148. def menuClick(event):
  149.     if event.x > 200 and event.x < 400 and event.y > 200 and event.y < 280:
  150.         startGame()
  151.     if 200 < event.x < 400 and 300 < event.y < 380:
  152.         sys.exit()
  153.    
  154. def showMenu():
  155.     global mode
  156.     mode = 'menu'
  157.     canv.delete(ALL)
  158.     canv.bind('<1>',menuClick)
  159.     root.bind('<Key>','')
  160.     root.bind('<KeyRelease>','')
  161.     canv.create_rectangle(200,200,400,280,fill='lightgreen')
  162.     canv.create_rectangle(200,300,400,380,fill='lightgreen')
  163.     canv.create_text(300,240,text = 'New Game')
  164.     canv.create_text(300,340,text = 'Exit')
  165. startGame()
  166.        
  167. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment