写俄罗斯方块代码
以下是一个简单的俄罗斯方块代码示例:
import pygame
import random
# 初始化游戏
pygame.init()
# 设置窗口大小
screen_width = 800
screen_height = 600
play_width = 300
play_height = 600
block_size = 30
top_left_x = (screen_width - play_width) // 2
top_left_y = screen_height - play_height
# 定义方块形状
S = [['.....',
'.....',
'..00.',
'.00..',
'.....'],
['.....',
'..0..',
'..00.',
'...0.',
'.....']]
Z = [['.....',
'.....',
'.00..',
'..00.',
'.....'],
['.....',
'..0..',
'.00..',
'.0...',
'.....']]
I = [['..0..',
'..0..',
'..0..',
'..0..',
'.....'],
['.....',
'0000.',
'.....',
'.....',
'.....']]
O = [['.....',
'.....',
'.00..',
'.00..',
'.....']]
J = [['.....',
'.0...',
'.000.',
'.....',
'.....'],
['.....',
'..00.',
'..0..',
'..0..',
'.....'],
['.....',
'.....',
'.000.',
'...0.',
'.....'],
['.....',
'..0..',
'..0..',
'.00..',
'.....']]
L = [['.....',
'...0.',
'.000.',
'.....',
'.....'],
['.....',
'..0..',
'..0..',
'..00.',
'.....'],
['.....',
'.....',
'.000.',
'.0...',
'.....'],
['.....',
'.00..',
'..0..',
'..0..',
'.....']]
T = [['.....',
'..0..',
'.000.',
'.....',
'.....'],
['.....',
'..0..',
'..00.',
'..0..',
'.....'],
['.....',
'.....',
'.000.',
'..0..',
'.....'],
['.....',
'..0..',
'.00..',
'..0..',
'.....']]
# 定义方块颜色
shape_colors = [(0, 255, 0), (255, 0, 0), (0, 255, 255), (255, 255, 0), (255, 165, 0), (0, 0, 255), (128, 0, 128)]
# 定义方块类
class Piece(object):
rows = 20
columns = 10
def __init__(self, column, row, shape):
self.x = column
self.y = row
self.shape = shape
self.color = shape_colors[shapes.index(shape)]
self.rotation = 0
# 定义游戏区域
def create_grid(locked_positions={}):
grid = [[(0, 0, 0) for _ in range(10)] for _ in range(20)]
for i in range(len(grid)):
for j in range(len(grid[i])):
if (j, i) in locked_positions:
color = locked_positions[(j, i)]
grid[i][j] = color
return grid
# 绘制游戏区域
def draw_grid(surface, grid):
pygame.draw.rect(surface, (0, 0, 0), (top_left_x, top_left_y, play_width, play_height))
for i in range(len(grid)):
for j in range(len(grid[i])):
pygame.draw.rect(surface, grid[i][j], (top_left_x + j * block_size, top_left_y + i * block_size, block_size, block_size))
pygame.draw.rect(surface, (255, 0, 0), (top_left_x, top_left_y, play_width, play_height), 5)
# 绘制方块
def draw_piece(surface, piece):
for i in range(len(piece.shape)):
for j in range(len(piece.shape[i])):
if piece.shape[i][j] == '0':
pygame.draw.rect(surface, piece.color, (top_left_x + piece.x * block_size + j * block_size, top_left_y + piece.y * block_size + i * block_size, block_size, block_size))
# 检查方块是否可以移动到指定位置
def valid_space(piece, grid):
accepted_positions = [[(j, i) for j in range(10) if grid[i][j] == (0, 0, 0)] for i in range(20)]
accepted_positions = [j for sub in accepted_positions for j in sub]
formatted = convert_shape_format(piece)
for pos in formatted:
if pos not in accepted_positions:
if pos[1] > -1:
return False
return True
# 检查游戏是否结束
def check_lost(positions):
for pos in positions:
x, y = pos
if y < 1:
return True
return False
# 将方块形状转化为坐标形式
def convert_shape_format(piece):
positions = []
shape_format = piece.shape[piece.rotation % len(piece.shape)]
for i, line in enumerate(shape_format):
row = list(line)
for j, column in enumerate(row):
if column == '0':
positions.append((piece.x + j, piece.y + i))
for i, pos in enumerate(positions):
positions[i] = (pos[0] - 2, pos[1] - 4)
return positions
# 游戏主循环
def main():
locked_positions = {}
grid = create_grid(locked_positions)
change_piece = False
run = True
current_piece = Piece(5, 0, random.choice([S, Z, I, O, J, L, T]))
next_piece = Piece(5, 0, random.choice([S, Z, I, O, J, L, T]))
clock = pygame.time.Clock()
fall_time = 0
fall_speed = 0.27
level_time = 0
score = 0
while run:
grid = create_grid(locked_positions)
fall_time += clock.get_rawtime()
level_time += clock.get_rawtime()
clock.tick()
# 方块下落
if level_time / 1000 > 5:
level_time = 0
if fall_speed > 0.12:
fall_speed -= 0.005
if fall_time / 1000 > fall_speed:
fall_time = 0
current_piece.y += 1
if not (valid_space(current_piece, grid)) and current_piece.y > 0:
current_piece.y -= 1
change_piece = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.quit()
quit()
# 方块操作
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_piece.x -= 1
if not valid_space(current_piece, grid):
current_piece.x += 1
elif event.key == pygame.K_RIGHT:
current_piece.x += 1
if not valid_space(current_piece, grid):
current_piece.x -= 1
elif event.key == pygame.K_DOWN:
current_piece.y += 1
if not valid_space(current_piece, grid):
current_piece.y -= 1
elif event.key == pygame.K_UP:
current_piece.rotation += 1
if not valid_space(current_piece, grid):
current_piece.rotation -= 1
shape_pos = convert_shape_format(current_piece)
# 更新已固定的方块位置
for i in range(len(shape_pos)):
x, y = shape_pos[i]
if y > -1:
grid[y][x] = current_piece.color
# 方块落地
if change_piece:
for pos in shape_pos:
p = (pos[0], pos[1])
locked_positions[p] = current_piece.color
current_piece = next_piece
next_piece = Piece(5, 0, random.choice([S, Z, I, O, J, L, T]))
change_piece = False
# 消除满行
for pos in shape_pos:
p = (pos[0], pos[1])
if p[1] > -1:
if p not in locked_positions:
locked_positions[p] = current_piece.color
score += 10
draw_grid(screen, grid)
pygame.display.update()
# 判断游戏结束
if check_lost(locked_positions):
run = False
print('Game Over! Your score is: ', score)
# 运行游戏
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Tetris')
main()
注意:该代码使用的是Python编程语言,需安装Pygame库并导入使用
原文地址: http://www.cveoy.top/t/topic/hZro 著作权归作者所有。请勿转载和采集!