Amazing model. This might be the best Qwen-2.5 has to offer.

#2
by rombodawg - opened

This model is the first sub 22b parameter model ive ever seen be able to code a tetris game in python. Its a little buggy, but it could easily fix this. This is the best AI model that can be run on a 10-12gb gpu EVER (Q6_K_L.gguf), at least at the moment.

Code

import random
import pygame
import sys

# Initialize Pygame
pygame.init()

# Screen dimensions
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 600
GRID_SIZE = 30
GRID_WIDTH = SCREEN_WIDTH // GRID_SIZE
GRID_HEIGHT = SCREEN_HEIGHT // GRID_SIZE

# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
YELLOW = (255, 255, 0)

# Tetromino shapes
SHAPES = [
    [[1, 1], [1, 1]],  # O
    [[0, 1, 0], [1, 1, 1]],  # I
    [[1, 1, 1], [0, 1, 0]],  # S
    [[0, 1, 1], [1, 1, 0]],  # Z
    [[1, 1, 1], [1, 0, 0]],  # T
    [[1, 1, 1], [0, 0, 1]],  # L
    [[1, 1, 1], [1, 0, 0]]   # J
]

# Tetromino colors
COLORS = [CYAN, YELLOW, GREEN, RED, MAGENTA, BLUE, WHITE]

class Tetris:
    def __init__(self):
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        self.clock = pygame.time.Clock()
        self.grid = [[BLACK for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
        self.current_piece = None
        self.piece_x = 0
        self.piece_y = 0
        self.game_over = False

    def new_piece(self):
        shape = random.choice(SHAPES)
        color = random.choice(COLORS)
        return {'shape': shape, 'color': color}

    def draw_grid(self):
        for y in range(GRID_HEIGHT):
            for x in range(GRID_WIDTH):
                pygame.draw.rect(self.screen, self.grid[y][x], (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE - 1, GRID_SIZE - 1))

    def draw_piece(self, piece, offset_x=0, offset_y=0):
        color = piece['color']
        shape = piece['shape']
        for y in range(len(shape)):
            for x in range(len(shape[y])):
                if shape[y][x]:
                    pygame.draw.rect(
                        self.screen,
                        color,
                        ((offset_x + x) * GRID_SIZE, (offset_y + y) * GRID_SIZE, GRID_SIZE - 1, GRID_SIZE - 1)
                    )

    def check_collision(self):
        for y in range(len(self.current_piece['shape'])):
            for x in range(len(self.current_piece['shape'][y])):
                if self.current_piece['shape'][y][x]:
                    grid_x = self.piece_x + x
                    grid_y = self.piece_y + y
                    if grid_x < 0 or grid_x >= GRID_WIDTH or grid_y >= GRID_HEIGHT:
                        return True
                    if self.grid[grid_y][grid_x] != BLACK:
                        return True
        return False

    def lock_piece(self):
        shape = self.current_piece['shape']
        color = self.current_piece['color']
        for y in range(len(shape)):
            for x in range(len(shape[y])):
                if shape[y][x]:
                    grid_y = self.piece_y + y
                    grid_x = self.piece_x + x
                    # Ensure the position is within bounds before setting it
                    if 0 <= grid_y < GRID_HEIGHT and 0 <= grid_x < GRID_WIDTH:
                        self.grid[grid_y][grid_x] = color

    def clear_lines(self):
        lines_cleared = 0
        for i, row in enumerate(self.grid[:-1]):
            if all(cell != BLACK for cell in row):
                del self.grid[i]
                self.grid.insert(0, [BLACK for _ in range(GRID_WIDTH)])
                lines_cleared += 1
        return lines_cleared

    def move_piece_down(self):
        if not self.check_collision():
            self.piece_y += 1
        else:
            self.lock_piece()
            self.clear_lines()
            self.new_piece()

    def rotate_piece(self):
        original_shape = self.current_piece['shape']
        rotated_shape = list(zip(*reversed(original_shape)))
        if not self.check_collision():
            self.current_piece['shape'] = rotated_shape

    def move_piece_left(self):
        self.piece_x -= 1
        if self.check_collision():
            self.piece_x += 1

    def move_piece_right(self):
        self.piece_x += 1
        if self.check_collision():
            self.piece_x -= 1

    def run(self):
        self.current_piece = self.new_piece()
        while not self.game_over:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT]:
                self.move_piece_left()
            if keys[pygame.K_RIGHT]:
                self.move_piece_right()
            if keys[pygame.K_UP]:
                self.rotate_piece()
            if keys[pygame.K_DOWN]:
                self.piece_y += 1
                if self.check_collision():
                    self.piece_y -= 1

            self.screen.fill(BLACK)
            self.draw_grid()

            # Draw the current piece and check for collision on next move down
            self.draw_piece(self.current_piece, offset_x=self.piece_x, offset_y=self.piece_y)

            pygame.display.flip()

            if not self.check_collision():
                self.piece_y += 1
            else:
                self.lock_piece()
                lines_cleared = self.clear_lines()
                # Spawn a new piece after locking the current one
                self.current_piece = self.new_piece()
                self.piece_x = GRID_WIDTH // 2 - len(self.current_piece['shape'][0]) // 2
                self.piece_y = 0

                if self.check_collision():
                    self.game_over = True

            self.clock.tick(5)

if __name__ == "__main__":
    game = Tetris()
    game.run()

Sign up or log in to comment