Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_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
f1702614
authored
Nov 22, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
d5739bd2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
66 deletions
snake.py
snake.py
View file @
f1702614
import
pygame
class
Hero
(
object
):
from
pygame
import
locals
def
__init__
(
self
,
name
):
self
.
name
=
name
# 初始化pygame,为使用硬件做准备
self
.
level
=
1
pygame
.
init
()
self
.
hp
=
250000
self
.
attack
=
4000
# 创建一个窗口
self
.
max_hp
=
self
.
hp
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
FPSCLOCK
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
def
cure
(
self
):
# 治疗
self
.
hp
+=
6000
# 背景
if
self
.
hp
>
self
.
max_hp
:
background
=
pygame
.
image
.
load
(
'bg.png'
)
self
.
hp
=
self
.
max_hp
right
=
pygame
.
image
.
load
(
'right.png'
)
# 头 朝右
print
(
self
.
name
+
"使用了治疗,血量增加:"
,
6000
,
",目前的血量为:"
,
self
.
hp
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
# 食物 苹果
body
=
pygame
.
image
.
load
(
'body.png'
)
# 身体
def
combat
(
self
,
enemy
):
# 普通攻击
left
=
pygame
.
image
.
load
(
'left.png'
)
# 头 朝左
enemy
.
hp
-=
self
.
attack
up
=
pygame
.
image
.
load
(
'up.png'
)
# 头 朝上
info1
=
self
.
name
+
"对"
+
enemy
.
name
+
"发起进攻,"
down
=
pygame
.
image
.
load
(
'down.png'
)
# 头 朝下
info2
=
"造成"
+
str
(
self
.
attack
)
+
"点伤害,"
if
enemy
.
hp
>
0
:
x
,
y
=
240
,
120
info3
=
enemy
.
name
+
"还剩下"
+
str
(
enemy
.
hp
)
+
"血量"
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
info
=
info1
+
info2
+
info3
print
(
info
)
setheading
=
"right"
else
:
snake_head
=
right
info3
=
enemy
.
name
+
"阵亡,游戏结束"
info
=
info1
+
info2
+
info3
while
True
:
print
(
info
)
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
exit
()
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_RIGHT
and
setheading
!=
"left"
:
setheading
=
'right'
snake_head
=
right
if
event
.
key
==
locals
.
K_LEFT
and
setheading
!=
"right"
:
setheading
=
'left'
snake_head
=
left
if
event
.
key
==
locals
.
K_UP
and
setheading
!=
"down"
:
setheading
=
'up'
snake_head
=
up
if
event
.
key
==
locals
.
K_DOWN
and
setheading
!=
"up"
:
setheading
=
'down'
snake_head
=
down
# 设置贪吃蛇的头部坐标
class
Player
(
Hero
):
if
setheading
==
"right"
:
def
__init__
(
self
,
hero_type
,
name
):
x
+=
30
super
()
.
__init__
(
name
)
elif
setheading
==
"left"
:
self
.
hp
=
200000
x
-=
30
self
.
attack
=
5000
elif
setheading
==
"up"
:
self
.
max_hp
=
self
.
hp
y
-=
30
self
.
hero_type
=
hero_type
else
:
print
(
"角色"
+
self
.
name
+
"创建成功,英雄类型为:"
,
self
.
hero_type
)
y
+=
30
print
(
"当前等级、血量、攻击力分别为:"
,
self
.
level
,
self
.
hp
,
self
.
attack
)
position
.
append
((
x
,
y
))
position
.
pop
(
0
)
houyi
=
Player
(
"射手"
,
"后羿"
)
# 将背景图画上去
yase
=
Hero
(
"垭瑟"
)
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇的头画上去
screen
.
blit
(
snake_head
,
position
[
-
1
])
# 将贪吃蛇的身体画上去
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
])
# 将果实画上去
screen
.
blit
(
food
,
(
360
,
300
))
# 刷新画面
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
3
)
\ 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