如何在一个房子里有多个敌人。Python文本RPG
我正在做一个游戏,一个玩家在房子里和怪物战斗。我想弄清楚如何在一个房子里有多个怪物。我不知道如何在列表中创建怪物的实例,或者这是否可行如何在一个房子里有多个敌人。Python文本RPG,python,Python,我正在做一个游戏,一个玩家在房子里和怪物战斗。我想弄清楚如何在一个房子里有多个怪物。我不知道如何在列表中创建怪物的实例,或者这是否可行 from random import randint import subprocess import platform import time import npc, player class Map: def __init__(self, width, height): self.width = width self
from random import randint
import subprocess
import platform
import time
import npc, player
class Map:
def __init__(self, width, height):
self.width = width
self.height = height
self.monsterHouse = []
self.start = (0, 0)
self.player = (0, 0)
def movePlayer(self, d):
x = self.player[0]
y = self.player[1]
pos = None
if d == "":
pos = (x, y)
elif d[0] == 'r':
pos = (x + 1, y)
elif d[0] == 'l':
pos = (x - 1, y)
elif d[0] == 'u':
pos = (x, y - 1)
elif d[0] == 'd':
pos = (x, y + 1)
else:
pos = (x, y)
if pos[0] > -1 and pos[0] < self.width and pos[1] > -1 and pos[1] < self.height:
if pos not in self.monsterHouse:
self.player = pos
elif pos in self.monsterHouse:
self.player = pos
clear()
enterHouse(self)
# MonsterHouse.modifyPlayer(self.player)
elif pos in self.start:
SafeHouse.modifyPlayer(self.player)
def drawGrid(self, width=2):
for y in range(self.height):
for x in range(self.width):
if (x, y) in self.monsterHouse:
symbol = 'H'
elif (x, y) == self.player:
symbol = '$'
elif (x, y) == self.start:
symbol = '&'
else:
symbol = '.'
print("%%-%ds" % width % symbol, end="")
print()
def getHouse(g: Map) -> list:
out = []
for i in range(0, 3):
x = randint(1, g.width-1)
y = randint(1, g.height-1)
if x == 0 and y == 0:
x = 3
y = 3
out.append((x,y))
return out
def enterHouse(self):
action = input("Monsters House! What do you do? ([a]ttack, [r]un)")
if action == 'a':
MonsterHouse.attackPhase(self)
elif action == 'r':
self.movePlayer(self, 'l')
def clear():
subprocess.Popen("cls" if platform.system() == "Windows" else "clear", shell=True)
time.sleep(.01)
def main():
g = Map(5, 5)
g.monsterHouse = getHouse(g)
while True:
drawGrid(g)
d = input("Which way? (r, l, u, d)")
g.movePlayer(d)
clear()
class SafeHouse(Map):
def modifyPlayer(self):
self.hp = randint(100, 125)
class MonsterHouse(Map):
def __init__(self, x, y, enemy):
self.enemy = enemy
super().__init__(x, y)
def attackPhase(self):
houseMonsters = []
numMonsters = randint(0,10)
for x in range(numMonsters):
typeMonster = randint(1, 4)
#1 = Zombie, 2 = Vampire, 3 = Ghoul, 4 = Werewolf
if typeMonster == 1:
houseMonsters += npc.Zombie
elif typeMonster == 2:
houseMonsters += npc.Vampire
elif typeMonster == 3:
houseMonsters += npc.Ghoul
elif typeMonster == 4:
houseMonsters += npc.Werewolf
# print(houseMonsters)
def modifyPlayer(self, thePlayer):
if self.enemy.isAlive():
thePlayer.hp = thePlayer.hp - self.enemy.damage
print("You have been attacked by the monsters! You have {} HP remaining.".format(thePlayer.hp))
if __name__ == '__main__':
main()
所以我已经设置好了,玩家可以移动到地图上的房子里,他们可以攻击它,也可以逃跑。如果他们攻击它,那么它会在房子里创造随机数量的怪物供玩家攻击
我收到的错误是TypeError:“type”对象不可编辑有几个小错误 首先,要创建对象的新实例,即使没有参数,也需要包含括号
houseMonsters += npc.Zombie()
第二个问题是,housenmonsters
是一个列表。说list+=object
是无效的语法,代码需要使用list.append()
,例如:
houseMonsters.append( npc.Vampire() )
将这些更改包装在一起会产生以下内容的MonsterHouse.attackPhase()
:
def attackPhase(self):
houseMonsters = []
numMonsters = randint(0,10)
for x in range(numMonsters):
typeMonster = randint(1, 4)
#1 = Zombie, 2 = Vampire, 3 = Ghoul, 4 = Werewolf
if typeMonster == 1:
newMonster = npc.Zombie()
elif typeMonster == 2:
newMonster = npc.Vampire()
elif typeMonster == 3:
newMonster = npc.Ghoul()
elif typeMonster == 4:
newMonster = npc.Werewolf()
# add to the list
houseMonsters.append( newMonster )
print( "DEBUG" + str (houseMonsters))
当然,您永远不会看到调试打印,因为代码会在每个循环中清除屏幕。也许在开发过程中,将“清除”更改为只打印一行破折号或类似的内容
& . . . .
$ H . . H
. . . . .
. . . . .
. . . H .
Which way? (r, l, u, d)r
----------------------------------
Monsters House! What do you do? ([a]ttack, [r]un)a
DEBUG[<npc.Werewolf object at 0x7f080722acd0>, <npc.Werewolf object at 0x7f0807734810>]
&。
$H。H
. . . . .
. . . . .
. . . H
哪条路?(r,l,u,d)r
----------------------------------
怪物之家!你是做什么的?([a]ttack[r]un)a
调试[,]
我想它就在这里的攻击阶段
方法中。您的代码创建了一个包含多个对象的列表。有什么问题吗?
& . . . .
$ H . . H
. . . . .
. . . . .
. . . H .
Which way? (r, l, u, d)r
----------------------------------
Monsters House! What do you do? ([a]ttack, [r]un)a
DEBUG[<npc.Werewolf object at 0x7f080722acd0>, <npc.Werewolf object at 0x7f0807734810>]