手机版 | 登陆 | 注册 | 留言 | 设首页 | 加收藏
当前位置: 网站首页 > 热门知识 > 文章 当前位置: 热门知识 > 文章

【Python练习】乌龟吃鱼小游戏

时间:2019-11-11    点击: 次    来源:【Python练习】乌龟吃鱼小游戏    作者:【Python练习】乌龟吃鱼小游戏 - 小 + 大

【Python练习】乌龟吃鱼小游戏


最近在学习Python的时候看到一个乌龟吃鱼的练习,下面是我做这个练习的过程。

游戏规则如下:

1. 游戏背景为10*10 
2.
游戏会自动生成1个乌龟和10条鱼; 
3.
它们移动方向随机
4.
乌龟最大移动能力为2 [-2,-1,1,2] 
5.
鱼最大移动能力为1 [-1,1] 
6.
当移动到场景边界,自动反方向移动
7.
乌龟初始化体能为100;每移动一次消耗体能1 
8.
当乌龟和鱼的坐标重合,代表乌龟吃掉鱼,体能增加20 

9. 乌龟无体力或者鱼吃光,游戏结束;

因为乌龟和鱼移动方向是随机的,而且乌龟的移动步长也是随机的,因此引入random模块

import random as r

首先定义一个乌龟类

		
  1. class Tortoise:
  2. step = 2 # 步长
  3. axis = [0, 0] # 初始化坐标
  4. area = [0, 0] # 初始化游戏场景大小
  5. def __init__(self, a, b): # 定义游戏场景大小
  6. self.area[0] = a
  7. self.area[1] = b
  8. def start_axis(self, x = 0, y = 0): # 设定乌龟开始的坐标
  9. self.axis[0] = x
  10. self.axis[1] = y
  11. return self.axis
  12. def move(self): # 乌龟的移动
  13. while True:
  14. temp_x = r.randint(-self.step, self.step)
  15. temp_y = r.randint(-self.step, self.step)
  16. if temp_x == 0 and temp_y == 0:
  17. continue
  18. self.axis[0] = self.axis[0] + temp_x
  19. self.axis[1] = self.axis[1] + temp_y
  20. for i in range(0, 2): # 当移动到边界时往反方向移动
  21. if self.axis[i] < 0: # 0 --> -2 = 0 --> 2
  22. self.axis[i] = 0 - self.axis[i]
  23. if self.axis[i] > 10: # 10 --> 12 = 10 --> 8
  24. self.axis[i] = 20 - self.axis[i]
  25. return self.axis

定义一个鱼类

		
  1. class Fish:
  2. step = 1 # 步长
  3. area = [0, 0] # 初始化坐标
  4. axis = [0, 0] # 初始化游戏场景大小
  5. def __init__(self, a, b): # 定义游戏场景大小
  6. self.area[0] = a
  7. self.area[1] = b
  8. def start_axis(self): # 设定鱼开始的坐标
  9. self.axis[0] = r.randint(self.area[0], self.area[1])
  10. self.axis[1] = r.randint(self.area[0], self.area[1])
  11. return self.axis
  12. def move(self, axis): # 鱼的移动
  13. self.axis = axis
  14. while True:
  15. temp_x = r.randint(-self.step, self.step)
  16. temp_y = r.randint(-self.step, self.step)
  17. if temp_x == 0 and temp_y == 0:
  18. continue
  19. self.axis[0] = self.axis[0] + temp_x
  20. self.axis[1] = self.axis[1] + temp_y
  21. for i in range(0, 2): # 当移动到边界时往反方向移动
  22. if self.axis[i] < 0: # 0 --> -1 = 0 --> 1
  23. self.axis[i] = 0 - self.axis[i]
  24. if self.axis[i] > 10: # 10 --> 11 = 10 --> 9
  25. self.axis[i] = 20 - self.axis[i]
  26. return self.axis

接下来就可以开始写游戏的主程序了,首先,将乌龟和10条鱼放进游戏场景中

		
  1. energy = 100
  2. tort = Tortoise(1, 10)
  3. tort_axis = tort.start_axis(1, 1) # 将乌龟放入场景内,设定初始坐标为(1,1)
  4. print('-----------------------------')
  5. print('乌龟的位置是:',tort_axis)
  6. print('乌龟的体力:%d' % energy)
  7. fish = Fish(1, 10)
  8. fish_axis = {}
  9. flag = 1
  10. while True: # 将10条鱼分别放入不同的坐标中
  11. temp_axis = fish.start_axis()
  12. if flag == 1 and temp_axis != tort_axis:
  13. fish_axis.setdefault(flag, 0)
  14. fish_axis[flag] = temp_axis
  15. print('Fish%d的位置是:' % flag, fish_axis[flag])
  16. flag += 1
  17. if flag > 1 and temp_axis != fish_axis[flag - 1] and temp_axis != tort_axis:
  18. fish_axis.setdefault(flag, 0)
  19. fish_axis[flag] = temp_axis
  20. print('Fish%d的位置是:' % flag, fish_axis[flag])
  21. flag += 1
  22. if flag > 10:
  23. break

然后实现乌龟和鱼在场景中移动,并设立胜利条件

		
  1. while True:
  2. num = list(fish_axis.keys())
  3. length = len(num)
  4. for i in range(0, length): # 小鱼的移动
  5. flag = 0
  6. while True:
  7. temp_axis = fish.move(fish_axis[num[i]])
  8. if i == 0:
  9. fish_axis[num[i]] = temp_axis.copy()
  10. print('fish%d的位置是:' % num[i], fish_axis[num[i]])
  11. flag = 1
  12. elif temp_axis != fish_axis[num[i - 1]]:
  13. # 字典中的前一项而不是i-1
  14. fish_axis[num[i]] = temp_axis.copy()
  15. print('fish%d的位置是:' % num[i], fish_axis[num[i]])
  16. flag = 1
  17. if flag == 1:
  18. break
  19. tort_axis = tort.move() # 乌龟的移动
  20. print('乌龟的位置是:',tort_axis)
  21. energy -= 1
  22. # 判断小鱼是否被乌龟吃掉
  23.     for i in fish_axis:
  24.         if fish_axis[i] == tort_axis:
  25.         print('fish%s被吃掉' % i)
  26.         fish_axis.pop(i)
  27.             print('乌龟回复20点体力')
  28.             energy += 20
  29. print('乌龟的体力:%d' % energy)
  30. print('-----------------------------')
  31. if fish_axis == {}:
  32. print('乌龟获胜')
  33. break
  34. elif energy == 0:
  35. print('小鱼获胜')
  36. break

 完成以后开始运行,结果......居然进入了死循环!!!抓狂

用ctrl+c终结程序后观察错误信息发现错误 temp_axis = fish.start_axis()位置

		
  1. Traceback (most recent call last):
  2. File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\example\test_37_1.py", line 64, in <module>
  3. temp_axis = fish.start_axis()
  4. File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\example\test_37_1.py", line 36, in start_axis
  5. self.axis[1] = random.randint(self.area[0], self.area[1])
  6. File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\random.py", line 220, in randint
  7. return self.randrange(a, b+1)
  8. File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\random.py", line 196, in randrange
  9. return istart + self._randbelow(width)
  10. File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\random.py", line 233, in _randbelow
  11. while r >= n:
  12. KeyboardInterrupt

 在仔细观察错误所在位置的程序发现错误位置为下面划线部分,原因是我使用的是赋值语句,这就说明fish_axis[flag]和temp_axis的地址是一样的,也就是说在temp_axis = fish.start_axis()改变temp_axis的值时fish_axis[flag]的值也改变了,因此if语句中的条件temp_axis != fish_axis[flag - 1]永远不成立,while语句进入死循环。

		
  1. while True: # 将10条鱼分别放入不同的坐标中
  2. temp_axis = fish.start_axis()
  3. if flag == 1 and temp_axis != tort_axis:
  4. fish_axis.setdefault(flag, 0)
  5. fish_axis[flag] = temp_axis
  6. print('Fish%d的位置是:' % flag, fish_axis[flag])
  7. flag += 1
  8. if flag > 1 and temp_axis != fish_axis[flag - 1] and temp_axis != tort_axis:
  9. fish_axis.setdefault(flag, 0)
  10. fish_axis[flag] = temp_axis
  11. print('Fish%d的位置是:' % flag, fish_axis[flag])
  12. flag += 1
  13. if flag > 10:
  14. break

于是我将赋值语句改为浅复制fish_axis[flag] = temp_axis.copy(),这然fish_axis[flag]的地址和temp_axis的地址就不一样了。

解决了这个问题之后,我又运行了一遍,结果又又又报错了再见

		
  1. Traceback (most recent call last):
  2. File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\example\test_37_1.py", line 125, in <module>
  3. for i in fish_axis:
  4. RuntimeError: dictionary changed size during iteration

错误信息显示错误在for i in fish_axis:语句中,错误类型为RuntimeError,在反反复复前前后后看了几遍之后还是看不出来有什么地方不对,只好求助万能的度娘,在大佬的指导下终于发现了错误。

		
  1. for i in fish_axis:
  2.     if fish_axis[i] == tort_axis:
  3.         print('fish%s被吃掉' % i)
  4.         fish_axis.pop(i)
  5.         print('乌龟回复20点体力')
  6.         energy += 20

上面这种写法在python2中是可与运行的,但是在python3中就会报错,因为在python3中keys()返回的是dict_keys而不是list此如果想遍历的同时删除字典中的元素应该改写为:

		
  1. for i in list(fish_axis.keys()):
  2. if fish_axis[i] == tort_axis:
  3.     print('fish%s被吃掉' % i)
  4.     fish_axis.pop(i)
  5.     print('乌龟回复20点体力')
  6.     energy += 20

这个问题解决之后终于可以运行了,下面附上完整的程序。

		
  1. import random as r
  2. class Tortoise:
  3. step = 2 # 步长
  4. axis = [0, 0] # 初始化坐标
  5. area = [0, 0] # 初始化游戏场景大小
  6. def __init__(self, a, b): # 定义游戏场景大小
  7. self.area[0] = a
  8. self.area[1] = b
  9. def start_axis(self, x = 0, y = 0): # 设定乌龟开始的坐标
  10. self.axis[0] = x
  11. self.axis[1] = y
  12. return self.axis
  13. def move(self): # 乌龟的移动
  14. while True:
  15. temp_x = r.randint(-self.step, self.step)
  16. temp_y = r.randint(-self.step, self.step)
  17. if temp_x == 0 and temp_y == 0:
  18. continue
  19. self.axis[0] = self.axis[0] + temp_x
  20. self.axis[1] = self.axis[1] + temp_y
  21. for i in range(0, 2): # 当移动到边界时往反方向移动
  22. if self.axis[i] < 0: # 0 --> -2 = 0 --> 2
  23. self.axis[i] = 0 - self.axis[i]
  24. if self.axis[i] > 10: # 10 --> 12 = 10 --> 8
  25. self.axis[i] = 20 - self.axis[i]
  26. return self.axis
  27. class Fish:
  28. step = 1 # 步长
  29. area = [0, 0] # 初始化坐标
  30. axis = [0, 0] # 初始化游戏场景大小
  31. def __init__(self, a, b): # 定义游戏场景大小
  32. self.area[0] = a
  33. self.area[1] = b
  34. def start_axis(self): # 设定鱼开始的坐标
  35. self.axis[0] = r.randint(self.area[0], self.area[1])
  36. self.axis[1] = r.randint(self.area[0], self.area[1])
  37. return self.axis
  38. def move(self, axis): # 鱼的移动
  39. self.axis = axis
  40. while True:
  41. temp_x = r.randint(-self.step, self.step)
  42. temp_y = r.randint(-self.step, self.step)
  43. if temp_x == 0 and temp_y == 0:
  44. continue
  45. self.axis[0] = self.axis[0] + temp_x
  46. self.axis[1] = self.axis[1] + temp_y
  47. for i in range(0, 2): # 当移动到边界时往反方向移动
  48. if self.axis[i] < 0: # 0 --> -1 = 0 --> 1
  49. self.axis[i] = 0 - self.axis[i]
  50. if self.axis[i] > 10: # 10 --> 11 = 10 --> 9
  51. self.axis[i] = 20 - self.axis[i]
  52. return self.axis
  53. # 游戏开始
  54. energy = 100
  55. tort = Tortoise(1, 10)
  56. tort_axis = tort.start_axis(1, 1) # 将乌龟放入场景内,设定初始坐标为(1,1)
  57. print('-----------------------------')
  58. print('乌龟的位置是:',tort_axis)
  59. print('乌龟的体力:%d' % energy)
  60. fish = Fish(1, 10)
  61. fish_axis = {}
  62. flag = 1
  63. while True: # 将10条鱼分别放入不同的坐标中
  64. temp_axis = fish.start_axis()
  65. if flag == 1 and temp_axis != tort_axis:
  66. fish_axis.setdefault(flag, 0)
  67. fish_axis[flag] = temp_axis.copy()
  68. print('Fish%d的位置是:' % flag, fish_axis[flag])
  69. flag += 1
  70. if flag > 1 and temp_axis != fish_axis[flag - 1] and temp_axis != tort_axis:
  71. fish_axis.setdefault(flag, 0)
  72. fish_axis[flag] = temp_axis.copy()
  73. print('Fish%d的位置是:' % flag, fish_axis[flag])
  74. flag += 1
  75. if flag > 10:
  76. break
  77. print('----------游戏开始-----------')
  78. # 乌龟和小鱼的移动
  79. while True:
  80. num = list(fish_axis.keys())
  81. length = len(num)
  82. for i in range(0, length): # 小鱼的移动
  83. flag = 0
  84. while True:
  85. temp_axis = fish.move(fish_axis[num[i]])
  86. if i == 0:
  87. fish_axis[num[i]] = temp_axis.copy()
  88. print('fish%d的位置是:' % num[i], fish_axis[num[i]])
  89. flag = 1
  90. elif temp_axis != fish_axis[num[i - 1]]:
  91. # 字典中的前一项而不是i-1
  92. fish_axis[num[i]] = temp_axis.copy()
  93. print('fish%d的位置是:' % num[i], fish_axis[num[i]])
  94. flag = 1
  95. if flag == 1:
  96. break
  97. tort_axis = tort.move() # 乌龟的移动
  98. print('乌龟的位置是:',tort_axis)
  99. energy -= 1
  100. #-------------------------
  101. # 判断小鱼是否被乌龟吃掉
  102. for i in num:
  103. if fish_axis[i] == tort_axis:
  104. print('fish%s被吃掉' % i)
  105. fish_axis.pop(i)
  106. print('乌龟回复20点体力')
  107. energy += 20
  108. print('乌龟的体力:%d' % energy)
  109. #-------------------------
  110. print('-----------------------------')
  111. if fish_axis == {}:
  112. print('乌龟获胜')
  113. break
  114. elif energy == 0:
  115. print('小鱼获胜')
  116. break

上一篇:850棋牌计算机器码的研究

下一篇:游戏中几种概率设计

 别浪费都给你吧懒得弄电脑来到宁波懒得弄便利店内懒得弄 哪来的呢懒得弄老地方能力的看法卡积分换员不是卡八VB不不大好说是 时空金币是那伤口局部深V不睡觉 100是开放道具卡类风湿VB你没收到是是你
备案5589485-ICP编号  |   QQ:微信:SL49568  |  地址:北京市东城区  |  电话:186-5901-6237  |