Python-类02(乌龟吃鱼游戏)
来源:互联网 发布:明扬三角梅淘宝店 编辑:程序博客网 时间:2024/05/02 02:41
import random as rlegal_x=[0,10]legal_y=[0,10]class Turtle: def __init__(self): #初始体力 self.power=100 #初始随机位置 self.x=r.randint(legal_x[0],legal_x[1]) self.y=r.randint(legal_y[0],legal_y[1]) def move(self): #随机计算方法并移动到新的位置(x,y) new_x=self.x+r.choice([-1,1,-2,2]) new_y=self.y+r.choice([-1,1,-2,2]) #检查移动后是否超出边界x轴 if new_x<legal_x[0]: self.x=legal_x[0]-(new_x-legal_x[0]) elif new_x>legal_x[1]: self.x=legal_x[1]-(new_x-legal_x[1]) else: self.x=new_x #检查移动后是否超出边界y轴 if new_y<legal_y[0]: self.y=legal_y[0]-(new_y-legal_y[0]) elif new_y>legal_y[1]: self.y=legal_y[1]-(new_y-legal_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=100class Fish: def __init__(self): self.x=r.randint(legal_x[0],legal_x[1]) self.y=r.randint(legal_y[0],legal_y[1]) def move(self): #随机计算方法并移动到新的位置(x,y) new_x=self.x+r.choice([-1,1,-2,2]) new_y=self.y+r.choice([-1,1,-2,2]) #检查移动后是否超出边界x轴 if new_x<legal_x[0]: self.x=legal_x[0]-(new_x-legal_x[0]) elif new_x>legal_x[1]: self.x=legal_x[1]-(new_x-legal_x[1]) else: self.x=new_x #检查移动后是否超出边界y轴 if new_y<legal_y[0]: self.y=legal_y[0]-(new_y-legal_y[0]) elif new_y>legal_y[1]: self.y=legal_y[1]-(new_y-legal_y[1]) 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('有一条鱼儿被吃掉了')
0 0
- Python-类02(乌龟吃鱼游戏)
- Python 小甲鱼教程 乌龟吃鱼游戏
- 模拟乌龟吃鱼的小游戏
- 【python卡牌游戏】抽乌龟
- 捉乌龟的游戏
- python:小乌龟turtle
- js_乌龟抓鸡游戏
- google吃豆子游戏
- 吃豆子游戏
- P1567 吃糖果游戏
- 吃盒子游戏
- CSS2D--吃豆子游戏
- 马里奥吃金币游戏
- 游戏吃金币+背景
- win32_吃砖块游戏
- 数乌龟(王八)
- 乌龟
- 乌龟
- LeetCode:Longest Consecutive Sequence
- BZOJ2321 [BeiJing2011集训]星器 能量守恒定律
- Android--List转换String,String转换List
- linux笔记-nfs
- Yii2的异步多线程服务之swoole
- Python-类02(乌龟吃鱼游戏)
- 新闻编辑器 ckeditor
- Zend中的联表查询
- C语言清除缓冲区的函数
- 机器学习案例实战-信用卡欺诈检测
- 11G Oracle报ORA-01078和LRM-00109错解决方法
- iOS转场动画之微信朋友圈图片查看器
- mysql索引
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)