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函数编程的一些特性
Apr 13 Python
python使用pil生成图片验证码的方法
May 08 Python
Python 专题五 列表基础知识(二维list排序、获取下标和处理txt文本实例)
Mar 20 Python
Python实现将sqlite数据库导出转成Excel(xls)表的方法
Jul 17 Python
Python编程实现的图片识别功能示例
Aug 03 Python
TensorFlow实现非线性支持向量机的实现方法
Apr 28 Python
python numpy存取文件的方式
Apr 01 Python
python图的深度优先和广度优先算法实例分析
Oct 26 Python
tensorflow 获取所有variable或tensor的name示例
Jan 04 Python
python实现四人制扑克牌游戏
Apr 22 Python
python实现飞船游戏的纵向移动
Apr 24 Python
Python 解析简单的XML数据
Jul 24 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 截取字符串 分别适合GB2312和UTF8编码情况
2009/02/12 PHP
供参考的 php 学习提高路线分享
2011/10/23 PHP
thinkphp 中的volist标签在ajax操作中的特殊性(推荐)
2018/01/15 PHP
Laravel 实现密码重置功能
2018/02/23 PHP
PHP 构造函数和析构函数原理与用法分析
2020/04/21 PHP
Prototype 学习 Prototype对象
2009/07/12 Javascript
Javascript将string类型转换int类型
2010/12/09 Javascript
jquery 选项卡效果 新手代码
2011/07/08 Javascript
jQuery性能优化28条建议你值得借鉴
2013/02/16 Javascript
extJS中常用的4种Ajax异步提交方式
2014/03/07 Javascript
js过滤特殊字符输入适合输入、粘贴、拖拽多种情况
2014/03/22 Javascript
jQuery实用函数用法总结
2014/08/29 Javascript
node.js中的buffer.copy方法使用说明
2014/12/14 Javascript
使用jQuery仿苹果官网焦点图特效
2014/12/23 Javascript
jquery.Jcrop结合JAVA后台实现图片裁剪上传实例
2016/11/05 Javascript
javascript滚轮事件基础实例讲解(37)
2017/02/14 Javascript
浅谈React中组件逻辑复用的那些事儿
2020/05/21 Javascript
python实现每次处理一个字符的三种方法
2014/10/09 Python
python动态性强类型用法实例
2015/05/09 Python
Python正则表达式教程之一:基础篇
2017/03/02 Python
Python面向对象编程基础解析(二)
2017/10/26 Python
替换python字典中的key值方法
2018/07/06 Python
python隐藏终端执行cmd命令的方法
2019/06/24 Python
Python numpy线性代数用法实例解析
2019/11/15 Python
Python Tkinter模块 GUI 可视化实例
2019/11/20 Python
python学习将数据写入文件并保存方法
2020/06/07 Python
Python enumerate() 函数如何实现索引功能
2020/06/29 Python
Python中logger日志模块详解
2020/08/04 Python
Visual Studio code 配置Python开发环境
2020/09/11 Python
Bloomingdale’s阿联酋:选购奢华时尚、美容及更多
2020/09/22 全球购物
remote接口和home接口主要作用
2013/05/15 面试题
网络工程师个人的自我评价范文
2013/10/01 职场文书
乡镇网格化管理实施方案
2014/03/23 职场文书
元宵节晚会主持人串词
2014/03/25 职场文书
背起爸爸上学观后感
2015/06/08 职场文书
Python编写冷笑话生成器
2022/04/20 Python