tetris_pile.py

Created by ferr0fluidmann

Created on August 23, 2018

3.17 KB

Play a simple stacking game using tetris blocks. 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_block()
  draw_string("GAME OVER",115,100)

tetris_blocks = [
  [
    [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] ]
  ]
  
block_color = [
  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_block(x,y,r,n,col):
  for i in range(len(tetris_blocks[n])):
    for j in range(len(tetris_blocks[n][i])):
      if tetris_blocks[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(tetris_blocks[n])*10-10
              py = y+j*10+Y
            elif r==270:
              px = x+i*10+X
              py = y-j*10+Y+len(tetris_blocks[n][i])*10-10
            elif r==180:
              px = x-j*10+X+len(tetris_blocks[n][i])*10-10
              py = y-i*10+Y+len(tetris_blocks[n])*10-10
            set_pixel(px, py, col)

def check_block(x,y,r,n):
  val = False
  for i in range(len(tetris_blocks[n])):
    for j in range(len(tetris_blocks[n][i])):
      if tetris_blocks[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(tetris_blocks[n])*10-10
              py = y+j*10+Y
            elif r==270:
              px = x+i*10+X
              py = y-j*10+Y+len(tetris_blocks[n][i])*10-10
            elif r==180:
              px = x-j*10+X+len(tetris_blocks[n][i])*10-10
              py = y-i*10+Y+len(tetris_blocks[n])*10-10
            if get_pixel(px, py)!=color(255,255,255):
              val = True
            if py>220:
              val = True
  return val

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