入门学习python第37集课后题,乌龟吃鱼游戏的问题
import random as r
legal_x =
legal_y =
class Turtle:
def __init__(self):
# 初始体力
self.power = 100
# 初始位置随机
self.x = r.randint(legal_x, legal_x)
self.y = r.randint(legal_y, legal_y)
def move(self):
# 随机计算方向并移动到新的位置(x, y)
new_x = self.x + r.choice()
new_y = self.y + r.choice()
# 检查移动后是否超出场景x轴边界
if new_x < legal_x:
self.x = legal_x - (new_x - legal_x)
elif new_x > legal_x:
self.x = legal_x - (new_x - legal_x)
else:
self.x = new_x
# 检查移动后是否超出场景y轴边界
if new_y < legal_y:
self.y = legal_y - (new_y - legal_y)
elif new_y > legal_y:
self.y = legal_y - (new_y - legal_y)
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.x = r.randint(legal_x, legal_x)
self.y = r.randint(legal_y, legal_y)
def move(self):
# 随机计算方向并移动到新的位置(x, y)
new_x = self.x + r.choice()
new_y = self.y + r.choice()
# 检查移动后是否超出场景x轴边界
if new_x < legal_x:
self.x = legal_x - (new_x - legal_x)
elif new_x > legal_x:
self.x = legal_x - (new_x - legal_x)
else:
self.x = new_x
# 检查移动后是否超出场景y轴边界
if new_y < legal_y:
self.y = legal_y - (new_y - legal_y)
elif new_y > legal_y:
self.y = legal_y - (new_y - legal_y)
else:
self.y = new_y
# 返回移动后的新位置
return (self.x, self.y)
turtle = Turtle()
fish = []
for i in range(10):
new_fish = Fish()
fish.append(new_fish)
while True:
if not len(fish):
print("鱼儿都吃完了,游戏结束!")
break
if not turtle.power:
print("乌龟体力耗尽,挂掉了!")
break
pos = turtle.move()
# 在迭代器中删除列表元素是非常危险的,经常会出现意想不到的问题,因为迭代器是直接引用列表的数据进行引用
# 这里我们把列表拷贝给迭代器,然后对原列表进行删除操作就不会有问题了^_^
for each_fish in fish[:]:
if each_fish.move() == pos:
# 鱼儿被吃掉了
turtle.eat()
fish.remove(each_fish)
print("有一条鱼儿被吃掉了...")
我不是很懂如下代码,主要是fish列表里面存储的是什么东西。我用print看了一下,是一个地址。
turtle = Turtle()
fish = [ ]
for i in range(10):
new_fish = Fish()
fish.append(new_fish)
while True:
if not len(fish):
print("鱼儿都吃完了,游戏结束!")
break
if not turtle.power:
print("乌龟体力耗尽,挂掉了!")
break
pos =turtle.move()
for each_fish in fish[:]:
if each_fish.move() == pos:
turtle.eat()
fish.remove(each_fish)
print("有一只鱼儿被吃掉了...")
列表里面指向的是对象的地址 如果想访问对象的属性的话你可以想象这个地址是一个盒子 盒子里面有x,y,move 你想获得x,y 前面就得加上这个盒子的名字 也就是地址 例如 (fish.x,fish.y,fish.move)(fish.x,fish,fish.move) move方法返回的就是(self.x, self.y)的值
turtle = Turtle() #实例化对象乌龟
fish = [ ]
for i in range(10):
new_fish = Fish() #创建实例化对象10个小鱼每一次实例化计算机都会给对象分配一个地址
fish.append(new_fish) #加入到列表中
while True:
if not len(fish): #列表是否为空not 0
print("鱼儿都吃完了,游戏结束!")
break
if not turtle.power:#乌龟对象的体力是否为not 0
print("乌龟体力耗尽,挂掉了!")
break
pos =turtle.move()#乌龟位置
for each_fish in fish[:]:
if each_fish.move() == pos: #each_fish 指向的是实例化小鱼的地址就是实例化Fish,小鱼的位置与乌龟的位置相同 (x,y) 坐标 就被吃掉
turtle.eat() #乌龟的吃方法 加体力
fish.remove(each_fish)#同时列表中的元素就少一个
print("有一只鱼儿被吃掉了...")
C201818z 发表于 2021-12-8 09:56
turtle = Turtle() #实例化对象乌龟
fish = [ ]
for i in range(10):
谢谢,还有一个问题,fish列表里面的数据指向的是【return (self.x, self.y)】 的结果吗?
如果我要让fish数组直接显示返回的值(self.x, self.y),可不可以呢? C201818z 发表于 2021-12-10 09:48
列表里面指向的是对象的地址 如果想访问对象的属性的话你可以想象这个地址是一个盒子 盒子里面有x,y,move ...
谢谢{:7_112:} while True:
if not len(fish):
print("鱼儿都吃完了,游戏结束!")
break
if not turtle.power:
print("乌龟体力耗尽,挂掉了!")
break
pos = turtle.move()
# 在迭代器中删除列表元素是非常危险的,经常会出现意想不到的问题,因为迭代器是直接引用列表的数据进行引用
# 这里我们把列表拷贝给迭代器,然后对原列表进行删除操作就不会有问题了^_^
for each_fish in fish[:]:
if each_fish.move() == pos:
# 鱼儿被吃掉了
turtle.eat()
fish.remove(each_fish)
print("有一条鱼儿被吃掉了...")这一块我给改成:turtle = Turtle()
fish = Fish()
while True:
if not turtle.power:
print('乌龟体力没了,游戏结束!')
break
if not fish.count:
print('鱼被吃完了,游戏结束!')
break
pos1=turtle.move()
pos2=fish.move()
if pos1 == pos2:
turtle.eat()
fish.eat()
print('有一条鱼被吃掉了...'),为什么这样弄出来的情况不对,我感觉也挺对的
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):
new_x = self.x + r.choice()
new_y = self.y + r.choice()
if new_x < 0:
self.x = 0-new_x
elif new_x > 10:
self.x = 10-(new_x-10)
else:
self.x = new_x
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.count=10
self.x = r.randint(0,10)
self.y = r.randint(0,10)
def move(self):
new_x = self.x + r.choice()
new_y = self.y + r.choice()
if new_x < 0:
self.x = 0-new_x
elif new_x > 10:
self.x = 10-(new_x-10)
else:
self.x = new_x
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)
def eat(self):
self.count-=1
turtle = Turtle()
fish = Fish()
while True:
if not turtle.power:
print('乌龟体力没了,游戏结束!')
break
if not fish.count:
print('鱼被吃完了,游戏结束!')
break
pos1=turtle.move()
pos2=fish.move()
if pos1 == pos2:
turtle.eat()
fish.eat()
print('有一条鱼被吃掉了...')
红字标出的地方我打算就是把鱼的数目也和乌龟的体力一样弄出来,然后再进行逐一相减,但结果不对,我感觉逻辑啥的应该没什么问题吧 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):
new_x=self.x+r.choice([-2,-1,1,2])
new_y=self.y+r.choice([-2,-1,1,2])
if new_x>10:
self.x=10-(new_x-10)
elif new_x<0:
self.x=0-new_x
else:
self.x = new_x
if new_y>10:
self.y=10-(new_y-10)
elif new_y<0:
self.y=0-new_y
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 __new__(Fish):
return super().__new__(Fish)
def __init__(self):
self.count=10
self.x=r.randint(0,10)
self.y=r.randint(0,10)
def move(self):
new_x=self.x+r.choice([-1,1])
new_y=self.y+r.choice([-1,1])
if new_x>10:
self.x=10-(new_x-10)
elif new_x<0:
self.x=0-new_x
else:
self.x = new_x
if new_y>10:
self.y=10-(new_y-10)
elif new_y<0:
self.y=0-new_y
else:
self.y = new_y
return (self.x,self.y)
def eat(self):
self.count-=1
while self.count == 0:
print('鱼被吃光了,游戏结束!')
break
turtle = Turtle()
fish = Fish()
while 1:
if not turtle.power:
print('乌龟体力耗尽,游戏结束')
break
pos=turtle.move()
if fish.move() == pos:
turtle.eat()
fish.eat()
print('有一条鱼被吃了')
红字部分是我想的代码,我想问下这样有瑕疵,请给我指正下
页:
[1]