当前位置:   article > 正文

python做本地双人井字旗游戏(不需要pygame,网络连接)_python小游戏不用pygame

python小游戏不用pygame

简单,111行代码。

就是打印区,判断区,debug区,输入区和初始化。

废话不多说,代码!

  1. d = open('debug.dat', 'rb').read().decode()
  2. debug = bool(d)
  3. if d != 'True' and d != 'False':
  4. debug = False
  5. list = [[None, None, None],[None, None, None],[None, None, None]]
  6. def __init__():
  7. global list
  8. global debug
  9. global d
  10. d = open('debug.dat', 'rb').read().decode()
  11. debug = bool(d)
  12. if d != 'True' and d != 'False':
  13. debug = False
  14. list = [[None, None, None], [None, None, None], [None, None, None]]
  15. def ying(b):
  16. if b:
  17. name = 'X'
  18. elif b is False:
  19. name = 'O'
  20. else:
  21. if debug:
  22. print('debug::错误!\n')
  23. return 0
  24. print(f'{name}赢了!')
  25. print('下一局...\n\n')
  26. __init__()
  27. def main():
  28. while True:
  29. # 行列输入
  30. for i in range(2):
  31. while True:
  32. ni = None
  33. if i == 0:
  34. ni = 'O'
  35. elif i == 1:
  36. ni = 'X'
  37. else:
  38. if debug:
  39. print('debug::错误!')
  40. a_ = input(ni + " 输入行/列:")
  41. a = a_.split('/')
  42. try:
  43. n = int(a[0]) - 1, int(a[1]) - 1
  44. if list[n[0]][n[1]] is not False and not list[n[0]][n[1]]:
  45. if n[0] >= 0 and n[1] >= 0:
  46. list[n[0]][n[1]] = bool(i)
  47. else:
  48. print('超出范围!')
  49. continue
  50. else:
  51. print('已有子')
  52. continue
  53. break
  54. except IndexError:
  55. print('超出范围!')
  56. continue
  57. except ValueError:
  58. print('字符错误!应为digit/digit,而不是' + a_ + '.')
  59. continue
  60. # 判断是否赢
  61. for i in range(len(list)):
  62. # 横
  63. if list[i][0] == list[i][1] == list[i][2] and list[i][0] is not None and list[i][1] is not None and list[i][2] is not None:
  64. ying(list[i][0])
  65. if debug:
  66. print('debug::横')
  67. continue
  68. # 竖
  69. elif list[0][i] == list[1][i] == list[2][i] and list[0][i] is not None and list[1][i] is not None and list[2][i] is not None:
  70. ying(list[0][i])
  71. if debug:
  72. print('debug::竖')
  73. continue
  74. # 右下斜
  75. elif list[0][0] == list[1][1] == list[2][2] and list[0][0] is not None and list[1][1] is not None and list[2][2] is not None:
  76. ying(list[0][0])
  77. if debug:
  78. print('debug::右下')
  79. continue
  80. # 左上斜
  81. elif list[0][2] == list[1][1] == list[2][0] and list[0][2] is not None and list[1][1] is not None and list[0][2] is not None:
  82. ying(list[0][2])
  83. if debug:
  84. print('debug::左上')
  85. continue
  86. if debug:
  87. print('debug::' + list)
  88. # 打印井字旗
  89. print('-------')
  90. for i in list:
  91. print('|', end = '')
  92. for i2 in i:
  93. if i2:
  94. print('X', end = '|')
  95. elif i2 is False:
  96. print('O', end = '|')
  97. else:
  98. print(' ', end = '|')
  99. print('\n-------')
  100. # 判断是否平局
  101. p = True
  102. for i in list:
  103. for i2 in i:
  104. if not i2 or i2 is not False:
  105. p = False
  106. break
  107. if p:
  108. print('平局!')
  109. __init__()
  110. if __name__ == '__main__':
  111. main()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/386698
推荐阅读
相关标签
  

闽ICP备14008679号