Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson12_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
b6bf4720
authored
May 15, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
9d8ede2e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
60 deletions
my_Tetris.py
my_Tetris.py
View file @
b6bf4720
...
...
@@ -10,6 +10,7 @@ grid_num_height = 25 # 纵向格子数量
FPS
=
30
# 帧率
count
=
0
states
=
False
gameover
=
False
# 新建变量gameover初始值设为False,表示游戏正常运行
# 创建窗口
screen
=
pygame
.
display
.
set_mode
((
460
,
500
))
...
...
@@ -18,6 +19,8 @@ clock = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 载入素材
background
=
pygame
.
image
.
load
(
'bg.png'
)
font
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
60
)
# 字体
# 新建字体对象
font_restart
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
25
)
# 俄罗斯方块所有形状
O
=
[[(
0
,
0
),
(
0
,
1
),
(
1
,
0
),
(
1
,
1
)]]
...
...
@@ -50,7 +53,7 @@ cube_colors = [
# 创建一个含有25个小列表,每个小列表都有15个元素,每个元素的值都是0的列表
num_list
=
[]
# 创建空列表存储最终的列表元素并命名为num_list
for
i
in
range
(
25
):
# 使用for循环让程序重复执行25次
num_list
.
append
([
0
]
*
15
)
# 用num_list.append()方法将小列表添加到
打
列表里
num_list
.
append
([
0
]
*
15
)
# 用num_list.append()方法将小列表添加到
大
列表里
def
check
(
center
):
for
cube
in
current_shape
:
...
...
@@ -93,66 +96,79 @@ while True:
index
=
old_index
current_shape
=
shape
[
index
]
if
states
==
False
:
states
=
True
center
=
[
2
,
8
]
# 第2行第8列
shape
=
random
.
choice
(
shape_list
)
index
=
random
.
randint
(
0
,
len
(
shape
)
-
1
)
# 随机形状索引
current_shape
=
shape
[
index
]
color
=
random
.
choice
(
cube_colors
)
# 随机选取一种颜色
count
+=
1
if
count
%
FPS
==
0
:
# 降落速度的算式
center
[
0
]
=
center
[
0
]
+
1
if
check
(
center
)
==
False
:
center
[
0
]
=
center
[
0
]
-
1
states
=
False
# for循环进行遍历取出每一个小方块的行、列位置
for
cube
in
current_pos
:
# 用它们减去1作为索引找到对应的值改成俄罗斯方块的颜色
num_list
[
cube
[
0
]
-
1
][
cube
[
1
]
-
1
]
=
color
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
# 计算出所有小方块的行、列位置
current_pos
=
[]
for
cube
in
current_shape
:
pos
=
(
cube
[
0
]
+
center
[
0
],
cube
[
1
]
+
center
[
1
])
current_pos
.
append
(
pos
)
# 取出所有小方块的行、列位置,计算坐标,绘制俄罗斯方块
for
cube
in
current_pos
:
pygame
.
draw
.
rect
(
screen
,
color
,
(
cube
[
1
]
*
20
-
20
,
cube
[
0
]
*
20
-
20
,
20
,
20
),
0
)
pygame
.
draw
.
rect
(
screen
,
(
255
,
255
,
255
),
(
cube
[
1
]
*
20
-
20
,
cube
[
0
]
*
20
-
20
,
20
,
20
),
1
)
# 创建一个1-25的列表进行遍历(记录行的位置)
for
i
,
row
in
zip
(
range
(
1
,
26
),
num_list
):
# 创建一个1-15的列表进行遍历(记录列的位置)
for
j
,
colors
in
zip
(
range
(
1
,
16
),
row
):
if
colors
!=
0
:
# if判断每一个格子对应的值不等于0
# 用pygame.draw.rect()方法画出小方块
pygame
.
draw
.
rect
(
screen
,
colors
,
(
j
*
20
-
20
,
i
*
20
-
20
,
20
,
20
))
pygame
.
draw
.
rect
(
screen
,
(
255
,
255
,
255
),
(
j
*
20
-
20
,
i
*
20
-
20
,
20
,
20
),
1
)
if
gameover
==
False
:
# if判断变量gameover的值等于False
# 生成新的俄罗斯方块
if
states
==
False
:
states
=
True
center
=
[
2
,
8
]
# 第2行第8列
shape
=
random
.
choice
(
shape_list
)
index
=
random
.
randint
(
0
,
len
(
shape
)
-
1
)
# 随机形状索引
current_shape
=
shape
[
index
]
color
=
random
.
choice
(
cube_colors
)
# 随机选取一种颜色
count
+=
1
if
count
%
FPS
==
0
:
# 降落速度的算式
center
[
0
]
=
center
[
0
]
+
1
if
check
(
center
)
==
False
:
center
[
0
]
=
center
[
0
]
-
1
states
=
False
# for循环进行遍历取出每一个小方块的行、列位置
for
cube
in
current_pos
:
# 用它们减去1作为索引找到对应的值改成俄罗斯方块的颜色
num_list
[
cube
[
0
]
-
1
][
cube
[
1
]
-
1
]
=
color
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
# 计算出所有小方块的行、列位置
current_pos
=
[]
for
cube
in
current_shape
:
pos
=
(
cube
[
0
]
+
center
[
0
],
cube
[
1
]
+
center
[
1
])
current_pos
.
append
(
pos
)
# 取出所有小方块的行、列位置,计算坐标,绘制俄罗斯方块
for
cube
in
current_pos
:
pygame
.
draw
.
rect
(
screen
,
color
,
(
cube
[
1
]
*
20
-
20
,
cube
[
0
]
*
20
-
20
,
20
,
20
),
0
)
pygame
.
draw
.
rect
(
screen
,
(
255
,
255
,
255
),
(
cube
[
1
]
*
20
-
20
,
cube
[
0
]
*
20
-
20
,
20
,
20
),
1
)
# 创建一个1-25的列表进行遍历(记录行的位置)
for
i
,
row
in
zip
(
range
(
1
,
26
),
num_list
):
# 创建一个1-15的列表进行遍历(记录列的位置)
for
j
,
colors
in
zip
(
range
(
1
,
16
),
row
):
if
colors
!=
0
:
# if判断每一个格子对应的值不等于0
# 用pygame.draw.rect()方法画出小方块
pygame
.
draw
.
rect
(
screen
,
colors
,
(
j
*
20
-
20
,
i
*
20
-
20
,
20
,
20
))
pygame
.
draw
.
rect
(
screen
,
(
255
,
255
,
255
),
(
j
*
20
-
20
,
i
*
20
-
20
,
20
,
20
),
1
)
new_list
=
[]
# 新的地图列表
for
i
in
range
(
25
):
new_list
.
append
([
0
]
*
15
)
row_index
=
24
for
i
in
range
(
24
,
-
1
,
-
1
):
is_full
=
Ture
for
j
in
range
(
grid_num_width
):
if
num_list
[
i
][
j
]
==
0
:
is_full
=
False
if
is_full
==
False
:
new_list
[
row_index
]
=
num_list
[
i
]
row_index
-=
1
else
:
score
+=
1
num_list
=
new_list
# 得分
text_surface
=
font
.
render
(
str
(
score
),
True
,
(
0
,
0
,
0
))
screen
.
blit
(
text_surface
,
(
350
,
70
))
# 新的地图列表
new_list
=
[]
# 给新的空白列表命名为new_list
for
i
in
range
(
25
):
# 使用for循环让程序重复执行25次
new_list
.
append
([
0
]
*
15
)
# 用num_list.append()方法将小列表添加到大列表里
# 新建变量row_index用与记录新网格列表改变的行数序号,并设置为24表最后一行
row_index
=
24
for
i
in
range
(
24
,
-
1
,
-
1
):
# 结合for循环生成一个倒着排序的列表索引
# (确认每一行网格有没有被小方块填满)
is_full
=
True
# 建立变量is_full并设置为Ture默认网格状态被小方块填满
for
j
in
range
(
grid_num_width
):
# for循环取出这一行网格的每一小格检查
if
num_list
[
i
][
j
]
==
0
:
# if判断这一行网格的每一小格的值是否等于0
is_full
=
False
# 如果等于0,说明还有格子没有填满,就将值设置为False
if
is_full
==
False
:
# if判断这一行没有被填满
new_list
[
row_index
]
=
num_list
[
i
]
# 通过下标将这一行数值赋值到新列表里
row_index
-=
1
# 将序列号减少1
else
:
# 否则(这一行填满)
score
+=
1
# 分数变量score的值增加1
num_list
=
new_list
# 网格列表的值new_list赋值给变量num_list,替换原来的数值
# 小方块超出高度
if
num_list
[
1
][
7
]
!=
0
:
# 判断列表num_list[1][7]不等于0
gameover
=
True
# 将变量gameover的值设为True
# 得分
text_surface
=
font
.
render
(
str
(
score
),
True
,
(
0
,
0
,
0
))
screen
.
blit
(
text_surface
,
(
350
,
70
))
if
gameover
==
True
:
# 渲染游戏失败的文字提示
text
=
font_restart
.
render
(
"游戏失败,按下任意键开始"
)
screen
.
blit
(
text
,
(
20
,
250
))
# 刷新画面
pygame
.
display
.
update
()
clock
.
tick
(
FPS
)
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