|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random as r
class 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([1, 2, -1, -2])
new_y = self.y + r.choice([1, 2, -1, -2])
# 检查移动后是否超出场景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([1, -1])
new_y = self.y + r.choice([1, -1])
# 检查移动后是否超出场景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)
红字部分这样改好像和原代码也差不多,大佬们给点指点这样改行吗?我运行一边修改后的代码也没问题
本帖最后由 傻眼貓咪 于 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 = [Fish() for _ in range(10)]
-
- def position(self):
- return [i.point for i in self.allfish]
-
- 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[A[0]][A[1]] = "T"
- for i in B:
- x, y = i
- res[y][x] = "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' ' ' ' ' ' ' ' ']]
复制代码
|
|