鱼C论坛

 找回密码
 立即注册
查看: 1057|回复: 5

python

[复制链接]
发表于 2018-5-20 18:46:06 | 显示全部楼层 |阅读模式

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

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

x
import random as r
legal_x = [0,10]
legal_y = [0,10]
class Turtle:
        def __intit__(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):
            # 随机计算方向并移向到新的位置
            new_x = self.x + r.choice([1,2,-1,-2])
            new_y = self.y + r.choice([1,2,-1,-2])
            #检测移动后是否超出场景x轴边界
            if new_x < legal_x[0]:
                    self.x = legal_x[0] - (new_x - legal_x[0])
            elif new_x > legal_x[0]:
                    self.x = legal_x[1] - (new_x - legal_x[1])
            else:
                    selif.x = new_x
             #检测移动后是否超出场景y轴边界
            if new_y < legal_x[0]:
                    self.x = legal_y[0] - (new_y - legal_y[0])
            elif new_y > legal_y[0]:
                    self.y = legal_y[1] - (new_y - legal_y[1])
            else:
                    selif.x = 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 __int__(self):
            self.x = r.randint(legal_x[0],legal_x[1])
            self.y = r.randint(legal_y[0],legal_y[1])
        def move(self):
              # 随机计算方向并移向到新的位置
            new_x = self.x + r.choice([1,-1])
            new_y = self.y + r.choice([1,-1])
            #检测移动后是否超出场景x轴边界
            if new_x < legal_x[0]:
                    self.x = legal_x[0] - (new_x - legal_x[0])
            elif new_x > legal_x[0]:
                    self.x = legal_x[1] - (new_x - legal_x[1])
            else:
                    selif.x = new_x
             #检测移动后是否超出场景y轴边界
            if new_y < legal_x[0]:
                    self.x = legal_y[0] - (new_y - legal_y[0])
            elif new_y > legal_y[0]:
                    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('乌龟体力耗尽,gg了!')
        break

    pos = turtle.move()

    for each_fish in fish[:]:
        if each_fish.move() == pos:
            #鱼儿被吃掉了
            turtle.eat()
            fish.remove(each_fish)
            print('有一条鱼儿被吃掉了。。。。。。')


Traceback (most recent call last):
  File "F:\pytest\self.py", line 75, in <module>
    if not turtle.power:
AttributeError: 'Turtle' object has no attribute 'power'
               
各位鱼油大大,75行哪里有错误啊,我看不出来。。。


                       
               
                    

              
                    
        
               
                        
                    
                                
                                
                 
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-20 19:15:47 | 显示全部楼层
第五行拼写错误:def __intit__(self): 是init
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-5-21 00:17:45 | 显示全部楼层
冬雪雪冬 发表于 2018-5-20 19:15
第五行拼写错误:def __intit__(self): 是init

我改完之后显示。。。


Traceback (most recent call last):
  File "F:\pytest\self.py", line 82, in <module>
    if each_fish.move() == pos:
  File "F:\pytest\self.py", line 45, in move
    new_x = self.x + r.choice([1,-1])
AttributeError: 'Fish' object has no attribute 'x'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-5-21 00:18:31 | 显示全部楼层
new_x = self.x + r.choice([1,2,-1,-2])
new_y = self.y + r.choice([1,2,-1,-2])

这些坐标要这么理解啊。。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-21 07:04:35 | 显示全部楼层
2楼非常明确地直戳错误要害。
请看报错信息:"AttributeError: 'Turtle' object has no attribute 'power'",该对象没有'power'属性。
请看Turtle定义类的属性:def ____(self):
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-21 08:52:01 | 显示全部楼层
hi 出错原因如下:

1. 第5行代码 ,__intit__手误,应该是:def __init__(self):

2.第22行代码,手误写为selif.x,应该是:self.x = new_x

3.第29行代码,手误写为selif.x ,应该是:self.x = new_y

4.第40行代码,手误写为__int__,应该是:def __init__(self):

5.第53行代码,手误写为selif.x,应该是:self.x = new_x

全部都是手误,建议楼主码代码时多留意下自己的拼写
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 09:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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