在国外网站上找到一个练习Python的小游戏感觉不错,自己实现了一下。

通过该练习你能学到:

  1. 元组
  2. 字典
  3. 简单定义函数和封装
  4. 条件控制语句

游戏说明

以下是3个房间和1个花园:

Hall 客厅 有一把钥匙,Kitchen 厨房 有一只怪物,Dinning Room 餐厅 有一瓶药水,Garden 花园

完成游戏条件:拿到钥匙和药水到达花园并躲避怪物。

游戏操作指令:
      go [direction]
      get [item]

[direction] 包含:east, west, south, north

[item] 包含:key, potion

游戏源码

#! python3
"""
author: laoxu
""" # 游戏说明
def showInstructions():
print('''
RPG Game
========
完成游戏条件:拿到钥匙和药水到达花园并躲避怪物。 命令:
go [direction]
get [item]
'''
) # 打印当前房间和背包信息
def showCurrentRoom(room, bag):
print('You are in the %s' % room)
print('Inventory: ', bag) rooms = {
'Hall': {
'south': 'Kitchen',
'east': 'Dinning Room',
'item': 'key'
},
'Kitchen': {
'north': 'Hall',
'item': 'monster'
},
'Dinning Room': {
'west': 'Hall',
'south': 'Garden',
'item': 'potion'
},
'Garden': {
'north': 'Dinning Room'
}
} # 初始化房间
currentRoom = 'Hall' # 初始化物品栏
inventory = [] # 打印游戏帮助
showInstructions() print('You are in the %s' % currentRoom)
print('Inventory: ', inventory)
print('You see a key') while True:
# 玩家进入厨房,游戏结束
if 'item' in rooms[currentRoom] and 'monster' in rooms[currentRoom]['item']:
print('你被怪物抓住了...游戏结束!')
break
# 玩家拿到钥匙和药水进入花园,游戏结束
if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
print('你逃脱了房子!你赢了!')
break # 接收操作步骤
step = input() # 客厅->厨房
if currentRoom == 'Hall' and step == 'go south':
currentRoom = 'Kitchen'
continue
# 客厅->餐厅
elif currentRoom == 'Hall' and step == 'go east':
currentRoom = 'Dinning Room'
# 厨房->客厅
elif currentRoom == 'Kitchen' and step == 'go north':
currentRoom = 'Hall'
# 餐厅->客厅
elif currentRoom == 'Dinning Room' and step == 'go west':
currentRoom = 'Hall'
# 餐厅->花园
elif currentRoom == 'Dinning Room' and step == 'go south':
currentRoom = 'Garden'
# 花园->餐厅
elif currentRoom == 'Garden' and step == 'go north':
currentRoom = 'Dinning Room'
# 拿到钥匙
elif currentRoom == 'Hall' and 'key' not in inventory and step == 'get key':
inventory.append('key')
print('key got!')
# 拿到药水
elif currentRoom == 'Dinning Room' and 'potion' not in inventory and step == 'get potion':
inventory.append('potion')
print('potion got!') # 打印房间和物品栏
showCurrentRoom(currentRoom, inventory) if currentRoom == 'Hall' and 'key' not in inventory:
print('You see a key')
if currentRoom == 'Dinning Room' and 'potion' not in inventory:
print('You see a potion') continue

运行效果

Python之初级RPG小游戏的更多相关文章

  1. 用Python设计一个经典小游戏

    这是关于Python的第9篇文章,介绍如何用Python设计一个经典小游戏:猜大小. 在这个游戏中,将用到前面我介绍过的所有内容:变量的使用.参数传递.函数设计.条件控制和循环等,做个整体的总结和复习 ...

  2. Python开发接水果小游戏

    我研发的Python游戏引擎Pylash已经更新到1.4了.如今我们就来使用它完毕一个极其简单的小游戏:接水果. 下面是游戏截图: 游戏操作说明:点击屏幕左右两边或者使用键盘方向键控制人物移动.使人物 ...

  3. python【控制台】小游戏--贪吃蛇

    传统贪吃蛇相信大家都玩过,也是一款很老很经典的游戏,今天我们用python控制台实现 项目有很多bug没有解决,因为本人一时兴起写的一个小游戏,所以只是实现可玩部分功能,并没有花较多的时间和精力去维护 ...

  4. python成语接龙小游戏

    上一篇讲了小游戏的坑现在把源码放出来 #coding:utf-8 import string import pypinyin import sys import random print(" ...

  5. 【Python】猜数小游戏(文件操作)

    人生苦短,我用Python 关键词 1.多用户 2.字典记录所有成绩 3.每次游戏轮数&总游戏次数&平均每次游戏需要多少轮 字典Dictionary.列表List.元组Tuple差异化 ...

  6. python做一个数独小游戏

    最近看了下python的一些知识,在这里记载一下. 1.首先是安装,在官网下载最新的版本3.6,安装的时候要注意在下面勾选上ADD TO PATH,安装的时候会自动写入到环境变量里面,如果没有勾选,可 ...

  7. 【Python】猜数小游戏

    有点沙雕 temp=input("猜猜我心里想的是哪个数字?") guess=int (temp) if guess==8: print("你是我肚里的蛔虫么?" ...

  8. Python制作塔防小游戏

    开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块.

  9. python——成语接龙小游戏

    小试牛刀的简易成语接龙. 思路—— 1.网上下载成语字典的txt版本 2.通过python进行处理得到格式化的成语,并整理成字典(python字典查找速度快) 3.python程序,查找 用户输入的最 ...

  10. python学习-6 猜拳小游戏

    import random # 调用随机数模块 pc = random.randint(1,3) # 产生1-3的随机数 print("来玩个猜拳游戏吧!") a = '石头' b ...

随机推荐

  1. [转帖]nginx中rewrite和if的用法及配置

    nginx中rewrite和if的用法及配置 文章目录 nginx中rewrite和if的用法及配置 @[toc] 一.rewrite应用 1.rewrite跳转场景 2.rewrite实际场景 3. ...

  2. [转帖]使用 Dumpling 导出数据

      16 Contributors 使用数据导出工具 Dumpling,你可以把存储在 TiDB 或 MySQL 中的数据导出为 SQL 或 CSV 格式,用于逻辑全量备份.Dumpling 也支持将 ...

  3. [转帖]kafka 配置认证与授权

    https://www.cnblogs.com/yjt1993/p/14739130.html 本例不使用kerberos做认证,使用用户名和密码的方式来进行认证 1.服务端配置 1.0 配置serv ...

  4. [转帖]优化命令之iotop命令

    文章目录 引言 一.iotop简介 1.iotop安装 2.iotop语法 3.iotop参数 二.I/O的常用快捷键 三.交互模式 四.iotop示例 1.只显示正在产生I/O的进程 2.显示指定P ...

  5. [转帖]一行Python代码实现同一局域网内的文件共享

    在不同的设备之间传输文件除了数据线,网盘传输外是否还有其他优雅的方法?我们可以使用一行Python代码使局域网内的所有设备都可以访问并下载文件夹内的文件. 要求: 电脑中安装配置好python 访问的 ...

  6. DBLink实现备份文件不落盘的导入其他Oracle数据库实例的方法

    DBLink实现备份文件不落盘的导入其他Oracle数据库实例的方法 背景 公司内经常有从其他服务器备份数据库实例的需求 之前的操作一般需要,备份源服务器使用expdp将source导出dump文件. ...

  7. SPI在Java中的实现与应用 | 京东物流技术团队

    1 SPI的概念 API API在我们日常开发工作中是比较直观可以看到的,比如在 Spring 项目中,我们通常习惯在写 service 层代码前,添加一个接口层,对于 service 的调用一般也都 ...

  8. axios发送请求时携带token

    请求头携带token async getUserlist(){ // 需要授权的Api,必须在青丘头中使用Authorization 字段提供token令牌 const AUTH_TOKEN=loca ...

  9. TienChin 开篇-运行 RuoYiVue

    开篇 目的: 让大家随心所欲的 DIY 若依的脚手架 不会涉及到太多基础知识 踊跃提问(不懂得地方大家提问我会根据提问,后续一一解答疑惑) 下载 RuoYiVue Gitee: https://git ...

  10. 设计模式学习-使用go实现备忘录模式

    备忘录模式 定义 优点 缺点 适用范围 代码实现 参考 备忘录模式 定义 备忘录( Memento ):在不破坏封装性的前提下,获取一个对象的内部状态,并在该对象之处保存该状态.这样以后就可将该对象恢 ...