生成一个俄罗斯方块游戏的代码
时间: 2024-03-11 20:23:58 浏览: 125
Excel 俄罗斯方块小游戏源代码,基于Excel VBA 宏制作的 俄罗斯方块小游戏源码
5星 · 资源好评率100%
以下是一个简单的俄罗斯方块游戏代码,使用Python和Pygame编写:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 游戏窗口大小
WINDOW_WIDTH = 500
WINDOW_HEIGHT = 600
# 方块大小
BLOCK_SIZE = 20
# 游戏区域大小
PLAY_WIDTH = 300
PLAY_HEIGHT = 600
# 游戏区域左上角的坐标
PLAY_X = 100
PLAY_Y = 0
# 方块移动速度
FPS = 30
# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
# 游戏区域的网格
grid = [(x, y) for x in range(PLAY_X, PLAY_X + PLAY_WIDTH, BLOCK_SIZE) for y in range(PLAY_Y, PLAY_Y + PLAY_HEIGHT, BLOCK_SIZE)]
# 方块形状
S_SHAPE_TEMPLATE = [['.....',
'.....',
'..00.',
'.00..',
'.....'],
['.....',
'..0..',
'..00.',
'...0.',
'.....']]
Z_SHAPE_TEMPLATE = [['.....',
'.....',
'.00..',
'..00.',
'.....'],
['.....',
'..0..',
'.00..',
'.0...',
'.....']]
I_SHAPE_TEMPLATE = [['..0..',
'..0..',
'..0..',
'..0..',
'.....'],
['.....',
'.....',
'0000.',
'.....',
'.....']]
O_SHAPE_TEMPLATE = [['.....',
'.....',
'.00..',
'.00..',
'.....']]
J_SHAPE_TEMPLATE = [['.....',
'.0...',
'.000.',
'.....',
'.....'],
['.....',
'..00.',
'..0..',
'..0..',
'.....'],
['.....',
'.....',
'.000.',
'...0.',
'.....'],
['.....',
'..0..',
'..0..',
'.00..',
'.....']]
L_SHAPE_TEMPLATE = [['.....',
'...0.',
'.000.',
'.....',
'.....'],
['.....',
'..0..',
'..0..',
'..00.',
'.....'],
['.....',
'.....',
'.000.',
'.0...',
'.....'],
['.....',
'.00..',
'..0..',
'..0..',
'.....']]
T_SHAPE_TEMPLATE = [['.....',
'..0..',
'.000.',
'.....',
'.....'],
['.....',
'..0..',
'..00.',
'..0..',
'.....'],
['.....',
'.....',
'.000.',
'..0..',
'.....'],
['.....',
'..0..',
'.00..',
'..0..',
'.....']]
SHAPES = {'S': S_SHAPE_TEMPLATE,
'Z': Z_SHAPE_TEMPLATE,
'J': J_SHAPE_TEMPLATE,
'L': L_SHAPE_TEMPLATE,
'I': I_SHAPE_TEMPLATE,
'O': O_SHAPE_TEMPLATE,
'T': T_SHAPE_TEMPLATE}
# 方块类
class Piece:
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
def draw(self, surface):
for i, row in enumerate(self.shape):
for j, col in enumerate(row):
if col == '0':
pygame.draw.rect(surface, YELLOW, (self.x + j * BLOCK_SIZE, self.y + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
def move(self, dx, dy):
self.x += dx
self.y += dy
def rotate(self):
self.shape = list(zip(*self.shape[::-1]))
def get_shape(self):
return self.shape
# 初始化游戏区域
def create_grid(locked_pos={}):
grid_surface = pygame.Surface((PLAY_WIDTH, PLAY_HEIGHT))
grid_surface.fill(BLACK)
for i in range(len(grid)):
x, y = grid[i]
if (x, y) in locked_pos:
pygame.draw.rect(grid_surface, YELLOW, (x, y, BLOCK_SIZE, BLOCK_SIZE), 0)
else:
pygame.draw.rect(grid_surface, BLUE, (x, y, BLOCK_SIZE, BLOCK_SIZE), 1)
return grid_surface
# 检查方块是否碰到边界或其他方块
def valid_space(piece, locked_pos):
for i, row in enumerate(piece.get_shape()):
for j, col in enumerate(row):
if col == '0':
x = piece.x + j * BLOCK_SIZE
y = piece.y + i * BLOCK_SIZE
if (x, y) not in grid or (x, y) in locked_pos:
return False
return True
# 将方块加入到已锁定方块的列表中
def add_to_locked(piece, locked_pos):
for i, row in enumerate(piece.get_shape()):
for j, col in enumerate(row):
if col == '0':
x = piece.x + j * BLOCK_SIZE
y = piece.y + i * BLOCK_SIZE
locked_pos[(x, y)] = YELLOW
# 随机生成一个方块
def get_piece():
shape = random.choice(list(SHAPES.keys()))
piece = Piece(PLAY_X + PLAY_WIDTH // 2 - BLOCK_SIZE * 2, PLAY_Y, SHAPES[shape][0])
return piece
# 显示分数
def draw_score(surface, score):
font = pygame.font.SysFont(None, 40)
text = font.render("Score: " + str(score), True, WHITE)
surface.blit(text, (10, 10))
# 主函数
def main():
# 创建游戏窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Tetris')
clock = pygame.time.Clock()
# 初始化游戏数据
piece = get_piece()
locked_pos = {}
score = 0
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
piece.move(-BLOCK_SIZE, 0)
if not valid_space(piece, locked_pos):
piece.move(BLOCK_SIZE, 0)
elif event.key == pygame.K_RIGHT:
piece.move(BLOCK_SIZE, 0)
if not valid_space(piece, locked_pos):
piece.move(-BLOCK_SIZE, 0)
elif event.key == pygame.K_DOWN:
piece.move(0, BLOCK_SIZE)
if not valid_space(piece, locked_pos):
piece.move(0, -BLOCK_SIZE)
elif event.key == pygame.K_UP:
piece.rotate()
if not valid_space(piece, locked_pos):
piece.rotate()
# 移动方块
piece.move(0, BLOCK_SIZE)
if not valid_space(piece, locked_pos):
piece.move(0, -BLOCK_SIZE)
add_to_locked(piece, locked_pos)
piece = get_piece()
score += 10
# 绘制游戏界面
screen.fill(BLACK)
grid_surface = create_grid(locked_pos)
screen.blit(grid_surface, (PLAY_X, PLAY_Y))
piece.draw(screen)
draw_score(screen, score)
pygame.display.update()
# 控制帧率
clock.tick(FPS)
# 运行游戏
if __name__ == '__main__':
main()
```
这段代码使用Pygame库创建了一个简单的俄罗斯方块游戏,包括方块的生成、移动、旋转,碰撞检测,分数计算等功能。
阅读全文