当前位置:   article > 正文

Python实战:编写生命游戏1

Python实战:编写生命游戏1

这是一个Python3.11及以上版本的实战练习

1,构建框架

首先要知道,全屏python一行最多能装237个0,那以237作为一行的字符数非常恰当

我们先要设置出长和宽(不用)

将宽设为50是我的选择,当然你也可以改

那么需要设置50个列表(或集合)

  1. l1 = [0] * 237
  2. l2 = [0] * 237
  3. l3 = [0] * 237
  4. l4 = [0] * 237
  5. l5 = [0] * 237
  6. l6 = [0] * 237
  7. l7 = [0] * 237
  8. l8 = [0] * 237
  9. l9 = [0] * 237
  10. l10 = [0] * 237
  11. l11 = [0] * 237
  12. l12 = [0] * 237
  13. l13 = [0] * 237
  14. l14 = [0] * 237
  15. l15 = [0] * 237
  16. l16 = [0] * 237
  17. l17 = [0] * 237
  18. l18 = [0] * 237
  19. l19 = [0] * 237
  20. l20 = [0] * 237
  21. l21 = [0] * 237
  22. l22 = [0] * 237
  23. l23 = [0] * 237
  24. l24 = [0] * 237
  25. l25 = [0] * 237
  26. l26 = [0] * 237
  27. l27 = [0] * 237
  28. l28 = [0] * 237
  29. l29 = [0] * 237
  30. l30 = [0] * 237
  31. l31 = [0] * 237
  32. l32 = [0] * 237
  33. l33 = [0] * 237
  34. l34 = [0] * 237
  35. l35 = [0] * 237
  36. l36 = [0] * 237
  37. l37 = [0] * 237
  38. l38 = [0] * 237
  39. l39 = [0] * 237
  40. l40 = [0] * 237
  41. l41 = [0] * 237
  42. l42 = [0] * 237
  43. l43 = [0] * 237
  44. l44 = [0] * 237
  45. l45 = [0] * 237
  46. l46 = [0] * 237
  47. l47 = [0] * 237
  48. l48 = [0] * 237
  49. l49 = [0] * 237
  50. l50 = [0] * 237

好了,现在长和宽都有了,终于可以构造索引器(必须要的,不然你在编写patch的时候就会有困难)了

都说了版本是3.11及以上,那么有match...case的python就好弄了

在这里,我们将使用def语句

  1. def ln(num):
  2. match num:
  3. case 1:
  4. return l1
  5. case 2:
  6. return l2
  7. case 3:
  8. return l3
  9. case 4:
  10. return l4
  11. case 5:
  12. return l5
  13. case 6:
  14. return l6
  15. case 7:
  16. return l7
  17. case 8:
  18. return l8
  19. case 9:
  20. return l9
  21. case 10:
  22. return l10
  23. case 11:
  24. return l11
  25. case 12:
  26. return l12
  27. case 13:
  28. return l13
  29. case 14:
  30. return l14
  31. case 15:
  32. return l15
  33. case 16:
  34. return l16
  35. case 17:
  36. return l17
  37. case 18:
  38. return l18
  39. case 19:
  40. return l19
  41. case 20:
  42. return l20
  43. case 21:
  44. return l21
  45. case 22:
  46. return l22
  47. case 23:
  48. return l23
  49. case 24:
  50. return l24
  51. case 25:
  52. return l25
  53. case 26:
  54. return l26
  55. case 27:
  56. return l27
  57. case 28:
  58. return l28
  59. case 29:
  60. return l29
  61. case 30:
  62. return l30
  63. case 31:
  64. return l31
  65. case 32:
  66. return l32
  67. case 33:
  68. return l33
  69. case 34:
  70. return l34
  71. case 35:
  72. return l35
  73. case 36:
  74. return l36
  75. case 37:
  76. return l37
  77. case 38:
  78. return l38
  79. case 39:
  80. return l39
  81. case 40:
  82. return l40
  83. case 41:
  84. return l41
  85. case 42:
  86. return l42
  87. case 43:
  88. return l43
  89. case 44:
  90. return l44
  91. case 45:
  92. return l45
  93. case 46:
  94. return l46
  95. case 47:
  96. return l47
  97. case 48:
  98. return l48
  99. case 49:
  100. return l49
  101. case 50:
  102. return l50
  103. case _:
  104. print('Not found the list.')

我还在后面加了个错误提示(在编辑patch时有帮助)

2,编写屏幕

当然,屏幕还是必不可少的元素之一,编写它最好用的语句就是for和print

首先要遍历长,而长的索引是ln,所以要用range

可编写出以下代码:

for w in range(1,51):

这样,我们就获得了1至50的所有数

那么这是要干吗呢?当然是用上ln函数啦!!!

我们要用for函数遍历ln函数索引出来的列表

那么就可以这样:

for cell in ln(w):

好了,现在每个字符我们都了解清楚了,它们就可以用print写出来了

全部代码:

  1. for w in range(1,51):
  2. for cell in ln(w):
  3. print(cell, end="")

但是请注意,他最后一个字符后面不会换行,若屏幕缩小,细胞就会错位,所以,要在第二个for后面加else:

  1. for cell in ln(w):
  2. print(cell, end="")
  3. else:
  4. print()

3,总结

本博客介绍了在python中构建生命游戏基础框架和屏幕的方法,虽然有可能有问题

全部代码:

  1. l1 = [0] * 237
  2. l2 = [0] * 237
  3. l3 = [0] * 237
  4. l4 = [0] * 237
  5. l5 = [0] * 237
  6. l6 = [0] * 237
  7. l7 = [0] * 237
  8. l8 = [0] * 237
  9. l9 = [0] * 237
  10. l10 = [0] * 237
  11. l11 = [0] * 237
  12. l12 = [0] * 237
  13. l13 = [0] * 237
  14. l14 = [0] * 237
  15. l15 = [0] * 237
  16. l16 = [0] * 237
  17. l17 = [0] * 237
  18. l18 = [0] * 237
  19. l19 = [0] * 237
  20. l20 = [0] * 237
  21. l21 = [0] * 237
  22. l22 = [0] * 237
  23. l23 = [0] * 237
  24. l24 = [0] * 237
  25. l25 = [0] * 237
  26. l26 = [0] * 237
  27. l27 = [0] * 237
  28. l28 = [0] * 237
  29. l29 = [0] * 237
  30. l30 = [0] * 237
  31. l31 = [0] * 237
  32. l32 = [0] * 237
  33. l33 = [0] * 237
  34. l34 = [0] * 237
  35. l35 = [0] * 237
  36. l36 = [0] * 237
  37. l37 = [0] * 237
  38. l38 = [0] * 237
  39. l39 = [0] * 237
  40. l40 = [0] * 237
  41. l41 = [0] * 237
  42. l42 = [0] * 237
  43. l43 = [0] * 237
  44. l44 = [0] * 237
  45. l45 = [0] * 237
  46. l46 = [0] * 237
  47. l47 = [0] * 237
  48. l48 = [0] * 237
  49. l49 = [0] * 237
  50. l50 = [0] * 237
  51. def ln(num):
  52. match num:
  53. case 1:
  54. return l1
  55. case 2:
  56. return l2
  57. case 3:
  58. return l3
  59. case 4:
  60. return l4
  61. case 5:
  62. return l5
  63. case 6:
  64. return l6
  65. case 7:
  66. return l7
  67. case 8:
  68. return l8
  69. case 9:
  70. return l9
  71. case 10:
  72. return l10
  73. case 11:
  74. return l11
  75. case 12:
  76. return l12
  77. case 13:
  78. return l13
  79. case 14:
  80. return l14
  81. case 15:
  82. return l15
  83. case 16:
  84. return l16
  85. case 17:
  86. return l17
  87. case 18:
  88. return l18
  89. case 19:
  90. return l19
  91. case 20:
  92. return l20
  93. case 21:
  94. return l21
  95. case 22:
  96. return l22
  97. case 23:
  98. return l23
  99. case 24:
  100. return l24
  101. case 25:
  102. return l25
  103. case 26:
  104. return l26
  105. case 27:
  106. return l27
  107. case 28:
  108. return l28
  109. case 29:
  110. return l29
  111. case 30:
  112. return l30
  113. case 31:
  114. return l31
  115. case 32:
  116. return l32
  117. case 33:
  118. return l33
  119. case 34:
  120. return l34
  121. case 35:
  122. return l35
  123. case 36:
  124. return l36
  125. case 37:
  126. return l37
  127. case 38:
  128. return l38
  129. case 39:
  130. return l39
  131. case 40:
  132. return l40
  133. case 41:
  134. return l41
  135. case 42:
  136. return l42
  137. case 43:
  138. return l43
  139. case 44:
  140. return l44
  141. case 45:
  142. return l45
  143. case 46:
  144. return l46
  145. case 47:
  146. return l47
  147. case 48:
  148. return l48
  149. case 49:
  150. return l49
  151. case 50:
  152. return l50
  153. case _:
  154. print('Not found the list.')
  155. for w in range(1,51):
  156. for cell in ln(w):
  157. print(cell, end="")
  158. else:
  159. print()

输出:

  1. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  2. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  3. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  4. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  5. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  6. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  7. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  8. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  9. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  10. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  11. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  12. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  13. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  14. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  15. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  16. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  17. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  18. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  19. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  20. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  21. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  22. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  23. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  24. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  25. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  26. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  27. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  28. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  29. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  30. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  31. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  32. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  33. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  34. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  35. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  36. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  37. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  38. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  39. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  40. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  41. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  42. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  43. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  44. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  45. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  46. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  47. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  48. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  49. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  50. 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

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

闽ICP备14008679号