用python做俄罗斯方块小游戏代码
时间: 2023-09-03 17:11:09 浏览: 53
好的,以下是用Python实现俄罗斯方块小游戏的代码:
```python
import pygame
import random
pygame.init()
# 游戏窗口大小
screen_width = 480
screen_height = 640
# 方块大小
block_size = 20
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# 初始化游戏窗口
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("俄罗斯方块")
clock = pygame.time.Clock()
# 定义方块形状
shape_list = [
[[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 1, 0],
[1, 1, 1],
[0, 0, 0]],
[[1, 1],
[1, 1]],
[[0, 1, 1],
[1, 1, 0],
[0, 0, 0]],
[[1, 1, 0],
[0, 1, 1],
[0, 0, 0]],
[[1, 0, 0],
[1, 1, 1],
[0, 0, 0]],
[[0, 0, 1],
[1, 1, 1],
[0, 0, 0]],
]
# 定义方块颜色
color_list = [red, green, blue]
# 随机生成方块形状和颜色
def new_block():
shape = random.choice(shape_list)
color = random.choice(color_list)
return shape, color
# 方块类
class Block:
def __init__(self):
self.shape, self.color = new_block()
self.row = 0
self.col = (screen_width // block_size - len(self.shape[0])) // 2
# 旋转方块
def rotate(self):
self.shape = [[self.shape[j][i] for j in range(len(self.shape))] for i in range(len(self.shape[0]) - 1, -1, -1)]
# 判断方块是否能下移
def can_move_down(self, board):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] != 0:
if self.row + i + 1 >= len(board) or board[self.row + i + 1][self.col + j] != black:
return False
return True
# 下移方块
def move_down(self):
self.row += 1
# 判断方块是否能左移
def can_move_left(self, board):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] != 0:
if self.col + j - 1 < 0 or board[self.row + i][self.col + j - 1] != black:
return False
return True
# 左移方块
def move_left(self):
self.col -= 1
# 判断方块是否能右移
def can_move_right(self, board):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] != 0:
if self.col + j + 1 >= len(board[0]) or board[self.row + i][self.col + j + 1] != black:
return False
return True
# 右移方块
def move_right(self):
self.col += 1
# 将方块放置到游戏板上
def place_on_board(self, board):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] != 0:
board[self.row + i][self.col + j] = self.color
# 游戏板类
class Board:
def __init__(self):
self.board = [[black for j in range(screen_width // block_size)] for i in range(screen_height // block_size)]
self.block = Block()
# 清除已经填满的行
def clear_full_rows(self):
for i in range(len(self.board)):
if black not in self.board[i]:
del self.board[i]
self.board.insert(0, [black for j in range(screen_width // block_size)])
# 判断方块是否能下移
def can_move_down(self):
return self.block.can_move_down(self.board)
# 下移方块
def move_down(self):
if self.can_move_down():
self.block.move_down()
else:
self.block.place_on_board(self.board)
self.block = Block()
self.clear_full_rows()
# 判断方块是否能左移
def can_move_left(self):
return self.block.can_move_left(self.board)
# 左移方块
def move_left(self):
if self.can_move_left():
self.block.move_left()
# 判断方块是否能右移
def can_move_right(self):
return self.block.can_move_right(self.board)
# 右移方块
def move_right(self):
if self.can_move_right():
self.block.move_right()
# 旋转方块
def rotate(self):
self.block.rotate()
# 绘制游戏板和方块
def draw(self):
for i in range(len(self.board)):
for j in range(len(self.board[i])):
pygame.draw.rect(screen, self.board[i][j], (j * block_size, i * block_size, block_size, block_size))
for i in range(len(self.block.shape)):
for j in range(len(self.block.shape[i])):
if self.block.shape[i][j] != 0:
pygame.draw.rect(screen, self.block.color, ((self.block.col + j) * block_size, (self.block.row + i) * block_size, block_size, block_size))
board = Board()
# 游戏主循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
exit()
elif event.key == pygame.K_LEFT:
board.move_left()
elif event.key == pygame.K_RIGHT:
board.move_right()
elif event.key == pygame.K_DOWN:
board.move_down()
elif event.key == pygame.K_UP:
board.rotate()
screen.fill(white)
board.draw()
pygame.display.update()
clock.tick(5)
```
希望能对你有所帮助!