Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson1_4
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
df3e308c
authored
Jul 02, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
bd3ff508
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
228 additions
and
0 deletions
Agame.py
Agame.py
0 → 100644
View file @
df3e308c
import
random
class
Hero
(
object
):
def
__init__
(
self
,
name
):
# 创建父类的属性
self
.
name
=
name
self
.
level
=
1
self
.
hp
=
250
self
.
attack
=
40
self
.
max_hp
=
self
.
hp
self
.
feel
=
1
def
cure
(
self
):
# 定义治疗
blood
=
random
.
randint
(
30
,
50
)
if
self
.
hp
<
self
.
max_hp
:
if
self
.
hp
+
blood
<=
self
.
max_hp
:
print
(
self
.
name
+
"使用了治疗,血量增加:"
,
blood
)
self
.
hp
=
self
.
hp
+
blood
print
(
"目前的血量为:"
,
self
.
hp
)
else
:
print
(
self
.
name
+
"使用了治疗,血量增加:"
,
self
.
max_hp
-
self
.
hp
)
self
.
hp
=
self
.
max_hp
print
(
"目前的血量为:"
,
self
.
hp
)
else
:
print
(
self
.
name
,
"已是满血,治疗无效,目前血量:"
,
self
.
hp
)
def
combat
(
self
,
enemy
):
# 定义普通攻击
info1
=
self
.
name
+
"对"
+
enemy
.
name
+
"发起进攻,"
info2
=
"造成"
+
str
(
self
.
attack
)
+
"点伤害,"
enemy
.
hp
-=
self
.
attack
if
enemy
.
hp
>
0
:
info3
=
enemy
.
name
+
"还剩下"
+
str
(
enemy
.
hp
)
+
"血量"
info
=
info1
+
info2
+
info3
print
(
info
)
else
:
enemy
.
feel
=
0
info3
=
enemy
.
name
+
"阵亡"
info
=
info1
+
info2
+
info3
print
(
info
)
class
Player
(
Hero
):
def
__init__
(
self
,
hero_type
,
name
):
#创建子类继承父类
super
()
.
__init__
(
name
)
self
.
hp
=
200
self
.
attack
=
50
self
.
max_hp
=
self
.
hp
self
.
hero_type
=
hero_type
self
.
feel
=
1
print
(
"角色"
,
self
.
name
,
"创建成功,英雄类型为:"
,
self
.
hero_type
)
#介绍玩家角色
print
(
"当前等级:"
,
self
.
level
,
", 血量:"
,
self
.
hp
,
", 攻击力为:"
,
self
.
attack
)
print
(
" 请创建玩家角色档案"
)
#玩家自创角色档案
name
=
input
(
"名称:"
)
name
=
" <"
+
name
+
"> "
type
=
input
(
"类型:"
)
houyi
=
Player
(
type
,
name
)
yase
=
Hero
(
" <亚瑟> "
)
#创建电脑角色
erlangshen
=
Hero
(
" <二郎神> "
)
print
(
"-"
*
30
)
while
True
:
if
houyi
.
feel
==
1
:
if
yase
.
feel
==
1
:
#玩家出招
an1
=
input
(
"请输入 1(攻击)/2(治疗)/q(退出游戏):"
)
if
an1
==
"1"
:
if
erlangshen
.
feel
==
1
:
ch1
=
input
(
"请输入 1(打 <亚瑟> )/2(打 <二郎神> )/q(返回):"
)
print
(
"-"
*
30
)
if
ch1
==
"1"
:
houyi
.
combat
(
yase
)
print
(
"-"
*
30
)
elif
ch1
==
"2"
:
houyi
.
combat
(
erlangshen
)
print
(
"-"
*
30
)
elif
ch1
==
"q"
:
continue
else
:
continue
else
:
ch1
=
input
(
"请输入 1(打 <亚瑟> )/q(返回):"
)
print
(
"-"
*
30
)
if
ch1
==
"1"
:
houyi
.
combat
(
yase
)
print
(
"-"
*
30
)
elif
ch1
==
"q"
:
continue
else
:
continue
elif
an1
==
"2"
:
print
(
"-"
*
30
)
houyi
.
cure
()
print
(
"-"
*
30
)
elif
an1
==
"q"
:
print
(
houyi
.
name
,
"逃跑了,已退出游戏"
)
break
else
:
print
(
"-"
*
30
)
continue
#电脑角色<亚瑟>出招
if
yase
.
feel
==
1
:
if
yase
.
hp
<
yase
.
max_hp
:
if
erlangshen
.
feel
==
1
:
an2
=
random
.
randint
(
1
,
3
)
if
an2
==
1
:
yase
.
cure
()
print
(
"-"
*
30
)
elif
an2
==
2
:
yase
.
combat
(
houyi
)
print
(
"-"
*
30
)
else
:
yase
.
combat
(
erlangshen
)
print
(
"-"
*
30
)
else
:
an2
=
random
.
randint
(
1
,
2
)
if
an2
==
1
:
yase
.
cure
()
print
(
"-"
*
30
)
elif
an2
==
2
:
yase
.
combat
(
houyi
)
print
(
"-"
*
30
)
else
:
if
erlangshen
.
feel
==
1
:
an2
=
random
.
randint
(
1
,
2
)
if
an2
==
1
:
yase
.
combat
(
houyi
)
print
(
"-"
*
30
)
else
:
yase
.
combat
(
erlangshen
)
print
(
"-"
*
30
)
else
:
yase
.
combat
(
houyi
)
print
(
"-"
*
30
)
#电脑角色<二郎神>出招
if
erlangshen
.
feel
==
1
:
an3
=
random
.
randint
(
0
,
1
)
if
an3
==
1
:
if
erlangshen
.
hp
<
erlangshen
.
max_hp
:
if
yase
.
feel
==
1
:
ch3
=
random
.
randint
(
1
,
2
)
if
ch3
==
1
:
erlangshen
.
combat
(
houyi
)
erlangshen
.
cure
()
print
(
"-"
*
30
)
else
:
erlangshen
.
combat
(
yase
)
erlangshen
.
cure
()
print
(
"-"
*
30
)
else
:
erlangshen
.
comAcure
(
houyi
)
print
(
"-"
*
30
)
else
:
if
yase
.
feel
==
1
:
ch3
=
random
.
randint
(
1
,
2
)
if
ch3
==
1
:
erlangshen
.
combat
(
houyi
)
print
(
"-"
*
30
)
else
:
erlangshen
.
combat
(
yase
)
print
(
"-"
*
30
)
else
:
erlangshen
.
combat
(
houyi
)
print
(
"-"
*
30
)
else
:
print
(
" <二郎神> 本回合在蓄力"
)
print
(
"-"
*
30
)
else
:
if
erlangshen
.
feel
==
0
:
print
(
"游戏结束,"
,
houyi
.
name
,
"胜利"
)
exit
()
else
:
#玩家出招
an1
=
input
(
"请输入 1(攻击)/2(治疗)/q(退出游戏):"
)
if
an1
==
"1"
:
ch1
=
input
(
"请输入 1(打 <二郎神> )/q(返回):"
)
print
(
"-"
*
30
)
if
ch1
==
"1"
:
houyi
.
combat
(
erlangshen
)
print
(
"-"
*
30
)
elif
ch1
==
"q"
:
continue
else
:
continue
elif
an1
==
"2"
:
print
(
"-"
*
30
)
houyi
.
cure
()
print
(
"-"
*
30
)
elif
an1
==
"q"
:
print
(
houyi
.
name
,
"逃跑了,已退出游戏"
)
break
else
:
print
(
"-"
*
30
)
continue
#电脑角色<二郎神>出招
if
erlangshen
.
feel
==
1
:
an3
=
random
.
randint
(
0
,
1
)
if
an3
==
1
:
if
erlangshen
.
hp
<
erlangshen
.
max_hp
:
erlangshen
.
combat
(
houyi
)
erlangshen
.
cure
()
print
(
"-"
*
30
)
else
:
erlangshen
.
combat
(
houyi
)
print
(
"-"
*
30
)
else
:
print
(
" <二郎神> 本回合在蓄力"
)
print
(
"-"
*
30
)
else
:
#判定游戏结果
if
yase
.
feel
==
1
:
if
erlangshen
.
feel
==
0
:
print
(
"游戏结束,"
,
yase
.
name
,
"胜利"
)
exit
()
else
:
print
(
"游戏结束,玩家"
,
houyi
.
name
,
"阵亡,"
,
yase
.
name
,
erlangshen
.
name
,
"存活"
)
print
()
exit
()
else
:
print
(
"游戏结束,"
,
erlangshen
.
name
,
"胜利"
)
exit
()
\ 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