|
1鱼币
import random
class wugui():
x_list1 = [-2,-1,1,2]
y_list1 = [-2,-1,1,2]
life_region = 100
remove_ability_y = random.choice(y_list1)
remove_ability_x = random.choice(x_list1)
x = 0
y = 0
x_y_random = random.randint(0,1)
def remove_y(self):
self.y += self.remove_ability_y
self.life_region -= 1
def remove_x(self):
self.x += self.remove_ability_x
self.life_region -= 1
class fish():
remove_ability = random.choice([-1,1])
x = random.randint(-5,5)
y = random.randint(-5,5)
x_y_random = random.randint(0, 1)
def remove_y(self):
self.y += self.remove_ability
def remove_x(self):
self.x += self.remove_ability
remove_count = 0
fishlie = fish()
wugui = wugui()
dict1 = {}
for each_fish in range(1,11):
dict1['fish%d'% each_fish] = fish()
list_fish = dict1.keys()
for each_count in range(100000000000000000000):
remove_count += 1
if wugui.x_y_random == 0:
wugui.remove_y()
elif wugui.x_y_random == 1:
wugui.remove_x()
for each_fish in list_fish:
if dict1[each_fish].x_y_random == 0:
dict1[each_fish].remove_y()
elif dict1[each_fish].x_y_random == 1:
dict1[each_fish].remove_x()
if dict1[each_fish].x == wugui.x and dict1[each_fish].y == wugui.y:
dict1.pop(each_fish)
wugui.life_region += 20
if wugui.x == 5:
self.x_list1.remove(1)
if wugui.x == -5:
self.x_list1.remove(-1)
if wugui.y == 5:
self.y_list1.remove(1)
if wugui.y == -5:
self.y_list1.remove(-1)
if wugui.x in range(-4,5) and wugui.y in range(-4,5):
wugui.remove_ability_x = random.randint(1,2)
wugui.remove_ability_y = random.randint(1,2)
if wugui.life_region == 0:
print("yu'si'l")
print(remove_count)
break
假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
游戏生成1只乌龟和10条鱼
它们的移动方向均随机
乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
当移动到场景边缘,自动向反方向移动
乌龟初始化体力为100(上限)
乌龟每移动一次,体力消耗1
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
鱼暂不计算体力
当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
能帮我看一下这段又臭又长的代码吗2222
或提供新思路
你的代码有一些问题,我来帮你检查和修复一下:
在 wugui 类中,你使用了 self.x_list1.remove(1) 和 self.y_list1.remove(1) 来移除列表中的元素,但是你应该使用类名 wugui.x_list1.remove(1) 和 wugui.y_list1.remove(1)。同样的问题也出现在后面的 if 语句中。
在乌龟移动时,你只更新了乌龟的坐标,但没有更新乌龟的移动方向。你需要在乌龟移动后重新随机选择一个移动方向,即在 remove_y 和 remove_x 方法中添加以下代码:
- self.x_y_random = random.randint(0, 1)
复制代码
当乌龟和鱼的坐标重叠时,你使用 dict1.pop(each_fish) 来移除被吃掉的鱼,但这会导致遍历字典时发生 RuntimeError。为了解决这个问题,你可以将需要移除的鱼的键先存储到一个列表中,在遍历完字典后再进行移除,即将以下代码:
改为:
- fish_to_remove.append(each_fish)
复制代码
并在循环结束后添加以下代码:
- for fish_key in fish_to_remove:
- dict1.pop(fish_key)
复制代码
最后一个问题是关于游戏结束条件的判断。你应该在 for 循环内部添加一个判断,即当鱼的数量变为0时,游戏结束。修改如下:
- if len(dict1) == 0:
- print("游戏结束")
- print(remove_count)
- break
复制代码
至此,我对你的代码进行了修复和改进。下面是完整的修复后的代码:
- import random
- class Wugui:
- x_list1 = [-2, -1, 1, 2]
- y_list1 = [-2, -1, 1, 2]
- life_region = 100
- def __init__(self):
- self.remove_ability_y = random.choice(Wugui.y_list1)
- self.remove_ability_x = random.choice(Wugui.x_list1)
- self.x = 0
- self.y = 0
- self.x_y_random = random.randint(0, 1)
- def remove_y(self):
- self.y += self.remove_ability_y
- self.life_region -= 1
- self.x_y_random = random.randint(0, 1)
- def remove_x(self):
- self.x += self.remove_ability_x
- self.life_region -= 1
- self.x_y_random = random.randint(0, 1)
- class Fish:
- remove_ability = random.choice([-1, 1])
- def __init__(self):
- self.x = random.randint(-5, 5)
- self.y = random.randint(-5, 5)
- self.x_y_random = random.randint(0, 1)
- def remove_y(self):
- self.y += self.remove_ability
- def remove_x(self):
- self.x += self.remove_ability
- remove_count = 0
- fishlie = Fish()
- wugui = Wugui()
- dict1 = {}
- for each_fish in range(1, 11):
- dict1['fish%d' % each_fish] = Fish()
- list_fish = dict1.keys()
- while True:
- fish_to_remove = []
- remove_count += 1
- if wugui.x_y_random == 0:
- wugui.remove_y()
- elif wugui.x_y_random == 1:
- wugui.remove_x()
- for each_fish in list_fish:
- if dict1[each_fish].x_y_random == 0:
- dict1[each_fish].remove_y()
- elif dict1[each_fish].x_y_random == 1:
- dict1[each_fish].remove_x()
- if dict1[each_fish].x == wugui.x and dict1[each_fish].y == wugui.y:
- fish_to_remove.append(each_fish)
- wugui.life_region += 20
- for fish_key in fish_to_remove:
- dict1.pop(fish_key)
- if wugui.x == 5:
- Wugui.x_list1.remove(1)
- if wugui.x == -5:
- Wugui.x_list1.remove(-1)
- if wugui.y == 5:
- Wugui.y_list1.remove(1)
- if wugui.y == -5:
- Wugui.y_list1.remove(-1)
- if wugui.x in range(-4, 5) and wugui.y in range(-4, 5):
- wugui.remove_ability_x = random.randint(1, 2)
- wugui.remove_ability_y = random.randint(1, 2)
- if wugui.life_region == 0:
- print("乌龟体力耗尽,游戏结束")
- print(remove_count)
- break
- if len(dict1) == 0:
- print("鱼儿已全部被吃掉,游戏结束")
- print(remove_count)
- break
复制代码
这段修复后的代码应该可以按照你的设定规则进行游戏,当乌龟体力耗尽或鱼儿全部被吃掉时,游戏会结束并输出移动次数。希望对你有帮助!如果还有其他问题,请随时提问。
|
最佳答案
查看完整内容
你的代码有一些问题,我来帮你检查和修复一下:
在 wugui 类中,你使用了 self.x_list1.remove(1) 和 self.y_list1.remove(1) 来移除列表中的元素,但是你应该使用类名 wugui.x_list1.remove(1) 和 wugui.y_list1.remove(1)。同样的问题也出现在后面的 if 语句中。
在乌龟移动时,你只更新了乌龟的坐标,但没有更新乌龟的移动方向。你需要在乌龟移动后重新随机选择一个移动方向,即在 remove_y 和 remove_x 方法中添加以下代 ...
|