标签:power randint python self list new 乌龟 fish
‘’’
1.首先要有一个画布
2.随机乌龟和鱼的位置
3.移动
‘’’
import random as r
list_x = [0,10]
list_y = [0,10]
class Turtle:
def init(self):
#初始体力
self.power=100
#初始位置
self.x = r.randint(list_x[0],list_x[1]) # 这里重点知道 randint
self.y = r.randint(list_y[0],list_y[1])
def move(self):
# 随机移动位置
new_x = self.x+r.choice([1,2,-1,-2])
new_y = self.y+r.choice([1,2,-1,-2])
#检查移动后是否超出区域
if new_x<list_x[0]:
self.x = list_x[0]-(new_x-list_x[0])
elif new_x>list_x[1]:
self.x = list_x[1]-(new_x-list_x[1])
else:
self.x = new_x
#检查是否超出Y轴
if new_y<list_y[0]:
self.y = list_y[0]-(new_y-list_y[0])
elif new_y>list_y[1]:
self.y = list_y[1]-(new_y-list_y[1])
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(list_x[0],list_x[1])
self.y = r.randint(list_y[0],list_y[1])
def move(self):
# 随机移动位置
new_x = self.x+r.choice([1,-1])
new_y = self.y+r.choice([1,-1])
#检查移动后是否超出区域
if new_x<list_x[0]:
self.x = list_x[0]-(new_x-list_x[0])
elif new_x>list_x[1]:
self.x = list_x[1]-(new_x-list_x[1])
else:
self.x = new_x
#检查是否超出Y轴
if new_y<list_y[0]:
self.y = list_y[0]-(new_y-list_y[0])
elif new_y>list_y[1]:
self.y = list_y[1]-(new_y-list_y[1])
else:
self.y = new_y
#返回移动后的位置
return (self.x,self.y)
t = Turtle()
fish = []
for i in range(10):
new_fish = Fish()
fish.append(new_fish)
while True:
if not len(fish):
print(“鱼儿被吃完了,游戏结束”)
break
if not t.power:
print(“乌龟体力耗尽,牺牲了”)
break
pos = t.move()
for each_fish in fish[:]:
if each_fish.move() ==pos:
#鱼儿被吃掉
t.eat()
fish.remove(each_fish)
print(“有一条鱼被吃了”)
标签:power,randint,python,self,list,new,乌龟,fish 来源: https://blog.csdn.net/yonggejiayou/article/details/104795668
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。