The Punto board game. Don’t hesitate to let me know if you’ve found a bug or if you have any advice to offer.
My discord : @elnix91 My mail: elnix91@gmail.com
from kandinsky import fill_rect as rect,draw_string as txt from random import randint,choice from ion import keydown as k import time SKY=(0,0,0) TXT=(255,)*3 IMP=(0,0,255) PC=[(255,0,0),(0,255,0),(0,0,255),(255,0,255)] PN=["Rouge","Vert","Bleu","Violet"] POINTS=[[5],[1,9],[1,5,9],[1,3,7,9],[1,3,5,7,9],[1,3,4,6,7,9],[1,3,4,5,6,7,9],[1,2,3,4,6,7,8,9],[1,2,3,4,5,6,7,8,9],] # Fill the screen with a color c clean=lambda c:rect(0,0,320,222,c) # Draw a centered text in the x axis txtc=lambda t,y,cf,cb:txt(str(t),160-5*len(str(t)),y,cf,cb) # Tests if keys are pressed v=lambda:k(4)|k(56) # Validate s=lambda:k(0)|k(1)|k(2)|k(3) # Directions # Calculate th sum of the cards in a collomn/row sumCollumn=lambda c:sum(PLATE[i][c]for i in range(7)) sumRow=lambda r:sum(PLATE[r]) # Draw a recct 3x3 in x,y point=lambda x,y:rect(x,y,3,3,SKY) # random.shuffle() because not existing in Numworks def shuffle(ol): l=ol[:] nl=[] for _ in range(len(l)): i=choice(l) nl.append(i) l.remove(i) return nl def draw_select(x,y,e,p): xb,yb=90,5 X=xb+31*x Y=yb+31*y c=p if e:c=SKY rect(X,Y,27,2,c) rect(X,Y+25,27,2,c) rect(X,Y,2,27,c) rect(X+25,Y,2,27,c) def draw_card(n,x=-1,y=-1,e=False): if x>-1: xb,yb=92,7 X=xb+31*x Y=yb+31*y elif x<-1:X,Y=(160-31*7//2)+31*-x-58,175 else:X,Y=25,100 p=getPcolor(n) c=PC[p] if e:c=SKY rect(X,Y,23,23,c) if e:return N=n-p*9 for y in range(3): for x in range(3): if 3*y+x+1 in POINTS[N-1]:point(X+4+x*6,Y+4+y*6) def isTouching(X,Y): if PLATE[X][Y]:return True if 0<X<6: if any([PLATE[Y][X-1],PLATE[Y][X+1]]):return True if 0<Y<6: if any([PLATE[Y-1][X-1],PLATE[Y-1][X],PLATE[Y-1][X+1],PLATE[Y+1][X-1],PLATE[Y+1][X],PLATE[Y+1][X+1]]):return True elif Y==0: if any([PLATE[Y+1][X-1],PLATE[Y+1][X],PLATE[Y+1][X+1]]):return True elif Y==6: if any([PLATE[Y-1][X-1],PLATE[Y-1][X],PLATE[Y-1][X+1]]):return True elif X==0: if PLATE[Y][X+1]:return True if 0<Y<6: if any([PLATE[Y+1][X],PLATE[Y+1][X+1],PLATE[Y-1][X],PLATE[Y-1][X+1]]):return True elif Y==0: if any([PLATE[Y+1][X],PLATE[Y+1][X+1]]):return True elif Y==6: if any([PLATE[Y-1][X],PLATE[Y-1][X+1],PLATE[Y][X+1]]):return True elif X==6: if PLATE[Y][X-1]:return True if 0<Y<6: if any([PLATE[Y-1][X-1],PLATE[Y-1][X],PLATE[Y+1][X-1],PLATE[Y+1][X]]):return True elif Y==0: if any([PLATE[Y+1][X-1],PLATE[Y+1][X]]):return True elif Y==6: if any([PLATE[Y-1][X-1]+PLATE[Y-1][X]]):return True return False def isSupperior(X,Y,C): if PLATE[Y][X]==0:return True pp=getPcolor(PLATE[Y][X]) pc=getPcolor(C) if PLATE[Y][X]-9*pp<C-9*pc:return True return False def isInPlate(X,Y): global l_x,l_y,s_x,s_y l_x=0 l_y=0 for i in range(7): if sumCollumn(i)>0: if l_x==0:s_x=i l_x+=1 if sumRow(i)>0: if l_y==0:s_y=i l_y+=1 if l_x==6: if X<s_x or X>=s_x+6:return False if l_y==6: if Y<s_y or Y>=s_y+6:return False return True def getPcolor(n): if n==0:p=-1 elif n<10:p=0 elif n<19:p=1 elif n<28:p=2 elif n<37:p=3 return p def draw_cards(): for i in range(7): for j in range(7): if PLATE[j][i]>0: draw_card(PLATE[j][i],i,j) else: draw_card(PLATE[j][i],i,j,True) def isVictory(p): for i in range(7): for j in range(7): if PLATE[i][j]>0: if( (j<4 and all(getPcolor(PLATE[i][j+k])==p for k in range(4)))or (i<4 and all(getPcolor(PLATE[i+k][j])==p for k in range(4)))or (i<4 and j<4 and all(getPcolor(PLATE[i+k][j+k])==p for k in range(4)))or (i<4 and j>2 and all(getPcolor(PLATE[i+k][j-k])==p for k in range(4)))):return True return False def move_if_close_to_border(): # Right to left if sumCollumn(6)>0 and l_x<5: for i in range(1,7): for j in range(1,7): PLATE[i][j-1]=PLATE[i][j] if i==6:PLATE[j][i]=0 # Left to right if sumCollumn(0)>0 and l_x<5: for i in range(6,-1,-1): for j in range(6,-1,-1): PLATE[i][j]=PLATE[i][j-1] if i==0:PLATE[i][j]=0 # Down to Up if sumRow(6)>0 and l_y<5: print(PLATE[6][3]) for j in range(1,7): for i in range(1,7): PLATE[j-1][i]=PLATE[j][i] if j==6: PLATE[j][i]=0 print(j) # Up to Down if sumRow(0)>0 and l_x<5: for j in range(6,-1,-1): for i in range(6,-1,-1): PLATE[j][i]=PLATE[j-1][i] if j==0:PLATE[j][i]=0 def menu(): a=130 clean(SKY) txt("P",a,5,PC[0],SKY) txt("U",a+10,5,PC[1],SKY) txt("N",a+20,5,PC[2],SKY) txt("T",a+30,5,PC[3],SKY) txt("O",a+40,5,PC[0],SKY) txt("!",a+50,5,PC[0],SKY) txtc("Number of players:",50,TXT,SKY) txtc("< >",111,IMP,SKY) while v():1 N=0 n_max=3 f=True l=0 p=False while not v(): if l==True: l=False a=time.monotonic() if (s()|f)&(p==False): p=True if f:f=False N=(N+k(3)-k(0))%n_max txtc(N+2,111,TXT,SKY) if not s():p=False if time.monotonic()-a>0.5: l=True for i in range(1,8):draw_card(randint(0,36),-(i+1)) return N+2 def game(): global PLATE N=menu() deck=[i for i in range(1,10)for _ in range(2)] DECKS=[shuffle(deck)for _ in range(N)] PLATE=[[0 for _ in range(7)]for _ in range(7)] PT=randint(0,N-1) # Player tour X,Y=3,3 V=0 clean(SKY) draw_cards() f2=True # Game loop for i in range(18*N-1):# Numbers of card*number of players rect(0,0,70,222,PC[PT]) txt("Tour:",10,2,SKY,PC[PT]) txt(PN[PT],35-len(PN[PT])*5,25,SKY,PC[PT]) rect(23,98,27,27,SKY) draw_card(DECKS[PT][0]+PT*9) f1=True p=True while 1: if s()|f1: draw_select(X,Y,1,PC[PT]) X=(X+k(3)-k(0))%7 Y=(Y+k(2)-k(1))%7 draw_select(X,Y,0,PC[PT]) # Test if the card can be placed ok=0 rect(13,202,11,11,SKY) if isTouching(X,Y): ok+=1 rect(16,205,5,5,PC[1]) else:rect(16,205,5,5,PC[0]) rect(31,202,11,11,SKY) if isSupperior(X,Y,DECKS[PT][0]): ok+=1 rect(34,205,5,5,PC[1]) else:rect(34,205,5,5,PC[0]) rect(48,202,11,11,SKY) if isInPlate(X,Y): ok+=1 rect(51,205,5,5,PC[1]) else:rect(51,205,5,5,PC[0]) if f1:f1=False while s():1 if v()&(not p): if (ok==3)|f2: PLATE[Y][X]=DECKS[PT][0]+PT*9 DECKS[PT].pop(0) move_if_close_to_border() draw_cards() if f2:f2=False break if not v():p=False for i in range(N): if isVictory(i): rect(99,90,120,62,TXT) rect(102,93,114,56,SKY) txtc("Victoire du",96,PC[i],SKY) txtc(PN[i],126,PC[i],SKY) while v():1 while not v():1 return PT=(PT+1)%N while 1: game()