python写个俄罗斯方块游戏
时间: 2023-08-24 11:07:08 浏览: 34
好的,下面是一个简单的俄罗斯方块游戏的Python实现,使用了Pygame库:
```python
import pygame
import random
# 游戏区域宽度和高度
WIDTH = 300
HEIGHT = 600
# 格子大小和边框宽度
BLOCK_SIZE = 30
BORDER_SIZE = 2
# 游戏区域的行数和列数
ROWS = HEIGHT // BLOCK_SIZE
COLS = WIDTH // BLOCK_SIZE
# 方块的种类和形状
SHAPES = [
[(0, 0), (1, 0), (0, 1), (1, 1)], # 正方形
[(0, 0), (1, 0), (2, 0), (3, 0)], # 长条形
[(0, 0), (1, 0), (1, 1), (2, 1)], # Z形
[(0, 1), (1, 1), (1, 0), (2, 0)], # 反Z形
[(0, 0), (1, 0), (2, 0), (1, 1)], # T形
[(0, 0), (1, 0), (2, 0), (2, 1)], # L形
[(0, 0), (1, 0), (2, 0), (0, 1)] # 反L形
]
# 方块的颜色
COLORS = [
(255, 0, 0), # 红色
(0, 255, 0), # 绿色
(0, 0, 255), # 蓝色
(255, 255, 0), # 黄色
(255, 0, 255), # 紫色
(0, 255, 255), # 青色
(255, 165, 0) # 橙色
]
class Block:
def __init__(self):
self.x = COLS // 2 - 2
self.y = 0
self.shape = random.choice(SHAPES)
self.color = random.choice(COLORS)
def draw(self, surface):
for bx, by in self.shape:
pygame.draw.rect(surface, self.color, (self.x * BLOCK_SIZE + bx * BLOCK_SIZE + BORDER_SIZE,
self.y * BLOCK_SIZE + by * BLOCK_SIZE + BORDER_SIZE,
BLOCK_SIZE - BORDER_SIZE * 2,
BLOCK_SIZE - BORDER_SIZE * 2))
def move(self, dx, dy):
self.x += dx
self.y += dy
def rotate(self):
new_shape = []
for bx, by in self.shape:
new_shape.append((by, -bx))
self.shape = new_shape
def check_collision(self, board):
for bx, by in self.shape:
x = self.x + bx
y = self.y + by
if x < 0 or x >= COLS or y >= ROWS or board[y][x] != -1:
return True
return False
def add_to_board(self, board):
for bx, by in self.shape:
x = self.x + bx
y = self.y + by
board[y][x] = COLORS.index(self.color)
class Game:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('俄罗斯方块')
self.board = [[-1 for x in range(COLS)] for y in range(ROWS)]
self.block = Block()
self.score = 0
def run(self):
clock = pygame.time.Clock()
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.move_block(-1, 0)
elif event.key == pygame.K_RIGHT:
self.move_block(1, 0)
elif event.key == pygame.K_DOWN:
self.move_block(0, 1)
elif event.key == pygame.K_UP:
self.rotate_block()
# 更新游戏状态
if not self.move_block(0, 1):
self.block.add_to_board(self.board)
self.check_lines()
self.block = Block()
if self.block.check_collision(self.board):
pygame.quit()
return
# 绘制游戏界面
self.screen.fill((255, 255, 255))
self.draw_board()
self.block.draw(self.screen)
pygame.display.update()
clock.tick(10)
def move_block(self, dx, dy):
if self.block.check_collision(self.board):
return False
self.block.move(dx, dy)
if self.block.check_collision(self.board):
self.block.move(-dx, -dy)
return False
return True
def rotate_block(self):
if self.block.check_collision(self.board):
return
self.block.rotate()
if self.block.check_collision(self.board):
self.block.rotate()
self.block.rotate()
self.block.rotate()
def check_lines(self):
lines_cleared = 0
for y in range(ROWS):
if -1 not in self.board[y]:
for y2 in range(y, 0, -1):
self.board[y2] = self.board[y2 - 1][:]
self.board[0] = [-1 for x in range(COLS)]
lines_cleared += 1
if lines_cleared > 0:
self.score += lines_cleared * 100
def draw_board(self):
for y in range(ROWS):
for x in range(COLS):
if self.board[y][x] != -1:
pygame.draw.rect(self.screen,
COLORS[self.board[y][x]],
(x * BLOCK_SIZE + BORDER_SIZE,
y * BLOCK_SIZE + BORDER_SIZE,
BLOCK_SIZE - BORDER_SIZE * 2,
BLOCK_SIZE - BORDER_SIZE * 2))
if __name__ == '__main__':
game = Game()
game.run()
```
该游戏的核心是Block类,它表示一个方块。它有一个位置和一个形状,可以绘制自己。它还可以移动和旋转,并检查是否与其他方块碰撞。当方块落到底部时,它会添加到游戏板上,并检查是否有可以消除的行。如果有,它们将被清除,并更新得分。游戏循环处理事件,更新游戏状态并绘制游戏画面。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)