pong.py

Created by dkfe123

Created on February 04, 2025

3.14 KB

Flèches pour se déplacer


from math import *
from kandinsky import *
from ion import *
from random import *
de,c,l=20,7,60
co=[(0,0,0),(255,255,255)]
vie=3
s=0
pi=3.141
def init(c,l):
  return(160-floor(c/2),220-floor(c),pi/1.5,160-floor(l/2),randint(1,3))

def iniv(ni):
  if ni==1:
    return([1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1])
  if ni==2:
    return([0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,0])
  if ni==3:
    return([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1])

def niv(n):
  fill_rect(0,de,320,240,co[0])
  for i in range(0,len(n)):
    if n[i]==1:
      fill_rect(32*(i%10)+2,de+16*floor(i/10)+2,28,12,co[1])
  fill_rect(floor(160-l/2),210,l,5,co[1])

def to(x,y,n):
  for i in range(0,len(n)):
    if n[i]==1:
      if x+c>32*(i%10)+2 and x<32*(i%10)+28:
        if y+c>16*floor(i/10)+2+de and y<16*floor(i/10)+de+12:
          n[i]=0
          fill_rect(32*(i%10)+2,16*floor(i/10)+2+de,28,12,co[0])
          return(1,n)
  return(0,n)
b=0
x,y,d,px,ni=init(c,l)
n=iniv(ni)
niv(n)
def fin(n):
  for i in range(0,len(n)):
    if n[i]==1:
      return False
  return True

def barre(vie,s):
  fill_rect(0,0,320,de,co[0])
  if vie>0:
    for z in range(0,vie):
      fill_rect(15+z*20,5,10,10,co[1])
  fill_rect(0,de-2,320,2,co[1])
  return(0)

def game(x,y,s,d,px,n,ni,vie):
  t,b=0,1
  ap=px
  ax=floor(x)
  ay=floor(y)
  x=x+sin(d)*3
  t,n=to(x,y,n)
  if t==1:
    s=s+10
    d=0-d#+(randint(0,1)*2-1)*(pi/randint(14,30))
    if fin(n):
      x,y,d,px,ni=init(c,l)
      n=iniv(ni)
      b=0
      niv(n)
  y=y+cos(d)*3
  t,n=to(x,y,n)
  if t==1:
    s=s+10
    d=-(d-pi)
    if fin(n):
      x,y,d,px,ni=init(c,l)
      n=iniv(ni)
      b=0
      niv(n)
  if x<0:
    x=0
    d=0-d
  if x>320-c:
    x=320-c
    d=0-d
  if y<de:
    y=de
    d=-(d-pi)
  if y>230-c:
    x,y,d,px,ni=init(c,l)
    b=0
    vie=vie-1
    barre(vie,s)
  if keydown(KEY_LEFT):
    px=px-3
  if keydown(KEY_RIGHT):
    px=px+3
  if px<0:
    px=0
  if px>320-l:
    px=320-l
  if x+c>px and x<px+l:
    if y>210-c:
      y=210-c
      if keydown(KEY_LEFT): 
        d=-(d-pi)+pi/100
      else:
        if keydown(KEY_RIGHT):
          d=-(d-pi)-pi/100
        else:
          d=pi-d
  fill_rect(ax,ay,c,c,co[0])
  fill_rect(floor(x),floor(y),c,c,co[1])
  if ap!=px:
    fill_rect(floor(ap),210,l,5,co[0])
    fill_rect(floor(px),210,l,5,co[1])
  return(x,y,s,d,px,n,vie,ni,b)

def score(s):
  dd=20
  b=10
  draw_string(str(s),320-b*len(str(s)),de-dd,(255,255,255),(0,0,0))
barre(vie,s)

while True:
  score(s)
  if b==1:
    x,y,s,d,px,n,vie,ni,b=game(x,y,s,d,px,n,ni,vie)
  if b==0:
    if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
      b=1
      if keydown(KEY_LEFT):
        d=-pi/4
      else:
        d=pi/4
  if vie==-1:
    draw_string("EXE pour rejouer",50,0,(255,255,255),(0,0,0))
    score(s)
    while not keydown(KEY_EXE):
      pass
    b=0
    x,y,d,px,ni=init(c,l)
    s,vie=0,3
    n=iniv(ni)
    niv(n)
    barre(vie,s)

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.