pong.py

Created by danio

Created on April 22, 2020

2.06 KB


from math import *
from kandinsky import *
from ion import *
from time import *
from random import *

def won_lost():
  global score1, score2, w_l
  sleep(0.1)
  fill_rect(0,0,400,300,blanc)
  if xb<=0:
    score2+=1
    w_l[1]="YOU WON !"
    w_l[0]="YOU LOST..."
  elif xb>=330:
    score1+=1
    w_l[0]="YOU WON !"
    w_l[1]="YOU LOST..."
  stat()
  draw_string(str(w_l[0])+"       "+str(w_l[1]),20,100)

def stat():
  draw_string(str(score1)+" | "+str(score2),135,5)

def j1():
  global y1
  if y1<0:
    y1=0
  elif y1>180:
    y1=180
  if keydown(KEY_UP):
    y1-=8
    fill_rect(x1,y1+8,10,40,blanc)
  elif keydown(KEY_DOWN):
    y1+=8
    fill_rect(x1,y1-8,10,40,blanc)
  fill_rect(x1,y1,10,40,bleu)

def j2():
  global y2
  if y2<0:
    y2=0
  elif y2>180:
    y2=180
  if keydown(KEY_RIGHTPARENTHESIS) or keydown(KEY_OK):
    y2-=8
    fill_rect(x2,y2+8,10,40,blanc)
  elif keydown(KEY_DIVISION) or keydown(KEY_TOOLBOX):
    y2+=8
    fill_rect(x2,y2-8,10,40,blanc)
  fill_rect(x2,y2,10,40,bleu)

def decor():
  fill_rect(0,220,340,10,noir)

def balle():
  global xb
  global yb
  xb+=xd
  yb+=yd
  fill_rect(xb-xd,yb-yd,10,10,blanc)
  fill_rect(xb,yb,10,10,jaune)

def rebond():
  global xd
  global yd
  if yb<=0:
    yd=3
  elif yb>=210:
    yd=-3
  elif get_pixel(xb+5,yb)==bleu2:
    yd=3
  elif get_pixel(xb+5,yb+10)==bleu2:
    yd=-3
  elif get_pixel(xb+10,yb+5)==bleu2:
    xd=-3
  elif get_pixel(xb,yb+5)==bleu2:
    xd=3


def pong():
  global x1
  global y1
  global x2
  global y2
  global xb
  global yb
  global xd
  global yd
  x1,y1=2,90
  x2,y2=308,90
  xb,yb=155,105
  xd,yd=choice(dir_départ),choice(dir_départ)
  t1=monotonic()
  while xb>0 and xb<330:
    stat()
    t2=monotonic()
    dt=t2-t1
    j1()
    j2()
    rebond()
    balle()
    decor()
    print(get_pixel(xb+10,yb+5))
  won_lost()

bleu=color(10,10,200)
blanc=color(255,255,255)
noir=color(0,0,0)
rouge=color(255,0,0)
jaune=color(200,200,0)
bleu2=color(8,8,200)
score1,score2=0,0
w_l=[0,0]
dir_départ=[-3,3]
pong()
while True:
  if keydown(KEY_EXE):
    fill_rect(0,0,400,300,blanc)
    pong()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.