pong4joueur.py

Created by loic-azavant

Created on December 07, 2022

8.32 KB

Un pong jouable à quatre joueur, plus vieux que mon autre pong


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

d=0

backcolor=(80,80,80)

blanc=(255,255,255)
noir=(0,0,0)
rouge=(255,0,0)
vert=(0,255,0)
jaune=(255,255,0)
bleu=(0,0,255)

yPlatformA=96
yPlatformB=96
xPlatformC=59
xPlatformD=59

scoreDeBase=10

scoreA=scoreDeBase
scoreB=scoreDeBase
scoreC=scoreDeBase
scoreD=scoreDeBase

taillePlatform=30
vitessePlatform=3

xBall=160
yBall=111
tailleBall=5
ballcolor=(250,180,0)
trajectoireDeLaBalle=[-1.75,-1.5,-1.25,-1,-0.75,0.75,1,1.25,1.5,1.75,2]
vXball=choice(trajectoireDeLaBalle)
vYball=choice(trajectoireDeLaBalle)
accelerationBall=0

tuPeuxJouer=False
tuAPerdu=False
menu=1
nbrJoueur=4
c=0

def bordure():
  fill_rect(0,0,49,222,noir)
  fill_rect(271,0,49,222,noir)
  draw_string(str(scoreA),10,10,vert,noir)
  if nbrJoueur>1:
    draw_string(str(scoreB),10,40,rouge,noir)
  if nbrJoueur>2:
    draw_string(str(scoreC),10,70,jaune,noir)
  if nbrJoueur>3:
    draw_string(str(scoreD),10,100,bleu,noir)

    
def ball(a):
  fill_rect(int(xBall),int(yBall),tailleBall,tailleBall,a)

def platformA():
  fill_rect(64,int(yPlatformA),5,taillePlatform,blanc)
  fill_rect(59,int(yPlatformA),5,taillePlatform,vert)
  fill_rect(59,int(yPlatformA)-vitessePlatform,10,vitessePlatform,backcolor)
  fill_rect(59,int(yPlatformA)+taillePlatform,10,vitessePlatform,backcolor)
  
def platformB():
  fill_rect(251,int(yPlatformB),5,taillePlatform,blanc)
  fill_rect(256,int(yPlatformB),5,taillePlatform,rouge)
  fill_rect(251,int(yPlatformB)-vitessePlatform,10,vitessePlatform,backcolor)
  fill_rect(251,int(yPlatformB)+taillePlatform,10,vitessePlatform,backcolor)
  
def platformC():
  fill_rect(int(xPlatformC),10,taillePlatform,5,jaune)
  fill_rect(int(xPlatformC),15,taillePlatform,5,blanc)
  fill_rect(int(xPlatformC)-vitessePlatform,10,vitessePlatform,10,backcolor)
  fill_rect(int(xPlatformC)+taillePlatform,10,vitessePlatform,10,backcolor)
  
def platformD():
  fill_rect(int(xPlatformD),200,taillePlatform,5,blanc)
  fill_rect(int(xPlatformD),205,taillePlatform,5,bleu)
  fill_rect(int(xPlatformD)-vitessePlatform,200,vitessePlatform,10,backcolor)
  fill_rect(int(xPlatformD)+taillePlatform,200,vitessePlatform,10,backcolor)

while True:
  
  if tuPeuxJouer==False:
    if menu==1:
      print("1) Jouer\n2) Nombre de joueur\n3) Reglage\n")
      c=int(input())
      print("\n ============ \n")
      if c==1:
        tuPeuxJouer=True
        fill_rect(0,0,320,222,backcolor)
        bordure()
        if nbrJoueur==1:
          scoreA=0
        yPlatformA=111-int(taillePlatform/2)
        yPlatformB=111-int(taillePlatform/2)
        xPlatformC=160-int(taillePlatform/2)
        xPlatformD=160-int(taillePlatform/2)
        vXball=choice(trajectoireDeLaBalle)
        vYball=choice(trajectoireDeLaBalle)
      elif c==2:
        menu=2
      elif c==3:
        menu=3
      else:
        print(" ERROR \n ============ \n")
        menu=1
    if menu==2:
      nbrJoueur=int(input("Nombre de joueur: "))
      print("\n ============ \n")
      if nbrJoueur<0 or nbrJoueur>4:
        print("ERROR \n\n ============\n")
      else:
        menu=1
    if menu==3:
      print("1) Score de base\n2) Taille des plateformes\n3) Vitesse des plateformes\n4) Taille de la balle\n5) Retour\n")
      c=int(input())
      print("\n ============ \n")
      if c==1:
        menu=11
      elif c==2:
        menu=12
      elif c==3:
        menu=13
      elif c==4:
        menu=14
      elif c==5:
        menu=1
      else:
        print("ERROR \n\n ============\n")
    if menu==11:
      scoreDeBase=int(input("Score de base: "))
      scoreA=scoreDeBase
      scoreB=scoreDeBase
      scoreC=scoreDeBase
      scoreD=scoreDeBase
      print("\n ============ \n")
      if 0<scoreDeBase<41:
        menu=3
      else:
        print("ERROR \n\n ============\n")
    if menu==12:
      taillePlatform=int(input("Taille des plateformes: "))
      print("\n ============ \n")
      if 0<taillePlatform<201:
        menu=3
      else:
        print("ERROR \n\n ============\n")
    if menu==13:
      vitessePlatform=int(input("Vitesse des plateformes: "))
      print("\n ============ \n")
      if 0<vitessePlatform<10:
        menu=3
      else:
        print("ERROR \n\n ============\n")
    if menu==14:
      tailleBall=int(input("Taille de la balle: "))
      print("\n ============ \n")
      if 0<tailleBall<31:
        menu=3
      else:
        print("ERROR \n\n ============\n")
    if menu==20:
      print("Tu as perdu\nTu as rebondis",scoreA,"fois\n\n1) Rejouer\n2) Quitter\n")
      c=int(input())
      if c==1:
        scoreA=0
        tuPeuxJouer=True
        tuAPerdu=False
        fill_rect(0,0,320,222,backcolor)
      elif c==2:
        menu=1
      else:
        print("ERROR \n\n ============\n")
    if menu==21:
      if scoreA==0:
        print("Le joueur vert a perdu")
      elif scoreB==0:
        print("Le joueur rouge a perdu")
      elif scoreC==0:
        print("Le joueur jaune a perdu")
      elif scoreD==0:
        print("Le joueur bleu a perdu")
      print("\n1) Rejouer\n2) Quitter\n")
      c=int(input())
      print("\n ============ \n")
      if c==1:
        scoreA=scoreDeBase
        scoreB=scoreDeBase
        scoreC=scoreDeBase
        scoreD=scoreDeBase
        tuPeuxJouer=True
        tuAPerdu=False
        fill_rect(0,0,320,222,backcolor)
      elif c==2:
        menu=1
      else:
        print("ERROR \n\n ============\n")
      



  if tuPeuxJouer==True:
    if keydown(KEY_EXP) and yPlatformA>0:
      yPlatformA-=(vitessePlatform/4)*nbrJoueur
    if keydown(KEY_SEVEN) and yPlatformA+taillePlatform<222:
      yPlatformA+=(vitessePlatform/4)*nbrJoueur
    platformA()
    
    if nbrJoueur>1:
      if keydown(KEY_POWER) and yPlatformB>0:
        yPlatformB-=(vitessePlatform/4)*nbrJoueur
      if keydown(KEY_RIGHTPARENTHESIS) and yPlatformB+taillePlatform<220:
        yPlatformB+=(vitessePlatform/4)*nbrJoueur
      platformB()
      
    if nbrJoueur>2:
      if keydown(KEY_ZERO) and xPlatformC>49:
        xPlatformC-=(vitessePlatform/4)*nbrJoueur
      if keydown(KEY_DOT) and xPlatformC+taillePlatform<271:
        xPlatformC+=(vitessePlatform/4)*nbrJoueur
      platformC()
        
    if nbrJoueur>3:
      if keydown(KEY_ANS) and xPlatformD>49:
        xPlatformD-=(vitessePlatform/4)*nbrJoueur
      if keydown(KEY_EXE) and xPlatformD+taillePlatform<271:
        xPlatformD+=(vitessePlatform/4)*nbrJoueur
      platformD()
      
    ball(backcolor)
    xBall+=(vXball/4)*nbrJoueur
    yBall+=(vYball/4)*nbrJoueur
    ball(ballcolor)
      


    for i in range(tailleBall):
      if (xBall<70 and get_pixel(int(xBall)-1,int(yBall)+i)==(248,252,248)) or (xBall+tailleBall>250 and get_pixel(int(xBall)+tailleBall,int(yBall)+i)==(248,252,248)):
        vXball=-vXball
        if (-3)<vXball<3 and (-3)<vYball<3:
          vXball=vXball*1.05
          vYball=vYball*1.05
        if nbrJoueur==1:
          scoreA+=1
        break
      if (yBall<21 and get_pixel(int(xBall)+i,int(yBall)-1)==(248,252,248)) or (yBall+tailleBall>199 and get_pixel(int(xBall)+i,int(yBall)+tailleBall)==(248,252,248)):
        vYball=-vYball
        if (-3)<vXball<3 and (-3)<vYball<3:
          vXball=vXball*1.05
          vYball=vYball*1.05
        break
    
    if xBall+tailleBall<49 :
      tuAPerdu=True
    if xBall+tailleBall>271:
      if nbrJoueur>1:
        tuAPerdu=True
      else:
        vXball=-vXball
        bordure()
    if yBall+tailleBall<0:
      if nbrJoueur>2:
        tuAPerdu=True
      else:
        vYball=-vYball
    if yBall>222:
      if nbrJoueur>3:
        tuAPerdu=True
      else:
        vYball=-vYball

    if tuAPerdu==True:
      if nbrJoueur==1:
        tuPeuxJouer=False
        menu=20
      if xBall+tailleBall>271:
        scoreB-=1
      if xBall+tailleBall<49 and nbrJoueur!=1:
    
        scoreA-=1
      if yBall>222:
        scoreD-=1
      if yBall+tailleBall<0:
        scoreC-=1
      xBall=160
      yBall=111
      vXball=choice(trajectoireDeLaBalle)
      vYball=choice(trajectoireDeLaBalle)
      yPlatformA=111-int(taillePlatform/2)
      yPlatformB=111-int(taillePlatform/2)
    
      xPlatformC=160-int(taillePlatform/2)
      xPlatformD=160-int(taillePlatform/2)
      fill_rect(0,0,320,222,backcolor)
      bordure()
      tuAPerdu=False
      sleep(1)
    
      
    bordure()
    
    if scoreA==0 and nbrJoueur>1 or scoreB==0 or scoreC==0 or scoreD==0:
      menu=21
      tuPeuxJouer=False
      tuAPerdu=False
      c=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.