memory_game.py

Created by ziii

Created on August 31, 2022

2.73 KB

A basic memory game with numbers instead of drawings.

How to play :

Arrows to move the cursor (red rectangle)

Ok to reveal the number under the selected card.


from random import *
from kandinsky import fill_rect as f, draw_string as d
from ion import *
from time import *
def sq(COLORS):
  f(pos[0]*size[0],pos[1]*size[1],size[0]+1,1,COLORS)
  f(pos[0]*size[0],(pos[1]+1)*size[1],size[0]+1,1,COLORS)
  f(pos[0]*size[0],pos[1]*size[1]+1,1,size[1]-1,COLORS)
  f((pos[0]+1)*size[0],pos[1]*size[1]+1,1,size[1]-1,COLORS)
def cur(vx=0,vy=0):
  global t, pos
  if monotonic()-t>0.125:
    sq(COLORS[0])
    t=monotonic()
    pos[0]=(pos[0]+vx)%col
    pos[1]=(pos[1]+vy)%row
    sq(COLORS[3])
def number(num,pos,col):
  if num>-1:
    num=[int(a) for a in str(num)]
    for n,k in enumerate(num):
      for x in range(7):
        if l[k][x]:
          f(pos[0]+coo[x][0]+n*(a[1]+2*a[0]+sp),pos[1]+coo[x][1],a[x%2],a[(x+1)%2],col)
size=(27,14)
col=int(input("Enter number of collumns\nOne must be even\nbetween 1 and {}: ".format(320//size[0])))
row=int(input("Enter number of rows\nbetween 1 and {}: ".format(222//size[1])))
if (row*col)%2:
  print("One must be even")
  raise KeyboardInterrupt
COLORS=(
(50,50,50),#background
(220,170,80),#square
(255,255,255),#number & text
(255,0,0))# cursor
t=0
pos=[0,0]
sel=(-1,-1)
score=0
grid=[[[0] for y in range(row)]for x in range(col)]
l=[x//2 for x in range(col*row)]
res={}
for x in range(len(l)):
  choice=randrange(0,len(l))
  res[x]=l[choice]
  del l[choice]
for x in range(row*col):
  grid[x//row][x%row]=[res[x],0]
l=((1,1,1,0,1,1,1),(1,0,0,0,1,0,0),(0,1,1,1,1,1,0),(0,1,1,1,0,1,1),(1,0,1,1,0,0,1),(1,1,0,1,0,1,1),(1,1,0,1,1,1,1),(0,1,1,0,0,0,1),(1,1,1,1,1,1,1),(1,1,1,1,0,0,1))
a=1
a=(1,(size[1]-3*a-1)//2)
sp=1
coo=((0,a[0]),(a[0],0),(a[0]+a[1],a[0]),(a[0],a[0]+a[1]),(0,2*a[0]+a[1]),(a[0],2*a[0]+2*a[1]),(a[0]+a[1],2*a[0]+a[1]))
f(0,0,320,222,COLORS[0])
for x in range(col):
  for y in range(row):
    f(size[0]*x+1,size[1]*y+1,size[0]-1,size[1]-1,COLORS[1])
cur()
run=1
while run:
  if keydown(KEY_LEFT):
    cur(vx=-1)
  elif keydown(KEY_RIGHT):
    cur(vx=1)
  elif keydown(KEY_UP):
    cur(vy=-1)
  elif keydown(KEY_DOWN):
    cur(vy=1)
  elif keydown(KEY_OK) and grid[pos[0]][pos[1]][1]==0:
    if sel[0]!=-1 and (pos[0]!=sel[0] or pos[1]!=sel[1]):
      number(grid[pos[0]][pos[1]][0],(pos[0]*size[0]+1,pos[1]*size[1]+1),COLORS[2])
      if grid[pos[0]][pos[1]][0]==grid[sel[0]][sel[1]][0]:
        grid[pos[0]][pos[1]][1]=1
        grid[sel[0]][sel[1]][1]=1
        score+=2
        if row*col==score:
          d("you win",125,102,COLORS[2],COLORS[0])
          run=0
      else:
        sleep(1)
        f(sel[0]*size[0]+1,sel[1]*size[1]+1,size[0]-1,size[1]-1,COLORS[1])
        f(pos[0]*size[0]+1,pos[1]*size[1]+1,size[0]-1,size[1]-1,COLORS[1])
      sel=(-1,-1)
    else:
      sel=tuple(pos)
      number(grid[pos[0]][pos[1]][0],(pos[0]*size[0]+1,pos[1]*size[1]+1),COLORS[2])

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.