tetrys.py

Created by chick-chicky

Created on September 24, 2023

3.34 KB


f=all;Z=None;Y=enumerate;X=len;W=map;Q=True;P=False;J=int;D=range;G=10;K=20;B=10;p=2
import kandinsky as R,ion as I,time as F
from random import choice,randint as o
g={-2:(255,255,255),-1:(128,128,128),0:(0,0,0),1:(0,255,255),2:(255,255,0),3:(255,0,255),4:(255,128,0),5:(0,0,255),6:(0,255,0),7:(255,0,0)}
S={1:[[[0,0],[1,0],[2,0],[3,0]],[[0,0],[0,1],[0,2],[0,3]],[[0,0],[1,0],[2,0],[3,0]],[[0,0],[0,1],[0,2],[0,3]]],2:[[[0,0],[1,0],[0,1],[1,1]],[[0,0],[1,0],[0,1],[1,1]],[[0,0],[1,0],[0,1],[1,1]],[[0,0],[1,0],[0,1],[1,1]]],3:[[[0,0],[1,0],[2,0],[1,1]],[[0,0],[0,1],[1,1],[0,2]],[[1,0],[0,1],[1,1],[2,1]],[[1,0],[0,1],[1,1],[1,2]]],4:[[[0,0],[0,1],[0,2],[1,2]],[[2,0],[0,1],[1,1],[2,1]],[[0,0],[1,0],[1,1],[1,2]],[[0,0],[1,0],[2,0],[0,1]]],5:[[[1,0],[1,1],[0,2],[1,2]],[[0,0],[1,0],[2,0],[2,1]],[[0,0],[1,0],[0,1],[0,2]],[[0,0],[0,1],[1,1],[2,1]]],6:[[[1,0],[2,0],[0,1],[1,1]],[[0,0],[0,1],[1,1],[1,2]],[[1,0],[2,0],[0,1],[1,1]],[[0,0],[0,1],[1,1],[1,2]]],7:[[[0,0],[1,0],[1,1],[2,1]],[[1,0],[0,1],[1,1],[0,2]],[[0,0],[1,0],[1,1],[2,1]],[[1,0],[0,1],[1,1],[0,2]]]}
C=[[0 for A in D(G)]for A in D(K)]
h=[[Z for A in D(G)]for A in D(K)]
def a():
  A=list(S.keys())
  for B in W(lambda v:o(0,X(A)-1),D(X(A)**2)):A.append(A.pop(B))
  return A
M=a()+a();A=[5,0];H=M.pop(0);E=0;N=0
def b(board,shape,shape_pos,shape_rot,color=Z):
  E=color;D=shape_pos;C=shape;H=S[C][shape_rot]
  for F in H:
    A,B=F[1]+D[1],F[0]+D[0]
    if not(A<0 or B<0 or A>=K or B>=G):board[A][B]=C if E==Z else E
def i(board,shape,shape_pos,shape_rot,ghost=P):A=[[board[A][B]for B in D(G)]for A in D(K)];b(A,shape,shape_pos,shape_rot,-1 if ghost else Z);return A
c=0
def j(board):
  global c
  for(A,F)in Y(board):
    for(C,E)in Y(F):
      if h[A][C]!=E:R.fill_rect(C*B,A*B,B,B,g[E]);h[A][C]=E
  H=[[-2 for A in D(4)]for A in D(4)];b(H,M[0],[0,0],0)
  for(A,F)in Y(H):
    for(C,E)in Y(F):R.fill_rect(J((G+1)*B+C*(B/2)),J(A*(B/2)),J(B/2),J(B/2),g[E])
  if c%3==0:R.fill_rect(J((G+1)*B),J(B/2*4),50,10,(255,255,255));R.draw_string(str(N),J((G+1)*B),J(B/2*4))
  c+=1
def O(board,shape,shape_pos,shape_rot):
  C=shape_pos;E=S[shape][shape_rot]
  for D in E:
    A,B=D[1]+C[1],D[0]+C[0]
    if(A<0 or B<0 or A>=K or B>=G)or board[A][B]!=0:return P
  return Q
j(C);d=F.monotonic();k=F.monotonic();l=F.monotonic();T=0;L=P;m=0
while Q:
  U=[A[0],A[1]]
  for V in D(K):
    if not O(C,H,[U[0],U[1]+1],E):break
    U[1]+=1
  n=lambda:j(i(i(C,H,U,E,Q),H,A,E))
  if F.monotonic()-k>.5:
    n();k=F.monotonic()
    if L:T-=1
  if F.monotonic()-d>.4:
    A[1]+=1
    if not O(C,H,A,E):
      A[1]-=1
      if not L:L=Q;T=p
      if L and T<=0:
        if A[1]==0:break
        else:
          b(C,H,A,E);e=0
          for V in D(K):
            if f(W(lambda c:c!=0,C[V])):C=[[0 for A in D(G)]]+C[:V]+C[V+1:];e+=1
          if f(W(lambda l:f(W(lambda c:c==0,l)),C)):N+={1:800,2:1200,3:1800,4:2000}.get(e,0)
          else:N+={1:100,2:300,3:500,4:800}.get(e,0)
          A=[5,0];H=M.pop(0)
          if X(M)<=X(S):M+=a()
          E=0
        L=P
    else:L=P
    d=F.monotonic()
  if F.monotonic()-l>.05:
    if I.keydown(I.KEY_UP)and m%2==0:
      while Q:
        E=(E+1)%4
        if O(C,H,A,E):break
    elif I.keydown(I.KEY_DOWN):d=-1e9;T=0;N+=1
    elif I.keydown(I.KEY_LEFT):
      A[0]-=1
      if not O(C,H,A,E):A[0]+=1
    elif I.keydown(I.KEY_RIGHT):
      A[0]+=1
      if not O(C,H,A,E):A[0]-=1
    l=F.monotonic();n();m+=1
print(N)

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.