鱼C论坛

 找回密码
 立即注册
查看: 2205|回复: 4

python教程37讲课后作业第二题我没看答案前自己写的,不知道错在哪里

[复制链接]
发表于 2020-1-8 23:10:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import random as r

class Turtle:
    orientx = r.randint(0,10)
    orienty = r.randint(0,10)
    strength = 100
    count = 0   
    def game(self):
        while self.strength > 0:
            move = r.randint(1,2) #移动1或者2
            direction = r.randint(1,4) #方向上下左右
            if direction == 1:
                self.orientx += move
            if direction == 2:
                self.orientx -= move
            if direction == 3:
                self.orienty += move
            if direction == 4:
                self.orienty -= move

            if self.orientx == -1:
                self.orientx = 1
            if self.orientx == -2:
                self.orientx = 2
            if self.orientx == 11:
                self.orientx = 9
            if self.orientx == 12:
                self.orientx = 8
            
            if self.orienty == -1:
                self.orienty = 1
            if self.orienty == -2:
                self.orienty = 2
            if self.orienty == 11:
                self.orienty = 9
            if self.orienty == 12:
                self.orienty = 8
               
            twhere = (self.orientx,self.orienty)
            self.strength -= 1
            count += 1

class Fish:
    orientx = r.randint(0,10)
    orienty = r.randint(0,10)
    count = 0
    def game(self):
        while 1:
            move = 1
            direction = r.randint(1,4) #方向上下左右
            if direction == 1:
                self.orientx += move
            if direction == 2:
                self.orientx -= move
            if direction == 3:
                self.orienty += move
            if direction == 4:
                self.orienty -= move

            if self.orientx == -1:
                self.orientx = 1
            if self.orientx == 11:
                self.orientx = 9
        
            if self.orienty == -1:
                self.orienty = 1
            if self.orienty == 11:
                self.orienty = 9               
            twhere = (self.orientx,self.orienty)
            

t1 = Turtle()
f1 = Fish();f2 = Fish();f3 = Fish();f4 = Fish();f5 = Fish();\
f6 = Fish();f7 = Fish();f8 = Fish();f9 = Fish();f10 = Fish()

d = {'1':f1,'2':f2,'3':f3,'4':f4,'5':f5,'6':f6,'7':f7,'8':f8,'9':f9,'10':f10}

while True:
    t1.game
    for each in range(1,11):
        each = str(each)
        d[each].game
        if d[each].count == t1.count and d[each].orientx == t1.orientx and d[each].orienty == t1.orienty:
            t1.strength += 20
            del d[each]
            print('小鱼%s被吃掉啦' % each)

    if t1.strength == 0:
        print('乌龟完犊子')
        break

    if len(d) == 0:
        print('鱼吃完了')
        break
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-1-8 23:52:35 | 显示全部楼层
本帖最后由 jackz007 于 2020-1-8 23:53 编辑

       为什么说代码是错的,你做出这个判断的依据是什么?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-1-9 11:08:32 | 显示全部楼层
题目是虾米呀???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-1-9 18:35:09 | 显示全部楼层
lixiangyv 发表于 2020-1-9 11:08
题目是虾米呀???

1. 游戏编程:按以下要求定义一个乌龟类和鱼类并尝试编写游戏。(初学者不一定可以完整实现,但请务必先自己动手,你会从中学习到很多知识的^_^)Ubo^9qN
SeT1@,QF>]o"N%lIq3C#G5g29
假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
游戏生成1只乌龟和10条鱼
它们的移动方向均随机
乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
当移动到场景边缘,自动向反方向移动
乌龟初始化体力为100(上限)
乌龟每移动一次,体力消耗1
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
鱼暂不计算体力
当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-1-9 18:36:03 | 显示全部楼层
jackz007 发表于 2020-1-8 23:52
为什么说代码是错的,你做出这个判断的依据是什么?

1. 游戏编程:按以下要求定义一个乌龟类和鱼类并尝试编写游戏。(初学者不一定可以完整实现,但请务必先自己动手,你会从中学习到很多知识的^_^)Ubo^9qN
SeT1@,QF>]o"N%lIq3C#G5g29
假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
游戏生成1只乌龟和10条鱼
它们的移动方向均随机
乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
当移动到场景边缘,自动向反方向移动
乌龟初始化体力为100(上限)
乌龟每移动一次,体力消耗1
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
鱼暂不计算体力
当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束

这是题目,我没办法实现功能。整个程序可以正常运行没有错误,但是运行了很久没有结果。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-21 18:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表