战斗的python代码_在文本冒险游戏中添加一个战斗系统;被我使用的代码结构搞糊涂了...

通过一些教育材料,我被要求使用下面的结构(类)的文本冒险游戏,并被要求添加一个简单的战斗系统之间的英雄和敌人的战斗。在

目前,我可以在每个房间创建一个敌人,并在起始房间(走廊)之间来回移动,但在这一点上我被卡住了。我无法确定我应该在哪里创建我的“英雄”,或者我如何传达我需要对健康属性进行的更改等

如果我可以用另一种方式来构造代码,我就可以完成游戏,但由于我对如何使代码的各个部分相互通信的理解存在差距。在

谢谢

戴夫# text based adventure game

import random

import time

from sys import exit

class Game(object):

def __init__(self, room_map):

self.room_map = room_map

def play(self):

current_room = self.room_map.opening_room()

while True:

next_room_name = current_room.enter()

current_room = self.room_map.next_room(next_room_name)

class Character(object):

def __init__(self, name, health, attack):

self.name = name

self.health = health

self.attack = attack

class Hero(Character):

def __init__(self, name):

super(Hero, self).__init__(name, 10, 2)

def __str__(self):

rep = "You, " + self.name + ", have " + str(self.health) + " health and " + \

str(self.attack) + " attack."

return rep

class Enemy(Character):

ENEMIES = ["Troll", "Witch", "Ogre", "Jeremy Corbyn"]

def __init__(self):

super(Enemy, self).__init__(random.choice(self.ENEMIES),

random.randint(4, 6), random.randint(2, 4))

def __str__(self):

rep = "The " + self.name + " has " + str(self.health) + \

" health, and " + str(self.attack) + " attack."

return rep

class Room(object):

def __init__(self):

self.commands = ["yes", "no"]

self.rooms = ["\'corridor\'", "\'bathroom\'", "\'bedroom\'"]

self.enemy = Enemy()

def command_list(self):

print("Commands: ", ", ".join(self.commands))

def enter_room_question(self):

print("Which room would you like to enter?")

print("Rooms:", ", ".join(self.rooms))

def leave_room_question(self):

print("Do you want to leave this room?")

print("Commands:", ", ".join(self.commands))

class Bathroom(Room):

def enter(self):

print("You enter the bathroom. But, wait! There is an", \

self.enemy.name, "!")

print(self.enemy)

print("You are in the bathroom. Need to take a dump? ")

self.command_list()

response = input("> ")

while response not in self.commands:

print("Sorry I didn't recognise that answer")

print("You are in the bathroom. Need to take a dump?")

self.command_list()

response = input("> ")

if response == "yes":

print("Not while I'm here!")

return "death"

elif response == "no":

print("Good.")

self.leave_room_question()

response = input("> ")

if response == "yes":

return "corridor"

else:

return "death"

class Bedroom(Room):

def enter(self):

pass

class Landing(Room):

def enter(self):

pass

class Corridor(Room):

def enter(self):

print("You are standing in the corridor. There are two rooms available to enter.")

self.enter_room_question()

response = input("> ")

if response == "corridor":

print("You're already here silly.")

else:

return response

class Death(Room):

QUIPS = ["Off to the man in sky. You are dead",

"You died, no-one cried.",

"Lolz. You're dead!"]

def enter(self):

time.sleep(1)

print(random.choice(Death.QUIPS))

exit()

class Map(object):

ROOMS = {"corridor": Corridor(),

"bathroom": Bathroom(),

"death": Death(),

"landing": Landing(),

"bedroom": Bedroom()}

def __init__(self, start_room):

self.start_room = start_room

self.hero = hero

def next_room(self, room_name):

return Map.ROOMS.get(room_name)

def opening_room(self):

return self.next_room(self.start_room)

a_hero = Hero("Dave")

a_map = Map("corridor")

a_game = Game(a_map, a_hero)

a_game.play()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值