tic_tac_toe_bolt.py

Created by florian-allard

Created on July 29, 2025

5.03 KB

Adaptation du jeu Tic Tac Toe Bolt, ou Morpion éclair.

Le principe est celui du Morpion, c’est-à-dire : à tour de rôle, deux joueurs dessinent une marque dans une case d’un plateau de 3 cases par 3 cases. Le premier joueur qui aligne 3 de ses marques (en ligne, colonne ou diagonale) a gagné.

Ici, il y a une variante : lorsqu’un joueur dessine sa 4ème marque, sa 1ère marque dessinée disparaît et libère une case. Après la 5ème marque dessinée, c’est la 2ème marque dessinée qui disparaît et ainsi de suite. Un changement de couleur montre quelle marque s’apprête à disparaître.

Pour placer une marque, on appuie sur une des touches de 1 à 9 (le jeu n’est pas jouable depuis le simulateur).


# Tic Tac Toe Bolt

from kandinsky import *
from ion import *
from time import sleep
from random import randint

def dessine_plateau(x,y):
  for i in range(4):
    fill_rect(x+40*i,y,1,120,"purple")
    fill_rect(x,y+40*i,120,1,"purple")

def dessine_jeton(x,y,joueur,clr=""):
  if clr=="":
    clr="br"[joueur]
  if joueur==0:
    # croix
    for i in range(21):
      fill_rect(x-1+i+(i==0),y+i,3-(i%20==0),1,clr)
      fill_rect(x-1+20-i+(i==20),y+i,3-(i%20==0),1,clr)
  else:
    # rond
    fill_rect(x+6,y,9,2,clr)
    fill_rect(x+4,y+1,3,2,clr)
    fill_rect(x+14,y+1,3,2,clr)
    fill_rect(x+3,y+2,2,2,clr)
    fill_rect(x+16,y+2,2,2,clr)
    fill_rect(x+2,y+3,2,2,clr)
    fill_rect(x+17,y+3,2,2,clr)
    fill_rect(x+1,y+4,2,3,clr)
    fill_rect(x+18,y+4,2,3,clr)
    fill_rect(x,y+6,2,9,clr)
    fill_rect(x+19,y+6,2,9,clr)

    fill_rect(x+1,y+14,2,3,clr)
    fill_rect(x+18,y+14,2,3,clr)
    fill_rect(x+2,y+16,2,2,clr)
    fill_rect(x+17,y+16,2,2,clr)
    fill_rect(x+3,y+17,2,2,clr)
    fill_rect(x+16,y+17,2,2,clr)
    fill_rect(x+4,y+18,3,2,clr)
    fill_rect(x+14,y+18,3,2,clr)
    fill_rect(x+6,y+19,9,2,clr)


def placement_jeton(n,joueur):
  Plateau[n]=8+joueur
  dessine_jeton(100+10+40*(n%3),50+10+40*(2-n//3),joueur)

def test_victoire(joueur):
  gagnant=-1
  for ligne in range(3):
    if Plateau[3*ligne]!=0 and Plateau[3*ligne+1]!=0 and Plateau[3*ligne+2]!=0 and Plateau[3*ligne]%2==joueur and Plateau[3*ligne+1]%2==joueur and Plateau[3*ligne+2]%2==joueur:
      gagnant=joueur # victoire du joueur Plateau[3*ligne]%2
  for colonne in range(3):
    if Plateau[colonne]!=0 and Plateau[colonne+3]!=0 and Plateau[colonne+6]!=0 and Plateau[colonne]%2==joueur and Plateau[colonne+3]%2==joueur and Plateau[colonne+6]%2==joueur:
      gagnant=joueur # victoire du joueur Plateau[colonne]%2
  if Plateau[0]!=0 and Plateau[4]!=0 and Plateau[8]!=0 and Plateau[0]%2==joueur and Plateau[4]%2==joueur and Plateau[8]%2==joueur:
    gagnant=joueur
  if Plateau[2]!=0 and Plateau[4]!=0 and Plateau[6]!=0 and Plateau[2]%2==joueur and Plateau[4]%2==joueur and Plateau[6]%2==joueur:
    gagnant=joueur
  if gagnant != -1:
    draw_string("Victoire de J"+str(gagnant+1),100,190,"br"[gagnant])
    return True
  return False

draw_string("Tic Tac Toe Bolt",80,70,"purple")
draw_string("Morpion éclair",90,95,"purple")
draw_string("Valider avec OK",85,135,"purple")
for k in range(2): 
  fill_rect(60,49+k*122,200,1,"g")

N=randint(1,13)
for k in range(14):
  dessine_jeton(15+45*(k%7),10+180*(k//7),k%2-k//7,(("cyan","b")[k!=N],((245,200,0),"r")[k!=N])[k%2-k//7])

for k in range(6):
  dessine_jeton(15+6*45*(k//3),10+45+45*(k%3),1-k%2-k//3,(("cyan","b")[k!=N],((245,200,0),"r")[k!=N])[1-k%2-k//3])

while not (keydown(KEY_OK) or keydown(KEY_EXE)):1
while keydown(KEY_OK) or keydown(KEY_EXE):1

fill_rect(0,0,320,222,"w")

draw_string("Présentation",100,10,(80,150,50))
Regles=("Chaque joueur place son","symbole dans la case de son","choix en appuyant sur une","des touches de 1 à 9 et","gagne un point s'il aligne","3 de ses symboles.","","Un symbole placé disparaît","après 3 coups joués.")
for k in range(len(Regles)):
  draw_string(Regles[k],20,33+18*k,"b")

while not (keydown(KEY_OK) or keydown(KEY_EXE)):1
while keydown(KEY_OK) or keydown(KEY_EXE):1

fill_rect(0,0,320,222,"w")

draw_string("Morpion éclair",90,15,"purple")
dessine_plateau(100,50)

for joueur in (0,1):
  draw_string("J"+str(joueur+1),35+230*joueur,90,"br"[joueur])
  draw_string("0 pt",30+230*joueur,110,"br"[joueur])

Plateau=[0]*9
scores=[0,0]

while 1:
  for joueur in (0,1):
    sleep(1)
    draw_string(" "*15,100,190,"w")
    for n in range(9):
      if Plateau[n]%2==joueur and Plateau[n]>0:
        Plateau[n]-=2
        if Plateau[n]==1:
          Plateau[n]=0
        if Plateau[n]//2==1:
          dessine_jeton(100+10+40*(n%3),50+10+40*(2-n//3),joueur,("cyan",(245,200,0))[joueur])
        
    # placement d'un jeton
    while 1:
      if keydown(KEY_ONE) and Plateau[0]==0:
        placement_jeton(0,joueur)
        break
      if keydown(KEY_TWO) and Plateau[1]==0:
        placement_jeton(1,joueur)
        break
      if keydown(KEY_THREE) and Plateau[2]==0:
        placement_jeton(2,joueur)
        break
      if keydown(KEY_FOUR) and Plateau[3]==0:
        placement_jeton(3,joueur)
        break
      if keydown(KEY_FIVE) and Plateau[4]==0:
        placement_jeton(4,joueur)
        break
      if keydown(KEY_SIX) and Plateau[5]==0:
        placement_jeton(5,joueur)
        break
      if keydown(KEY_SEVEN) and Plateau[6]==0:
        placement_jeton(6,joueur)
        break
      if keydown(KEY_EIGHT) and Plateau[7]==0:
        placement_jeton(7,joueur)
        break
      if keydown(KEY_NINE) and Plateau[8]==0:
        placement_jeton(8,joueur)
        break
    for n in range(9):
      if Plateau[n]%2==joueur and Plateau[n]>0:
        if Plateau[n]//2==1:
          dessine_jeton(100+10+40*(n%3),50+10+40*(2-n//3),joueur,"w")
          Plateau[n]=0
    if test_victoire(joueur):
      scores[joueur]+=1
      draw_string(str(scores[joueur])+" pt"+"s"*(scores[joueur]>1),30+230*joueur,110,"br"[joueur])

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.