新人又来求助啦,大佬们帮忙康康这个改进的小游戏的毛病在哪里,怎么修改,谢谢啦
#乌龟与鱼的小游戏import random as r
"""初始化边界范围"""
w_x =
w_y =
class Turtle:
"""创建一个乌龟的类,具备位置与体力属性以及移动与吃鱼的方法"""
def __init__(self):
"""初始化乌龟的位置坐标,体力值,体力上限以及等级属性"""
self.x = r.choice(w_x)
self.y = r.choice(w_y)
self.power = 100
self.power_legal = 100
self.level = 1
def move(self):
"""乌龟每次随机移动1-2个坐标单位,每移动一次体力-20"""
new_x = self.x + r.choice([-2,-1,1,2])
if new_x < w_x:
self.x = abs(new_x)
elif new_x > w_x:
self.x = w_x - (new_x - w_x)
else:
self.x = new_x
new_y = self.y + r.choice([-2,-1,1,2])
if new_y < w_y:
self.y = abs(new_y)
elif new_y > w_y:
self.y = w_y - (new_y - w_y)
else:
self.y = new_y
self.power -= 1
return(self.x,self.y)
def eat(self):
"""定义乌龟吃鱼的方法,乌龟每次吃鱼等级+1,体力+20,体力上限+30(这里想乌龟每次升级的条件是上次升级的2倍,1级升2级的条件是吃掉1条鱼,这个不会搞,嘿嘿)"""
self.level += 1
if self.level > 15:
self.level = 15
self.power_legal += 30
if self.power_legal > 550:
self.power_legal = 550
self.power += 20
if self.power > 550:
self.power = 550
class Fish:
"""创建一个鱼类,具备位置属性以及移动的方法"""
def __init__(self):
"""初始化鱼的位置坐标"""
self.x = r.choice(range(10))
self.y = r.choice(range(10))
def move(self):
"""鱼每次随机移动1个坐标单位"""
new_x = self.x + r.choice([-1,1])
if new_x < w_x:
self.x = abs(new_x)
elif new_x > w_x:
self.x = w_x - (new_x - w_x)
else:
self.x = new_x
new_y = self.y + r.choice([-1,1])
if new_y < w_y:
self.y = abs(new_y)
elif new_y > w_y:
self.y = w_y - (new_y - w_y)
else:
self.y = new_y
return(self.x,self.y)
class Goldfish(Fish):
pass
class Sandvenfish(Fish):
pass
class Clarfish(Fish):
pass
"""实例化乌龟和鱼"""
turtle = Turtle()
fish = []
ber = []
goldfish = Goldfish()
sandvenfish = Sandvenfish()
clarfish = Clarfish()
ber.append(goldfish)
ber.append(sandvenfish)
ber.append(clarfish)
while turtle.power:
new_fish = r.choice(ber)
fish.append(new_fish)
if not turtle.power:
print("小龟龟没有体力啦,游戏结束^_^")
break
else:
pos = turtle.move()
for each in fish[:]:
if each.move() == pos:
turtle.eat()
fish.remove(each)
if each == goldfish:
print("一条小金鱼被吃掉了^_^")
if turtle.level < 15:
print(f"小龟龟升级啦,当前等级{turtle.level}")
print(f"小龟龟当前体力值为{turtle.power},当前体力上限为{turtle.power_legal}")
elif each == sandvenfish:
print("一条三文鱼被吃掉了^_^")
if turtle.level < 15:
print(f"小龟龟升级啦,当前等级{turtle.level}")
print(f"小龟龟当前体力值为{turtle.power},当前体力上限为{turtle.power_legal}")
else:
print("一条小鲤鱼被吃掉了^_^")
if turtle.level < 15:
print(f"小龟龟升级啦,当前等级{turtle.level}")
print(f"小龟龟当前体力值为{turtle.power},当前体力上限为{turtle.power_legal}")
{:5_109:} {:5_109:} {:5_109:} 可以把问题精简一下吗 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地标:(4, 7) 鱼被乌龟吃了
地标:(5, 9) 鱼被乌龟吃了
地标:(9, 6) 鱼被乌龟吃了
地标:(8, 0) 鱼被乌龟吃了
地标:(4, 4) 鱼被乌龟吃了
地标:(2, 2) 鱼被乌龟吃了
地标:(4, 1) 鱼被乌龟吃了
地标:(2, 3) 鱼被乌龟吃了
地标:(4, 6) 鱼被乌龟吃了
乌龟体力耗尽了,鱼剩下 1 只,最后鱼和乌龟位置:
[[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' 'T' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' 'F' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']] newu 发表于 2021-9-23 09:24
可以把问题精简一下吗
不好意思,最近感冒了,一直没上鱼C官网,今天才看到回复,这个游戏我就是想在小甲鱼留的课后作业的基础上扩展一下,给乌龟加个等级的属性,原来是想着升级的条件是阶梯式增长,但是发现自己做不出来,就只好作罢了{:5_109:} 傻眼貓咪 发表于 2021-9-24 15:26
不好意思,最近感冒了,一直没上鱼C官网,今天才看到回复,大佬辛苦了,就是还有点想法,那个乌龟升级的那里大佬好像漏了哈{:5_91:}
页:
[1]