Advertisement
here2share

# Tk_pong_vs_cpu.py

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