Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_pong_vs_cpu.py
- from Tkinter import *
- from random import randrange as rnd, choice
- import sys
- root = Tk()
- root.geometry('600x600+100+100')
- canv = Canvas(bg='white')
- canv.pack(fill=BOTH,expand=1)
- class Ball():
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.r = 12
- self.vx = 0
- self.vy = 5
- self.color = 'orange'
- 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)
- self.update()
- def update(self):
- if mode != 'pause':
- canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
- self.x += self.vx
- self.y += self.vy
- if self.x >= 550 or self.x <= 50:
- self.vx *= -1
- if self.y > 600:
- goal('down')
- if self.y < 0:
- goal('up')
- root.after(30,self.update)
- class Platform():
- def __init__(self,y):
- self.y = y
- self.h = 20
- self.w = 80
- self.x = 300-self.w/2
- self.vx = 0
- self.points = 0
- self.id = canv.create_rectangle(self.x,self.y,self.x+self.w,self.y+self.h,fill = 'green')
- self.update()
- def update(self):
- if mode != 'pause':
- self.x += self.vx
- self.vx *= 0.8
- canv.coords(self.id,self.x,self.y,self.x+self.w,self.y+self.h)
- root.after(30,self.update)
- def auto(self):
- if j.x < self.x:
- self.vx -= 3
- if j.x > self.x+self.w:
- self.vx += 3
- root.after(40,self.auto)
- def goal(side):
- global mode
- if side == 'up':
- p_down.points += 1
- else:
- p_up.points += 1
- mode = 'goal_'+side
- j.vx = 0
- j.vy = 0
- j.x = 300
- j.y = 300
- canv.delete('points')
- canv.create_text(300,200,text=p_up.points, font = 'Tahoma 40', tag = 'points')
- canv.create_text(300,400,text=p_down.points, font = 'Tahoma 40', tag = 'points')
- def keyDown(event):
- global mode
- #print(event.keycode)
- keys.add(event.keycode)
- if mode == 'pause' and event.keycode == 88:
- showMenu()
- if event.keycode == 27:
- if mode != 'game':
- mode = 'game'
- canv.delete('pause')
- elif mode == 'game':
- mode = 'pause'
- canv.create_text(300,300,text='PAUSE', font = 'Tahoma 40', tag = 'pause')
- def keyUp(event):
- keys.remove(event.keycode)
- def update():
- global mode
- if 65 in keys:
- p_up.vx = -5
- if 68 in keys:
- p_up.vx = 5
- if 37 in keys:
- p_down.vx = -5
- if 39 in keys:
- p_down.vx = 5
- 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:
- j.vy *= -1
- j.vx = (j.x - (p_down.x + p_down.w/2))/3
- 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:
- j.vy *= -1
- j.vx = (j.x-(p_up.x + p_up.w/2))/3
- if 32 in keys and 'goal' in mode:
- if 'up' in mode:
- j.vy = 5
- if 'down' in mode:
- j.vy = -5
- mode = 'game'
- root.after(30,update)
- def startGame(event=0):
- global mode,keys,p_down,p_up,j
- canv.delete(ALL)
- mode = 'game'
- keys = set()
- j = Ball(300,300)
- p_up = Platform(50)
- p_up.auto()
- p_down = Platform(550)
- update()
- root.bind('<Key>',keyDown)
- root.bind('<KeyRelease>',keyUp)
- canv.bind('<1>','')
- def menuClick(event):
- if event.x > 200 and event.x < 400 and event.y > 200 and event.y < 280:
- startGame()
- if 200 < event.x < 400 and 300 < event.y < 380:
- sys.exit()
- def showMenu():
- global mode
- mode = 'menu'
- canv.delete(ALL)
- canv.bind('<1>',menuClick)
- root.bind('<Key>','')
- root.bind('<KeyRelease>','')
- canv.create_rectangle(200,200,400,280,fill='lightgreen')
- canv.create_rectangle(200,300,400,380,fill='lightgreen')
- canv.create_text(300,240,text = 'New Game')
- canv.create_text(300,340,text = 'Exit')
- startGame()
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement