Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
bellcode
/
lesson2-1-2_DIY1
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
01293755
authored
May 14, 2023
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
dc148bbd
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
137 deletions
star.py
star.py
View file @
01293755
import
pygame
import
random
import
random
player
=
input
(
"出拳:拳头,剪刀,布"
)
# 颜色定义
print
(
"玩家出拳"
,
player
)
GRAY
=
(
200
,
200
,
200
)
list
=
[
"拳头""布""剪刀"
]
BLACK
=
(
0
,
0
,
0
)
fuuuuuck
=
random
.
choice
(
list
)
RED
=
(
200
,
0
,
0
)
print
(
"只因算只因出拳"
,
fuuuuuck
)
GREEN
=
(
0
,
200
,
0
)
\ No newline at end of file
BLUE
=
(
0
,
0
,
200
)
# 游戏窗口大小
WINDOW_WIDTH
=
400
WINDOW_HEIGHT
=
400
# 单元格大小
CELL_SIZE
=
20
# 扫雷板类
class
MinesweeperBoard
:
def
__init__
(
self
,
rows
,
columns
,
num_bombs
):
self
.
rows
=
rows
self
.
columns
=
columns
self
.
num_bombs
=
num_bombs
self
.
board
=
[[
0
for
x
in
range
(
self
.
columns
)]
for
y
in
range
(
self
.
rows
)]
self
.
discovered
=
[[
False
for
x
in
range
(
self
.
columns
)]
for
y
in
range
(
self
.
rows
)]
self
.
bomb_indices
=
[]
# 随机放置地雷
bombs_placed
=
0
while
bombs_placed
<
self
.
num_bombs
:
row
=
random
.
randint
(
0
,
self
.
rows
-
1
)
col
=
random
.
randint
(
0
,
self
.
columns
-
1
)
if
self
.
board
[
row
][
col
]
==
0
:
self
.
board
[
row
][
col
]
=
-
1
self
.
bomb_indices
.
append
((
row
,
col
))
bombs_placed
+=
1
# 计算每个单元格周围的地雷数量
for
i
in
range
(
self
.
rows
):
for
j
in
range
(
self
.
columns
):
if
self
.
board
[
i
][
j
]
!=
-
1
:
count
=
0
for
x
in
[
-
1
,
0
,
1
]:
for
y
in
[
-
1
,
0
,
1
]:
if
(
i
+
x
)
>=
0
and
(
i
+
x
)
<
self
.
rows
and
\
(
j
+
y
)
>=
0
and
(
j
+
y
)
<
self
.
columns
and
\
self
.
board
[
i
+
x
][
j
+
y
]
==
-
1
:
count
+=
1
self
.
board
[
i
][
j
]
=
count
# 点击某个单元格,发现它;如果是地雷则返回 True,否则返回 False。
def
discover_cell
(
self
,
row
,
col
):
if
self
.
discovered
[
row
][
col
]:
return
False
self
.
discovered
[
row
][
col
]
=
True
if
self
.
board
[
row
][
col
]
==
0
:
for
i
in
[
-
1
,
0
,
1
]:
for
j
in
[
-
1
,
0
,
1
]:
if
(
row
+
i
)
>=
0
and
(
row
+
i
)
<
self
.
rows
and
\
(
col
+
j
)
>=
0
and
(
col
+
j
)
<
self
.
columns
and
\
not
self
.
discovered
[
row
+
i
][
col
+
j
]:
self
.
discover_cell
(
row
+
i
,
col
+
j
)
return
self
.
board
[
row
][
col
]
==
-
1
# 获取单元格的状态(覆盖或者已发现)
def
get_cell_state
(
self
,
row
,
col
):
return
self
.
discovered
[
row
][
col
]
# 获取单元格的值(-1 表示此单元格是地雷,其它数字表示周围的地雷数量)
def
get_cell_value
(
self
,
row
,
col
):
return
self
.
board
[
row
][
col
]
# 扫雷游戏类
class
MinesweeperGame
:
def
__init__
(
self
,
rows
,
columns
,
num_bombs
):
self
.
board
=
MinesweeperBoard
(
rows
,
columns
,
num_bombs
)
self
.
game_over
=
False
self
.
won
=
False
# 处理输入事件(鼠标按下)
def
handle_mousedown
(
self
,
row
,
col
,
button
):
if
self
.
game_over
:
return
if
button
==
1
:
is_bomb
=
self
.
board
.
discover_cell
(
row
,
col
)
if
is_bomb
:
self
.
game_over
=
True
else
:
self
.
check_win
()
elif
button
==
3
:
pass
# TODO: 标记潜在地雷
# 检查是否胜利
def
check_win
(
self
):
for
i
in
range
(
self
.
board
.
rows
):
for
j
in
range
(
self
.
board
.
columns
):
if
not
self
.
board
.
get_cell_state
(
i
,
j
)
and
\
self
.
board
.
get_cell_value
(
i
,
j
)
!=
-
1
:
return
self
.
game_over
=
True
self
.
won
=
True
# Pygame 初始化
pygame
.
init
()
# 创建游戏窗口
window_surface
=
pygame
.
display
.
set_mode
((
WINDOW_WIDTH
,
WINDOW_HEIGHT
))
# 创建扫雷游戏实例
game
=
MinesweeperGame
(
20
,
20
,
30
)
# 游戏主循环
while
True
:
# 扫描输入事件队列
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
# 点击 X 按钮退出程序
pygame
.
quit
()
exit
()
elif
event
.
type
==
pygame
.
MOUSEBUTTONDOWN
:
mouse_x
,
mouse_y
=
pygame
.
mouse
.
get_pos
()
cell_col
=
mouse_x
//
CELL_SIZE
cell_row
=
mouse_y
//
CELL_SIZE
button
=
event
.
button
game
.
handle_mousedown
(
cell_row
,
cell_col
,
button
)
# 绘制游戏界面
window_surface
.
fill
(
GRAY
)
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