在之前发布了这里,阅读了这条建议,并做了一些研究之后,我重新计划并重新开始了。
我很感谢我在“代码评论”中得到的评论。
我做的第一件事就是创建一个地图,这样我就可以看到我必须为什么编码。https://sta.sh/01tlcemlvumr
然后,我完成了游戏中第一个房间的代码,看看我的新代码结构将如何工作。以下是一些细节:
回到玩家的location_name属性..。
主游戏循环函数检查location_name。然后,它为该位置调用一个函数。这个位置函数检查两个元组。如果玩家的条目在一个移动项中,则调用另一个函数进行移动。如果条目在动作元组中,则为操作调用action函数。
由于房间/位置对象中有两个元组,所以我有所有需要编码的值/播放器输入的可能性,可以很容易地找到和修改它们。这也给了我一个测试值的列表。
现在,我只有第一个房间的代码和带玩家到下一个房间的代码。我在第一个房间里也有行动守则。
我认为这个整体结构将允许我以一种有组织的方式加入到游戏中来。我可以继续在每个房间/位置的play_game()函数中添加elif语句。而且,我可以为每个房间添加功能,并在这些单独的房间中为移动和动作提供子功能。
我使用REPL.it网站和本地图书馆的计算机对此进行了编码。我现在买不起自己的电脑。这个网站真的很不错。我试过了PythonAnywhere。但是,REPL.it现在更适合我。也许如果我做一个更大的项目,PythonAnywhere会更好。
我不知道这是否有关。如果不是,就删除它。我看了布莱恩·唐的YouTube视频,内容是做一个Python文本RPG。我没有复制他做的事。但是,我很喜欢他的很多想法,尤其是他的代码组织得很好。他为这个系列制作了六段视频。https://www.youtube.com/channel/UC5akxkiQHpxCzPZWskdBbQQ
# Since there are no methods needed for a player a dictionary may be a better option since this is only storing data.
player_location = "start_room"
player_data = {
"name": "none", # This will come from input.
"location": "You are at the start of the game in a small dining room in a small house.\n", # The player will always start here.
"location_name": "start_room",
"health": 100
}
# Create locations for the game with directions as to where a player can go in them.
class Location:
def __init__(self, moves, actions, keys):
self.moves = moves
self.actions = actions
self.keys = keys
self.description = "You are in a small dining room in a small house. There is no furniture in this room.\n\nYou are facing a short hallway leading to a staircase.\n\nThere is an open door to your left leading to a small porch.\n\nTo your right is the living room.\n\nThe area of the room back behind you is dark.\n\nThere is a light switch on the wall.\n\n"
# ---------------------------------------------------------
# This will print at the beginning of the game and every time a player enters "help".
def help_file():
print("\n- - - - - - - - - - - - - - - - - - - - - - - - -\n")
print("You will be exploring places and interacting with items in the game.\n")
print("Please use words like 'forward', 'left', 'right' and 'back' for movement inside a structure.\n")
print("Directions such as 'north' or 'n'; 'south' or 's'; 'east' or 'e'; and 'west' or 'w' will work when you are outside.\n")
print("When you want to use an item in the game, just enter the name of the item like 'boomerang'. You will then be prompted for what you want to do with that item.\n")
print("To see a description of where you are in a room or an area, just type 'description'.\n")
print("To see this introductory list again, type 'help'.\n")
print("When you are ready to continue, press the ENTER key.\n")
input()
print("\n- - - - - - - - - - - - - - - - - - - - - - - - -\n")
# ---------------------------------------------------------
# The starting room:
# This creates a new object based on the Location class.
# The two tuples are the moves list and the actions list for later validation.
start_room_keys = {"light": "off"}
start_room = Location(("stairs", "staircase", "forward", "porch", "left", "living room", "right", "back"),
("look", "light", "light switch", "box", "open"), start_room_keys)
# Functions for a player move or action in this room:
def start_room_moves_actions(player_input):
if player_input in start_room.moves:
start_room_moves(player_input)
elif player_input in start_room.actions:
start_room_action(player_input)
else:
print("\nThat is not a valid request for this situation.\n")
play_game()
def start_room_moves(player_move):
print()
if player_move == "stairs" or player_move == "staircase" or player_move == "forward":
print("You walk to the bottom of the stairs. There is an old rug at the base of the stairs.")
player_data["location"] = "At the bottom of the stairs in the small house"
player_data["location_name"] = "house_stairs"
elif player_move == "porch" or player_move == "left":
print("You walk through the open door to the porch.")
print("The view from here is spectacular. You are looking out over a wooded valley with a river far below.")
print("There are rolling hills off in the distance.")
print("You walk to the edge of the porch and see some letters carved in the railing.")
print()
print("RWBL")
print()
print("Hmm...I wonder what that means?\n")
player_data["location"] = "On the back porch of a small house."
player_data["location_name"] = "house_porch"
elif player_move == "right" or player_move == "living room":
print("As you enter the living room you notice a large painting of a sunset scene over a wooded valley on one wall.\n")
player_data["location"] = "You are in the living room in a small house."
player_data["location_name"] = "house_living_room"
elif player_move == "back":
if start_room.keys["light"] == "off":
print("It is too dark to go in that direction. You should be more careful about where you go.\n")
else:
print("There is an old shoe box in the back next to the wall.\n")
def start_room_action(player_action):
if player_action == "look":
print("There is no furniture in this room. There is an old hanging light fixture that has a bulb in the middle of the ceiling. There is a light switch to your right on the wall. The room back behind you is dark and you cannot see what is there.\n")
elif player_action == "light" or player_action == "light switch":
print("\nThe hanging light in the middle of the ceiling illuminates the room. There appears to be a box back of the room where it was previously dark.\n")
start_room.keys["light"] = "on"
elif player_action == "box" or player_action == "open":
if start_room.keys["light"] == "off":
print("What are you trying to do? You need to lighten up.\n")
else:
print("\nThere is a brand new pair of running shoes in the box.\n")
print("As you take them out, you see...SOMETHING.\n")
# ---------------------------------------------------------
# The porch:
# ---------------------------------------------------------
# The stairs:
# ---------------------------------------------------------
# The living room:
# -------------------------------------------------------------
# End of code/functions for individual rooms/locations.
# -------------------------------------------------------------
# Get a name from player and update player object.
print("Welcome to the game.")
def get_player_name():
name_input = input("What is your name?")
if len(name_input) > 10 or len(name_input) < 1:
print("Please enter a name with 0 - 10 letters.")
get_player_name()
else:
player_data["name"] = name_input
get_player_name()
# Print welcome to player using name.
print("\nWelcome, " + str(player_data["name"]) + "!")
# Print the starting story text.
# Print a description of the game with examples of valid entries.
help_file()
# Print a description of the starting room.
print(start_room.description)
# ---------------------------------------------------------
# Main game loop
def play_game():
# Get input from player for move or interaction.
player_input = input("What would you like to do now?\n")
# If 'description', then print the current location's description, and then prompt again for input.
if player_input == "description":
current_location = player_data["location"]
print("\n" + str(current_location))
elif player_input == "help":
help_file()
elif player_input == "":
print("Please try again.")
else:
# Get the name of the player's current location.
current_location_name = player_data["location_name"]
if current_location_name == "start_room":
start_room_moves_actions(player_input)
# Add elif statements here checking for rooms.
# Check to see if the game is over at this point.
if player_data["health"] < 1:
print("You are dead! Too bad. You seemed like a nice person.")
else:
play_game()
play_game() 发布于 2018-05-28 03:32:08
在回顾您的设计时,您提到了I was going to use a player object. But, I don't need any methods,但是您有:
在一场游戏中,这些都是玩家的行为和多个玩家的行为。健康,例如,你在哪里管理“饮料药剂”和“被攻击”的功能?似乎您需要一个播放器类和两个方法,heal(amount)和damage(amount) --这意味着我们还需要player().is_dead属性(不是真正的代码):
if damage(amount)>= self.health:
self.is_dead = True此外,对于您的位置,将功能和属性分离为元组没有意义,您需要创建数百个元组并跟踪所有元组(如何将它们链接到冒险中?用户如何在它们之间移动?)。创建要继承的Location和Room(Location)或Outside(Location)的基类会更容易。Location类将包含用户进出位置的逻辑,房间将具有额外的功能,如灯开关(默认情况下是打开或关闭的话)。为此,我将使用一个功能映射(一个包含Location's默认属性的矩阵),并让加载它们的过程负责创建对象(并向房间添加某些项目)。
由于此设计非常复杂,我建议将位置数据与代码分离,可能的方法是使用ConfigParser并将数据存储到.ini文件中。
类似于:
[room_x1_y1]
name = Dining Room
contains_physical = dining_table,chairs,plates
contains_npc = npc_butler
[npc_butler]
name = Butler
contains_physical = building_key当程序运行时,它将加载玩家数据和位置数据(从每个部分的定义中了解x和y)并开始游戏。
最后,听起来您可能需要一个像样的IDE,我建议从JetBrains的社区版开始。
希望这有助于您的设计和代码。祝好运!
https://codereview.stackexchange.com/questions/194971
复制相似问题