pong.py

Created by dkfe123

Created on February 26, 2025

4.83 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),(125,125,125)]
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])
  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])
    else:
      if n[i]==2:
        fill_rect(32*(i%10)+2,de+16*floor(i/10)+2,28,12,co[2])
  fill_rect(floor(160-l/2),210,l,5,co[1])

def to(x,y,n):
  z=0
  su=[1000,1000]
  for i in range(len(n)-1,-1,-1):
    if z==0:
      if n[i]==0:
        n[i]=-1
        su.append(i)
      else:
        z=1
    if n[i]>0:
      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:
          if n[i]==1:
            n[i]=0
            fill_rect(32*(i%10)+2,16*floor(i/10)+2+de,28,12,co[0])
          for w in range(2,len(su)):
            n.pop(su[i])
          return(1,n)
  for i in range(2,len(su)):
    if i<=len(n):
      n.pop(su[i])
  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
  ad=d
  ap=px
  ax=floor(x)
  ay=floor(y)
  x=x+sin(d)*3
  t,n=to(x,y,n)
  if t==1:
    x=x-sin(d)*3
    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(ad)*3
  t,n=to(x,y,n)
  if t==1:
    s=s+10
    y=y-cos(ad)*3
    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)
  m=0
  if y>230-c:
    x,y,d,px,ni=init(c,l)
    b=0
    m=1
    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,m)

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)

def attt():
  while True:
    if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
      pass
    else:
      return None

def att():
  while True:
    if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
      return None

def res():
  while True:
    if keydown(KEY_EXE) or keydown(KEY_OK):
      return None
bb=1
m=0

def adv():
  for o in range(0,10000,10):
    c=(o/((o%10)+1),o,o/((o%3)+1))
    fill_rect(0,0,50,240,c)
    fill_rect(0,0,320,50,c)
    fill_rect(0,65,320,240,c)
    fill_rect(50+10*len("dkfe123"),50,320,240,c)
    draw_string("dkfe123",50,50,(0,0,0),c)
vu=0
while True:
  score(s)
  if b==1:
    x,y,s,d,px,n,vie,ni,b,m=game(x,y,s,d,px,n,ni,vie)
  if m==1:
    score(s)
    if vie>-1:
      attt()
      att()
  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 keydown(KEY_ZERO):
    if bb==0:
      draw_string("Pause  0",80,0,(255,255,255),(0,0,0))
      while keydown(KEY_ZERO):
        pass
      while not keydown(KEY_ZERO):
        pass
      fill_rect(80,0,20*len("Pause 0"),15,(0,0,0))
    bb=1
  else:
    bb=0
  if vie==-1:
    if vu==0:
      fill_rect(20,50,280,80,(125,125,125))
      draw_string("Watch ad to have new life",25,60,(255,255,255),(125,125,125))
      draw_string("Press OK",100,80,(255,255,255),(125,125,125))
    draw_string("EXE pour rejouer",50,100,(255,255,255),(0,0,0))
    score(s)
    kp=False
    res()
    if keydown(KEY_EXE):
      kp=True
    b=0
    if vu==0:
      if keydown(KEY_OK):
        adv()
        vu=1 
        kp=True
      else:
        s=0
    else:
      s=0
    if kp:
      x,y,d,px,ni=init(c,l)
      vie=3
      n=iniv(ni)
      niv(n)
      barre(vie,s)
    if keydown(KEY_EXE):
      vu=0

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.