python实现简单飞行棋

所属分类: 脚本专栏 / python 阅读数: 447
收藏 0 赞 0 分享

本人刚开始学习python,看了一段时间视频教程之后,决定做一个小游戏来巩固一下知识点,就做了一个文字版飞行棋,暂不具备图形界面。把代码贴出来,给自己留个标记。

chess_main.py

import chess_tools

# 输入玩家信息
chess_tools.input_player()

while chess_tools.end_flag:
  for player in chess_tools.player_list:
    chess_tools.ironman(player)
    input(player["name"]+",请按enter掷骰子")
    chess_tools.throw(player)
    if chess_tools.judge(player) == 1:
      break
    chess_tools.hit(player)
    chess_tools.speedup(player)
    print("")

chess_tools.py

from random import randint


player_list = []
end_flag = 1


def input_player():
  num = input("数据玩家数量:")
  i = 0
  while i < int(num):
    player_name = input("输入第" + str(i + 1) + "位玩家名字:")
    play_info = {"name": player_name,
           "score": 0}
    player_list.append(play_info)
    i = i + 1


def throw(player):
  """
  玩家扔骰子,随机1-6点
  :param player: 当前玩家
  :return:
  """
  points = randint(1,6)

  # 首先判定是否起飞
  if player["score"] == 0 and points == 6:
    player["score"] = 1
    print("%d 点,恭喜起飞!当前在第%d格" % (points, player["score"]))

  elif player["score"] == 0 and points < 6:
    print("%d 点,起飞失败!" % points)
    return
  # 分数大于100,要后退,多几分退几步
  elif player["score"] + points > 100:
    player["score"] = player["score"] - (player["score"]+ points) % 100 + 1
    print("%d 点,飞过头了!回到%d格" % (points,player["score"]))
  else:
    player["score"] += points
    print("%d 点!当前在第%d格" % (points, player["score"]))


def judge(player):
  global end_flag
  if player["score"] == 100:
    end_flag = 0
    print(player["name"]+"赢了")
    return 1


def hit(player):
  """
  判断当前玩家是否会将领先的玩家撞回起飞位置
  :param player: 当前玩家名字
  """
  for other_player in player_list:
    if player["score"] == other_player["score"] \
        and other_player["name"] != player["name"]:
      other_player["score"] = 0


def speedup(player):
  if player["score"] == 15 or \
      player["score"] == 35 or \
      player["score"] == 85:
    player["score"] += 5
    print("加速5格,当前在%d格" % player["score"])


def ironman(player):
  """
  主角光环,名字中含有指定字符的人可以获得50分加成
  :param player:当前玩家的名字
  """
  master = player["name"].count("t")
  if master > 0 and player["score"] == 0:
    player["score"] = 50
    print("- I am Iron Man!贾维斯,先给我加50分。")
    print("- 好的,%s 。当前已走到第50格。" % player["name"])
    print("")

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

更多精彩内容其他人还在看

Python实现按学生年龄排序的实际问题详解

这篇文章主要给大家介绍了关于Python实现按学生年龄排序实际问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
收藏 0 赞 0 分享

Python开发的HTTP库requests详解

Requests是用Python语言编写,基于urllib,采用Apache2 Licensed开源协议的HTTP库。它比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求。Requests的哲学是以PEP 20 的习语为中心开发的,所以它比urllib更加P
收藏 0 赞 0 分享

Python网络爬虫与信息提取(实例讲解)

下面小编就为大家带来一篇Python网络爬虫与信息提取(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

在python3环境下的Django中使用MySQL数据库的实例

下面小编就为大家带来一篇在python3环境下的Django中使用MySQL数据库的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python 3.x读写csv文件中数字的方法示例

在我们日常开发中经常需要对csv文件进行读写,下面这篇文章主要给大家介绍了关于Python 3.x读写csv文件中数字的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
收藏 0 赞 0 分享

Python实现解析Bit Torrent种子文件内容的方法

这篇文章主要介绍了Python实现解析Bit Torrent种子文件内容的方法,结合实例形式分析了Python针对Torrent文件的读取与解析相关操作技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Python实现文件内容批量追加的方法示例

这篇文章主要介绍了Python实现文件内容批量追加的方法,结合实例形式分析了Python文件的读写相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python简单实现自动删除目录下空文件夹的方法

这篇文章主要介绍了Python简单实现自动删除目录下空文件夹的方法,涉及Python针对文件与目录的读取、判断、删除等相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

简单学习Python多进程Multiprocessing

这篇文章主要和大家一起简单的学习Python多进程Multiprocessing ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Python导入模块时遇到的错误分析

这篇文章主要给大家详细解释了在Python处理导入模块的时候出现错误以及具体的情况分析,非常的详尽,有需要的小伙伴可以参考下
收藏 0 赞 0 分享
查看更多