Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson7_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
419bac80
authored
Feb 18, 2023
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
fbbe75c0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
17 deletions
my_game.py
my_game.py
View file @
419bac80
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
random
import
random
from
random
import
*
#----------------------------------
#----------------------------------
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
class
Spr
(
pygame
.
sprite
.
Sprite
):
def
__init__
(
self
,
i1
,
i2
,
i3
):
super
()
.
__init__
()
self
.
i
=
choice
([
i1
,
i2
,
i3
])
self
.
rect
=
self
.
i
.
get_rect
()
self
.
rect
.
x
=
1000
self
.
rect
.
y
=
500
-
self
.
rect
.
height
#----------------------------------
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
1145
,
600
))
screen
=
pygame
.
display
.
set_mode
((
1145
,
600
))
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
#----------------------------------
pygame
.
display
.
set_caption
(
"Braised Intestines in Brown Sauce"
)
#-"感人故事:我保留了一部分寒假作业"-
pygame
.
display
.
set_caption
(
"Braised Intestines in Brown Sauce"
)
#-"感人故事:我保留了一部分寒假作业"-
#----------------------------------
# 载入图片
# 载入图片
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
#----------------------------------
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
stone
=
pygame
.
image
.
load
(
'stone.png'
)
# 石头
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
cacti
=
pygame
.
image
.
load
(
'cacti.png'
)
# 仙人掌
bush
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
bush
=
pygame
.
image
.
load
(
'bush.png'
)
# 灌木丛
#----------------------------------
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
),]
hero
=
[
pygame
.
image
.
load
(
'hero1.png'
),
pygame
.
image
.
load
(
'hero2.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
),]
stone_x
=
1000
#----------------------------------
#---------------------
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
obs
=
Spr
(
bush
,
cacti
,
stone
)
rest
=
obstacle
.
get_rect
()
road_x
=
0
rest
.
x
=
1000
bg_x
=
0
rest
.
y
=
500
-
rest
.
height
index
=
0
index
=
0
#---------------------
#---------------------
y
=
400
y
=
400
...
@@ -42,19 +59,24 @@ while True:
...
@@ -42,19 +59,24 @@ while True:
exit
()
exit
()
if
event
.
type
==
locals
.
KEYDOWN
and
jump_
==
"run"
:
if
event
.
type
==
locals
.
KEYDOWN
and
jump_
==
"run"
:
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
pygame
.
display
.
set_caption
(
"我保留了一部分九转大肠"
)
pygame
.
display
.
set_caption
(
choice
([
"俞涛保留了一部分九转大肠的鲜味"
,
"感人故事:我保留了一部分寒假作业"
,
"压缩毛巾"
,
"任何答辩终将原地爆炸"
,
"YATM又TM的死机了"
,
"极速地球2"
,
"假酒害人"
,
"逃离物锂卷"
,
"距离唱跳还有2.5年"
,
choice
([
"船新升级中."
,
"船新升级中.."
,
"船新升级中..."
])])
)
jump_
=
"up"
jump_
=
"up"
#----------------------------------
if
jump_
==
"up"
:
if
jump_
==
"up"
:
if
speed_y
>
0
:
#----------------------------------
if
speed_y
>
0
:
y
-=
speed_y
y
-=
speed_y
speed_y
-=
2
speed_y
-=
1.5
#----------------------------------
else
:
else
:
jump_
=
"down"
jump_
=
"down"
#----------------------------------
else
:
else
:
#----------------------------------
if
speed_y
<=
30
:
if
speed_y
<=
30
:
y
+=
speed_y
y
+=
speed_y
speed_y
+=
2
speed_y
+=
1.5
#----------------------------------
else
:
else
:
y
=
400
y
=
400
speed_y
=
30
speed_y
=
30
...
@@ -65,8 +87,14 @@ while True:
...
@@ -65,8 +87,14 @@ while True:
# 将背景图画上去
# 将背景图画上去
#-------------------------------
#-------------------------------
screen
.
blit
(
background
,
(
0
,
0
))
bg_x
-=
2
screen
.
blit
(
road
,
(
0
,
500
))
if
bg_x
<=
-
1000
:
bg_x
=
0
screen
.
blit
(
background
,
(
bg_x
,
0
))
road_x
-=
10
if
road_x
<=
-
1000
:
road_x
=
0
screen
.
blit
(
road
,
(
road_x
,
500
))
#-------------------------------跑步
#-------------------------------跑步
if
jump_
==
"run"
:
if
jump_
==
"run"
:
screen
.
blit
(
hero
[
index
],
(
150
,
400
))
screen
.
blit
(
hero
[
index
],
(
150
,
400
))
...
@@ -74,8 +102,13 @@ while True:
...
@@ -74,8 +102,13 @@ while True:
else
:
else
:
screen
.
blit
(
hero
[
index
],
(
150
,
y
))
screen
.
blit
(
hero
[
index
],
(
150
,
y
))
#-------------------------------
#-------------------------------
rest
.
x
-=
15
screen
.
blit
(
obstacle
,
(
rest
.
x
,
rest
.
y
))
#-------------------------------
if
obs
.
rect
.
x
<=
0
-
obs
.
rect
.
height
:
obs
.
rect
.
x
=
1000
obs
=
Spr
(
bush
,
cacti
,
stone
)
obs
.
rect
.
x
-=
10
screen
.
blit
(
obs
.
i
,
(
obs
.
rect
.
x
,
obs
.
rect
.
y
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
...
...
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