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中装饰器的一个妙用
Feb 08 Python
Python编程实战之Oracle数据库操作示例
Jun 21 Python
python smtplib模块实现发送邮件带附件sendmail
May 22 Python
Python把csv数据写入list和字典类型的变量脚本方法
Jun 15 Python
python保存数据到本地文件的方法
Jun 23 Python
pandas数据集的端到端处理
Feb 18 Python
如何用C代码给Python写扩展库(Cython)
May 17 Python
使用python模拟命令行终端的示例
Aug 13 Python
Python通过cv2读取多个USB摄像头
Aug 28 Python
分享一个pycharm专业版安装的永久使用方法
Sep 24 Python
python如何进行基准测试
Apr 26 Python
详解Python魔法方法之描述符类
May 26 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获得用户使用的代理服务器ip即真实ip
2006/12/31 PHP
实用函数5
2007/11/08 PHP
PHP 递归效率分析
2009/11/24 PHP
微博短链接算法php版本实现代码
2012/09/15 PHP
Laravel find in set排序实例
2019/10/09 PHP
php使用fputcsv实现大数据的导出操作详解
2020/02/27 PHP
js 判断一个元素是否在页面中存在
2012/12/27 Javascript
jquery写个checkbox——类似邮箱全选功能
2013/03/19 Javascript
javascript中的nextSibling使用陷(da)阱(keng)
2014/05/05 Javascript
SpringMVC返回json数据的三种方式
2015/12/10 Javascript
AngularJS Ajax详解及示例代码
2016/08/17 Javascript
jQuery实现QQ空间汉字转拼音功能示例
2017/07/10 jQuery
JavaScript防止全局变量污染的方法总结
2018/08/02 Javascript
Bootstrap导航菜单点击后无法自动添加active的处理方法
2018/08/10 Javascript
Vue检测屏幕变化来改变不同的charts样式实例
2020/10/26 Javascript
[51:07]VGJ.S vs Pain 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
用实例解释Python中的继承和多态的概念
2015/04/27 Python
Python内建数据结构详解
2016/02/03 Python
Python定时任务工具之APScheduler使用方式
2019/07/24 Python
python基于三阶贝塞尔曲线的数据平滑算法
2019/12/27 Python
浅谈keras使用预训练模型vgg16分类,损失和准确度不变
2020/07/02 Python
python实现AdaBoost算法的示例
2020/10/03 Python
带有css3动画效果的兼容多浏览器简单导航条示例
2014/01/26 HTML / CSS
澳大利亚的奢侈品牌:Oroton
2016/08/26 全球购物
Move Free官方海外旗舰店:美国骨关节健康专业品牌
2017/12/06 全球购物
迪卡侬印尼体育用品商店:Decathlon印尼
2020/03/11 全球购物
薇姿法国官网:Vichy法国
2021/01/28 全球购物
教师应聘个人求职信
2013/12/10 职场文书
秘书英文求职信范文
2014/01/31 职场文书
另类冲刺标语
2014/06/24 职场文书
婚礼庆典答谢词
2015/01/20 职场文书
2016新年感言
2015/08/03 职场文书
python 实现定时任务的四种方式
2021/04/01 Python
Javascript中的解构赋值语法详解
2021/04/02 Javascript
用 Python 元类的特性实现 ORM 框架
2021/05/19 Python
浅谈Python numpy创建空数组的问题
2021/05/25 Python