pythonidle模式俄罗斯方块代码
时间: 2023-08-31 09:05:24 浏览: 125
俄罗斯方块python代码
5星 · 资源好评率100%
以下是用Python实现俄罗斯方块的代码:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口大小
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 设置字体
font = pygame.font.SysFont(None, 25)
# 定义方块大小
BLOCK_SIZE = 20
# 定义俄罗斯方块类
class Block(pygame.sprite.Sprite):
def __init__(self, x, y, color):
super().__init__()
self.image = pygame.Surface([BLOCK_SIZE, BLOCK_SIZE])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# 定义俄罗斯方块组类
class BlockGroup(pygame.sprite.Group):
def __init__(self):
super().__init__()
self.create_block()
# 创建新的方块
def create_block(self):
block_type = random.choice(['I', 'O', 'T', 'S', 'Z', 'J', 'L'])
if block_type == 'I':
self.add(Block(400, 0, BLUE))
self.add(Block(400, BLOCK_SIZE, BLUE))
self.add(Block(400, BLOCK_SIZE * 2, BLUE))
self.add(Block(400, BLOCK_SIZE * 3, BLUE))
elif block_type == 'O':
self.add(Block(400, 0, GREEN))
self.add(Block(400 + BLOCK_SIZE, 0, GREEN))
self.add(Block(400, BLOCK_SIZE, GREEN))
self.add(Block(400 + BLOCK_SIZE, BLOCK_SIZE, GREEN))
elif block_type == 'T':
self.add(Block(400, 0, RED))
self.add(Block(400 - BLOCK_SIZE, BLOCK_SIZE, RED))
self.add(Block(400, BLOCK_SIZE, RED))
self.add(Block(400 + BLOCK_SIZE, BLOCK_SIZE, RED))
elif block_type == 'S':
self.add(Block(400, 0, BLUE))
self.add(Block(400 + BLOCK_SIZE, 0, BLUE))
self.add(Block(400, BLOCK_SIZE, BLUE))
self.add(Block(400 - BLOCK_SIZE, BLOCK_SIZE, BLUE))
elif block_type == 'Z':
self.add(Block(400, 0, GREEN))
self.add(Block(400 - BLOCK_SIZE, 0, GREEN))
self.add(Block(400, BLOCK_SIZE, GREEN))
self.add(Block(400 + BLOCK_SIZE, BLOCK_SIZE, GREEN))
elif block_type == 'J':
self.add(Block(400, 0, RED))
self.add(Block(400, BLOCK_SIZE, RED))
self.add(Block(400 - BLOCK_SIZE, BLOCK_SIZE, RED))
self.add(Block(400 - BLOCK_SIZE, BLOCK_SIZE * 2, RED))
elif block_type == 'L':
self.add(Block(400, 0, BLUE))
self.add(Block(400, BLOCK_SIZE, BLUE))
self.add(Block(400 + BLOCK_SIZE, BLOCK_SIZE, BLUE))
self.add(Block(400 + BLOCK_SIZE, BLOCK_SIZE * 2, BLUE))
# 判断方块是否可以移动
def can_move(self, x, y):
for block in self.sprites():
if block.rect.x + x < 0 or block.rect.x + x >= WIDTH:
return False
if block.rect.y + y < 0 or block.rect.y + y >= HEIGHT:
return False
return True
# 移动方块
def move(self, x, y):
if self.can_move(x, y):
for block in self.sprites():
block.rect.x += x
block.rect.y += y
# 判断是否可以旋转
def can_rotate(self):
# 获取中心点
center_x = self.sprites()[1].rect.x
center_y = self.sprites()[1].rect.y
# 计算旋转后的位置
for block in self.sprites():
delta_x = block.rect.x - center_x
delta_y = block.rect.y - center_y
block.rect.x = center_x - delta_y
block.rect.y = center_y + delta_x
if block.rect.x < 0 or block.rect.x >= WIDTH or block.rect.y < 0 or block.rect.y >= HEIGHT:
return False
return True
# 旋转方块
def rotate(self):
if self.can_rotate():
center_x = self.sprites()[1].rect.x
center_y = self.sprites()[1].rect.y
for block in self.sprites():
delta_x = block.rect.x - center_x
delta_y = block.rect.y - center_y
block.rect.x = center_x - delta_y
block.rect.y = center_y + delta_x
# 创建方块组
block_group = BlockGroup()
# 设置时钟
clock = pygame.time.Clock()
# 开始游戏循环
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
block_group.move(-BLOCK_SIZE, 0)
elif event.key == pygame.K_RIGHT:
block_group.move(BLOCK_SIZE, 0)
elif event.key == pygame.K_DOWN:
block_group.move(0, BLOCK_SIZE)
elif event.key == pygame.K_UP:
block_group.rotate()
# 检测方块是否可以下落
if block_group.can_move(0, BLOCK_SIZE):
block_group.move(0, BLOCK_SIZE)
else:
# 方块到达底部,创建新的方块
block_group = BlockGroup()
# 绘制背景
screen.fill(WHITE)
# 绘制方块
block_group.draw(screen)
# 绘制分数
score_text = font.render('Score: 0', True, BLACK)
screen.blit(score_text, (10, 10))
# 更新屏幕
pygame.display.flip()
# 设置帧率
clock.tick(30)
# 退出游戏
pygame.quit()
```
这个代码可以在Python IDE的交互模式下运行,也可以保存为.py文件后运行。运行后,可以使用方向键左右移动方块,使用方向键下加速方块下落,使用方向键上旋转方块。游戏结束时,可以按ESC键退出游戏。
阅读全文