俄罗斯方块代码:使用 Python 实现经典游戏
俄罗斯方块是一种经典的游戏,以下是一个简单的俄罗斯方块代码示例(使用 Python 语言):
import pygame
import random
# 游戏窗口大小
WIDTH = 800
HEIGHT = 600
# 方块大小
BLOCK_SIZE = 30
# 方块颜色
COLORS = [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (255, 165, 0)]
# 游戏区域大小
BOARD_WIDTH = 10
BOARD_HEIGHT = 20
# 游戏区域位置
BOARD_X = (WIDTH - BLOCK_SIZE * BOARD_WIDTH) // 2
BOARD_Y = HEIGHT - BLOCK_SIZE * BOARD_HEIGHT
# 方块形状
SHAPES = [
[[1, 1, 1, 1]],
[[1, 1], [1, 1]],
[[1, 1, 0], [0, 1, 1]],
[[0, 1, 1], [1, 1, 0]],
[[1, 1, 1], [0, 1, 0]],
[[1, 1, 1], [1, 0, 0]],
[[1, 1, 1], [0, 0, 1]]
]
def create_block():
shape = random.choice(SHAPES)
color = random.randint(1, len(COLORS) - 1)
return shape, color
def draw_block(screen, x, y, color):
pygame.draw.rect(screen, COLORS[color], (BOARD_X + x * BLOCK_SIZE, BOARD_Y + y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
def draw_board(screen, board):
for y in range(BOARD_HEIGHT):
for x in range(BOARD_WIDTH):
if board[y][x] != 0:
draw_block(screen, x, y, board[y][x])
def check_collision(board, shape, x, y):
for row in range(len(shape)):
for col in range(len(shape[row])):
if shape[row][col] != 0:
if x + col < 0 or x + col >= BOARD_WIDTH or y + row >= BOARD_HEIGHT or board[y + row][x + col] != 0:
return True
return False
def merge_board(board, shape, x, y):
for row in range(len(shape)):
for col in range(len(shape[row])):
if shape[row][col] != 0:
board[y + row][x + col] = shape[row][col]
def remove_lines(board):
full_lines = []
for row in range(BOARD_HEIGHT):
if all(board[row]):
full_lines.append(row)
for row in full_lines:
del board[row]
board.insert(0, [0] * BOARD_WIDTH)
return len(full_lines)
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('俄罗斯方块')
clock = pygame.time.Clock()
board = [[0] * BOARD_WIDTH for _ in range(BOARD_HEIGHT)]
shape, color = create_block()
x, y = BOARD_WIDTH // 2 - len(shape[0]) // 2, 0
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and not check_collision(board, shape, x - 1, y):
x -= 1
elif event.key == pygame.K_RIGHT and not check_collision(board, shape, x + 1, y):
x += 1
elif event.key == pygame.K_DOWN and not check_collision(board, shape, x, y + 1):
y += 1
elif event.key == pygame.K_UP:
rotated_shape = list(zip(*reversed(shape)))
if not check_collision(board, rotated_shape, x, y):
shape = rotated_shape
if not check_collision(board, shape, x, y + 1):
y += 1
else:
merge_board(board, shape, x, y)
lines_removed = remove_lines(board)
shape, color = create_block()
x, y = BOARD_WIDTH // 2 - len(shape[0]) // 2, 0
if check_collision(board, shape, x, y):
game_over = True
screen.fill(COLORS[0])
draw_board(screen, board)
for row in range(len(shape)):
for col in range(len(shape[row])):
if shape[row][col] != 0:
draw_block(screen, x + col, y + row, color)
pygame.display.flip()
clock.tick(10)
pygame.quit()
if __name__ == '__main__':
main()
请注意,这只是一个简单的示例,可能还有其他的优化和功能可以添加。
原文地址: https://www.cveoy.top/t/topic/p8uM 著作权归作者所有。请勿转载和采集!