编辑代码

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
import sys
import pygame
from pygame.locals import *
import random
import time

# 设置颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# 游戏窗口尺寸
WINDOW_WIDTH = 10
WINDOW_HEIGHT = 20

#砖块大小
BLOCK_SIZE = 30
SCROLL_SPEED = 15

# 初始得分和级别
BASE_SCORE = 100
LEVELS = [
    {'score': BASE_SCORE, 'speed': SCROLL_SPEED},
]

def create_block():
    # 定义各个形状的颜色和坐标
    shape_colors = [
        (-1, -1), (1, -1),
        (-1, 1), (1, 1)
    ]
    colors = [
        BLACK,
        WHITE,
        WHITE,
        WHITE
    ]
    
    # 随机选择形状和颜色
    shape = random.choice(shape_colors)
    color = random.choice(colors)
    
    return shape, color

def create_window():
    pygame.init()
    screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    pygame.display.set_caption('俄罗斯方块')
    return screen

def is_game_over(screen, score, level):
    if score >= LEVELS[level]['score']:
        return True
    return False  # 这里需要根据游戏逻辑判断是否结束

def main():
    screen = create_window()
    
    clock = pygame.time.Clock()
    font = pygame.font.Font(None, 74)
    
    level_index = 0
    score = BASE_SCORE
    
    while True:
        if is_game_over(screen, score, level_index):
            break
        
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key == K_LEFT:
                    move_block('left')
                elif event.key == K_RIGHT:
                    move_block('right')
                elif event.key == K_UP:
                    rotate_block()
                elif event.key == K_DOWN or event.key == K_PAGEUP:
                    drop_block()
        
        # 绘制
        screen.fill(BLACK)
        draw_score(font, 100, (WINDOW_WIDTH//2 - 140, WINDOW_HEIGHT - 50), score,
                     size=30)
        draw_level(screen, WINDOW_HEIGHT - 70, level_index + 1, font, 25, size=30)
        
        pygame.display.flip()
        clock.tick(60)

def move_block(direction):
    # 挪动砖块
    global block_x, block_y, block_shape, block_color
    if direction == 'left':
        block_x -= BLOCK_SIZE
        if block_x < 0:
            block_x = 0
    elif direction == 'right':
        block_x += BLOCK_SIZE
        if block_x > WINDOW_WIDTH - BLOCK_SIZE:
            block_x = WINDOW_WIDTH - BLOCK_SIZE
    
    # 检查是否有碰撞
    if (block_x, block_y) in occupied_blocks:
        return False  # 不能移动
    else:
        # 移动砖块
        for i in range(len(block_shape)):
            if block_shape[i]:
                x = block_x + BLOCK_SIZE * i
                y = block_y
                occupied_blocks.add((x, y))
        block_x_change += 1

def rotate_block():
    global block_shape, block_color
    new_shape = list(block_shape)
    new_shape.append(new_shape[0])
    del new_shape[-1]
    
    old_shape = block_shape
    block_shape = new_shape
    
    # 刷新当前位置的砖块形状
    for i in range(len(old_shape)):
        if old_shape[i]:
            x = block_x + BLOCK_SIZE * i
            y = block_y
            occupied_blocks.remove((x, y))
    
    for i in range(len(block_shape)):
        if block_shape[i]:
            x = block_x + BLOCK_SIZE * i
            y = block_y
            occupied_blocks.add((x, y))

def drop_block():
    global block_y
    while True:
        block_y += 1
        if block_y > WINDOW_HEIGHT - BLOCK_SIZE:
            # 下移到底部
            block_y = WINDOW_HEIGHT - BLOCK_SIZE
            break
        
        # 检查是否有砖块在下方
        hasCollision = False
        for i in range(len(block_shape)):
            if block_shape[i]:
                x = block_x + BLOCK_SIZE * i
                y = block_y
                if (x, y) in occupied_blocks:
                    hasCollision = True
                    break
        if hasCollision:
            # 放置砖块
            for i in range(len(block_shape)):
                if block_shape[i]:
                    x = block_x + BLOCK_SIZE * i
                    y = block_y
                    occupied_blocks.add((x, y))
            # 增加分数
            score += 10
            level_index += 1
            if level_index < len(LEVELS):
                time.sleep(1000)  # 等待下一个级别
            else:
                break
            return
        
        block_y = WINDOW_HEIGHT - BLOCK_SIZE

# 初始化游戏变量
block_x = (WINDOW_WIDTH // 2) - (BLOCK_SIZE * (len(block_shape) // 2))
block_y = WINDOW_HEIGHT - BLOCK_SIZE
occupied_blocks = set()
block_shape, block_color = create_block()

def draw_score(font, size, x_pos, y_pos):
    text = font.render(f"得分:{score}", True, WHITE)
    text_rect = text.get_rect(x=x_pos, y=y_pos)
    screen.blit(text, text_rect)

def draw_level(font, size, x_pos, y_pos):
    level_text = str(level_index + 1)
    text = font.render(level_text, True, WHITE)
    text_rect = text.get_rect(x=x_pos, y=y_pos)
    screen.blit(text, text_rect)

# 运行主循环
pygame.init()
screen = create_window()
clock = pygame.time.Clock()

while True:
    # 处理事件
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            break
    
    # 绘制并更新
    screen.fill(BLACK)
    draw_score(font, 100, (WINDOW_WIDTH//2 - 140), WINDOW_HEIGHT - 50)
    draw_level(font, 25, (WINDOW_WIDTH//2 - 85), WINDOW_HEIGHT - 70)
    
    pygame.display.flip()
    clock.tick(60)

pygame.quit()