python实现飞行棋游戏


Posted in Python onFebruary 05, 2020

本文实例为大家分享了python实现飞行棋的具体代码,供大家参考,具体内容如下

import random
# 地图初始坐标
Maps = [0] *100 
 
# 玩家A和玩家B的初始坐标
PlayerPos = [0]*2
# 存储玩家姓名
playerNames = [""] *2
 
# 俩个玩家行动的标记
Flags = [True]*2
 
# 封装一个不换行的print
def print_end(num):
 print(num,end="")
 
 
def gameshow():
 """
 飞行棋游戏头
 """
 print('\033[1;31;m')
 print("*"*50)
 print('\033[1;32;m')
 print("*" * 50)
 print('\033[5;33;m')
 print("*" * 15 + "飞行棋爵士版 v1.0" + "*"*20)
 print('\033[1;34;m')
 print("*" * 50)
 print('\033[1;35;m')
 print("*" * 50)
def chushihuamap():
 luckyturn_list = [3,15,33,36,45,71,89,95] # 幸运轮盘 ◎
 for number1 in luckyturn_list:
  Maps[number1] = 1
 
 landmine_list = [7,19,39,67,77,97] # 地雷 ●
 for number2 in landmine_list:
  Maps[number2] = 2
 
 pause_list = [2,5,9,31,37,56,87] # 暂停 ▲
 for number3 in pause_list:
  Maps[number3] = 3
 
 timeTunnel_list = [1,10,28,60,88,] # 时空隧道 ?e
 for number4 in timeTunnel_list:
  Maps[number4] = 4
def drawstringmap(a):
 """
  构造地图
 :param a: 0~99 的地图坐标
 :return: 返回地图坐标所在的 图
 """
 # 玩家A和玩家B在同一坐标用<>表示
 str = ""
 if PlayerPos[0] == PlayerPos[1] and PlayerPos[0] == a:
  str = "<>"
 elif PlayerPos[0] == a:
  str = "A"
 elif PlayerPos[1] == a:
  str = "B"
 else:
  if Maps[a] == 0:
   print_end('\033[1;32;m')
   str = " □"
 
  elif Maps[a] == 1:
   print_end('\033[1;34;m')
   str = " ◎"
 
  elif Maps[a] == 2:
   print_end('\033[1;31;m')
   str = " ●"
 
  elif Maps[a] == 3:
   print_end('\033[1;35;m')
   str = " ▲"
 
  else:
   print_end('\033[1;33;m')
   str = "?e"
 return str
def drawmap():
 print("玩家A和玩家B在同一位置时用<>表示")
 print("图例:幸运轮盘:◎ 地雷:● 暂停:▲ 时空隧道:?e")
 # 第一横行
 for a in range(0,30):
  print_end(drawstringmap(a))
 print() # 第一横行结束后应该换行
 # 第一竖行
 for a in range(30,35):
  for b in range(0,29):
   print_end(" ")
  print_end(drawstringmap(a))
  print()
 # 第二横行
 a = 64
 while a >=35:
  print_end(drawstringmap(a))
  a -= 1
 print() # 换行
 # 第二竖行
 for a in range(65,70):
  print(drawstringmap(a))
 # 第三竖行
 for a in range(70,100):
  print_end(drawstringmap(a))
 # 画完最后一行应换行
 print()
def playGame(playnumber):
 """
  玩游戏
 :param playnumber: 玩家坐标
 """
 rNumber = random.randint(1,6)
 input()
 print("玩家{0}按下任意键开始掷骰子".format(playerNames[playnumber]))
 input()
 print("玩家{0}掷出了{1}".format(playerNames[playnumber],rNumber))
 PlayerPos[playnumber] += rNumber
 changePos()
 input()
 print("玩家{0}按任意键开始行动".format(playerNames[playnumber]))
 input()
 print("玩家{0}行动完了".format(playerNames[playnumber]))
 input()
 if Maps[PlayerPos[playnumber]] == 0:
  print("玩家{0}踩到了方块,什么也没发生".format(playerNames[playnumber]))
 elif Maps[PlayerPos[playnumber]] == 1:
  input_num = input("玩家{0}踩到了幸运轮盘,请选择 1.轰炸对方(后退6格) 2.交换位置".format(playerNames[playnumber]))
 
  while True:
   if input_num == "1":
    print("玩家{0}被轰炸,后退6格".format(playerNames[1 - playnumber]))
    PlayerPos[1 - playnumber] -= 6
    changePos()
    input()
    break
   elif input_num == "2":
    print("玩家{0}选择交换位置".format(playerNames[playnumber]))
    PlayerPos[playnumber],PlayerPos[1 - playnumber] = PlayerPos[1 - playnumber],PlayerPos[playnumber]
    input("交换完成,按任意键继续游戏")
    break
   else:
    input_num = input("只能输入 1.轰炸对方(后退6格) 2.交换位置 请重新输入")
 
 elif Maps[PlayerPos[playnumber]] == 2:
  print("玩家{0}踩中了地雷,后退6格".format(playerNames[playnumber]))
  PlayerPos[playnumber] -= 6
  changePos()
  input()
 elif Maps[PlayerPos[playnumber]] == 3:
  print("玩家{0}暂停一回合".format(playerNames[playnumber]))
 
  Flags[playnumber] = False
  input()
 elif Maps[PlayerPos[playnumber]] == 4:
  print("恭喜玩家{0}进入时空隧道,前进10步".format(playerNames[playnumber]))
  PlayerPos[playnumber] += 10
  changePos()
  input()
 changePos()
 # TODO 清屏 。。。。。
 drawmap()
def changePos():
 if PlayerPos[0] < 0:
  PlayerPos[0] = 0
 if PlayerPos[0] >99:
  PlayerPos[0] = 99
 if PlayerPos[1] < 0:
  PlayerPos[1] = 0
 if PlayerPos[1] > 99:
  PlayerPos[1] = 99
def win():
 print('\033[5;33;m')
 print("*" * 80)
 print("       ■      ■    ■   ")
 print("  ■■■■■■■■  ■ ■      ■     ■   ")
 print("  ■  ■  ■ ■      ■ ■   ■  ■   ")
 print("  ■  ■  ■■■■■■■■■■    ■ ■   ■  ■   ")
 print("  ■■■■■■■■ ■  ■     ■■■■■■■■  ■  ■   ")
 print("  ■  ■ ■  ■      ●■ ●  ■  ■   ")
 print("  ■  ■   ■      ● ■ ●  ■  ■   ")
 print("  ■  ■  ■■■■■■■■■■■    ● ■ ● ■  ■   ")
 print("  ■■■■■■■■   ■     ●  ■  ● ■  ■   ")
 print("  ■  ■   ■      ■   ■  ■   ")
 print("  ■  ■   ■      ■   ■  ■   ")
 print("  ■   ■   ■      ■   ■  ■ ■   ")
 print(" ■   ■ ■■■■■■■■■■■■■■    ■     ■   ")
 print("*" * 80)
def input_names():
 print('\033[1;34;m')
 playerNames[0] = input("请输入玩家A的姓名")
 while playerNames[0] == "":
  playerNames[0] = input("玩家A的名字不能为空,请重新输入")
 playerNames[1] = input("请输入玩家B的姓名")
 while playerNames[1] =="" or playerNames[0] == playerNames[1]:
  if playerNames[1] == "":
   playerNames[1] = input("玩家B的名字不能为空,请重新输入")
  else:
   playerNames[1] = input("玩家A的名字不能和玩家B的名字一样,请重新输入")
def a_and_b_plaing():
 while PlayerPos[0] < 99 and PlayerPos[1] < 99:
  if Flags[0] == True:
   playGame(0)
  else:
   Flags[0] = True
 
  if PlayerPos[0] >= 99:
   print("玩家{0}漂亮的赢了玩家{1}".format(playerNames[0], playerNames[1]))
   break
 
  if Flags[1] == True:
   playGame(1)
  else:
   Flags[1] = True
 
  if PlayerPos[1] >= 99:
   print("玩家{0}无耻的赢了玩家{1}".format(playerNames[1], playerNames[0]))
   break
 
# TODO 怎么清空控制台?
 
# 开始游戏
gameshow()
input_names()
print("玩家{0}的姓名用A表示".format(playerNames[0]))
print("玩家{0}的姓名用B表示".format(playerNames[1]))
chushihuamap()
drawmap()
# 玩家A和玩家B 都没有到达终点
a_and_b_plaing()
drawmap()
win()

python实现飞行棋游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python抓取京东商城手机列表url实例代码
Dec 18 Python
Python判断文本中消息重复次数的方法
Apr 27 Python
一张图带我们入门Python基础教程
Feb 05 Python
python学习教程之使用py2exe打包
Sep 24 Python
Python内置函数—vars的具体使用方法
Dec 04 Python
Python将list中的string批量转化成int/float的方法
Jun 26 Python
python3调用百度翻译API实现实时翻译
Aug 16 Python
Python缓存技术实现过程详解
Sep 25 Python
解决pycharm上的jupyter notebook端口被占用问题
Dec 17 Python
Python unittest工作原理和使用过程解析
Feb 24 Python
Python selenium爬取微博数据代码实例
May 22 Python
python字符串的多行输出的实例详解
Jun 08 Python
以SQLite和PySqlite为例来学习Python DB API
Feb 05 #Python
Python操作Sqlite正确实现方法解析
Feb 05 #Python
Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)
Feb 05 #Python
Tensorflow累加的实现案例
Feb 05 #Python
详谈tensorflow gfile文件的用法
Feb 05 #Python
TensorFlow实现从txt文件读取数据
Feb 05 #Python
TensorFlow 读取CSV数据的实例
Feb 05 #Python
You might like
PHP 和 MySQL 基础教程(三)
2006/10/09 PHP
PHP实现的连贯操作、链式操作实例
2014/07/08 PHP
PHP中判断文件存在使用is_file还是file_exists?
2015/04/03 PHP
php线性表的入栈与出栈实例分析
2015/06/12 PHP
PHP获取音频文件的相关信息
2015/06/22 PHP
php7安装mongoDB扩展的方法分析
2017/08/02 PHP
在laravel框架中使用model层的方法
2019/10/08 PHP
CSS和JS标签style属性对照表(方便js开发的朋友)
2010/11/11 Javascript
JavaScript中Number.MAX_VALUE属性的使用方法
2015/06/04 Javascript
实例讲解jquery中mouseleave和mouseout的区别
2016/02/17 Javascript
Angular表单验证实例详解
2016/10/20 Javascript
JavaScript实现获取select下拉框中第一个值的方法
2018/02/06 Javascript
Angularjs实现多图片上传预览功能
2018/07/18 Javascript
在React项目中使用Eslint代码检查工具及常见问题
2018/10/10 Javascript
vue通过指令(directives)实现点击空白处收起下拉框
2018/12/06 Javascript
ES6模板字符串和标签模板的应用实例分析
2019/06/25 Javascript
vue分页器组件编写方法详解
2019/06/28 Javascript
vue+axios实现post文件下载
2019/09/25 Javascript
element-ui 文件上传修改文件名的方法示例
2019/11/05 Javascript
bat和python批量重命名文件的实现代码
2016/05/19 Python
Python 实现引用其他.py文件中的类和类的方法
2018/04/29 Python
python版本单链表实现代码
2018/09/28 Python
Python实现登陆文件验证方法
2018/10/06 Python
解决py2exe打包后,总是多显示一个DOS黑色窗口的问题
2019/06/21 Python
Python+Pyqt实现简单GUI电子时钟
2021/02/22 Python
Python中免验证跳转到内容页的实例代码
2020/10/23 Python
python可视化分析的实现(matplotlib、seaborn、ggplot2)
2021/02/03 Python
Juicy Couture Beauty官方网站:香水和化妆品
2019/03/12 全球购物
Linux管理员面试经常问道的相关命令
2013/04/29 面试题
安全资料员岗位职责
2013/12/14 职场文书
置业顾问岗位职责
2014/03/02 职场文书
关于教师节的演讲稿
2014/09/04 职场文书
网络工程专业大学生求职信
2014/10/01 职场文书
个人租房协议书样本
2014/10/01 职场文书
Windows安装Anaconda3的方法及使用过程详解
2021/06/11 Python
table设置超出部分隐藏,鼠标移上去显示全部内容的方法
2022/12/24 HTML / CSS