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
de509fba
authored
Mar 05, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
9afe71bc
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
14 deletions
my_Tetris.py
my_Tetris.py
View file @
de509fba
...
@@ -7,6 +7,7 @@ grid_size = 20 # 格子大小
...
@@ -7,6 +7,7 @@ grid_size = 20 # 格子大小
grid_num_width
=
15
# 横向格子数量
grid_num_width
=
15
# 横向格子数量
grid_num_height
=
25
# 纵向格子数量
grid_num_height
=
25
# 纵向格子数量
FPS
=
30
FPS
=
30
#通过帧率进行运算
count
=
0
count
=
0
states
=
False
states
=
False
...
@@ -39,20 +40,33 @@ L = [[(-1, 0), (0, 0), (1, 0), (1, 1)],
...
@@ -39,20 +40,33 @@ L = [[(-1, 0), (0, 0), (1, 0), (1, 1)],
[(
-
1
,
0
),
(
0
,
0
),
(
1
,
0
),
(
-
1
,
-
1
)],
[(
-
1
,
0
),
(
0
,
0
),
(
1
,
0
),
(
-
1
,
-
1
)],
[(
0
,
-
1
),
(
0
,
0
),
(
0
,
1
),
(
-
1
,
1
)]]
[(
0
,
-
1
),
(
0
,
0
),
(
0
,
1
),
(
-
1
,
1
)]]
shape_list
=
[
I
,
J
,
L
,
O
,
S
,
T
,
Z
]
# 7种类型俄罗斯方块
shape_list
=
[
I
,
J
,
L
,
O
,
S
,
T
,
Z
]
# 7种类型俄罗斯方块
current_shape
=
[(
0
,
-
1
),
(
0
,
0
),
(
0
,
1
),
(
-
1
,
0
)]
# current_shape = [(0, -1), (0, 0), (0, 1), (-1, 0)]
# 一些RGB颜色
# 一些RGB颜色
cube_colors
=
[
cube_colors
=
[
(
204
,
153
,
153
),
(
102
,
102
,
153
),(
153
,
0
,
102
),
(
204
,
153
,
153
),
(
102
,
102
,
153
),(
153
,
0
,
102
),
(
255
,
204
,
0
),
(
204
,
0
,
51
),(
255
,
0
,
51
),
(
0
,
102
,
153
),
(
255
,
204
,
0
),
(
204
,
0
,
51
),(
255
,
0
,
51
),
(
0
,
102
,
153
),
(
153
,
0
,
51
),
(
204
,
255
,
102
),
(
255
,
153
,
0
)]
(
153
,
0
,
51
),
(
204
,
255
,
102
),
(
255
,
153
,
0
)]
# center = [2,8]
# #随机选出俄罗斯类型
# shape = random.choice(shape_list)
# #随机选出俄罗斯类型的一种具体形状的下标
# index = random.randint(0,len(shape)-1)
# #根据下标选取具体形状
# current_shape=shape[index]
# #随机选取颜色
# color = random.choice(cube_colors)
#封装函数
#封装函数
def
cleck
(
center
):
def
cleck
(
center
):
#注意形参为中心点
for
cube
in
current_shape
:
for
cube
in
current_shape
:
#先计算每一个小方块行和列的位置
cube
=
(
cube
[
0
]
+
center
[
0
],
cube
[
1
]
+
center
[
1
])
cube
=
(
cube
[
0
]
+
center
[
0
],
cube
[
1
]
+
center
[
1
])
#判断小方块有没有超出网格范围
if
cube
[
0
]
<
1
or
cube
[
1
]
<
1
or
cube
[
0
]
>
grid_num_height
or
cube
[
1
]
>
grid_num_width
:
if
cube
[
0
]
<
1
or
cube
[
1
]
<
1
or
cube
[
0
]
>
grid_num_height
or
cube
[
1
]
>
grid_num_width
:
#返回值
return
False
return
False
while
True
:
while
True
:
...
@@ -62,8 +76,9 @@ while True:
...
@@ -62,8 +76,9 @@ while True:
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_RIGHT
:
# 向右
if
event
.
key
==
locals
.
K_RIGHT
:
# 向右
center
[
1
]
=
center
[
1
]
+
1
#先让中心点向左移动1列
center
[
1
]
=
center
[
1
]
+
1
#先让中心点向左移动1列
if
cleck
(
center
)
==
False
:
if
cleck
(
center
)
==
False
:
#小方块超出了网格范围
center
[
1
]
=
center
[
1
]
-
1
center
[
1
]
=
center
[
1
]
-
1
#将小方块还原
elif
event
.
key
==
locals
.
K_LEFT
:
# 向左
elif
event
.
key
==
locals
.
K_LEFT
:
# 向左
center
[
1
]
=
center
[
1
]
-
1
#先让中心点向左移动1列
center
[
1
]
=
center
[
1
]
-
1
#先让中心点向左移动1列
if
cleck
(
center
)
==
False
:
if
cleck
(
center
)
==
False
:
...
@@ -78,32 +93,35 @@ while True:
...
@@ -78,32 +93,35 @@ while True:
center
[
0
]
=
center
[
0
]
+
1
#先让中心点向下移动1列
center
[
0
]
=
center
[
0
]
+
1
#先让中心点向下移动1列
if
cleck
(
center
)
==
False
:
if
cleck
(
center
)
==
False
:
center
[
0
]
=
center
[
0
]
-
1
center
[
0
]
=
center
[
0
]
-
1
#实现俄罗斯方块变形,并且防止在边缘变形是超出网格范围
elif
event
.
key
==
locals
.
K_UP
:
# 向
下
elif
event
.
key
==
locals
.
K_UP
:
# 向
上
old_index
=
index
old_index
=
index
#先将原来索引备份下来
index
+=
1
index
+=
1
if
index
>=
len
(
shape
):
if
index
>=
len
(
shape
):
#注意下标,最多4种,超过即重置
index
=
0
index
=
0
current_shape
=
shape
[
index
]
current_shape
=
shape
[
index
]
if
cleck
(
center
)
==
False
:
if
cleck
(
center
)
==
False
:
#防止在网格边缘变形,利用自定义函数检测
index
=
old_index
index
=
old_index
#若是超出则还原回来
current_shape
=
shape
[
index
]
current_shape
=
shape
[
index
]
#重新取出原来形状
#到达底部时要不要创建新的俄罗斯方块
if
states
==
False
:
if
states
==
False
:
states
=
True
states
=
True
#不断生成俄罗斯方块
#不断生成俄罗斯方块
center
=
[
2
,
8
]
# 第2行第8列
center
=
[
2
,
8
]
# 第2行第8列
#随机
#随机
shape
=
random
.
choice
(
shape_list
)
#随机形状
shape
=
random
.
choice
(
shape_list
)
#随机形状
index
=
random
.
randint
(
0
,
len
(
shape
)
-
1
)
#随机形状的索引位置
index
=
random
.
randint
(
0
,
len
(
shape
)
-
1
)
#随机形状的索引位置
current_shape
=
shape
[
index
]
#根据下标取出方块的形状
current_shape
=
shape
[
index
]
#根据下标取出方块的形状
color
=
random
.
choice
(
cube_colors
)
#随机颜色
color
=
random
.
choice
(
cube_colors
)
#随机颜色
#下落速度
count
+=
1
count
+=
1
#如果下落速度对帧率能够整除
if
count
%
FPS
==
0
:
if
count
%
FPS
==
0
:
#就下降一个网格
center
[
0
]
=
center
[
0
]
+
1
#先让中心点向下移动1列
center
[
0
]
=
center
[
0
]
+
1
#先让中心点向下移动1列
if
cleck
(
center
)
==
False
:
if
cleck
(
center
)
==
False
:
#如果下降到底部,就重新生成一个
center
[
0
]
=
center
[
0
]
-
1
center
[
0
]
=
center
[
0
]
-
1
states
=
False
states
=
False
# 将背景图画上去
# 将背景图画上去
...
...
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