# !/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@author: spring.wang
@license:
@contact: wyu_01@163.com
@software: tetris
@file: tetris1.py
@time: 2024/2/20 17:26
@description:
pyinstaller.exe -F -w --distpath D:\myproject\python\pygame\bin\tetris --workpath D:\myproject\python\pygame\bin\temp --specpath D:\myproject\python\pygame\bin\temp D:\myproject\python\pygame\game\tetris.py
'''
import pygame
import random
import time
import sys
# 游戏窗口尺寸
WINDOW_WIDTH = 200
WINDOW_HEIGHT = 400
# 方块尺寸
BLOCK_SIZE = 20
# 初始化 Pygame
pygame.init()
# 初始化窗口
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
class Block:
def __init__(self, shape, color):
self.shape = shape
self.color = color
self.x = WINDOW_WIDTH // 2 - BLOCK_SIZE
self.y = 0
self.timer = 0
self.fall_speed = 10
self.move_speed = 1
self.max_speed = 3
def set_speed(self):
self.move_speed += 1
if self.move_speed > self.max_speed:
self.move_speed = self.max_speed
def reset_speed(self):
self.move_speed = 1
def move_up(self):
self.y -= BLOCK_SIZE
def move_down(self):
self.y += BLOCK_SIZE
def move_left(self):
self.x -= BLOCK_SIZE
def move_right(self):
self.x += BLOCK_SIZE
def rotate(self):
self.shape = [[row[i] for row in self.shape][::-1] for i in range(len(self.shape[0]))]
# print(self.shape)
def draw(self):
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] == 1:
pygame.draw.rect(window, self.color,
(self.x + j * BLOCK_SIZE, self.y + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
class Tetris:
# 方块颜色
BLACK = (0, 0, 0)
CYAN = (0, 255, 255)
YELLOW = (255, 255, 0)
PURPLE = (255, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
ORANGE = (255, 165, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
# 定义方块形状
SHAPES = [
[[1, 1, 1, 1]],
[[1, 1], [1, 1]],
[[1, 1, 0], [0, 1, 1]],
[[0, 1, 1], [1, 1, 0]],
[[1, 1, 1], [0, 1, 0]],
[[1, 1, 1], [1, 0, 0]],
[[1, 1, 1], [0, 0, 1]]
]
# 定义方块颜色
COLORS = [CYAN, YELLOW, PURPLE, GREEN, RED, ORANGE, BLUE]
def __init__(self):
self.score = 0
self.timer = 0
self.fall_speed = 1
self.FPS = 60
self.game_running = True
pygame.display.set_caption("俄罗斯方块")
self.clock = pygame.time.Clock()
self.grid = [[self.BLACK] * (WINDOW_WIDTH // BLOCK_SIZE) for _ in range(WINDOW_HEIGHT // BLOCK_SIZE)]
self.block = Block(random.choice(self.SHAPES), random.choice(self.COLORS))
self.key_down_time = None
self.key_left_time = None
self.key_right_time = None
self.down_flag = True
self.direction = 0
def draw_grid(self):
for x in range(0, WINDOW_WIDTH, BLOCK_SIZE):
pygame.draw.line(window, self.BLACK, (x, 0), (x, WINDOW_HEIGHT))
for y in range(0, WINDOW_HEIGHT, BLOCK_SIZE):
pygame.draw.line(window, self.BLACK, (0, y), (WINDOW_WIDTH, y))
def check_collision(self):
for i in range(len(self.block.shape)):
for j in range(len(self.block.shape[i])):
if self.block.shape[i][j] == 1:
if self.block.y + (i + 1) * BLOCK_SIZE >= WINDOW_HEIGHT or \
self.block.x + j * BLOCK_SIZE < 0 or \
self.block.x + j * BLOCK_SIZE >= WINDOW_WIDTH or \
self.grid[self.block.y // BLOCK_SIZE + i + 1][self.block.x // BLOCK_SIZE + j] != self.BLACK:
return True
return False
def event_handler(self):
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game_running = False
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.key_left_time = time.time()
self.block.move_left()
if self.check_collision():
self.block.move_right()
elif event.key == pygame.K_RIGHT:
self.key_right_time = time.time()
self.block.move_right()
if self.check_collision():
self.block.move_left()
elif event.key == pygame.K_DOWN:
if not self.check_collision():
self.key_down_time = time.time()
self.block.move_down()
elif event.key == pygame.K_UP:
self.block.rotate()
if self.check_collision():
for _ in range(3):
self.block.rotate()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
self.key_left_time = None
self.down_flag = True
self.timer = 0
self.direction = 0
if event.key == pygame.K_RIGHT:
self.key_right_time = None
self.down_flag = True
self.timer = 0
self.direction = 0
if event.key == pygame.K_DOWN:
self.key_down_time = None
self.timer=0
def update(self):
if self.timer >= 1 / self.fall_speed:
self.timer -= 1 / self.fall_speed
if not self.check_collision() and self.down_flag:
self.block.move_down()
elif not self.check_collision():
if self.direction == 1:
self.block.move_left()
if self.check_collision():
self.block.move_right()
if self.direction == 2:
self.block.move_right()
if self.check_collision():
self.block.move_left()
else:
for i in range(len(self.block.shape)):
for j in range(len(self.block.shape[i])):
if self.block.shape[i][j] == 1:
# print(block.y // BLOCK_SIZE + i, block.x // BLOCK_SIZE + j,i,j)
self.grid[self.block.y // BLOCK_SIZE + i][self.block.x // BLOCK_SIZE + j] = self.block.color
self.score += self.clear_rows()
if self.score%20 == 0 and self.score != 0 :
self.fall_speed += 1
self.block = Block(random.choice(self.SHAPES), random.choice(self.COLORS))
if self.check_collision():
self.game_running = False
current_time = time.time()
if self.key_down_time is not None and (current_time - self.key_down_time) > 0.1: # 假设0.1秒后开始加速
self.key_down_time = current_time
self.timer += 1 / self.fall_speed*2
if self.key_left_time is not None and (current_time - self.key_left_time) > 0.1: # 假设0.1秒后开始加速
self.key_left_time = current_time
self.down_flag = False
self.direction = 1
self.timer += 1 / self.fall_speed*20
if self.key_right_time is not None and (current_time - self.key_right_time) > 0.1: # 假设0.1秒后开始加速
self.key_right_time = current_time
self.dow
没有合适的资源?快使用搜索试试~ 我知道了~
python编写俄罗斯方块游戏
共8个文件
png:3个
ttf:2个
py:2个
需积分: 5 0 下载量 195 浏览量
2024-03-26
15:05:06
上传
评论
收藏 7.59MB RAR 举报
温馨提示
python编写俄罗斯方块游戏 ,主要使用pygame
资源推荐
资源详情
资源评论
收起资源包目录
game.rar (8个子文件)
game
__init__.py 225B
tetris.py 12KB
resource
music
gotime.mp3 2.08MB
font
COOPBL.TTF 79KB
simfang.ttf 10.09MB
icon
back.png 60KB
start.png 6KB
start_hover.png 4KB
共 8 条
- 1
资源评论
semicolon_helloword
- 粉丝: 863
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功