Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson9_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
35da87cc
authored
Feb 18, 2023
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
686c5f10
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
111 additions
and
126 deletions
hero3.png
my_game.py
record.txt
hero3.png
View file @
35da87cc
11.3 KB
|
W:
|
H:
21 KB
|
W:
|
H:
2-up
Swipe
Onion skin
my_game.py
View file @
35da87cc
import
pygame
FPS
.
tick
(
60
)
from
pygame
import
locals
FPS
.
tick
(
60
)
import
random
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
pygame
.
init
()
# 初始化
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
class
Block
(
pygame
.
sprite
.
Sprite
):
# 障碍物精灵类
FPS
.
tick
(
60
)
def
__init__
(
self
,
image1
,
image2
,
image3
):
FPS
.
tick
(
60
)
super
()
.
__init__
()
FPS
.
tick
(
60
)
# 加载障碍物的图片
FPS
.
tick
(
60
)
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
FPS
.
tick
(
60
)
# 根据障碍物位图的宽高设置矩形
FPS
.
tick
(
60
)
self
.
rect
=
self
.
image
.
get_rect
()
FPS
.
tick
(
60
)
# 障碍物绘制坐标
FPS
.
tick
(
60
)
self
.
rect
.
x
=
1000
FPS
.
tick
(
60
)
self
.
rect
.
y
=
500
-
self
.
rect
.
height
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
class
Player
(
pygame
.
sprite
.
Sprite
):
# 悟空
FPS
.
tick
(
60
)
def
__init__
(
self
,
image
):
FPS
.
tick
(
60
)
super
()
.
__init__
()
FPS
.
tick
(
60
)
# 加载悟空精灵图像
FPS
.
tick
(
60
)
self
.
image
=
image
FPS
.
tick
(
60
)
# image的get_rect()方法,可以返回pygame.Rect(0,0,图像宽,图像高)的对象
FPS
.
tick
(
60
)
self
.
rect
=
self
.
image
.
get_rect
()
FPS
.
tick
(
60
)
self
.
rect
.
x
=
150
FPS
.
tick
(
60
)
self
.
rect
.
y
=
400
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
# 创建一个窗口
FPS
.
tick
(
60
)
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
FPS
.
tick
(
60
)
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
FPS
.
tick
(
60
)
pygame
.
display
.
set_caption
(
"悟空酷跑"
)
FPS
.
tick
(
60
)
# 载入图片
FPS
.
tick
(
60
)
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
FPS
.
tick
(
60
)
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
FPS
.
tick
(
60
)
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
FPS
.
tick
(
60
)
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
FPS
.
tick
(
60
)
bush
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
FPS
.
tick
(
60
)
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
FPS
.
tick
(
60
)
pygame
.
image
.
load
(
'hero2.png'
),
FPS
.
tick
(
60
)
pygame
.
image
.
load
(
'hero3.png'
),
FPS
.
tick
(
60
)
pygame
.
image
.
load
(
'hero4.png'
),
FPS
.
tick
(
60
)
pygame
.
image
.
load
(
'hero5.png'
)]
FPS
.
tick
(
60
)
basic_font
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
32
)
FPS
.
tick
(
60
)
index
=
0
FPS
.
tick
(
60
)
y
=
400
FPS
.
tick
(
60
)
jumpState
=
"runing"
FPS
.
tick
(
60
)
t
=
30
FPS
.
tick
(
60
)
road_x
=
0
FPS
.
tick
(
60
)
bg_x
=
0
FPS
.
tick
(
60
)
time
=
0
FPS
.
tick
(
60
)
gamestate
=
True
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
block_list
=
pygame
.
sprite
.
Group
()
# 创建精灵组
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
while
True
:
FPS
.
tick
(
60
)
for
event
in
pygame
.
event
.
get
():
FPS
.
tick
(
60
)
if
event
.
type
==
locals
.
QUIT
:
FPS
.
tick
(
60
)
# 接收到退出事件后退出程序
FPS
.
tick
(
60
)
exit
()
FPS
.
tick
(
60
)
if
event
.
type
==
locals
.
KEYDOWN
:
FPS
.
tick
(
60
)
if
jumpState
==
"runing"
:
FPS
.
tick
(
60
)
if
event
.
key
==
locals
.
K_SPACE
:
FPS
.
tick
(
60
)
jumpState
=
"up"
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
# 悟空造型
FPS
.
tick
(
60
)
wukong
=
Player
(
hero
[
index
])
FPS
.
tick
(
60
)
if
jumpState
==
"runing"
:
# 跑步状态下
FPS
.
tick
(
60
)
index
+=
1
FPS
.
tick
(
60
)
if
index
>=
5
:
FPS
.
tick
(
60
)
index
=
0
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
if
gamestate
==
True
:
FPS
.
tick
(
60
)
if
jumpState
==
"up"
:
# 起跳状态
FPS
.
tick
(
60
)
if
t
>
0
:
FPS
.
tick
(
60
)
y
-=
t
FPS
.
tick
(
60
)
wukong
.
rect
.
y
=
y
FPS
.
tick
(
60
)
t
-=
2
FPS
.
tick
(
60
)
else
:
FPS
.
tick
(
60
)
jumpState
=
"down"
FPS
.
tick
(
60
)
if
jumpState
==
"down"
:
# 降落状态
FPS
.
tick
(
60
)
if
t
<=
30
:
FPS
.
tick
(
60
)
y
+=
t
FPS
.
tick
(
60
)
wukong
.
rect
.
y
=
y
FPS
.
tick
(
60
)
t
+=
2
FPS
.
tick
(
60
)
else
:
FPS
.
tick
(
60
)
jumpState
=
"runing"
FPS
.
tick
(
60
)
t
=
30
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
# 将背景图画上去
FPS
.
tick
(
60
)
bg_x
-=
1
FPS
.
tick
(
60
)
if
bg_x
<=-
1000
:
FPS
.
tick
(
60
)
bg_x
=
0
FPS
.
tick
(
60
)
screen
.
blit
(
background
,
(
bg_x
,
0
))
# 远景
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
road_x
-=
8
FPS
.
tick
(
60
)
if
road_x
<=-
1000
:
FPS
.
tick
(
60
)
road_x
=
0
FPS
.
tick
(
60
)
screen
.
blit
(
road
,
(
road_x
,
500
))
# 道路
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
# 悟空
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
time
+=
1
FPS
.
tick
(
60
)
if
time
>=
60
:
# 创建障碍物精灵
FPS
.
tick
(
60
)
time
=
0
FPS
.
tick
(
60
)
num
=
random
.
randint
(
0
,
50
)
FPS
.
tick
(
60
)
if
num
>
20
:
FPS
.
tick
(
60
)
obstacle
=
Block
(
bush
,
cacti
,
stone
)
FPS
.
tick
(
60
)
block_list
.
add
(
obstacle
)
FPS
.
tick
(
60
)
for
sprite
in
block_list
:
# 遍历、展示障碍物精灵
FPS
.
tick
(
60
)
sprite
.
rect
.
x
-=
8
FPS
.
tick
(
6
screen
.
blit
(
sprite
.
image
,
(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
\ No newline at end of file
if
sprite
.
rect
.
x
<=
0
-
sprite
.
rect
.
width
:
sprite
.
kill
()
if
pygame
.
sprite
.
collide_rect
(
wukong
,
sprite
):
# 精灵碰撞检测
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
# 游戏结束
screen
.
blit
(
gameover
,
(
400
,
200
))
gamestate
=
False
scoreSurf
=
basic_font
.
render
(
"分数:"
+
str
(
score
),
True
,(
255
,
0
,
0
))
screen
.
blit
(
scoreSurf
,(
850
,
20
))
# 刷新画面
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
\ No newline at end of file
record.txt
View file @
35da87cc
{"第1名": 0, "第2名": 0, "第3名": 0}
\ 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