python实现简单飞行棋

所属分类: 脚本专栏 / python 阅读数: 443
收藏 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实现图像几何变换的方法,实例分析了Python基于Image模块实现图像翻转、旋转、改变大小等操作的相关技巧,非常简单实用,需要的朋友可以参考下
收藏 0 赞 0 分享

Python中的urllib模块使用详解

这篇文章主要介绍了Python中的urllib模块使用详解,是Python入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

Python的多态性实例分析

这篇文章主要介绍了Python的多态性,以实例形式深入浅出的分析了Python在面向对象编程中多态性的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

python生成IP段的方法

这篇文章主要介绍了python生成IP段的方法,涉及Python文件读写及随机数操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python操作redis的方法

这篇文章主要介绍了python操作redis的方法,包括Python针对redis的连接、设置、获取、删除等常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python妹子图简单爬虫实例

这篇文章主要介绍了python妹子图简单爬虫,实例分析了Python爬虫程序所涉及的页面源码获取、进度显示、正则匹配等技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

分析用Python脚本关闭文件操作的机制

这篇文章主要介绍了分析用Python脚本关闭文件操作的机制,作者分Python2.x版本和3.x版本两种情况进行了阐述,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现搜索指定目录下文件及文件内搜索指定关键词的方法

这篇文章主要介绍了python实现搜索指定目录下文件及文件内搜索指定关键词的方法,可实现针对文件夹及文件内关键词的搜索功能,需要的朋友可以参考下
收藏 0 赞 0 分享

python中getaddrinfo()基本用法实例分析

这篇文章主要介绍了python中getaddrinfo()基本用法,实例分析了Python中使用getaddrinfo方法进行IP地址解析的基本技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python查找指定具有相同内容文件的方法

这篇文章主要介绍了python查找指定具有相同内容文件的方法,涉及Python针对文件操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多