morpion.py

Created by kbdechez-lidl

Created on January 26, 2023

5.75 KB


from math import *
from kandinsky import *
from ion import *
from time import *
###
def menu(j1,j2,coup):
  fill_rect(0,150,320,3,color(0,0,0))
  nbpartie=str(j1[1])
  draw_string("Joueur 1( ):"+str(j1[1])+"   Partie:"+nbpartie,0,165)
  draw_string(j1[0],90,165,j1[2])
  draw_string("Joueur 2( ):"+str(j2[1])+"   Coups:"+str(coup),0,190)
  draw_string(j2[0],90,190,j2[2])
def terrain():
  fill_rect(142,20,2,100,color(0,0,0))
  fill_rect(174,20,2,100,color(0,0,0))
  fill_rect(110,52,100,2,color(0,0,0))
  fill_rect(110,84,100,2,color(0,0,0))
def afficher(map):
  for i in range(len(map)):
    if map[i][0]==j1[0]:
      draw_string(str(map[i][0]),map[i][1],map[i][2],j1[2])
    if map[i][0]==j2[0]:
      draw_string(str(map[i][0]),map[i][1],map[i][2],j2[2])
def placer(n,tour,aqui,coup):
  if aqui[tour-1]==0:
    joueur=j1[0]
  else:
    joueur=j2[0]
  if map[n][0]!="":
    pass
  else:
    map[n][0]=joueur
    tour=tour+1
    coup=coup+1
  return map,tour,coup
def check(map,j1,j2):
  if map[0][0]==j1[0]==map[1][0]==j1[0]==map[2][0]==j1[0] or map[0][0]==j1[0]==map[4][0]==j1[0]==map[8][0]==j1[0] or map[0][0]==j1[0]== map[3][0]==j1[0]==map[6][0]==j1[0] or map[2][0]==j1[0]==map[5][0]==j1[0]==map[8][0]==j1[0] or map[6][0]==j1[0]==map[7][0]==j1[0]==map[8][0]==j1[0] or map[1][0]==j1[0]==map[4][0]==j1[0]==map[7][0]==j1[0] or map[3][0]==j1[0]==map[4][0]==j1[0]==map[5][0]==j1[0] or map[6][0]==j1[0]==map[7][0]==j1[0]==map[8][0]==j1[0] or map[6][0]==j1[0]==map[4][0]==j1[0]==map[2][0]==j1[0]:
    draw_string(j1[0]+" gagne!",120,75)
    sleep(0.8)
    return j1[0]
  if map[0][0]==j2[0]==map[1][0]==j2[0]==map[2][0]==j2[0] or map[0][0]==j2[0]==map[4][0]==j2[0]==map[8][0]==j2[0] or map[0][0]==j2[0]==map[3][0]==j2[0]==map[6][0]==j2[0] or map[2][0]==j2[0]==map[5][0]==j2[0]==map[8][0]==j2[0] or map[6][0]==j2[0]==map[7][0]==j2[0]==map[8][0]==j2[0] or map[1][0]==j2[0]==map[4][0]==j2[0]==map[7][0]==j2[0] or map[3][0]==j2[0]==map[4][0]==j2[0]==map[5][0]==j2[0] or map[6][0]==j2[0]==map[7][0]==j2[0]==map[8][0]==j2[0] or map[6][0]==j2[0]==map[4][0]==j2[0]==map[2][0]==j2[0]:
    draw_string(j2[0]+" gagne!",120,75)
    sleep(0.8)
    return j2[0]
  if map[0][0]!="" and map[1][0]!="" and map[2][0]!="" and map[3][0]!="" and map[4][0]!="" and map[5][0]!="" and map[6][0]!="" and map[7][0]!="" and map[8][0]!="":
    draw_string("Egalite!",120,75)
    sleep(0.8)
    return "draw"
  else:
    return 0
def jeu(map,j1,j2,coup):
  print(terrain())
  tour=1
  aqui=[0,1,0,1,0,1,0,1,0]
  while True:
    print(menu(j1,j2,coup))
    print(afficher(map))
    if check(map,j1,j2)==j1[0]:
      j1[1]=j1[1]+1
      return j1,j2,coup
    elif check(map,j1,j2)==j2[0]:
      j2[1]=j2[1]+1
      return j1,j2,coup
    elif check(map,j1,j2)=="draw":
      return j1,j2,coup
    if keydown(KEY_SEVEN)==True:
      sleep(0.1)
      map,tour,coup=placer(0,tour,aqui,coup)
    if keydown(KEY_EIGHT)==True:
      sleep(0.1)
      map,tour,coup=placer(1,tour,aqui,coup)
    if keydown(KEY_NINE)==True:
      sleep(0.1)
      map,tour,coup=placer(2,tour,aqui,coup)
    if keydown(KEY_FOUR)==True:
      sleep(0.1)
      map,tour,coup=placer(3,tour,aqui,coup)
    if keydown(KEY_FIVE)==True:
      sleep(0.1)
      map,tour,coup=placer(4,tour,aqui,coup)
    if keydown(KEY_SIX)==True:
      sleep(0.1)
      map,tour,coup=placer(5,tour,aqui,coup)
    if keydown(KEY_ONE)==True:
      sleep(0.1)
      map,tour,coup=placer(6,tour,aqui,coup)
    if keydown(KEY_TWO)==True:
      sleep(0.1)
      map,tour,coup=placer(7,tour,aqui,coup)
    if keydown(KEY_THREE)==True:
      sleep(0.1)
      map,tour,coup=placer(8,tour,aqui,coup)
    if keydown(KEY_EXE)==True:
      j1,j2=choix()  
def choix():
  alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"]
  couleur=['black','blue','red','yellow','green','orange','pink','purple','gray','brown']
  alph=0
  coul=0
  vari=0
  draw_string("[FLECHE] pour changer",0,0)
  while True:
    if vari==0:
      draw_string(alphabet[alph],90,165,couleur[coul])
      draw_string(j2[0],90,190,j2[2])
    else:
      draw_string(alphabet[alph],90,190,couleur[coul])
      draw_string(j1[0],90,165,j1[2])
    if keydown(KEY_DOWN)==True:
      sleep(0.1)
      alph=alph+1
      draw_string(" ",90,165)
      draw_string(" ",90,190)
    if keydown(KEY_UP)==True:
      sleep(0.1)
      alph=alph-1
      draw_string(" ",90,165)
      draw_string(" ",90,190)
    if alph<0:
      alph=35
    if alph>35:
      alph=0
    if keydown(KEY_RIGHT)==True:
      sleep(0.1)
      coul=coul+1
      draw_string(" ",90,165)
      draw_string(" ",90,190)
    if keydown(KEY_LEFT)==True:
      sleep(0.1)
      coul=coul-1
      draw_string(" ",90,165)
      draw_string(" ",90,190)
    if coul<0:
      coul=9
    if coul>9:
      coul=0
    if keydown(KEY_OK)==True:
      sleep(0.2)
      if vari==0:
        j1[0]=alphabet[alph]
        j1[2]=couleur[coul]
        vari=1
      else:
        j2[0]=alphabet[alph]
        j2[2]=couleur[coul]
        draw_string("                     ",0,0)
        if j1[0]==j2[0] and j1[2]==j2[2]:
          draw_string("Meme personage",0,0)
          vari=0
          draw_string("[FLECHE] pour changer",0,0)        
        else:
          return j1,j2        
while True:
  draw_string("MORPION",125,60)
  fill_rect(120,75,80,1,color(255,0,0))
  draw_string("[OK] pour commencer",65,90)
  draw_string("[EXE] personnaliser",65,120)
  draw_string("[NUMERO] pour poser",65,140)
  draw_string("M.R.",280,200)
  if keydown(KEY_OK) == True:
    fill_rect(0,0,400,300,color(255,255,255))
    break
j1=["X",0,"blue"]
j2=["O",0,"red"]
coup=0
while True:
  map=[["",120,27],["",155,27],["",190,27],["",120,60],["",155,60],["",190,60],["",120,95],["",155,95],["",190,95]]
  j1,j2,coup=jeu(map,j1,j2,coup)
  fill_rect(0,0,400,300,color(255,255,255))

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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.