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
96465410
authored
Oct 12, 2025
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
1dd7b9f7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
9 deletions
my_Tetris.py
test.py
my_Tetris.py
View file @
96465410
import
pygame
from
pygame
import
locals
import
random
# 提到随机 就应该想到随机模块,前面便是它的导入方式
pygame
.
init
()
# 初始化
score
=
0
...
...
@@ -45,22 +46,33 @@ cube_colors = [
(
153
,
0
,
51
),
(
204
,
255
,
102
),
(
255
,
153
,
0
)]
center
=
[
2
,
8
]
# 第2行第8列
current_shape
=
[(
0
,
-
1
),
(
0
,
0
),
(
0
,
1
),
(
-
1
,
0
)]
shape
=
random
.
choice
(
shape_list
)
#random.choice() 是指从非空序列(列表、元组等)中随机选取一个元素
index
=
random
.
randint
(
0
,
len
(
shape
)
-
1
)
# random.randint() 用于生成指定范围内的整数
current_shape
=
shape
[
index
]
color
=
random
.
choice
(
cube_colors
)
# 在python中 变量及自定义函数的命名规则是:只能包含数字、字母、下划线且不能以数字开头。
def
check
(
center
):
# 在python中 定义自定义函数应以 def开头 例如 : def abc():
for
cube
in
current_shape
:
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
:
return
False
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_RIGHT
:
# 向右
if
center
[
1
]
<
15
:
if
event
.
key
==
locals
.
K_LEFT
:
center
[
1
]
-=
1
if
check
(
center
)
==
False
:
center
[
1
]
+=
1
elif
event
.
key
==
locals
.
K_LEFT
:
# 向左
if
center
[
1
]
>
1
:
elif
event
.
key
==
locals
.
K_RIGHT
:
center
[
1
]
+=
1
if
check
(
center
)
==
False
:
center
[
1
]
-=
1
elif
event
.
key
==
locals
.
K_DOWN
:
# 向下
if
center
[
0
]
<
25
:
center
[
0
]
+=
1
elif
event
.
key
==
locals
.
K_DOWN
:
center
[
0
]
+=
1
if
check
(
center
)
==
False
:
center
[
0
]
-=
1
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
...
...
test.py
0 → 100644
View file @
96465410
a
=
0
if
a
>=
4
:
print
(
"123"
)
elif
1
<=
a
<=
3
print
(
'456'
)
# NameError: name '*****' is not defined 其中 ***为我们的报错变量,此报错信息为 *** 这个变量未被定义(not defined),
# 因此我们需要去前面定义一下此变量
# SyntaxError: invalid syntax 这个报错是提示我们的语法有错误,因此我们需要去检查本行代码,
# 如若没有问题,则去前面几行去检查
\ No newline at end of file
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