乌龟和鱼的问题
import random as rclass Turtle:
def __init__(self):
# 初始体力
self.power = 100
# 初始位置随机
self.x = r.randint(0, 10)
self.y = r.randint(0, 10)
def move(self):
# 随机计算方向并移动到新的位置(x, y)
new_x = self.x + r.choice()
new_y = self.y + r.choice()
# 检查移动后是否超出场景x轴边界
if new_x < 0:
self.x = 0 - new_x
elif new_x > 10:
self.x = 10 - (new_x - 10)
else:
self.x = new_x
# 检查移动后是否超出场景y轴边界
if new_y < 0:
self.y = 0 - new_y
elif new_y > 10:
self.y = 10 - (new_y - 10)
else:
self.y = new_y
# 体力消耗
self.power -= 1
# 返回移动后的新位置
return (self.x, self.y)
def eat(self):
self.power += 20
if self.power > 100:
self.power = 100
class Fish:
def __init__(self):
self.num=10
self.x = r.randint(0, 10)
self.y = r.randint(0, 10)
def move(self):
# 随机计算方向并移动到新的位置(x, y)
new_x = self.x + r.choice()
new_y = self.y + r.choice()
# 检查移动后是否超出场景x轴边界
if new_x < 0:
self.x = 0 - new_x
elif new_x > 10:
self.x = 10 - (new_x - 10)
else:
self.x = new_x
# 检查移动后是否超出场景y轴边界
if new_y < 0:
self.y = 0 - new_y
elif new_y > 10:
self.y = 10 - (new_y - 10)
else:
self.y = new_y
# 返回移动后的新位置
return (self.x, self.y)
turtle = Turtle()
fish=Fish()
while True:
if not fish.num:
print('鱼都被吃完了!')
break
if not turtle.power:
print('乌龟体力耗尽,挂了!')
break
print("乌龟移动前坐标:", (turtle.x ,turtle.y)) #乌龟移动前
turtle.move()
print("乌龟移动后坐标:", (turtle.x ,turtle.y)) #乌龟移动后
print("鱼移动前坐标:", (fish.x ,fish.y))
fish.move()# 感觉鱼的移动前后的坐标差有问题
print("鱼移动后坐标:", (fish.x ,fish.y))
if fish.x==turtle.x and fish.y==turtle.y:
turtle.eat()
fish.num-=1
print("死了一只鱼")
print("乌龟最新体力值为 %d"%turtle.power)
红字部分这样改好像和原代码也差不多,大佬们给点指点这样改行吗?我运行一边修改后的代码也没问题 代碼都是你寫的?你好厲害啊{:5_106:}{:5_106:} 傻眼貓咪 发表于 2021-9-12 20:04
代碼都是你寫的?你好厲害啊
红字相比较于原代码有改变,请指点
原代码是加入了十个Fish 对象,即 加入了十只鱼,每只鱼坐标可以不同
但是你的代码是直接一次性加入了十只捆绑在一起的鱼,只能有一个坐标
本帖最后由 傻眼貓咪 于 2021-9-16 14:56 编辑
我的代码(稍作改写代码):
import random
import numpy as np
class Turtle:
def __init__(self, stamina = 100):
self.stamina = stamina
self.x = random.randint(0, 10)
self.y = random.randint(0, 10)
self.point = (self.x, self.y)
def move(self):
while True:
a = (self.x + random.choice([-2, -1, 1, 2]))%10
b = (self.y + random.choice([-2, -1, 1, 2]))%10
if a >= 0 and b >= 0:
self.x = a
self.y = b
break
self.point = (self.x, self.y)
self.stamina -= 1
class Fish:
def __init__(self):
self.x = random.randint(0, 10)
self.y = random.randint(0, 10)
self.point = (self.x, self.y)
def move(self):
while True:
a = (self.x + random.choice([-2, -1, 1, 2]))%10
b = (self.y + random.choice([-2, -1, 1, 2]))%10
if a >= 0 and b >= 0:
self.x = a
self.y = b
break
self.point = (self.x, self.y)
class AllFish:
def __init__(self):
self.allfish =
def position(self):
return
def kill(self, point):
for n, i in enumerate(self.allfish):
if i.point == point:
self.allfish.pop(n)
break
def coordinate(A, B):
res = [[" " for _ in range(10)] for _ in range(10)]
res]] = "T"
for i in B:
x, y = i
res = "F"
res = np.array(res)
print(res)
t = Turtle()
f = AllFish()
while True:
if not f.allfish:
print("鱼被吃完了,最后乌龟位置:")
coordinate(t.point, f.position())
break
if not t.stamina:
print(f"乌龟体力耗尽了,鱼剩下 {len(f.allfish)} 只,最后鱼和乌龟位置:")
coordinate(t.point, f.position())
break
t.move()
for i in f.allfish:
i.move()
if t.point in f.position():
print(f"地标:{t.point} 鱼被乌龟吃了")
f.kill(t.point)
t.stamina += 20
if t.stamina > 100:
t.stamina = 100地标:(6, 2) 鱼被乌龟吃了
地标:(5, 9) 鱼被乌龟吃了
地标:(4, 1) 鱼被乌龟吃了
地标:(6, 2) 鱼被乌龟吃了
地标:(5, 3) 鱼被乌龟吃了
地标:(2, 2) 鱼被乌龟吃了
地标:(2, 6) 鱼被乌龟吃了
地标:(0, 1) 鱼被乌龟吃了
乌龟体力耗尽了,鱼剩下 2 只,最后鱼和乌龟位置:
[[' ' ' ' 'F' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' 'T' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' 'F' ' ' ' ' ' ' ' ']] Twilight6 发表于 2021-9-16 12:46
原代码是加入了十个Fish 对象,即 加入了十只鱼,每只鱼坐标可以不同
但是你的代码是直接一次性加 ...
原来如此,懂了 你不也很厉害 祝建宇 发表于 2021-9-16 14:54
你不也很厉害
谢谢你的夸奖,我还在学习
大家共同学习,哈哈哈哈
{:5_91:} 傻眼貓咪 发表于 2021-9-16 14:59
谢谢你的夸奖,我还在学习
大家共同学习,哈哈哈哈
想加你好友,但是有权限{:5_100:}
页:
[1]