编写Python小游戏

这是之前学习python时自己跟着《Beginner’s Guide v2
》这本书籍学习,并且自己修改了一些之后的一个小游戏。

游戏介绍

 游戏人物的出生地是 Hall,他的南边是 Kitchen,东边是 Dining Room,客厅有一把钥匙。 Kitchen 有一个怪物,若进入的话就 Game over 了。游戏的通关方法一是拿住 Key 后,从 Garden 离开,就成功了。通关方法二是可以选择在Dining Room拿到宝剑后去击败怪物。

代码:

#!/bin/python3
# author: Wilson
# Replace RPG starter project with this code when new instructions are live
def showInstructions():
    # print a main menu and the commands
    print('''
RPG Game
========
Commands:
go [direction]
get [item]
''')

 这一部分是游戏的开始界面,告诉玩家操作的命令。

def showStatus():
    # print the player's current status
    print('---------------------------')
    print('You are in the ' + currentRoom)
    # print the current inventory
    print('Inventory : ' + str(inventory))
    # print an item if there is one
    if "item" in rooms[currentRoom]:
        print('You see a ' + rooms[currentRoom]['item'])
    print("---------------------------")
# an inventory, which is initially empty
inventory = []  # bag
currentRoom = 'Hall'

这一部分是游戏的开始界面,显示玩家的方向以及所获物品。运行如下图:

下方代码则是对地图的一个描述以实现地图的构造。

# a dictionary linking a room to other rooms
rooms = {
    'Hall': {
        'south': 'Kitchen',
        'east': 'Dining Room',
        'item': 'key'
    },
    'Kitchen': {
        'north': 'Hall',
        'item': 'monster'
    },
    'Dining Room': {
        'west': 'Hall',
        'south': 'Garden',
        'item': 'sward'
    },
    'Garden': {
        'north': 'Dining Room'
    }
}
# start the player in the Hall currentRoom = 'Hall'

下方代码是控制人物移动以及游戏对人物能否移动到此位置的提示和游戏进程的提示。 



showInstructions()
# loop forever
while True:
    showStatus()
    # get the player's next 'move'
    # .split() breaks it up into an list array
    # eg typing 'go east' would give the list:
    # ['go','east']
    move = ''
    while move == '':
        move = input('>')  # 输入指令
    move = move.lower().split()  # 把输入的全部小写化且将go与get切片
    # if they type 'go' first
    if move[0] == 'go':  # 移动指令
        # check that they are allowed wherever they want to go
        if move[1] in rooms[currentRoom]:
            #set the current room to the new room
            currentRoom = rooms[currentRoom][move[1]]
        # there is no door (link) to the new room
        else:
            print('You can\'t go that way!')
    # if they type 'get' first
    if move[0] == 'get': #获取指令
        # if the room contains an item, and the item is the one they want to get
        if "item" in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
            # add the item to their inventory
            inventory += [move[1]]
            # display a helpful message
            print(move[1] + ' got!')
            # delete the item from the room
            del rooms[currentRoom]['item']
            # otherwise, if the item isn't there to get
        else:
            # tell them they can't get it
            print('Can\'t get ' + move[1] + '!')
    # player loses if they enter a room with a monster
    if 'item' in rooms[currentRoom] and 'monster' in rooms[currentRoom]['item']:
        if 'sward'in inventory:
            print('You defeated the monster!!! You are a good soldier!!!')
            break
        else:
            print('A monster has got you... GAME OVER!')
            break
    # player wins if they get to the garden with key and potion
    if currentRoom == 'Garden' and 'key' in inventory: #and 'potion' in inventory:
            print('You have escaped!!! Congratulations!')
            break

欢迎各位评论指教,这篇文章是接近一年前就打算发布的结果一直拖着,有些代码细节也记的不太清楚了,当时在学习的过程中也借鉴了很多大佬的文章,从而成功完成了这个小游戏。 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值