|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#乌龟与鱼的小游戏
import random as r
"""初始化边界范围"""
w_x = [0,10]
w_y = [0,10]
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[0]:
self.x = abs(new_x)
elif new_x > w_x[1]:
self.x = w_x[1] - (new_x - w_x[1])
else:
self.x = new_x
new_y = self.y + r.choice([-2,-1,1,2])
if new_y < w_y[0]:
self.y = abs(new_y)
elif new_y > w_y[1]:
self.y = w_y[1] - (new_y - w_y[1])
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[0]:
self.x = abs(new_x)
elif new_x > w_x[1]:
self.x = w_x[1] - (new_x - w_x[1])
else:
self.x = new_x
new_y = self.y + r.choice([-1,1])
if new_y < w_y[0]:
self.y = abs(new_y)
elif new_y > w_y[1]:
self.y = w_y[1] - (new_y - w_y[1])
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}")
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
地标:(4, 7) 鱼被乌龟吃了
地标:(5, 9) 鱼被乌龟吃了
地标:(9, 6) 鱼被乌龟吃了
地标:(8, 0) 鱼被乌龟吃了
地标:(4, 4) 鱼被乌龟吃了
地标:(2, 2) 鱼被乌龟吃了
地标:(4, 1) 鱼被乌龟吃了
地标:(2, 3) 鱼被乌龟吃了
地标:(4, 6) 鱼被乌龟吃了
乌龟体力耗尽了,鱼剩下 1 只,最后鱼和乌龟位置:
[[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' 'T' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' 'F' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']
[' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']]
|
|