新手学python ——《小乌龟吃小鱼》小游戏 第二版
本帖最后由 wushaominkk 于 2020-7-30 09:13 编辑{:300_965:}同志们,我又回来了。{:300_965:}
https://static.52pojie.cn/static/image/hrline/4.gifhttps://static.52pojie.cn/static/image/hrline/4.gif
首先常规的是讲一下有的没的唠一些5毛钱的叨,如不感兴趣鼠标滚轮往下翻直接看正文《小乌龟吃小鱼》
https://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.png
还记得前段时间版面上有一份关于小乌龟吃小鱼的帖子嘛 哈哈~~~
先说下之前版本的缺点:
[*]程序结构有点乱,不太清晰
[*]各个功能也都混在一起,阅读起来比较麻烦,修改起来更加麻烦
[*]有些需要实现的功能没实现,不需要的功能有比较多余
[*]虽然用到了面向对象,但是没有用好(刚开始学,所以见谅)
[*]暂时就这么多
https://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.png
放个传送门——关于我之前一个版本的小乌龟吃小鱼的帖子有兴趣的同志们可以先看下之前那个版本,再回来看这个
《小乌龟吃小鱼》第一版 链接放边上以免边上的标题指向不到之前的帖子 https://www.52pojie.cn/thread-1230572-1-1.htmlhttps://static.52pojie.cn/static/image/hrline/4.gifhttps://static.52pojie.cn/static/image/hrline/4.gif
现在 现在 现在 就是现在,新版本的 小乌龟吃小鱼 我终于琢磨出来了。
这次感觉比之前的思路更清晰,所以再次分享出来!
不仅如此,经过了第一次发帖后我感觉我宛如一个老手了哈哈(手动狗头){:1_932:},页面版式之类的也有了一点点的长进。
看起来应该也会顺眼一点,程序方面我还是会分段展示,分段解释,但是不会像第一版分的这么细,第一版的帖子我看下来,原本程序就乱,再细分贴上来,感觉阅读起来更加不友好(自我吐槽)。
但是有一部分不会变,那就是此贴还是跟原来一样,和新手共同学习,共同进步,同时欢迎大佬进来光顾,提出指导意见!谢谢大家!
好吐槽和反思部分就到这。
下面我们开始正式的程序部分:
{:300_951:}《小乌龟吃小鱼》{:300_951:}
https://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.png
第一步:写码之前的沉思
思路不变:
[*]初始范围是10*10的方格内
[*]移动方向随机(上下左右)
[*]两种生物 ①乌龟(数量1) ②鱼(数量10)
[*]乌龟移动1-2步
[*]鱼移动1步
[*]乌龟有体力吃鱼增加体力但有上限100
[*]鱼没有体力
https://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.png
第二步:对键盘下手
1.习惯
入口主函数main()并调用
def main():
pass
if __name__ == '__main__':
main()
https://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.png
2.确认变量
需要的变量都放在全局里面了,可能对资源有点浪费,但是刚开始学,大气点吧,还不会精打细算
另一个原因是放在全局是方便修改参数
# 最大最小边界(由于算法关系x和y分别的MAX和MIN差最小为1)
MAP_MIN_X = 0
MAP_MAX_X = 10
MAP_MIN_Y = 0
MAP_MAX_Y = 10
# 符号 正 负
SYMBOL = ['+','-']
# 方向 水平 垂直
DIRECTION = ['horizontal','vertical']
# 鱼群
# 鱼群堆
SHOAL_OF_FISH = []
# 最大数量
MAX_FISH = 10
# 乌龟
# 移动次数
TORTOISE_MOVE_MAX = 2
# 最大体力
TORTOISE_SPIRIT_MAX = 100
https://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.png
3.父类对象 Ainmal
这里开始用到了生成随机数,所有程序顶部需要导入import random 模块。
针对生物大群的共性
A.创建时有随机位置
B.可以移动
基于以上条件写了该父类对象
初始化对象__init__
A.MAP_MIN_X,MAP_MAX_X之间的随机x
B.MAP_MIN_Y,MAP_MAX_Y之间的随机y
移动函数move
ymbol和direction随机生成0到1
原本直接用01来判断的地方,我用到了一个列表,把名字写入列表通过列表来判断上下左右的方向(此处有带你脱裤子放屁)
主要是方便阅读,后来想想可以用注释,节省占用。
此处还有另外一个问题,我一直觉得写的不太好,但是又不太会改,有没有大佬帮一下,问题如下
随机移动触壁反弹,现在我是x和y还有+和-都是分开写的,重复性有点高,有没有办法能实现写在一起?
class Ainmal():
def __init__(self):
self.x = random.randint(MAP_MIN_X,MAP_MAX_X)
self.y = random.randint(MAP_MIN_Y,MAP_MAX_Y)
def move(self):
self.symbol = SYMBOL
self.direction = DIRECTION
if self.direction == 'horizontal':
if self.symbol == '+':
self.x = self.x + 1
if self.x > MAP_MAX_X:
self.x = MAP_MAX_X * 2 - self.x
elif self.symbol == '-':
self.x = self.x - 1
if self.x < MAP_MIN_X:
self.x = abs(self.x)
if self.direction == 'vertical':
if self.symbol == '+':
self.y = self.y + 1
if self.y > MAP_MAX_Y:
self.y = MAP_MAX_Y * 2 - self.y
elif self.symbol == '-':
self.y = self.y - 1
if self.y < MAP_MIN_X:
self.y = abs(self.y)
https://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.png
4.乌龟对象 Tortoise
乌龟对象比较复杂
继承父类对象Ainmal
初始化对象__init__
A.初始化父类对象的初始化
B.获取传入体力参数
C.取个名字,日后行走好方便
移动函数move
A.继承父类移动函数
B.体力函数(传参此处因为移动一步所以-1)
C.触碰函数
打印显示函数print_position
A.打印输出乌龟的坐标和体力
B.体力函数blood(传参数值)
C.计算体力
触碰函数touch
A.创建临时列表——存放触碰到的小鱼对象
B.循环便利列表中的小鱼,判断是否触碰乌龟
C.触碰到(保险起见在触碰判断之前先判断了下是否还存在小鱼)
①小鱼对象加入临时列表
②体力函数(传参此处因为吃到小鱼所以20)
D.循环删除列表中被吃掉的小鱼
class Tortoise(Ainmal):
def __init__(self,spirit):
Ainmal.__init__(self)
self.spirit = spirit
self.name = '乌龟'
def move(self):
Ainmal.move(self)
self.blood(-1)
self.touch()
def print_position(self):
print("{0}\t({1},{2})\t体力:{3}".format(str(self.name),str(self.x),str(self.y),str(self.spirit)))
def blood(self,flag):
self.spirit = self.spirit + flag
if self.spirit > TORTOISE_SPIRIT_MAX:
self.spirit = TORTOISE_SPIRIT_MAX
def touch(self):
shoal_of_fish_to_move = []
if SHOAL_OF_FISH:
for fish in SHOAL_OF_FISH:
if fish.x == self.x and fish.y == self.y:
shoal_of_fish_to_move.append(fish)
self.blood(20)
for fish_move in shoal_of_fish_to_move:
SHOAL_OF_FISH.remove(fish_move)
https://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.png
5.小鱼对象 Fish
小鱼对象就比较简单了
初始化对象__init__
A初始化父类对象的初始化
B获取传入编号参数
C取个名字,日后行走好方便
移动函数move
A继承父类移动函数
打印显示函数print_position
A打印输出小鱼的坐标和体力
class Fish(Ainmal):
def __init__(self,number):
Ainmal.__init__(self)
self.name = '鱼儿 ' + str(number)
def move(self):
Ainmal.move(self)
def print_position(self):
print("{0}\t({1},{2})".format(str(self.name),str(self.x),str(self.y)))
https://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.pnghttps://static.52pojie.cn/static/image/hrline/line4.png
6.主函数
最后把pass的main主函数完善实例化Tortoise——>一个乌龟wugui
循环实例化Fish——>一堆小鱼Fish 添加到列表中
循环体内
A涉及到小乌龟移动随机步数,有用到随机函数生成随机数
B按照随机数执行循环次数
①乌龟移动
②乌龟显示
C乌龟移动结束
D小鱼只移动一次,所以只循环遍历小鱼列表一次
①小鱼移动
②小鱼显示
E打印输出剩余小鱼,分隔符
F判断是否存在小鱼
①不存在打印乌龟WIN,跳出while循环
G判断乌龟体力是否为0
①体力为0打印小鱼WIN,跳出while循环
H继续循环
def main():
wugui = Tortoise(TORTOISE_SPIRIT_MAX)
for i in range(0,MAX_FISH):
SHOAL_OF_FISH.append(Fish(i))
while 1:
tortoise_move = random.randint(1,TORTOISE_MOVE_MAX)
for i in range(0,tortoise_move):
wugui.move()
wugui.print_position()
for fish in SHOAL_OF_FISH:
fish.move()
fish.print_position()
print('场上还剩{0}条小鱼'.format(len(SHOAL_OF_FISH)))
print('===============================')
time.sleep(0.5)
if wugui.spirit == 0:
print('小鱼存活,乌龟扑街')
break
elif len(SHOAL_OF_FISH) == 0:
print('小鱼灭绝,乌龟WIN!\t剩余体力:{0}'.format(wugui.spirit))
break
https://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.pnghttps://static.52pojie.cn/static/image/hrline/line7.png
以上为全部程序各个部分的解释说明也已经一一附在每段程序的前面
程序部分我会附件在最下面,供大家下载和学习
对程序有新想法的同志们可以把建议或意见留言叠楼我会一一回复
{:301_1008:} 又碰到了新问题,帖子码完我不会不知道怎么看预览,还是说没有预览功能,只能在编辑页面看,看不到我的Markdown功能还有链接功能能不能正常,有同志能告知下嘛
完整代码:
# 游戏场景为范围(x,y)为 0<=x<=10,0<=y<=10
# 游戏生成1只乌龟和10条鱼
# 它们的移动方向均随机
# 乌龟的最大移动能力为2(可以随机选择1还是2),鱼儿的最大移动能力为1
# 当移动到场景边缘,自动向反方向移动
# 乌龟初始化体力为100(上限)
# 乌龟每移动一次,体力消耗1
# 当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
# 鱼暂不计算体力
# 当乌龟体力值为0(挂掉)或鱼儿的数量为0游戏结束
import random
import time
# 最大最小边界(由于算法关系x和y分别的MAX和MIN差最小为1)
MAP_MIN_X = 0
MAP_MAX_X = 10
MAP_MIN_Y = 0
MAP_MAX_Y = 10
# 符号 正 负
SYMBOL = ['+','-']
# 方向 水平 垂直
DIRECTION = ['horizontal','vertical']
# 鱼群
# 鱼群堆
SHOAL_OF_FISH = []
# 最大数量
MAX_FISH = 10
# 乌龟
# 移动次数
TORTOISE_MOVE_MAX = 2
# 最大体力
TORTOISE_SPIRIT_MAX = 100
class Ainmal():
def __init__(self):
self.x = random.randint(MAP_MIN_X,MAP_MAX_X)
self.y = random.randint(MAP_MIN_Y,MAP_MAX_Y)
def move(self):
self.symbol = SYMBOL
self.direction = DIRECTION
if self.direction == 'horizontal':
if self.symbol == '+':
self.x = self.x + 1
if self.x > MAP_MAX_X:
self.x = MAP_MAX_X * 2 - self.x
elif self.symbol == '-':
self.x = self.x - 1
if self.x < MAP_MIN_X:
self.x = abs(self.x)
if self.direction == 'vertical':
if self.symbol == '+':
self.y = self.y + 1
if self.y > MAP_MAX_Y:
self.y = MAP_MAX_Y * 2 - self.y
elif self.symbol == '-':
self.y = self.y - 1
if self.y < MAP_MIN_X:
self.y = abs(self.y)
class Tortoise(Ainmal):
def __init__(self,spirit):
Ainmal.__init__(self)
self.spirit = spirit
self.name = '乌龟'
def move(self):
Ainmal.move(self)
self.blood(-1)
self.touch()
def print_position(self):
print("{0}\t({1},{2})\t体力:{3}".format(str(self.name),str(self.x),str(self.y),str(self.spirit)))
def blood(self,flag):
self.spirit = self.spirit + flag
if self.spirit > TORTOISE_SPIRIT_MAX:
self.spirit = TORTOISE_SPIRIT_MAX
def touch(self):
shoal_of_fish_to_move = []
if SHOAL_OF_FISH:
for fish in SHOAL_OF_FISH:
if fish.x == self.x and fish.y == self.y:
shoal_of_fish_to_move.append(fish)
self.blood(20)
for fish_move in shoal_of_fish_to_move:
SHOAL_OF_FISH.remove(fish_move)
class Fish(Ainmal):
def __init__(self,number):
Ainmal.__init__(self)
self.name = '鱼儿 ' + str(number)
def move(self):
Ainmal.move(self)
def print_position(self):
print("{0}\t({1},{2})".format(str(self.name),str(self.x),str(self.y)))
def main():
wugui = Tortoise(TORTOISE_SPIRIT_MAX)
for i in range(0,MAX_FISH):
SHOAL_OF_FISH.append(Fish(i))
while 1:
tortoise_move = random.randint(1,TORTOISE_MOVE_MAX)
for i in range(0,tortoise_move):
wugui.move()
wugui.print_position()
for fish in SHOAL_OF_FISH:
fish.move()
fish.print_position()
print('场上还剩{0}条小鱼'.format(len(SHOAL_OF_FISH)))
print('===============================')
time.sleep(0.5)
if wugui.spirit == 0:
print('小鱼存活,乌龟扑街')
break
elif len(SHOAL_OF_FISH) == 0:
print('小鱼灭绝,乌龟WIN!\t剩余体力:{0}'.format(wugui.spirit))
break
if __name__ == '__main__':
main()
最后的最后都翻到最后了 收藏 免费评分 有用 随手点一下, 谢谢谢大家捧场!!
{:301_984:} {:301_984:} {:301_984:} {:301_984:} {:301_984:} {:301_984:} {:301_984:} {:301_984:}
感谢楼主分享 我最佩服的就是靠技术吃饭的人了 马克学习下,感谢分享! 学习了,感谢楼主分享 多谢细致分享昂 楼主精进了,改进了第一版程序 你这个游戏运行起来屏幕一直滚动看的眼疼 正在学习,感觉入门简单,精通难,需要长时间的练习 屏幕好伤眼啊 本帖最后由 233500 于 2020-7-30 08:23 编辑
time.sleep(0.5)
if wugui.spirit == 0: //这里要用<= 否则会变成负数然后提示乌龟赢了
print('小鱼存活,乌龟扑街')
break
elif len(SHOAL_OF_FISH) == 0:
print('小鱼灭绝,乌龟WIN!\t剩余体力:{0}'.format(wugui.spirit))
break
偶尔会出现负数情况.负数的原因应该是一次走两步的问题,体力直接跳过0 从1跳到-1
不知道小鱼会不会一下被吃2条鱼,感觉如果严谨点的话,小鱼 应该也用 <=比较好
新手也是初学也不太懂,说错了请指正