1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| from tkinter import *
from random import *
fen= Tk()
fen.title("Pong")
p1,p2,xb,yb,yj1,yj2=0,0,350,150,0,0
x2=randint(-2,2)
y2=randint(-2,2)
while (y2==0 and x2==0) or x2==0 or y2==0:
x2=randint(-2,2)
y2=randint(-2,2)
def mouvement(event):
global yj1,yj2
if yj1<250 and event.keysym=='Down':
yj1+=10
can.coords(rectangle, 695,yj1, 700, 50+yj1)
if yj1>0 and event.keysym=='Up':
yj1-=10
can.coords(rectangle, 695,yj1, 700, 50+yj1)
if yj2<250 and event.keysym=='s':
yj2+=10
can.coords(rectangle1,0,yj2, 5, 50+yj2)
if yj2>0 and event.keysym=='z':
yj2-=10
can.coords(rectangle1, 0,yj2, 5, 50+yj2)
def start():
global xb,yb,x2,y2,p1,p2
xb=xb+x2
yb=yb+y2
can.coords(boule,xb-5,yb-5,xb+5,yb+5)
if xb+10>=700 and yb>yj1 and yb<yj1+50:
x2=-x2
if xb+10>=700 and (yb<yj1 or yb>yj1+50):
p2+=1
xb=350
yb=150
can.coords(boule,xb-5,yb-5,xb+5,yb+5)
score.configure(text=str(p2)+' - '+str(p1))
x2=randint(-2,2)
y2=randint(-2,2)
while (y2==0 and x2==0) or x2==0 or y2==0:
x2=randint(-2,2)
y2=randint(-2,2)
fen.after(10)
xb=xb+x2
yb=yb+y2
can.coords(boule,xb-5,yb-5,xb+5,yb+5)
if yb+5>=300:
y2=-y2
if xb-10<=0 and yb>yj2 and yb<yj2+50:
x2=-x2
if xb-10<=0 and (yb<yj2 or yb>yj2+50):
p1+=1
xb=350
yb=150
can.coords(boule,xb-5,yb-5,xb+5,yb+5)
score.configure(text=str(p2)+' - '+str(p1))
x2=randint(-2,2)
y2=randint(-2,2)
while (y2==0 and x2==0) or x2==0 or y2==0:
x2=randint(-2,2)
y2=randint(-2,2)
fen.after(10)
xb=xb+x2
yb=yb+y2
can.coords(boule,xb-5,yb-5,xb+5,yb+5)
if yb-5<=0:
y2=-y2
fen.after(3,start)
can= Canvas(fen, bg="Black", width=700, height=300)
can.grid(row=1,column=0, columnspan=3, rowspan=3)
rectangle= can.create_rectangle(695,0,700,50, fill='white')
rectangle1= can.create_rectangle(0,0,5,50, fill='white')
boule= can.create_oval(xb-5,yb-5,xb+5,yb+5, fill="blue")
score= Label(fen, text=str(p2)+' - '+str(p1))
score.grid(row=0, column=1)
Button(fen, text="Start", command=start,).grid(row=4, column=1)
fen.bind('<Key>', mouvement)
fen.mainloop() |
Partager