# pong.py

Created by fedyna-kevin

Created on August 10, 2020

4.83 KB

```from ion import keydown
from time import *
from random import randint
from kandinsky import draw_string, fill_rect
from math import cos,sin,radians

try:
from kandinsky import get_keys
clr = (192, 53, 53)
except:
clr = (255,183,52)

def start():
fill_rect(0,0,320,222,(24,)*3)
draw_string("PONG",140,50,(255,)*3,(24,)*3)
draw_string("nsi.xyz",125,70,(180,)*3,(24,)*3)
j = 0
d = 1
while not (keydown(4) or keydown(52)):
draw_string("1 joueur",120,110,1-j and clr or (180,)*3,(24,)*3)
draw_string("Facile",50,130, d==0 and clr or (180,)*3,(24,)*3)
draw_string("Moyen",120,130, d==1 and clr or (180,)*3,(24,)*3)
draw_string("Difficile",180,130, d==2 and clr or (180,)*3,(24,)*3)
draw_string("2 joueurs",115,160,j and clr or (180,)*3,(24,)*3)
if keydown(1) or keydown(2):
j = 1-j
sleep(0.12)
if keydown(0) and not j:
d = (d-1) % 3
sleep(0.12)
if keydown(3) and not j:
d = (d+1) % 3
sleep(0.12)
fill_rect(0,0,320,222,(24,)*3)
fill_rect(30,18,260,2,(255,)*3)
fill_rect(30,201,260,2,(255,)*3)
return j,d

def rebond(a,b,f=12):
angle = (-a+2*b)%360
if b == 90:
return (180*(90<angle<270)-(45-(45/12)*(abs(f-24*(not 90<angle<270)))))%360
else:
return angle

def deplacement(angle,x,y):
f = 1/max(abs(dx),abs(dy))
return x+dx*f,y-dy*f

def difficulties(d, angle):
angle = abs((90 - ((angle + 90) % 180)) / 90 * 10)
if d == 0:
if angle > 2.5 and randint(1, 5) % 2:
dist = 15
else:
dist = 3
elif d == 1:
if angle > 2.5 and randint(1, 5) == 2:
dist = 15
else:
dist = 6
elif d == 2:
if angle > 2.5 and randint(1, 8) == 2:
dist = 15
else:
dist = 9
return int(angle) + dist

def ia(x,y,angle,o):
global xf,yf
if not xf and not yf:
while 33<x<287:
x,y = deplacement(angle,x,y)
if not(21<y<200):
angle = rebond(angle,0)
x,y = deplacement(angle,x,y)
xf,yf = x,y
return 1*(yf/(o+12)>1 and o<177)-1*(yf/(o+12)<1 and o>20)

def move(x, y, dy):
fill_rect(x,y+24*(dy != 1),3,1,(24,)*3)
y += dy
fill_rect(x,y+24*(dy == 1),3,1,(255,)*3)
return y

def jeu(pvp, d):
global xf,yf
pt1, pt2 = 0, 0
begin = 0
while pt1 != 11 and pt2 != 11:
xf, yf = 0, 0
angle = 180*begin
x = 160
y = 111
j = 99
up, do = 1+pvp*29 , 2+pvp*34
o = 99
err = difficulties(d, angle)
fill_rect(30,j,3,24,(255,)*3)
fill_rect(287,o,3,24,(255,)*3)
for i in range(21):
if i != 10:
fill_rect(i*15+5, 4, 10, 10, i+1 <= pt1 and (255, 0, 0) or i >= 21-pt2 and (0,0,255) or (180,)*3)
else:
fill_rect(i*15+3, 2, 14, 14, 11 in (pt1, pt2) and clr or (180,)*3)
sleep(2)
while 1:
depart = monotonic()
while monotonic()<depart + 0.002 - 0.0005*pvp:
if pvp:
if keydown(34) and o > 20:
o = move(287, o, -1)
if keydown(40) and o < 176:
o = move(287, o, 1)
else:
if not 90 < angle < 270 and not(o+12-err<yf<o+12+err):
o = move(287, o, ia(x,y,angle,o))
if keydown(up) and j > 20:
j = move(30, j, -1)
if keydown(do) and j < 176:
j = move(30, j, 1)

fill_rect(int(x)-1,int(y)-1,3,3,(24,)*3)
x,y = deplacement(angle,x,y)
if not(21<y<200):
angle = rebond(angle,0)
x,y = deplacement(angle,x,y)
if 30<x<34 and j<=y<=j+24:
xf,yf = 0,0
angle = rebond(angle,90,y-j)
err = difficulties(d, angle)
x,y = deplacement(angle,x,y)
if 283<x<287 and o<=y<=o+24:
xf,yf = 0,0
angle = rebond(angle,90,y-o)
err = difficulties(d, angle)
x,y = deplacement(angle,x,y)
if 30>x:
pt2 += 1
begin = 1-begin
fill_rect(30, j, 3, 25, (24,) * 3)
fill_rect(287, o, 3, 25, (24,) * 3)
break
if 287<x:
pt1 += 1
begin = 1 - begin
fill_rect(30, j, 3, 25, (24,) * 3)
fill_rect(287, o, 3, 25, (24,) * 3)
break
fill_rect(int(x)-1,int(y)-1,3,3,(255,)*3)
draw_string("Victoire du joueur "+str(1+(pt2 == 11)),60,100,clr,(24,)*3)
sleep(3)

r = 1
while r:
jeu(*start())```