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
020c2386
authored
Apr 15, 2023
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
ddbbb886
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
223 additions
and
1 deletions
俄罗斯方块.py
俄罗斯方块.py
View file @
020c2386
-- "a/\344\277\204\347\275\227\346\226\257\346\226\271\345\235\227.py"
import
pygame
import
pygame
import
random
# 游戏参数设置
cell_size
=
30
board_width
=
10
board_height
=
20
game_speed
=
10
# 游戏速度
# 颜色
white
=
(
255
,
255
,
255
)
black
=
(
0
,
0
,
0
)
gray
=
(
150
,
150
,
150
)
red
=
(
255
,
0
,
0
)
green
=
(
0
,
255
,
0
)
blue
=
(
0
,
0
,
255
)
yellow
=
(
255
,
255
,
0
)
cyan
=
(
0
,
255
,
255
)
purple
=
(
255
,
0
,
255
)
tetromino_colors
=
[
gray
,
red
,
green
,
blue
,
yellow
,
cyan
,
purple
]
# 定义方块形状
tetrominos
=
[
# I 形状
[[
1
,
1
,
1
,
1
]],
# J 形状
[[
1
,
0
,
0
],
[
1
,
1
,
1
]],
# L 形状
[[
0
,
0
,
1
],
[
1
,
1
,
1
]],
# O 形状
[[
1
,
1
],
[
1
,
1
]],
# S 形状
[[
0
,
1
,
1
],
[
1
,
1
,
0
]],
# T 形状
[[
0
,
1
,
0
],
[
1
,
1
,
1
]],
# Z 形状
[[
1
,
1
,
0
],
[
0
,
1
,
1
]]
]
class
Tetromino
:
"""表示俄罗斯方块的类"""
def
__init__
(
self
,
shape
,
x
,
y
,
color
):
self
.
shape
=
shape
self
.
x
=
x
self
.
y
=
y
self
.
color
=
color
def
rotate
(
self
):
"""将方块旋转 90 度"""
self
.
shape
=
list
(
zip
(
*
self
.
shape
[::
-
1
]))
def
move_left
(
self
):
"""将方块向左移动一个单位"""
self
.
x
-=
1
def
move_right
(
self
):
"""将方块向右移动一个单位"""
self
.
x
+=
1
def
move_down
(
self
):
"""将方块向下移动一个单位"""
self
.
y
+=
1
def
draw
(
self
,
surface
):
"""绘制方块"""
for
i
in
range
(
len
(
self
.
shape
)):
for
j
in
range
(
len
(
self
.
shape
[
i
])):
if
self
.
shape
[
i
][
j
]
==
1
:
pygame
.
draw
.
rect
(
surface
,
self
.
color
,
(
self
.
x
*
cell_size
+
j
*
cell_size
,
self
.
y
*
cell_size
+
i
*
cell_size
,
cell_size
,
cell_size
))
class
Game
:
"""表示游戏的类"""
def
__init__
(
self
):
self
.
board
=
[[
0
]
*
board_width
for
_
in
range
(
board_height
)]
self
.
current_tetromino
=
None
self
.
next_tetromino
=
None
self
.
score
=
0
self
.
level
=
1
self
.
lines
=
0
def
new_tetromino
(
self
):
"""生成新的方块"""
if
self
.
next_tetromino
is
None
:
self
.
current_tetromino
=
Tetromino
(
random
.
choice
(
tetrominos
),
board_width
//
2
-
2
,
0
,
random
.
choice
(
tetromino_colors
[
1
:]))
else
:
self
.
current_tetromino
=
self
.
next_tetromino
self
.
current_tetromino
.
x
=
board_width
//
2
-
2
self
.
current_tetromino
.
y
=
0
self
.
current_tetromino
.
color
=
random
.
choice
(
tetromino_colors
[
1
:])
self
.
next_tetromino
=
Tetromino
(
random
.
choice
(
tetrominos
),
board_width
+
2
,
2
,
random
.
choice
(
tetromino_colors
[
1
:]))
# 如果新方块出现的位置已经有方块存在,则游戏结束
if
not
self
.
check_valid
(
self
.
current_tetromino
):
pygame
.
quit
()
quit
()
def
check_valid
(
self
,
tetromino
):
"""检查方块是否出界或与已存在方块重叠"""
for
i
in
range
(
len
(
tetromino
.
shape
)):
for
j
in
range
(
len
(
tetromino
.
shape
[
i
])):
if
tetromino
.
shape
[
i
][
j
]
==
1
:
if
x
<
0
or
x
>=
board_width
or
y
>=
board_height
:
return
False
elif
y
>=
0
and
self
.
board
[
y
][
x
]
!=
0
:
return
False
return
True
def
update_board
(
self
):
"""将当前正在运动的方块加入到游戏面板中"""
for
i
in
range
(
len
(
self
.
current_tetromino
.
shape
)):
for
j
in
range
(
len
(
self
.
current_tetromino
.
shape
[
i
])):
if
self
.
current_tetromino
.
shape
[
i
][
j
]
==
1
:
x
,
y
=
self
.
current_tetromino
.
x
+
j
,
self
.
current_tetromino
.
y
+
i
self
.
board
[
y
][
x
]
=
self
.
current_tetromino
.
color
def
check_full_lines
(
self
):
"""检查是否有满行"""
count
=
0
for
i
in
range
(
board_height
):
if
0
not
in
self
.
board
[
i
]:
self
.
board
.
pop
(
i
)
self
.
board
.
insert
(
0
,
[
0
]
*
board_width
)
count
+=
1
if
count
>
0
:
self
.
score
+=
count
**
2
*
100
self
.
lines
+=
count
if
self
.
lines
>=
self
.
level
*
10
:
self
.
level
+=
1
def
draw_board
(
self
,
surface
):
"""绘制游戏面板"""
for
i
in
range
(
board_width
):
for
j
in
range
(
board_height
):
pygame
.
draw
.
rect
(
surface
,
gray
,
(
i
*
cell_size
,
j
*
cell_size
,
cell_size
,
cell_size
),
1
)
if
self
.
board
[
j
][
i
]
!=
0
:
pygame
.
draw
.
rect
(
surface
,
self
.
board
[
j
][
i
],
(
i
*
cell_size
,
j
*
cell_size
,
cell_size
,
cell_size
))
def
draw_score
(
self
,
surface
):
"""绘制分数和等级"""
font
=
pygame
.
font
.
SysFont
(
None
,
30
)
text
=
font
.
render
(
'Score: {}'
.
format
(
self
.
score
),
True
,
white
)
surface
.
blit
(
text
,
(
board_width
*
cell_size
+
20
,
50
))
text
=
font
.
render
(
'Level: {}'
.
format
(
self
.
level
),
True
,
white
)
surface
.
blit
(
text
,
(
board_width
*
cell_size
+
20
,
100
))
def
draw_next_tetromino
(
self
,
surface
):
"""绘制下一个方块"""
font
=
pygame
.
font
.
SysFont
(
None
,
30
)
text
=
font
.
render
(
'Next:'
,
True
,
white
)
surface
.
blit
(
text
,
(
board_width
*
cell_size
+
20
,
200
))
if
self
.
next_tetromino
is
not
None
:
self
.
next_tetromino
.
draw
(
surface
)
def
run
(
self
):
"""游戏主循环"""
pygame
.
init
()
screen
=
pygame
.
display
.
set_mode
((
board_width
*
cell_size
+
200
,
board_height
*
cell_size
))
pygame
.
display
.
set_caption
(
'Tetris'
)
self
.
new_tetromino
()
clock
=
pygame
.
time
.
Clock
()
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
pygame
.
quit
()
quit
()
elif
event
.
type
==
pygame
.
KEYDOWN
:
if
event
.
key
==
pygame
.
K_LEFT
:
self
.
current_tetromino
.
move_left
()
if
not
self
.
check_valid
(
self
.
current_tetromino
):
self
.
current_tetromino
.
move_right
()
elif
event
.
key
==
pygame
.
K_RIGHT
:
self
.
current_tetromino
.
move_right
()
if
not
self
.
check_valid
(
self
.
current_tetromino
):
self
.
current_tetromino
.
move_left
()
elif
event
.
key
==
pygame
.
K_UP
:
self
.
current_tetromino
.
rotate
()
if
not
self
.
check_valid
(
self
.
current_tetromino
):
self
.
current_tetromino
.
rotate
()
self
.
current_tetromino
.
rotate
()
self
.
current_tetromino
.
rotate
()
elif
event
.
key
==
pygame
.
K_DOWN
:
self
.
current_tetromino
.
move_down
()
if
not
self
.
check_valid
(
self
.
current_tetromino
):
self
.
current_tetromino
.
move_up
()
self
.
update_board
()
self
.
check_full_lines
()
self
.
new_tetromino
()
# 绘制游戏界面
screen
.
fill
(
black
)
self
.
draw_board
(
screen
)
self
.
current_tetromino
.
draw
(
screen
)
self
.
draw_score
(
screen
)
self
.
draw_next_tetromino
(
screen
)
pygame
.
display
.
update
()
# 控制游戏速度
clock
.
tick
(
game_speed
*
self
)
\ 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