Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_43552579
Python - AI test
提交
8d8e489b
P
Python - AI test
项目概览
weixin_43552579
/
Python - AI test
与 Fork 源项目一致
Fork自
inscode / Python
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Python - AI test
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
8d8e489b
编写于
4月 28, 2023
作者:
6
644b3f306180332c2cd6a215
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
UPDATE
上级
6eb1626e
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
222 addition
and
0 deletion
+222
-0
main.py
main.py
+222
-0
未找到文件。
main.py
浏览文件 @
8d8e489b
print
(
'欢迎来到 InsCode'
)
import
pygame
import
random
# 初始化 Pygame
pygame
.
init
()
# 定义游戏窗口大小
screen_width
=
800
screen_height
=
600
# 定义方块大小和间隔
block_size
=
30
block_space
=
2
# 定义颜色
WHITE
=
(
255
,
255
,
255
)
BLACK
=
(
0
,
0
,
0
)
RED
=
(
200
,
0
,
0
)
GREEN
=
(
0
,
200
,
0
)
BLUE
=
(
0
,
0
,
200
)
YELLOW
=
(
200
,
200
,
0
)
PURPLE
=
(
200
,
0
,
200
)
CYAN
=
(
0
,
200
,
200
)
# 定义七种方块的形状
tetrominoes
=
[
[[
1
,
1
,
1
,
1
]],
# I
[[
1
,
1
,
0
],
[
0
,
1
,
1
]],
# O
[[
0
,
1
,
1
],
[
1
,
1
,
0
]],
# Z
[[
1
,
1
,
0
],
[
0
,
1
,
1
]],
# S
[[
1
,
1
,
1
],
[
0
,
1
,
0
]],
# T
[[
1
,
1
,
1
],
[
0
,
0
,
1
]],
# L
[[
1
,
1
,
1
],
[
1
,
0
,
0
]]
# J
]
# 定义方块的颜色
tetromino_colors
=
[
WHITE
,
YELLOW
,
RED
,
GREEN
,
BLUE
,
PURPLE
,
CYAN
]
# 定义游戏区域的大小
play_width
=
10
*
(
block_size
+
block_space
)
play_height
=
20
*
(
block_size
+
block_space
)
play_x
=
(
screen_width
-
play_width
)
//
2
play_y
=
screen_height
-
play_height
-
50
# 初始化游戏窗口
screen
=
pygame
.
display
.
set_mode
((
screen_width
,
screen_height
))
pygame
.
display
.
set_caption
(
"Tetris"
)
# 定义字体
font
=
pygame
.
font
.
Font
(
None
,
36
)
# 定义游戏结束标志
game_over
=
False
# 定义方块类
class
Block
:
def
__init__
(
self
,
x
,
y
,
shape
):
self
.
x
=
x
self
.
y
=
y
self
.
shape
=
shape
self
.
color
=
tetromino_colors
[
tetrominoes
.
index
(
shape
)]
self
.
rotation
=
0
# 绘制方块
def
draw
(
self
,
x
,
y
):
for
i
in
range
(
len
(
self
.
shape
[
self
.
rotation
])):
for
j
in
range
(
len
(
self
.
shape
[
self
.
rotation
][
i
])):
if
self
.
shape
[
self
.
rotation
][
i
][
j
]
==
1
:
pygame
.
draw
.
rect
(
screen
,
self
.
color
,
[
x
+
j
*
(
block_size
+
block_space
),
y
+
i
*
(
block_size
+
block_space
),
block_size
,
block_size
])
# 移动方块
def
move
(
self
,
direction
,
play_area
):
if
direction
==
"left"
:
if
self
.
x
>
0
:
new_x
=
self
.
x
-
1
if
not
self
.
check_collision
(
new_x
,
self
.
y
,
self
.
rotation
,
play_area
):
self
.
x
=
new_x
elif
direction
==
"right"
:
if
self
.
x
<
play_width
-
len
(
self
.
shape
[
self
.
rotation
][
0
]):
new_x
=
self
.
x
+
1
if
not
self
.
check_collision
(
new_x
,
self
.
y
,
self
.
rotation
,
play_area
):
self
.
x
=
new_x
elif
direction
==
"down"
:
if
self
.
y
<
play_height
-
len
(
self
.
shape
[
self
.
rotation
]):
new_y
=
self
.
y
+
1
if
not
self
.
check_collision
(
self
.
x
,
new_y
,
self
.
rotation
,
play_area
):
self
.
y
=
new_y
# 旋转方块
def
rotate
(
self
,
play_area
):
new_rotation
=
self
.
rotation
+
1
if
new_rotation
>=
len
(
self
.
shape
):
new_rotation
=
0
if
not
self
.
check_collision
(
self
.
x
,
self
.
y
,
new_rotation
,
play_area
):
self
.
rotation
=
new_rotation
# 检查方块是否与障碍物相撞
def
check_collision
(
self
,
x
,
y
,
rotation
,
play_area
):
for
i
in
range
(
len
(
self
.
shape
[
rotation
])):
for
j
in
range
(
len
(
self
.
shape
[
rotation
][
i
])):
if
self
.
shape
[
rotation
][
i
][
j
]
==
1
:
position_x
=
x
+
j
position_y
=
y
+
i
if
position_y
<
0
:
continue
if
position_x
<
0
or
position_x
>=
play_width
or
position_y
>=
play_height
:
return
True
if
play_area
[
position_y
][
position_x
]
!=
BLACK
:
return
True
return
False
# 定义游戏主函数
def
main
():
global
game_over
# 初始化游戏区域
play_area
=
[[
BLACK
for
_
in
range
(
play_width
)]
for
_
in
range
(
play_height
)]
# 定义当前方块和下一个方块
current_block
=
Block
(
3
,
0
,
random
.
choice
(
tetrominoes
))
next_block
=
Block
(
3
,
0
,
random
.
choice
(
tetrominoes
))
# 定义计时器和计时器步长
clock
=
pygame
.
time
.
Clock
()
timer
=
0
timer_step
=
1000
# 定义得分
score
=
0
# 游戏主循环
while
not
game_over
:
# 处理事件
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
game_over
=
True
if
event
.
type
==
pygame
.
KEYDOWN
:
if
event
.
key
==
pygame
.
K_LEFT
:
current_block
.
move
(
"left"
,
play_area
)
if
event
.
key
==
pygame
.
K_RIGHT
:
current_block
.
move
(
"right"
,
play_area
)
if
event
.
key
==
pygame
.
K_DOWN
:
current_block
.
move
(
"down"
,
play_area
)
if
event
.
key
==
pygame
.
K_UP
:
current_block
.
rotate
(
play_area
)
# 计时器递增
timer
+=
clock
.
get_rawtime
()
clock
.
tick
()
# 判断是否需要降落方块
if
timer
>=
timer_step
:
timer
=
0
current_block
.
move
(
"down"
,
play_area
)
# 检查方块是否落地
if
current_block
.
check_collision
(
current_block
.
x
,
current_block
.
y
+
1
,
current_block
.
rotation
,
play_area
):
# 将方块加入游戏区域
for
i
in
range
(
len
(
current_block
.
shape
[
current_block
.
rotation
])):
for
j
in
range
(
len
(
current_block
.
shape
[
current_block
.
rotation
][
i
])):
if
current_block
.
shape
[
current_block
.
rotation
][
i
][
j
]
==
1
:
position_x
=
current_block
.
x
+
j
position_y
=
current_block
.
y
+
i
play_area
[
position_y
][
position_x
]
=
current_block
.
color
# 计算得分
rows_cleared
=
0
for
i
in
range
(
len
(
play_area
)):
if
BLACK
not
in
play_area
[
i
]:
rows_cleared
+=
1
for
j
in
range
(
i
,
0
,
-
1
):
play_area
[
j
]
=
play_area
[
j
-
1
][:]
play_area
[
0
]
=
[
BLACK
for
_
in
range
(
play_width
)]
score
+=
rows_cleared
*
10
# 放置下一个方块
current_block
=
next_block
next_block
=
Block
(
3
,
0
,
random
.
choice
(
tetrominoes
))
# 判断游戏是否结束
if
current_block
.
check_collision
(
current_block
.
x
,
current_block
.
y
,
current_block
.
rotation
,
play_area
):
game_over
=
True
# 绘制游戏区域
screen
.
fill
(
BLACK
)
for
i
in
range
(
len
(
play_area
)):
for
j
in
range
(
len
(
play_area
[
i
])):
pygame
.
draw
.
rect
(
screen
,
play_area
[
i
][
j
],
[
play_x
+
j
*
(
block_size
+
block_space
),
play_y
+
i
*
(
block_size
+
block_space
),
block_size
,
block_size
])
current_block
.
draw
(
play_x
,
play_y
)
next_block
.
draw
(
play_x
+
(
play_width
+
50
),
play_y
)
text
=
font
.
render
(
"Score: "
+
str
(
score
),
True
,
WHITE
)
screen
.
blit
(
text
,
[
play_x
,
play_y
-
40
])
# 更新屏幕
pygame
.
display
.
update
()
# 退出 Pygame
pygame
.
quit
()
# 运行游戏主函数
if
__name__
==
"__main__"
:
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录