tetris_2.py

Created by wperez274

Created on August 22, 2022

3.71 KB

Not my Program…..

…Until I make it 3 times better than the Original :-)


from kandinsky import fill_rect as f, draw_string as d
from ion import *
from time import *
from random import randrange
st=(
# o piece
[[(0,0),(1,0),(0,1),(1,1)]],
# s piece
(((1,0),(2,0),(0,1),(1,1)),((0,0),(0,1),(1,1),(1,2))),
# z piece
(((0,0),(1,0),(1,1),(2,1)),((1,0),(1,1),(0,1),(0,2))),
# i piece
(((0,0),(1,0),(2,0),(3,0)),((0,0),(0,1),(0,2),(0,3))),
# j piece
(((0,0),(1,0),(2,0),(2,1)),((1,0),(1,1),(1,2),(0,2)),((0,0),(0,1),(1,1),(2,1)),((1,0),(0,0),(0,1),(0,2))),
# l piece
(((0,1),(0,0),(1,0),(2,0)),((0,0),(1,0),(1,1),(1,2)),((0,1),(1,1),(2,1),(2,0)),((0,0),(0,1),(0,2),(1,2))),
# t piece
(((1,0),(0,1),(1,1),(2,1)),((0,0),(0,1),(1,1),(0,2)),((0,0),(1,0),(2,0),(1,1)),((1,0),(0,1),(1,1),(1,2))))
def update_grid():
  line_check()
  f(5,1,110,220,colors[8])
  for x in range(10):
    for y in range(20):
      if grid[y][x]!=-1:
        f(x*11+5,210-(y*11),11,11,colors[grid[y][x]])
def reset():
  global id,stock,grid,coo,st,score,state
  id=stock
  stock=randrange(7)
  coo=[3,19]
  state=0
  f(120,50,60,16,colors[7])
  if collide(coo,state):
    draw(colors[id])
    f(130,10,30,30,colors[8])
    f(130,10,40,40,colors[7])
    for c in st[stock][0]:
      f(130+(c[0]*10),10+(c[1]*10),10,10,colors[stock])
    d(str(score),130,50,colors[8],colors[7])
  else:
    d("you lost !",10,10) 
    d("press OK",10,30)
    while not keydown(KEY_OK):
      pass
    grid=[[-1 for _ in range(10)]for _ in range(20)]
    score=0
    update_grid()
def draw(col):
  for c in st[id][state%len(st[id])]:
    f(5+((coo[0]+c[0])*11),210-((coo[1]-c[1])*11),11,11,col)
def collide(coo,state):
  for c in st[id][state%len(st[id])]:
    try:
      if coo[1]-c[1]==-1 or coo[0]+c[0]>9 or coo[0]+c[0]<0 or grid[coo[1]-c[1]][coo[0]+c[0]]!=-1:
        return 0
    except:
      return 0
  return 1
def line_check():
  global grid,score
  line=[y for y in range(19,-1,-1) if all([grid[y][x]!=-1 for x in range(10)])]
  for x in line:
    del grid[x]
    grid.append([-1 for _ in range(10)])
    score+=10
  if len(line)>0:
    update_grid()
def move(vx,vy):
  global coo
  if collide([coo[0]+vx,coo[1]+vy],state):
    draw(colors[8])
    coo[0]+=vx
    coo[1]+=vy
    draw(colors[id])
    return 1
def move_bottom():
  global coo
  draw(colors[8])
  while collide([coo[0],coo[1]-1],state):
    coo[1]-=1
  draw(colors[id])
colors=(
(0,206,209),# o piece
(0,191,255),# s piece
(0,128,0),# z piece
(239,0,29),# i piece
(218,112,214),# j piece
(255,215,0),# l piece
(105,105,105),# t piece
(40,40,40),# background
(255,255,255))# text & game background
tick=monotonic()
grid=[[-1 for _ in range(10)]for _ in range(20)]
f(0,0,320,222,colors[7])
d("L&R arrow:",180,0,colors[8],colors[7])
d("Move",180,16,colors[8],colors[7])
d("Down arrow:",180,36,colors[8],colors[7])
d("Fall faster",180,52,colors[8],colors[7])
d("Shift:",180,72,colors[8],colors[7])
d("drops",180,88,colors[8],colors[7])
d("alpha:",180,108,colors[8],colors[7])
d("pause",180,124,colors[8],colors[7])
stock=randrange(7)
score=0
state=0
update_grid()
reset()
pause=0
while 1:
  if keydown(KEY_ALPHA):
    pause = not pause
    sleep(0.2)
  if not pause:
    if monotonic()-tick>0.25:
      if collide((coo[0],coo[1]-1),state):
        move(0,-1)
      else:
        score+=2
        for c in st[id][state%len(st[id])]:
          grid[coo[1]-c[1]][coo[0]+c[0]]=id
        line_check()
        reset()
      tick=monotonic()
    elif keydown(KEY_DOWN):
      move(0,-1)
      sleep(0.05)
    if keydown(KEY_BACKSPACE):
      if collide(coo,state+1):
        draw(colors[8])
        state=(state+1)%4
        draw(colors[id])
        sleep(0.125)
    elif keydown(KEY_LEFT):
      move(-1,0)
      sleep(0.1)
    elif keydown(KEY_RIGHT):
      move(1,0)
      sleep(0.1)
    elif keydown(KEY_SHIFT):
      move_bottom()
      sleep(0.4)

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.