Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
纵享孤独
Python_670749
提交
24d53c09
P
Python_670749
项目概览
纵享孤独
/
Python_670749
与 Fork 源项目一致
Fork自
inscode / Python
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Python_670749
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
“09fa31a322c8f03871a741cd1d26e5ca04aacbbd”上不存在“...arm/plat-mxc/git@gitcode.net:openharmony/kernel_linux.git”
提交
24d53c09
编写于
2月 21, 2025
作者:
R
reilh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fri Feb 21 14:29:00 CST 2025 inscode
上级
0a575cc3
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
118 addition
and
1 deletion
+118
-1
main.py
main.py
+118
-1
未找到文件。
main.py
浏览文件 @
24d53c09
print
(
'欢迎来到 InsCode'
)
import
pygame
\ No newline at end of file
import
random
import
sys
# 初始化Pygame
pygame
.
init
()
# 游戏窗口设置
WIDTH
,
HEIGHT
=
800
,
400
WIN
=
pygame
.
display
.
set_mode
((
WIDTH
,
HEIGHT
))
pygame
.
display
.
set_caption
(
"跑酷小恐龙"
)
# 颜色定义
WHITE
=
(
255
,
255
,
255
)
BLACK
=
(
0
,
0
,
0
)
# 游戏元素类
class
Dinosaur
:
def
__init__
(
self
):
self
.
x
=
50
self
.
y
=
300
self
.
jump
=
False
self
.
jump_speed
=
10
self
.
gravity
=
0.5
self
.
rect
=
pygame
.
Rect
(
self
.
x
,
self
.
y
,
40
,
60
)
def
move
(
self
):
if
self
.
jump
:
self
.
jump_speed
-=
self
.
gravity
self
.
y
-=
self
.
jump_speed
if
self
.
y
>=
300
:
self
.
y
=
300
self
.
jump
=
False
self
.
jump_speed
=
10
self
.
rect
.
y
=
self
.
y
class
Obstacle
:
def
__init__
(
self
):
self
.
x
=
WIDTH
self
.
y
=
310
self
.
width
=
30
self
.
height
=
40
self
.
speed
=
7
self
.
rect
=
pygame
.
Rect
(
self
.
x
,
self
.
y
,
self
.
width
,
self
.
height
)
def
update
(
self
):
self
.
x
-=
self
.
speed
self
.
rect
.
x
=
self
.
x
if
self
.
x
<
-
self
.
width
:
return
True
return
False
# 游戏主循环
def
main
():
clock
=
pygame
.
time
.
Clock
()
score
=
0
font
=
pygame
.
font
.
SysFont
(
None
,
36
)
dino
=
Dinosaur
()
obstacles
=
[]
spawn_timer
=
0
running
=
True
game_over
=
False
while
running
:
WIN
.
fill
(
WHITE
)
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
running
=
False
if
event
.
type
==
pygame
.
KEYDOWN
:
if
event
.
key
==
pygame
.
K_SPACE
and
not
dino
.
jump
and
not
game_over
:
dino
.
jump
=
True
if
event
.
key
==
pygame
.
K_r
and
game_over
:
main
()
if
not
game_over
:
# 生成障碍物
spawn_timer
+=
1
if
spawn_timer
>
random
.
randint
(
50
,
100
):
obstacles
.
append
(
Obstacle
())
spawn_timer
=
0
# 更新障碍物
for
obs
in
obstacles
[:]:
if
obs
.
update
():
obstacles
.
remove
(
obs
)
score
+=
1
# 碰撞检测
dino
.
move
()
for
obs
in
obstacles
:
if
dino
.
rect
.
colliderect
(
obs
.
rect
):
game_over
=
True
# 绘制游戏元素
pygame
.
draw
.
rect
(
WIN
,
BLACK
,
dino
.
rect
)
# 恐龙
for
obs
in
obstacles
:
pygame
.
draw
.
rect
(
WIN
,
(
255
,
0
,
0
),
obs
.
rect
)
# 障碍物
# 显示分数
score_text
=
font
.
render
(
f
"Score:
{
score
}
"
,
True
,
BLACK
)
WIN
.
blit
(
score_text
,
(
10
,
10
))
else
:
# 游戏结束画面
game_over_text
=
font
.
render
(
"Game Over! Press R to restart"
,
True
,
BLACK
)
WIN
.
blit
(
game_over_text
,
(
WIDTH
//
2
-
150
,
HEIGHT
//
2
))
pygame
.
display
.
update
()
clock
.
tick
(
60
)
pygame
.
quit
()
sys
.
exit
()
if
__name__
==
"__main__"
:
main
()
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录