tetris.py

Created by 8488

Created on September 27, 2025

2.84 KB



from kandinsky import fill_rect, draw_string
import ion
from time import sleep
import random

# Effacer l'écran
def clear():
    fill_rect(0, 0, 320, 240, (0, 0, 0))

# Paramètres du jeu
CELL = 10
COLS = 10
ROWS = 16
X0 = 20
Y0 = 20

# Couleurs des pièces
COLORS = [
    (0,0,0), (0,191,255), (255,165,0),
    (0,0,255), (255,255,0), (0,255,0),
    (255,0,0), (160,32,240)
]

# Pièces (formes 4x4 simplifiées)
SHAPES = {
  1: [[[1,1,1,1]]],
  2: [[[2,2,2],[2,0,0]]],
  3: [[[3,3,3],[0,0,3]]],
  4: [[[4,4],[4,4]]],
  5: [[[0,5,5],[5,5,0]]],
  6: [[[6,6,0],[0,6,6]]],
  7: [[[7,7,7],[0,7,0]]]
}

# Plateau
board = [[0]*COLS for _ in range(ROWS)]

def draw_board():
    for r in range(ROWS):
        for c in range(COLS):
            color = COLORS[board[r][c]]
            fill_rect(X0+c*CELL, Y0+r*CELL, CELL-1, CELL-1, color)

def new_piece():
    t = random.randint(1,7)
    return [t, 0, 0, COLS//2-2]  # type, rotation, row, col

def get_cells(piece):
    t, rot, r, c = piece
    shape = SHAPES[t][rot % len(SHAPES[t])]
    cells=[]
    for i in range(len(shape)):
        for j in range(len(shape[i])):
            if shape[i][j]!=0:
                cells.append((r+i,c+j,shape[i][j]))
    return cells

def collision(piece, dr=0, dc=0):
    for r,c,v in get_cells(piece):
        nr, nc = r+dr, c+dc
        if nr>=ROWS or nc<0 or nc>=COLS: return True
        if nr>=0 and board[nr][nc]!=0: return True
    return False

def lock(piece):
    for r,c,v in get_cells(piece):
        if 0<=r<ROWS and 0<=c<COLS:
            board[r][c]=v
    clear_lines()

def clear_lines():
    global board
    board=[row for row in board if any(v==0 for v in row)]
    while len(board)<ROWS:
        board.insert(0,[0]*COLS)

def draw_piece(piece, erase=False):
    for r,c,v in get_cells(piece):
        if 0<=r<ROWS and 0<=c<COLS:
            color = (0,0,0) if erase else COLORS[v]
            fill_rect(X0+c*CELL,Y0+r*CELL,CELL-1,CELL-1,color)

def run():
    clear()
    piece=new_piece()
    drop_count=0

    while True:
        draw_board()
        draw_piece(piece)

        # Déplacement touches
        if ion.keydown(ion.KEY_LEFT) and not collision(piece,dc=-1):
            draw_piece(piece,True); piece[3]-=1
        if ion.keydown(ion.KEY_RIGHT) and not collision(piece,dc=1):
            draw_piece(piece,True); piece[3]+=1
        if ion.keydown(ion.KEY_DOWN) and not collision(piece,dr=1):
            draw_piece(piece,True); piece[2]+=1

        sleep(0.1)
        drop_count+=1

        # Gravité
        if drop_count>=5:
            drop_count=0
            if not collision(piece,dr=1):
                draw_piece(piece,True)
                piece[2]+=1
            else:
                lock(piece)
                piece=new_piece()
                if collision(piece):
                    draw_string("GAME OVER",120,120,(255,0,0),(0,0,0))
                    return

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.