俄罗斯方块代码python
时间: 2023-12-01 17:41:19 浏览: 104
以下是一个简单的俄罗斯方块游戏的Python代码示例:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 设置屏幕大小
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 500
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
# 设置游戏标题
pygame.display.set_caption("Tetris")
# 定义方块大小
BLOCK_SIZE = 20
# 定义方块类型和颜色
S = [['.....',
'.....',
'..00.',
'.00..',
'.....'],
['.....',
'..0..',
'..00.',
'...0.',
'.....']]
Z = [['.....',
'.....',
'.00..',
'..00.',
'.....'],
['.....',
'..0..',
'.00..',
'.0...',
'.....']]
I = [['..0..',
'..0..',
'..0..',
'..0..',
'.....'],
['.....',
'0000.',
'.....',
'.....',
'.....']]
O = [['.....',
'.....',
'.00..',
'.00..',
'.....']]
J = [['.....',
'.0...',
'.000.',
'.....',
'.....'],
['.....',
'..00.',
'..0..',
'..0..',
'.....'],
['.....',
'.....',
'.000.',
'...0.',
'.....'],
['.....',
'..0..',
'..0..',
'.00..',
'.....']]
L = [['.....',
'...0.',
'.000.',
'.....',
'.....'],
['.....',
'..0..',
'..0..',
'..00.',
'.....'],
['.....',
'.....',
'.000.',
'.0...',
'.....'],
['.....',
'.00..',
'..0..',
'..0..',
'.....']]
T = [['.....',
'..0..',
'.000.',
'.....',
'.....'],
['.....',
'..0..',
'..00.',
'..0..',
'.....'],
['.....',
'.....',
'.000.',
'..0..',
'.....'],
['.....',
'..0..',
'.00..',
'..0..',
'.....']]
SHAPES = [S, Z, I, O, J, L, T]
COLORS = [GREEN, RED, BLUE, WHITE, YELLOW, PURPLE, ORANGE]
# 定义方块类
class Block(pygame.sprite.Sprite):
def __init__(self, shape, color):
super().__init__()
self.shape = shape
self.color = color
self.rotation = 0
self.image = self._create_image()
self.rect = self.image.get_rect()
def _create_image(self):
rows = len(self.shape[self.rotation])
cols = len(self.shape[self.rotation][0])
image = pygame.Surface([cols * BLOCK_SIZE, rows * BLOCK_SIZE])
image.fill(BLACK)
for row in range(rows):
for col in range(cols):
if self.shape[self.rotation][row][col] == '0':
pygame.draw.rect(image, self.color, [col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE])
return image
def rotate(self):
self.rotation = (self.rotation + 1) % len(self.shape)
self.image = self._create_image()
def move_down(self):
self.rect.y += BLOCK_SIZE
def move_left(self):
self.rect.x -= BLOCK_SIZE
def move_right(self):
self.rect.x += BLOCK_SIZE
# 定义游戏类
class Game:
def __init__(self):
self.score = 0
self.game_over = False
self.block_group = pygame.sprite.Group()
self._create_new_block()
def _create_new_block(self):
shape = random.choice(SHAPES)
color = random.choice(COLORS)
block = Block(shape, color)
block.rect.x = SCREEN_WIDTH // 2 - BLOCK_SIZE * 2
block.rect.y = 0
self.block_group.add(block)
def _check_rows(self):
full_rows = []
for row in range(SCREEN_HEIGHT // BLOCK_SIZE):
blocks = [block for block in self.block_group if block.rect.y == row * BLOCK_SIZE]
if len(blocks) == SCREEN_WIDTH // BLOCK_SIZE:
full_rows.append(row)
for row in full_rows:
for block in self.block_group:
if block.rect.y == row * BLOCK_SIZE:
self.block_group.remove(block)
self.score += 10
for block in self.block_group:
if block.rect.y < row * BLOCK_SIZE:
block.move_down()
def _check_game_over(self):
for block in self.block_group:
if block.rect.y < 0:
self.game_over = True
def run(self):
clock = pygame.time.Clock()
while not self.game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.block_group.update(-BLOCK_SIZE, 0)
elif event.key == pygame.K_RIGHT:
self.block_group.update(BLOCK_SIZE, 0)
elif event.key == pygame.K_DOWN:
self.block_group.update(0, BLOCK_SIZE)
elif event.key == pygame.K_UP:
self.block_group.sprites()[0].rotate()
self.block_group.update(0, BLOCK_SIZE // 2)
self._check_rows()
self._check_game_over()
screen.fill(BLACK)
self.block_group.draw(screen)
pygame.display.flip()
clock.tick(10)
pygame.quit()
# 运行游戏
game = Game()
game.run()
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)