calculatris_3of3.py

Created by tristan-lusson

Created on September 24, 2022

5.22 KB

Unofficial tetris for Numwork (needs others modules)


from math import *
from kandinsky import *
from random import *
from time import *
from ion import *

difficulty_scale = [100,35,28,23,19,15,12,9,6,4,2]

def reset_board():
  global board
  board = [[0 for i in range(board_size[0])]for i in range(board_size[1])]
  
def block(type, color, x, y):
  if y > -1:
    for i in range(8):
      for j in range(8):
        tone =255//7* tile[i][j]
        if x+j>=0:
          set_pixel(8*x+j+16,8*y+i+16,(color[0]*tone,color[1]*tone,color[2]*tone))
      
def refresh():
  for x in range(board_size[1]):
    for y in range(board_size[0]):
      if board[x][y] == 0:
        fill_rect(8*y+16,8*x+16,8,8,(100,100,100))
      else:  
        block(3,board[x][y],y,x)
      
def rotab(rotation):
  rotab=[0,1,1,1]
  if rotation%2==1:rotab[0],rotab[1]=1,0
  if rotation>1:rotab[3]=-1
  if rotation==1 or rotation==2:rotab[2]=-1
  return rotab
  
def print_tetrominos(type,x,y,rotation):
  r=rotab(rotation)
  for i in range(len(tetrominos[type])):    
    block(tile,(colors[type][0],colors[type][1],colors[type][2]),x+(tetrominos[type][i][r[0]]*r[2]),\
    y+(tetrominos[type][i][r[1]]*r[3]))
    
def clear_tetrominos(type,x,y,rotation):
  r=rotab(rotation)
  for i in range(len(tetrominos[type])):
    if y+(tetrominos[type][i][r[1]]*r[3])>=0 :
      fill_rect(8*(x+(tetrominos[type][i][r[0]]*r[2]))+16,\
      8*(y+(tetrominos[type][i][r[1]]*r[3]))+16,8,8,(100,100,100))
      
def frame():
  """display the game frame"""
  fill_rect(0,0,320,222,(20,20,20))
  fill_rect(8,8,64+(board_size[0]*8),16+(24*8),(20,20,20))
  fill_rect(16,16,board_size[0]*8,board_size[1]*8,(100,100,100))
  fill_rect(20+(board_size[0]*8),24,48,32,(100,100,100))
  draw_string("SCORE",board_size[0]*8+19,70,(255,255,255),(20,20,20))
  draw_string("LEVEL",board_size[0]*8+19,110,(255,255,255),(20,20,20))
  
def is_clipping(type,x,y,rotation):
  global board
  r=rotab(rotation)
  for i in range(len(tetrominos[type])):
    p = x + (tetrominos[type][i][r[0]]*r[2])
    q = y + (tetrominos[type][i][r[1]]*r[3])
    if p > board_size[0]-1 or q > board_size[1]-1 or p < 0 or board[q][p]!=0:
      return True
  return False
    
def down():
  global type,x,y,rotation,is_falling
  r=rotab(rotation)
  if not is_clipping(type,x,y+1,rotation):
    clear_tetrominos(type,x,y,rotation)
    print_tetrominos(type,x,y+1,rotation)
    y+=1
  else:    
    for i in range(len(tetrominos[type])):      
      board[y+(tetrominos[type][i][r[1]]*r[3])]\
      [x+(tetrominos[type][i][r[0]]*r[2])]=colors[type]
      is_falling=0      
      
def line_rearange():
  L=[]
  for loop in range(board_size[1]):
    if not 0 in board[loop]:
      L.append(0)
    else:
      L.append(1)
  return L
  
def delete_filled_lines():
  global score
  L=line_rearange()
  offset=0
  for i in range(board_size[1]-1,-1,-1):
    if L[i] == 0:
      fill_rect(16,16+(8*i),board_size[0]*8,8,(255,255,255))
      offset+=1
    elif offset!=0:
      board[i+offset]=board[i]
      board[i]=[0 for j in range(board_size[0])]
  if offset!=0:
    score += 2**offset
    draw_string(str(score),board_size[0]*8+19,85,(255,255,255),(20,20,20))
    refresh()
    
def input_detection():
  global current,type,x,y,rotation,rotation_cooldown
  if rotation_cooldown>0:rotation_cooldown+=-1
    
  if keydown(KEY_LEFT) and not is_clipping(type,x-1,y,rotation):
    clear_tetrominos(type,x,y,rotation)
    x+=-1
    print_tetrominos(type,x,y,rotation)
  elif keydown(KEY_RIGHT) and not is_clipping(type,x+1,y,rotation):
    clear_tetrominos(type,x,y,rotation)
    x+=1
    print_tetrominos(type,x,y,rotation)
  elif keydown(KEY_POWER) and not is_clipping(type,x,y,(rotation+1)%4) and rotation_cooldown==0:
    clear_tetrominos(type,x,y,rotation)
    rotation=(rotation+1)%4
    rotation_cooldown=3
    print_tetrominos(type,x,y,rotation)
  elif keydown(KEY_COMMA) and not is_clipping(type,x,y,(rotation-1)%4) and rotation_cooldown==0:
    clear_tetrominos(type,x,y,rotation)
    rotation=(rotation-1)%4
    rotation_cooldown=3
    print_tetrominos(type,x,y,rotation)
  elif keydown(KEY_DOWN):down()

def A___play(size,pieces,colors_,sspeed,tile_):
  global type,x,y,tetrominos,colors,rotation,is_falling,board,rotation_cooldown,score,difficulty,board_size,tile
  board_size=size
  tetrominos=pieces
  colors=colors_
  tile=tile_
  frame()
  reset_board()
  score=0
  playing=1
  mix_bag=[i for i in range(len(tetrominos))]
  next_type=choice(mix_bag)
  mix_bag.pop(mix_bag.index(next_type))
  while playing == 1:
    if len(mix_bag)==0:mix_bag=[i for i in range(len(tetrominos))]
    type=next_type
    clear_tetrominos(next_type,board_size[0]+3,3,0)
    next_type=choice(mix_bag)
    mix_bag.pop(mix_bag.index(next_type))
    print_tetrominos(next_type,board_size[0]+3,3,0)
    
    x=board_size[0]//2
    y=0
    rotation=0
    is_falling=1
    rotation_cooldown=0
    
    difficulty=score//20+sspeed
    if difficulty > 10:difficulty = 10
    down()
    if is_clipping(type,x,y,0):is_falling,playing=0,0
    draw_string(str(difficulty),board_size[0]*8+19,125,(255,255-(25*difficulty),255-(25*difficulty)),(20,20,20))
    while is_falling==1:
      for i in range(difficulty_scale[difficulty]):
        input_detection()
        sleep(0.04)
      down()
    delete_filled_lines()
  draw_string("GAME OVER",200,103,(0,255,255),(0,0,0))
  sleep(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.