Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson11_diy01
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b7d9335f
authored
Sep 12, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
54e06744
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
9 deletions
my_Tetris.py
my_Tetris.py
View file @
b7d9335f
...
...
@@ -7,7 +7,9 @@ score = 0
grid_size
=
20
# 格子大小
grid_num_width
=
15
# 横向格子数量
grid_num_height
=
25
# 纵向格子数量
FPS
=
30
FPS
=
30
# 帧率
count
=
0
# 设置变量count,初始值为0
states
=
False
# 创建变量states来设置是否创建新方块,初始值设为False
# 创建窗口
screen
=
pygame
.
display
.
set_mode
((
460
,
500
))
...
...
@@ -45,14 +47,6 @@ cube_colors = [
(
255
,
204
,
0
),
(
204
,
0
,
51
),(
255
,
0
,
51
),
(
0
,
102
,
153
),
(
153
,
0
,
51
),
(
204
,
255
,
102
),
(
255
,
153
,
0
)]
center
=
[
2
,
8
]
# 第2行第8列
# 调用random模块中的choice方法从7种方块列表中随机选出一个,并赋值给变量shape
shape
=
random
.
choice
(
shape_list
)
# 随机形状索引
index
=
random
.
randint
(
0
,
len
(
shape
)
-
1
)
# 随机选出一个方块形状绘制出来
current_shape
=
shape
[
index
]
# 生成索引下标
color
=
random
.
choice
(
cube_colors
)
# 方块颜色调用random模块的choice方法随机选取一种
def
check
(
center
):
# def定义函数check(),参数center表示行、列
for
cube
in
current_shape
:
# for循环
# 计算出移动后每个小方块的行、列位置
...
...
@@ -82,6 +76,33 @@ while True:
if
check
(
center
)
==
False
:
center
[
0
]
=
center
[
0
]
-
1
# 上移1列,位置还原
# 设置向上方向键来触发俄罗斯方块变形的功能
elif
event
.
key
==
locals
.
K_UP
:
# 向上
old_index
=
index
# 用变量old_index将原来的索引index备份下来
index
+=
1
# 表示索引的变量index增加1
if
index
>=
len
(
shape
):
# if判断变量index的值是否与形状列表一样长
index
=
0
# 变量indfex设为0
current_shape
=
shape
[
index
]
# 根据索引取出对应的方块形状
if
check
(
center
)
==
False
:
# if判断是否超出范围,用自定义check()函数进行检测
index
=
old_index
# 将索引index的值还原回来
current_shape
=
shape
[
index
]
# 重新取出原来的形状
if
states
==
False
:
# if判断变量states的值为False时,创建新方块
states
=
True
# 变量states的值设为Ture
center
=
[
2
,
8
]
# 第2行第8列
# 调用random模块中的choice方法从7种方块列表中随机选出一个,并赋值给变量shape
shape
=
random
.
choice
(
shape_list
)
# 随机形状索引
index
=
random
.
randint
(
0
,
len
(
shape
)
-
1
)
# 随机选出一个方块形状绘制出来
current_shape
=
shape
[
index
]
# 生成索引下标
color
=
random
.
choice
(
cube_colors
)
# 方块颜色调用random模块的choice方法随机选取一种
count
+=
1
# 用变量count记录while循环运行次数
if
count
%
FPS
==
0
:
# if判断变量count除以FPS帧率等于0,如果能够整除则自动下移一行
center
[
0
]
=
center
[
0
]
+
1
# 中心点下移1行
# if判断方块有没有超出网格范围,使用检查函数进行检测
if
check
(
center
)
==
False
:
center
[
0
]
=
center
[
0
]
-
1
# 上移1列,位置还原
states
=
False
# 方块下降到底部时,重新将变量states的值设为False
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
# 计算出所有小方块的行、列位置
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment