Absolutely! While I can't create a fully interactive graphical Tetris game directly in this chat, I can make a simple text-based Tetris game that you can play by entering commands. This will run in a Python environment (copy-paste into your local Python interpreter or an online Python shell like repl.it).
Here's a simple text-based Tetris you can play in your terminal. Use a (left), d (right), s (down), and w (rotate). Press Enter after each move.
import random
import os
import sys
import time
WIDTH, HEIGHT = 10, 20
TETROMINOS = {
'I': [[1, 1, 1, 1]],
'O': [[1, 1],
[1, 1]],
'T': [[0, 1, 0],
[1, 1, 1]],
'S': [[0, 1, 1],
[1, 1, 0]],
'Z': [[1, 1, 0],
[0, 1, 1]],
'J': [[1, 0, 0],
[1, 1, 1]],
'L': [[0, 0, 1],
[1, 1, 1]],
}
def rotate(shape):
return [list(row) for row in zip(*shape[::-1])]
def new_board():
return [[0]*WIDTH for _ in range(HEIGHT)]
def print_board(board, piece=None, offset=(0,0)):
os.system('cls' if os.name == 'nt' else 'clear')
temp = [row[:] for row in board]
if piece:
px, py = offset
for y, row in enumerate(piece):
for x, cell in enumerate(row):
if cell and 0 <= py+y < HEIGHT and 0 <= px+x < WIDTH:
temp[py+y][px+x] = 2
print('+' + '-'*WIDTH + '+')
for row in temp:
print('|' + ''.join(['#' if x==1 else ('@' if x==2 else ' ') for x in row]) + '|')
print('+' + '-'*WIDTH + '+')
def check_collision(board, piece, offset):
px, py = offset
for y, row in enumerate(piece):
for x, cell in enumerate(row):
if cell:
if (x+px < 0 or x+px >= WIDTH or y+py >= HEIGHT or
(y+py >= 0 and board[y+py][x+px])):
return True
return False
def merge(board, piece, offset):
px, py = offset
for y, row in enumerate(piece):
for x, cell in enumerate(row):
if cell and 0 <= py+y < HEIGHT and 0 <= px+x < WIDTH:
board[py+y][px+x] = 1
def clear_lines(board):
new_board = [row for row in board if any(cell == 0 for cell in row)]
lines_cleared = HEIGHT - len(new_board)
while len(new_board) < HEIGHT:
new_board.insert(0, [0]*WIDTH)
return new_board, lines_cleared
def get_new_piece():
shape = random.choice(list(TETROMINOS.values()))
return [row[:] for row in shape]
def main():
board = new_board()
score = 0
piece = get_new_piece()
px, py = WIDTH//2 - len(piece[0])//2, 0
while True:
print_board(board, piece, (px, py))
print(f"Score: {score}")
print("Controls: a=left, d=right, s=down, w=rotate, q=quit")
move = input("Move: ").strip().lower()
if move == 'q':
print("Game Over!")
break
new_px, new_py = px, py
new_piece = [row[:] for row in piece]
if move == 'a':
new_px -= 1
elif move == 'd':
new_px += 1
elif move == 's':
new_py += 1
elif move == 'w':
new_piece = rotate(piece)
# Try move
if not check_collision(board, new_piece, (new_px, new_py)):
px, py, piece = new_px, new_py, new_piece
else:
if move == 's':
# Place piece
merge(board, piece, (px, py))
board, lines = clear_lines(board)
score += lines * 100
piece = get_new_piece()
px, py = WIDTH//2 - len(piece[0])//2, 0
if check_collision(board, piece, (px, py)):
print_board(board)
print("Game Over! Final Score:", score)
break
if __name__ == "__main__":
main()
How to play:
- Copy and paste this code into a Python file or an online Python interpreter.
- Run it.
- Use
a (left), d (right), s (down), w (rotate), and q (quit).
- Press Enter after each move.
Let me know if you want a more advanced version or a different style!