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开发之字符串string操作方法实例详解
Nov 12 Python
Python使用设计模式中的责任链模式与迭代器模式的示例
Mar 02 Python
python if not in 多条件判断代码
Sep 21 Python
Python黑帽编程 3.4 跨越VLAN详解
Sep 28 Python
CentOS 7下Python 2.7升级至Python3.6.1的实战教程
Jul 06 Python
Python通过future处理并发问题
Oct 17 Python
python中requests库session对象的妙用详解
Oct 30 Python
Python通用循环的构造方法实例分析
Dec 19 Python
使用Python画股票的K线图的方法步骤
Jun 28 Python
python实现图像检索的三种(直方图/OpenCV/哈希法)
Aug 08 Python
python统计指定目录内文件的代码行数
Sep 19 Python
python单向循环链表原理与实现方法示例
Dec 03 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系统流量分析的程序
2006/10/09 PHP
2014年最新推荐的10款 PHP 开发框架
2014/08/01 PHP
给WordPress的编辑后台添加提示框的代码实例分享
2015/12/25 PHP
PHP的Yii框架入门使用教程
2016/02/15 PHP
超级简单的图片防盗(HTML),好用
2007/04/08 Javascript
HTML中事件触发列表与解说
2007/07/09 Javascript
myEvent.js javascript跨浏览器事件框架
2011/10/24 Javascript
sencha touch 模仿tabpanel导航栏TabBar的实例代码
2013/10/24 Javascript
JS使用parseInt解析数字实现求和的方法
2015/08/05 Javascript
js实现上一页下一页的效果【附代码】
2016/03/10 Javascript
简单实现JS倒计时效果
2016/12/23 Javascript
JavaScript利用Date实现简单的倒计时实例
2017/01/12 Javascript
JS实现的tab切换选项卡效果示例
2017/02/28 Javascript
深入理解AngularJs-scope的脏检查(一)
2017/06/19 Javascript
深入探索VueJS Scoped CSS 实现原理
2019/09/23 Javascript
python缩进区别分析
2014/02/15 Python
python检查序列seq是否含有aset中项的方法
2015/06/30 Python
python统计日志ip访问数的方法
2015/07/06 Python
python实现网站用户名密码自动登录功能
2019/08/09 Python
关于python字符串方法分类详解
2019/08/20 Python
python列表推导式入门学习解析
2019/12/02 Python
Python GUI编程学习笔记之tkinter中messagebox、filedialog控件用法详解
2020/03/30 Python
css3简单练习实现遨游浏览器logo的绘制
2013/01/30 HTML / CSS
荷兰网上药店:Drogisterij.net
2019/09/03 全球购物
Parfumdreams芬兰:购买香水和化妆品
2021/02/13 全球购物
住宿生擅自离校检讨书
2014/09/22 职场文书
人身意外保险授权委托书
2014/10/01 职场文书
医学专业大学生职业生涯规划书
2014/10/25 职场文书
五四青年节比赛演讲稿
2015/03/18 职场文书
2015年酒店工作总结范文
2015/04/07 职场文书
社区义诊通知
2015/04/24 职场文书
2015年库房工作总结
2015/04/30 职场文书
《1942》观后感
2015/06/08 职场文书
车间安全生产管理制度
2015/08/06 职场文书
Python使用random模块实现掷骰子游戏的示例代码
2021/04/29 Python
Python中OpenCV实现查找轮廓的实例
2021/06/08 Python