Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import time, sys
- from random import randrange as rnd
- root = Tk()
- root.geometry('800x600')
- canv = Canvas(bg='white')
- canv.pack(fill=BOTH,expand=1)
- x = 400
- y = 300
- r = 20
- vx = 0
- vy = 0
- w = 20
- h = 120
- x1 = 10
- y1 = 300-h/2
- p1 = canv.create_rectangle(x1,y1,x1+w,y1+h,fill='green')
- x2 = 770
- y2 = y1
- p2 = canv.create_rectangle(x2,y2,x2+w,y2+h,fill='green')
- vy1 = 0
- vy2 = 0
- leftpoints = 0
- rightpoints = 0
- lp = canv.create_text(200,300,text = leftpoints, font='Arial 240',fill='lightgray')
- rp = canv.create_text(600,300,text = rightpoints, font='Arial 240',fill='lightgray')
- canv.create_text(400,273,text = ':', font='Arial 240',fill='lightgray')
- ball = canv.create_oval(x-r,y-r,x+r,y+r,fill='orange')
- def endgame(text):
- root.unbind('<Key>')
- canv.create_text(400,100,text = text+' is win', font='Arial 140')
- def stopgame(text):
- global x,y,vx,vy,y1,y2,vy1,vy2,leftpoints,rightpoints
- if text == 'left fail':
- rightpoints += 1
- if rightpoints > 5:
- endgame('right')
- elif text == 'right fail':
- leftpoints += 1
- if leftpoints > 5:
- endgame('left')
- canv.itemconfig(lp,text=leftpoints)
- canv.itemconfig(rp,text=rightpoints)
- x = 400
- y = 300
- vx = 0
- vy = 0
- y1 = 300-h/2
- y2 = y1
- vy1 = 0
- vy2 = 0
- def tick():
- global x,y,vx,vy,y2,y1,vy1,vy2
- # ---ball----vvvvv
- x += vx
- y += vy
- if y > 590-r/2-vy:
- y = 590-r/2
- vy *= -1
- if y < 10+vy+r/2:
- y = 10+r/2
- vy *= -1
- if x > 800:
- stopgame('right fail')
- if x < 0:
- stopgame('left fail')
- # ---ball----^^^^^^
- y1 += vy1
- vy1 *= 0.9
- y2 += vy2
- vy2 *= 0.9
- if y2 < 0:
- y2 = 0
- vy2 = 0
- if y1 < 0:
- y1 = 0
- vy1 = 0
- if y2+h > 600:
- y2 = 600-h
- vy2 = 0
- if y1+h > 600:
- y1 = 600-h
- vy1 = 0
- if x-r/2 < x1+w and y1 < y < y1+h:
- vx=-vx
- if x+r/2 > x2 and y2 < y < y2+h:
- vx=-vx
- canv.coords(ball,x-r,y-r,x+r,y+r)
- canv.coords(p1,x1,y1,x1+w,y1+h)
- canv.coords(p2,x2,y2,x2+w,y2+h)
- canv.update()
- root.after(30,tick)
- def keyPressed(event):
- global vy2,vy1,vx,vy
- if event.keycode == 38:
- vy2 -= 12
- elif event.keycode == 40:
- vy2 += 12
- elif event.keycode == 87:
- vy1 -= 12
- elif event.keycode == 83:
- vy1 += 12
- elif event.keycode == 32:
- vx = 7
- if rnd(2) == 0:
- vx = -vx
- vy = rnd(-3,3)
- root.bind('<Key>',keyPressed)
- tick()
- mainloop()
- #~ 38 up
- #~ 40 down
- #~ 87 w
- #~ 83 s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement