tic_tac_toe.py

Created by ziii

Created on October 22, 2022

1.8 KB

a simple tic tac toe game.

how to play :

arrows to move cursor.

ok to play.


from kandinsky import fill_rect as f, draw_string as d
from ion import *
from time import monotonic
def square(col):
  f(pos[0]*size,pos[1]*size,size+1,1,col)
  f(pos[0]*size,(pos[1]+1)*size,size+1,1,col)
  f(pos[0]*size,pos[1]*size+1,1,size-1,col)
  f((pos[0]+1)*size,pos[1]*size+1,1,size-1,col)
def cur(vx,vy):
  global time,pos
  if monotonic()-time>0.125:
    time=monotonic()
    square(COLORS[1])
    pos[0]=(pos[0]+vx)%3
    pos[1]=(pos[1]+vy)%3
    square(COLORS[2])
def cross(size,thick,pos,col):
  for x in range(size-thick+1):
    f(x+pos[0],pos[1]+x,thick,thick,col)
    f(x+pos[0],pos[1]+size-x,thick,-thick,col)
COLORS=(
(50,50,50),#background
(220,170,80),#grid & symbols & text
(255,0,0))#cursor
grid=[[-1 for _ in range(3)] for _ in range(3)]
pos=[0,0]
turn=0
size=73
f(0,0,320,222,COLORS[0])
for a in range(4):
  f(size*a,0,1,220,COLORS[1])
  f(0,size*a,220,1,COLORS[1])
square(COLORS[2])
time=monotonic()
run=1
while run:
  if keydown(KEY_LEFT):
    cur(-1,0)
  elif keydown(KEY_UP):
      cur(0,-1)
  elif keydown(KEY_RIGHT):
      cur(1,0)
  elif keydown(KEY_DOWN):
    cur(0,1)
  elif keydown(KEY_OK) and grid[pos[0]][pos[1]]==-1:
    if turn:
      f(pos[0]*size+7,pos[1]*size+7,size-13,size-13,COLORS[1])
      f(pos[0]*size+12,pos[1]*size+12,size-23,size-23,COLORS[0])
    else:
      cross(size-13,3,[pos[0]*size+7,pos[1]*size+7],COLORS[1])
    grid[pos[0]][pos[1]]=turn
    if all([grid[pos[0]][y]==turn for y in range(3)]) or all([grid[x][pos[1]]==turn for x in range(3)]) or all([grid[a][a]==turn for a in range(3)]) or all([grid[a][2-a]==turn for a in range(3)]):
      d("{} win".format(["cross","square"][turn]),222,0,COLORS[1],COLORS[0])
      run=0
    elif all([all([grid[x][y]>-1 for y in range(3)]) for x in range(3)]):
      d("draw",222,0,COLORS[1],COLORS[0])
      run=0
    turn=not turn

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.