Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson12_diy3
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
1ba7f5b1
authored
Nov 12, 2023
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
d031bdb1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
105 additions
and
152 deletions
my_Tetris.py
my_Tetris.py
View file @
1ba7f5b1
import
pygame
import
tkinter
from
pygame
import
locals
import
random
pygame
.
init
()
# 初始化
def
login_to_reg
():
# 登录界面转注册界面
score
=
0
app
.
login
.
root
.
destory
()
grid_size
=
20
# 格子大小
global
app_reg
grid_num_width
=
15
# 横向格子数量
app_rep
=
My_register
()
grid_num_height
=
25
# 纵向格子数量
app
.
reg
.
show
()
FPS
=
30
# 帧率
count
=
0
states
=
False
gameover
=
False
# 创建窗口
def
reg_to_login
():
# 注册界面转登录界面
screen
=
pygame
.
display
.
set_mode
((
460
,
500
)
)
app_reg
.
root
.
destory
(
)
pygame
.
display
.
set_caption
(
"俄罗斯方块"
)
global
app_login
clock
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
app_login
=
My_login
()
# 载入素材
app
.
login
.
show
()
background
=
pygame
.
image
.
load
(
'bg.png'
)
def
register
():
# 注册验证
font
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
60
)
# 字体
pass
# 俄罗斯方块所有形状
def
login
():
# 登录验证
O
=
[[(
0
,
0
),
(
0
,
1
),
(
1
,
0
),
(
1
,
1
)]]
pass
I
=
[[(
0
,
-
1
),
(
0
,
0
),
(
0
,
1
),
(
0
,
2
)],
[(
-
1
,
0
),
(
0
,
0
),
(
1
,
0
),
(
2
,
0
)]]
Z
=
[[(
0
,
-
1
),
(
0
,
0
),
(
1
,
0
),
(
1
,
1
)],
[(
-
1
,
0
),
(
0
,
0
),
(
0
,
-
1
),
(
1
,
-
1
)]]
S
=
[[(
-
1
,
0
),
(
0
,
0
),
(
0
,
1
),
(
1
,
1
)],
[(
1
,
-
1
),
(
1
,
0
),
(
0
,
0
),
(
0
,
1
)]]
T
=
[[(
0
,
-
1
),
(
0
,
0
),
(
0
,
1
),
(
-
1
,
0
)],
[(
-
1
,
0
),
(
0
,
0
),
(
1
,
0
),
(
0
,
1
)],
[(
0
,
-
1
),
(
0
,
0
),
(
0
,
1
),
(
1
,
0
)],
[(
-
1
,
0
),
(
0
,
0
),
(
1
,
0
),
(
0
,
-
1
)]]
J
=
[[(
-
1
,
0
),
(
0
,
0
),
(
1
,
0
),
(
1
,
-
1
)],
[(
0
,
-
1
),
(
0
,
0
),
(
0
,
1
),
(
-
1
,
-
1
)],
[(
-
1
,
0
),
(
0
,
0
),
(
1
,
0
),
(
-
1
,
1
)],
[(
0
,
-
1
),
(
0
,
0
),
(
0
,
1
),
(
1
,
1
)]]
L
=
[[(
-
1
,
0
),
(
0
,
0
),
(
1
,
0
),
(
1
,
1
)],
[(
0
,
-
1
),
(
0
,
0
),
(
0
,
1
),
(
1
,
-
1
)],
[(
-
1
,
0
),
(
0
,
0
),
(
1
,
0
),
(
-
1
,
-
1
)],
[(
0
,
-
1
),
(
0
,
0
),
(
0
,
1
),
(
-
1
,
1
)]]
shape_list
=
[
I
,
J
,
L
,
O
,
S
,
T
,
Z
]
# 7种类型俄罗斯方块
# 一些RGB颜色
class
My_login
():
# 登录窗口
cube_colors
=
[
def
__init__
(
self
):
(
204
,
153
,
153
),
(
102
,
102
,
153
),(
153
,
0
,
102
),
self
.
root
=
tkinter
.
Tk
()
(
255
,
204
,
0
),
(
204
,
0
,
51
),(
255
,
0
,
51
),
(
0
,
102
,
153
),
self
.
root
.
title
(
"登录窗口"
)
(
153
,
0
,
51
),
(
204
,
255
,
102
),
(
255
,
153
,
0
)]
self
.
root
.
geometry
(
'400x300+500+300'
)
self
.
root
.
resizable
(
width
=
False
,
height
=
False
)
# True可以拉伸,False不能拉伸
def
check
(
center
):
def
show
(
self
):
# 登录界面的所有控件
for
cube
in
current_shape
:
# Label文本标签,界面提示信息
cube
=
(
cube
[
0
]
+
center
[
0
],
cube
[
1
]
+
center
[
1
])
l
=
tkinter
.
Label
(
self
.
root
,
text
=
'您好!请登录'
,
bg
=
'green'
,
# 当前的小方块超出网格的行、列,就返回False
font
=
(
'宋体'
,
15
),
width
=
40
,
height
=
2
)
if
cube
[
0
]
<
1
or
cube
[
1
]
<
1
or
cube
[
0
]
>
grid_num_height
\
l
.
place
(
x
=
0
,
y
=
0
)
or
cube
[
1
]
>
grid_num_width
:
l
=
tkinter
.
Label
(
self
.
root
,
text
=
'用户名:'
,
font
=
(
"宋体"
,
12
),
return
False
fg
=
"black"
)
if
num_list
[
cube
[
0
]
-
1
][
cube
[
1
]
-
1
]
!=
0
:
l
.
place
(
x
=
50
,
y
=
80
)
return
False
l
=
tkinter
.
Label
(
self
.
root
,
text
=
'密 码:'
,
font
=
(
"宋体"
,
12
),
fg
=
"black"
)
l
.
place
(
x
=
50
,
y
=
140
)
num_list
=
[]
# Entry单行文本输入框:用户名、密码
for
i
in
range
(
25
):
self
.
e1
=
tkinter
.
Entry
(
self
.
root
,
show
=
None
,
font
=
(
'宋体'
,
14
),
num_list
.
append
([
0
]
*
15
)
bg
=
"light grey"
,
width
=
18
)
# 显示成明文形式
self
.
e2
=
tkinter
.
Entry
(
self
.
root
,
show
=
'*'
,
font
=
(
'宋体'
,
14
),
width
=
18
)
# 显示成密文形式
self
.
e1
.
place
(
x
=
120
,
y
=
80
)
self
.
e2
.
place
(
x
=
120
,
y
=
140
)
while
True
:
# 登录、注册按钮
for
event
in
pygame
.
event
.
get
():
button1
=
tkinter
.
Button
(
self
.
root
,
text
=
'登录'
,
bg
=
"lightblue"
,
if
event
.
type
==
locals
.
QUIT
:
fg
=
"black"
,
width
=
15
,
command
=
login
)
exit
(
)
button1
.
place
(
x
=
60
,
y
=
220
)
if
event
.
type
==
locals
.
KEYDOWN
:
button1
=
tkinter
.
Button
(
self
.
root
,
text
=
'注册'
,
bg
=
"lightgreen"
,
if
event
.
key
==
locals
.
K_LEFT
:
# 向左
fg
=
"black"
,
width
=
15
,
command
=
login_to_reg
)
center
[
1
]
=
center
[
1
]
-
1
# 左移1列
button1
.
place
(
x
=
230
,
y
=
220
)
if
check
(
center
)
==
False
:
# 进入消息循环,监听事件
center
[
1
]
=
center
[
1
]
+
1
# 右移1列
self
.
root
.
mainloop
()
elif
event
.
key
==
locals
.
K_RIGHT
:
# 向右
def
get_input
(
self
):
# 获取输入的用户名、密码
center
[
1
]
=
center
[
1
]
+
1
self
.
name
=
self
.
e1
.
get
()
if
check
(
center
)
==
False
:
self
.
password
=
self
.
e2
.
get
()
center
[
1
]
=
center
[
1
]
-
1
return
self
.
name
,
self
.
password
elif
event
.
key
==
locals
.
K_DOWN
:
# 向下
class
My_register
():
# 注册窗口
center
[
0
]
=
center
[
0
]
+
1
def
__init__
(
self
):
if
check
(
center
)
==
False
:
self
.
root
=
tkinter
.
Tk
()
center
[
0
]
=
center
[
0
]
-
1
self
.
root
.
title
(
"注册窗口"
)
self
.
root
.
geometry
(
'400x320+500+300'
)
elif
event
.
key
==
locals
.
K_UP
:
# 向上键
self
.
root
.
resizable
(
width
=
False
,
height
=
False
)
# True可以拉伸,False不能拉伸
old_index
=
index
index
+=
1
def
show
(
self
):
# 注册界面的所有控件
if
index
>=
len
(
shape
):
# 用户名、密码、确认密码输入框
index
=
0
self
.
e1
=
tkinter
.
Entry
(
self
.
root
,
show
=
None
,
font
=
(
'宋体'
,
14
),
current_shape
=
shape
[
index
]
bg
=
"light grey"
,
width
=
18
)
# 显示成明文形式
if
check
(
center
)
==
False
:
self
.
e1
.
place
(
x
=
140
,
y
=
80
)
index
=
old_index
self
.
e2
=
tkinter
.
Entry
(
self
.
root
,
show
=
'*'
,
font
=
(
'宋体'
,
14
),
current_shape
=
shape
[
index
]
width
=
18
)
if
gameover
==
False
:
self
.
e2
.
place
(
x
=
140
,
y
=
140
)
if
states
==
False
:
self
.
e3
=
tkinter
.
Entry
(
self
.
root
,
show
=
'*'
,
font
=
(
'宋体'
,
14
),
states
=
True
width
=
18
)
center
=
[
2
,
8
]
# 第2行第8列
self
.
e3
.
place
(
x
=
140
,
y
=
200
)
shape
=
random
.
choice
(
shape_list
)
index
=
random
.
randint
(
0
,
len
(
shape
)
-
1
)
# 随机形状索引
# Label文本标签,窗口界面文字:用户名、密码、确认密码
current_shape
=
shape
[
index
]
lab
=
tkinter
.
Label
(
self
.
root
,
text
=
'您好!请填写注册信息'
,
color
=
random
.
choice
(
cube_colors
)
# 随机选取一种颜色
font
=
(
'宋体'
,
15
),
fg
=
"black"
,
width
=
40
,
height
=
2
,
bg
=
"green"
)
count
+=
1
lab
.
place
(
x
=
0
,
y
=
0
)
if
count
%
FPS
==
0
:
# 降落速度的算式
lab1
=
tkinter
.
Label
(
self
.
root
,
text
=
'用户名:'
,
font
=
(
"宋体"
,
12
),
center
[
0
]
=
center
[
0
]
+
1
fg
=
"black"
)
if
check
(
center
)
==
False
:
lab1
.
place
(
x
=
60
,
y
=
80
)
center
[
0
]
=
center
[
0
]
-
1
lab2
=
tkinter
.
Label
(
self
.
root
,
text
=
'密 码:'
,
font
=
(
"宋体"
,
12
),
states
=
False
fg
=
"black"
)
for
cube
in
current_pos
:
lab2
.
place
(
x
=
60
,
y
=
140
)
num_list
[
cube
[
0
]
-
1
][
cube
[
1
]
-
1
]
=
color
lab3
=
tkinter
.
Label
(
self
.
root
,
text
=
'确认密码:'
,
font
=
(
"宋体"
,
12
),
# 将背景图画上去
fg
=
"black"
)
screen
.
blit
(
background
,
(
0
,
0
))
lab3
.
place
(
x
=
50
,
y
=
200
)
# 计算出所有小方块的行、列位置
current_pos
=
[]
# 注册界面的提交、取消按钮
for
cube
in
current_shape
:
button1
=
tkinter
.
Button
(
self
.
root
,
text
=
'提交'
,
bg
=
"lightblue"
,
pos
=
(
cube
[
0
]
+
center
[
0
],
cube
[
1
]
+
center
[
1
])
width
=
15
,
command
=
register
)
current_pos
.
append
(
pos
)
button1
.
place
(
x
=
230
,
y
=
250
)
# 取出所有小方块的行、列位置,计算坐标,绘制俄罗斯方块
button1
=
tkinter
.
Button
(
self
.
root
,
text
=
'取消'
,
bg
=
"lightgreen"
,
for
cube
in
current_pos
:
width
=
15
,
command
=
reg_to_login
)
pygame
.
draw
.
rect
(
screen
,
color
,
button1
.
place
(
x
=
80
,
y
=
250
)
(
cube
[
1
]
*
20
-
20
,
cube
[
0
]
*
20
-
20
,
20
,
20
),
0
)
# 进入消息循环
pygame
.
draw
.
rect
(
screen
,
(
255
,
255
,
255
),
self
.
root
.
mainloop
()
(
cube
[
1
]
*
20
-
20
,
cube
[
0
]
*
20
-
20
,
20
,
20
),
1
)
def
get_input
(
self
):
# 获取提交的注册信息
for
i
,
row
in
zip
(
range
(
1
,
26
),
num_list
):
self
.
name
=
self
.
e1
.
get
()
for
j
,
colors
in
zip
(
range
(
1
,
16
),
row
):
self
.
password1
=
self
.
e2
.
get
()
if
colors
!=
0
:
self
.
password2
=
self
.
e3
.
get
()
pygame
.
draw
.
rect
(
screen
,
colors
,
return
self
.
name
,
self
.
password1
,
self
.
password2
(
j
*
20
-
20
,
i
*
20
-
20
,
20
,
20
))
pygame
.
draw
.
rect
(
screen
,(
255
,
255
,
255
),
app_login
=
My_login
()
(
j
*
20
-
20
,
i
*
20
-
20
,
20
,
20
),
1
)
app_login
.
show
()
new_list
=
[]
\ No newline at end of file
for
i
in
range
(
25
):
new_list
.
append
([
0
]
*
15
)
row_index
=
24
for
i
in
range
(
24
,
-
1
,
-
1
):
is_full
=
True
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
if
num_list
[
1
][
7
]
!=
0
:
gameover
=
True
# 得分
text_surface
=
font
.
render
(
str
(
score
),
True
,
(
0
,
0
,
0
))
screen
.
blit
(
text_surface
,
(
350
,
70
))
if
gameover
==
True
:
text_surface
=
font
.
render
(
"游戏失败,按任意键开始"
,
True
,(
0
,
0
,
0
))
screen
.
blit
(
text
,
(
20
,
250
))
score
=
0
num_list
=
[]
for
i
in
range
(
25
):
num_list
.
append
([
e
]
*
15
)
# 刷新画面
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