趣味python编程之经典俄罗斯方块
时间: 2023-09-28 18:11:23 浏览: 87
好的,让我们开始编写经典俄罗斯方块游戏吧!
首先,我们需要导入 Pygame 库:
```python
import pygame
```
然后,我们需要初始化 Pygame:
```python
pygame.init()
```
接下来,我们定义一些常量:
```python
# 游戏界面大小
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 800
# 方块大小
BLOCK_SIZE = 40
# 游戏界面边距
MARGIN_LEFT = 100
MARGIN_TOP = 100
# 方块颜色
COLORS = [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (255, 255, 255)]
# 方块形状
SHAPES = [
[[1, 1, 1], [0, 1, 0]],
[[2, 2, 0], [0, 2, 2]],
[[0, 3, 3], [3, 3, 0]],
[[4, 4], [4, 4]],
[[5, 5, 5, 5]],
[[6, 6], [6, 6]],
[[7, 7, 7], [0, 7, 0], [0, 7, 0]]
]
```
其中,`COLORS` 列表保存了不同颜色的方块,`SHAPES` 列表保存了所有可能的方块形状。
接下来,我们需要定义一个 `Block` 类,用来表示游戏中的方块:
```python
class Block:
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
self.color = COLORS[shape[0][0]]
```
`Block` 类有三个属性:`x` 和 `y` 表示方块左上角的坐标,`shape` 表示方块的形状。
接下来,我们需要定义一个 `Board` 类,用来表示游戏面板:
```python
class Board:
def __init__(self):
self.width = 10
self.height = 20
self.board = [[0 for _ in range(self.width)] for _ in range(self.height)]
self.score = 0
self.current_block = None
self.next_block = Block(0, 0, random.choice(SHAPES))
```
`Board` 类有五个属性:`width` 和 `height` 分别表示面板的宽度和高度,`board` 是一个二维列表,表示面板上每个位置的方块颜色,`score` 表示当前得分,`current_block` 表示当前正在下落的方块,`next_block` 表示下一个要下落的方块。
接下来,我们需要定义一些方法来操作游戏面板:
```python
def clear(self):
for i in range(self.height):
if all(self.board[i]):
self.board.pop(i)
self.board.insert(0, [0 for _ in range(self.width)])
self.score += 10
def can_move(self, block, dx, dy):
for i in range(len(block.shape)):
for j in range(len(block.shape[0])):
if block.shape[i][j] != 0:
x = block.x + j + dx
y = block.y + i + dy
if x < 0 or x >= self.width or y >= self.height or (y >= 0 and self.board[y][x] != 0):
return False
return True
def move(self, block, dx, dy):
if self.can_move(block, dx, dy):
block.x += dx
block.y += dy
return True
return False
def rotate(self, block):
shape = block.shape
new_shape = [[0 for _ in range(len(shape))] for _ in range(len(shape[0]))]
for i in range(len(shape)):
for j in range(len(shape[0])):
new_shape[j][len(shape)-1-i] = shape[i][j]
if self.can_move(Block(block.x, block.y, new_shape), 0, 0):
block.shape = new_shape
return True
return False
def update(self):
if not self.current_block:
self.current_block = self.next_block
self.next_block = Block(0, 0, random.choice(SHAPES))
if not self.can_move(self.current_block, 0, 0):
return False
else:
if not self.move(self.current_block, 0, 1):
for i in range(len(self.current_block.shape)):
for j in range(len(self.current_block.shape[0])):
if self.current_block.shape[i][j] != 0:
x = self.current_block.x + j
y = self.current_block.y + i
self.board[y][x] = self.current_block.color
self.clear()
self.current_block = None
return True
```
`clear` 方法用来清理已经填满的行,并更新得分。`can_move` 方法用来判断方块是否可以移动到指定位置,`move` 方法用来移动方块,`rotate` 方法用来旋转方块。`update` 方法则用来更新游戏状态。
接下来,我们需要定义一个 `Game` 类,用来控制游戏的运行:
```python
class Game:
def __init__(self):
self.board = Board()
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('俄罗斯方块')
def run(self):
clock = pygame.time.Clock()
while True:
clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.board.move(self.board.current_block, -1, 0)
elif event.key == pygame.K_RIGHT:
self.board.move(self.board.current_block, 1, 0)
elif event.key == pygame.K_DOWN:
self.board.move(self.board.current_block, 0, 1)
elif event.key == pygame.K_UP:
self.board.rotate(self.board.current_block)
if not self.board.update():
pygame.quit()
exit()
self.screen.fill((0, 0, 0))
for i in range(self.board.height):
for j in range(self.board.width):
pygame.draw.rect(self.screen, COLORS[self.board.board[i][j]], (MARGIN_LEFT+j*BLOCK_SIZE, MARGIN_TOP+i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
for i in range(len(self.board.next_block.shape)):
for j in range(len(self.board.next_block.shape[0])):
if self.board.next_block.shape[i][j] != 0:
pygame.draw.rect(self.screen, COLORS[self.board.next_block.shape[i][j]], (MARGIN_LEFT+(self.board.width+j)*BLOCK_SIZE, MARGIN_TOP+i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
pygame.display.update()
```
`Game` 类有一个属性 `board`,表示游戏面板。`run` 方法则是游戏的主循环,处理用户输入和更新游戏状态。
最后,我们需要创建一个 `Game` 对象,并调用它的 `run` 方法:
```python
if __name__ == '__main__':
game = Game()
game.run()
```
好了,现在我们已经完成了一个简单的俄罗斯方块游戏!