spider_solitaire.py

Created by ziii

Created on August 26, 2023

3.99 KB

A classic spider solitaire game.

Due to an obvious lack of screen space, if there is a stack of card only the one on top and at the bottom will be shown.

How to play :

L&R to move cursor

down arrow to select.

Shift to deal new cards


from random import *
from kandinsky import fill_rect as f,draw_string as d
from ion import *
from time import *
def draw(x):
  y=len(board[x])-1
  f(29*x,height(x)-1,29,22+20*(board[x][y][1]!=1),COLORS[3])
  f(29*x+1,height(x),27,20+20*(board[x][y][1]!=1),COLORS[2])
  d(INDEX[board[x][y][0]],29*x+10-5*(board[x][y][0]==9),height(x),COLORS[1],COLORS[2])
  if 1-board[x][y][1]:
    d(INDEX[board[x][y][0]-board[x][y][1]+1],29*x+10-5*((board[x][y][0]-board[x][y][1])==8),height(x)+20,COLORS[1],COLORS[2])
def height(x):
  h=21*len(board[x])+1
  for i in range(len(board[x])-1):
    if board[x][i][1]!=1:
      h+=21
  return h
COLORS=(
(41,42,43),# background
(255,255,255),# text
(41,42,43),# cards
(100,100,100),# back
(50,190,50),# cursor
(50,50,190))# select
INDEX=("A","2","3","4","5","6","7","8","9","10","J","Q","K")
t=monotonic()
board=[[] for _ in range(10)]
stack=[i//8 for i in range(104)]
hid=[[] for _ in range(10)]
for a in range(54):
  r=randrange(len(stack))
  if a>43:
    board[a%10].append([stack[r],1])
  else:
    hid[a%10].append(stack[r])
  del stack[r]
sel=-1
cur=0
count=0
key_pressing={KEY_DOWN:0,KEY_SHIFT:0}
key_pressed={k:0 for k in key_pressing}
run=1
f(0,0,320,222,COLORS[0])
for a in range(10):
  f(29*a+1,0,27,20,COLORS[4-bool(a)])
  d(str(len(hid[a])),29*a+10,0,COLORS[1],COLORS[4-bool(a)])
  for b in range(len(board[a])):
    draw(a)
d(str(len(stack)//10),292,10,COLORS[1],COLORS[0])
while run:
  check=0
  for k in key_pressing:
    key_pressed[k]=0
    if keydown(k):
      if not key_pressing[k]:
        key_pressed[k]=1
      key_pressing[k]=1
    else:
      key_pressing[k]=0
  if monotonic()-t>0.13 and (keydown(KEY_RIGHT)-keydown(KEY_LEFT)):
    t=monotonic()
    f(29*cur+1,0,27,20,COLORS[3+2*(sel==cur)])
    d(str(len(hid[cur])),29*cur+10,0,COLORS[1],COLORS[3+2*(sel==cur)])
    cur=(cur-keydown(KEY_LEFT)+keydown(KEY_RIGHT))%10
    f(29*cur+1,0,27,20,COLORS[4+(sel==cur)])
    d(str(len(hid[cur])),29*cur+10,0,COLORS[1],COLORS[4+(sel==cur)])
  elif sel!=-1 and keydown(KEY_UP):
    f(29*sel+1,0,27,20,COLORS[3+(sel==cur)])
    d(str(len(hid[sel])),29*sel+10,0,COLORS[1],COLORS[3+(sel==cur)])
    sel=-1
  elif key_pressed[KEY_DOWN]:
    if sel==-1:
      if len(board[cur]):
        sel=cur
        f(29*cur+1,0,27,20,COLORS[5])
        d(str(len(hid[cur])),29*cur+10,0,COLORS[1],COLORS[5])
    elif sel==cur:
      f(29*sel+1,0,27,20,COLORS[4])
      d(str(len(hid[sel])),29*sel+10,0,COLORS[1],COLORS[4])
      sel=-1
    else:
      check=1
      do=1
      if len(board[cur])==0:
        board[cur].append(board[sel][-1])
      elif board[cur][-1][0]-board[cur][-1][1]==board[sel][-1][0]:
        board[cur][-1][1]+=board[sel][-1][1]
      elif 0>board[cur][-1][0]-board[cur][-1][1]-board[sel][-1][0]>-board[sel][-1][1]:
        delta=board[sel][-1][0]-board[sel][-1][1]-board[cur][-1][0]+board[cur][-1][1]
        board[cur][-1][1]-=delta
        board[sel][-1][1]+=delta
        do=2
      else:
        do=0
      if do:
        f(29*sel,height(sel),29,42,COLORS[0])
        f(29*sel+1,0,27,20,COLORS[3])
        d(str(len(hid[sel])),29*sel+10,0,COLORS[1],COLORS[3])
        draw(cur)
        if do==1:
          board[sel].pop(-1)
        else:
          draw(sel)
        sel=-1
  elif sel==-1 and key_pressed[KEY_SHIFT] and len(stack):
    check=1
    for a in range(10):
      r=randrange(len(stack))
      if len(board[a]) and board[a][-1][0]-board[a][-1][1]==stack[r]:
        board[a][-1][1]+=1
      else:
        board[a].append([stack[r],1])
      stack.pop(r)
      draw(a)
    d(str(len(stack)//10),292,10,COLORS[1],COLORS[0])
  if check:
    for a in range(10):
      if len(board[a]) and board[a][-1][1]==13:
        f(29*a,height(a),29,42,COLORS[0])
        board[a].pop(-1)
        count+=1
      if len(board[a])==0 and len(hid[a])>0:
        board[a].append([hid[a][-1],1])
        hid[a].pop(-1)
        draw(a)
        d(str(len(hid[a])),29*a+10,0,COLORS[1],COLORS[3+(a==cur)])
    if count==8:
      f(0,0,320,222,COLORS[0])
      d("YOU WIN",125,104,COLORS[1],COLORS[0])
      run=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.