Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
bellcode
/
lesson10-1
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
4da2d963
authored
Jan 31, 2024
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
2f4bb0d7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
0 deletions
uashf.py
uashf.py
0 → 100644
View file @
4da2d963
import
pygame
# 游戏初始化
pygame
.
init
()
screen_width
=
800
screen_height
=
600
screen
=
pygame
.
display
.
set_mode
((
screen_width
,
screen_height
))
pygame
.
display
.
set_caption
(
"My Minecraft Game"
)
# 加载图片
player_image
=
pygame
.
image
.
load
(
"player.png"
)
monster_image
=
pygame
.
image
.
load
(
"monster.png"
)
grass_image
=
pygame
.
image
.
load
(
"grass.png"
)
dirt_image
=
pygame
.
image
.
load
(
"dirt.png"
)
stone_image
=
pygame
.
image
.
load
(
"stone.png"
)
background_image
=
pygame
.
image
.
load
(
"background.png"
)
# 定义游戏角色和场景
player_x
=
100
player_y
=
100
player_speed
=
5
player_rect
=
player_image
.
get_rect
()
player_rect
.
x
=
player_x
player_rect
.
y
=
player_y
blocks
=
[]
for
i
in
range
(
10
):
for
j
in
range
(
10
):
block
=
{
"image"
:
grass_image
,
"rect"
:
grass_image
.
get_rect
()}
block
[
"rect"
]
.
x
=
i
*
50
block
[
"rect"
]
.
y
=
j
*
50
blocks
.
append
(
block
)
# 游戏循环
running
=
True
while
running
:
# 处理事件
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
running
=
False
elif
event
.
type
==
pygame
.
MOUSEBUTTONDOWN
:
if
event
.
button
==
1
:
for
block
in
blocks
:
if
block
[
"rect"
]
.
collidepoint
(
event
.
pos
):
blocks
.
remove
(
block
)
elif
event
.
button
==
3
:
block
=
{
"image"
:
dirt_image
,
"rect"
:
dirt_image
.
get_rect
()}
block
[
"rect"
]
.
x
=
event
.
pos
[
0
]
-
block
[
"rect"
]
.
width
/
2
block
[
"rect"
]
.
y
=
event
.
pos
[
1
]
-
block
[
"rect"
]
.
height
/
2
blocks
.
append
(
block
)
# 更新游戏状态
keys
=
pygame
.
key
.
get_pressed
()
if
keys
[
pygame
.
K_LEFT
]:
player_x
-=
player_speed
elif
keys
[
pygame
.
K_RIGHT
]:
player_x
+=
player_speed
elif
keys
[
pygame
.
K_UP
]:
player_y
-=
player_speed
elif
keys
[
pygame
.
K_DOWN
]:
player_y
+=
player_speed
player_rect
.
x
=
player_x
player_rect
.
y
=
player_y
# 绘制游戏画面
screen
.
blit
(
background_image
,
(
0
,
0
))
for
block
in
blocks
:
screen
.
blit
(
block
[
"image"
],
block
[
"rect"
])
screen
.
blit
(
player_image
,
player_rect
)
pygame
.
display
.
update
()
# 退出游戏
pygame
.
quit
()
\ 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