|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
原题:
游戏编程:按以下要求定义一个乌龟类和鱼类并尝试编写游戏。
假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
游戏生成1只乌龟和10条鱼
它们的移动方向均随机
乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
当移动到场景边缘,自动向反方向移动
乌龟初始化体力为100(上限)
乌龟每移动一次,体力消耗1
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
鱼暂不计算体力
当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
个人代码:
import random
class Fish:
x = random.randint(1,10)
y = random.randint(1,10)
def move(self):
a = random.randint(1,4)
if a ==1:
if self.x != 10:
self.x += 1
else:
self.x -= 1
elif a == 2:
if self.y != 10:
self.y += 1
else:
self.y -= 1
elif a == 3:
if self.x != 0:
self.x -= 1
else:
self.x +=1
elif a == 4:
if self.y != 0:
self.y -= 1
else:
self.y += 1
return (self.x,self.y)
class Turtle:
health = 100
x = random.randint(1,10)
y = random.randint(1,10)
def move(self):
b = random.randint(1,4)
step = random.randint(1,2)
if b ==1:
if self.x != 10 and self.x != 9:
self.x += step
elif self.x == 10:
self.x -= step
elif self.x == 9 and step == 1:
self.x += step
elif b == 2:
if self.y != 10 and self.y != 9:
self.y += step
elif self.y == 10:
self.y -= step
elif self.y == 9 and step == 1:
self.y += step
elif b == 3:
if self.x != 0 and self.x != 1:
self.x -= step
elif self.x == 0:
self.x += step
elif self.x == 1 and step == 1:
self.x -= step
elif b == 4:
if self.y != 0 and self.y != 1:
self.y -= step
elif self.y == 0:
self.y += step
elif self.y == 1 and step ==1:
self.y -= step
self.health -= 1
return (self.x,self.y)
t = Turtle()
a = Fish()
b = Fish()
c = Fish()
d = Fish()
e = Fish()
f = Fish()
g = Fish()
h = Fish()
i = Fish()
j = Fish()
count = 10
while True:
if t.health == 0:
print("游戏结束,乌龟被饿死了")
break
if count == 0:
print("游戏结束,小鱼被吃光了")
break
t.move()
for each in [a,b,c,d,e,f,g,h,i,j]:
each.move()
if each.x == t.x and each.y == t.y:
del each
count -= 1
t.health += 20
if t.health > 100:
t.health = 100
print("有一条小鱼被吃掉了")
|
|