tetris_lite.py

Created by ferr0fluidmann

Created on September 27, 2018

2.28 KB

Play a simple stacking game using Tetris blocks (lite version). You have the whole screen to stack and order blocks however you want. You can move and rotate the blocks but you cannot remove blocks. The game ends when you reach the top of the screen. Start the game by executing the play() function. Type a character and press EXE to move or rotate your block. Press 4 to move left, 6 to move right, 7 to rotate 90 degrees counterclockwise, 9 to rotate 90 degrees clockwise, and any other key to drop.


from math import *
from random import *
from kandinsky import *

def play():
  go=True
  while go:
    go=move()
  draw_string("GAME OVER",115,100)

b=[[[1,1],[1,1]],
  [[1,1,1,1]],
  [[0,1,0],[1,1,1]],
  [[1,0],[1,1],[0,1]],
  [[0,1],[1,1],[1,0]],
  [[0,0,1],[1,1,1]],
  [[1,0,0],[1,1,1]]]

c=[color(255,255,0),
  color(0,200,200),
  color(255,0,255),
  color(0,255,0),
  color(255,0,0),
  color(255,165,0),
  color(0,0,255)]

def print_b(x,y,r,n,col,chk=0):
  val=False
  for i in range(len(b[n])):
    for j in range(len(b[n][i])):
      if b[n][i][j]:
        for Y in range(10):
          for X in range(10):
            if r==0:
              px=x+j*10+X
              py=y+i*10+Y
            elif r==90:
              px=x-i*10+X+len(b[n])*10-10
              py=y+j*10+Y
            elif r==270:
              px=x+i*10+X
              py=y-j*10+Y+len(b[n][i])*10-10
            elif r==180:
              px=x-j*10+X+len(b[n][i])*10-10
              py=y-i*10+Y+len(b[n])*10-10
            if get_pixel(px,py)!=color(255,255,255):
              val=True
            if py>220:
              val=True
            if chk==0:
              set_pixel(px,py,col)
  return val

def move():
  x = 150
  y = 0
  r = 0
  n = randint(0, len(b)-1)
  go = True
  print_b(x,y,r,n,c[n])
  while go:
    inpt = input("input: ")
    print_b(x,y,r,n,color(255,255,255))
    if print_b(x,y+10,r,n,0,1):
      go = False
    if inpt!='':
      if inpt[0]=='4':
        x-=10
        if go:
          if print_b(x,y+10,r,n,0,1):
            x+=10
        else:
          if print_b(x,y,r,n,0,1):
            x+=10
      if inpt[0]=='6':
        x+=10
        if go:
          if print_b(x,y+10,r,n,0,1):
            x-=10
        else:
          if print_b(x,y,r,n,0,1):
            x-=10
      if inpt[0]=='7':
        if r==0:
          r=270
        else:
          r-=90
        if print_b(x,y+10,r,n,0,1):
          if r==270:
            r=0
          else:
            r+=90
      if inpt[0]=='9':
        if r==270:
          r=0
        else:
          r+=90
        if print_b(x,y+10,r,n,0,1):
          if r==0:
            r=270
          else:
            r-=90
    if print_b(x,y+10,r,n,0,1):
      go=False  
    else:
      go=True
      y+=10
    print_b(x,y,r,n,c[n])
  if y==0:
    return False
  return True