|
5鱼币
问题是这样的:
看了小甲鱼老师第37讲的课后作业(乌龟吃鱼),我想自己修改一下。
修改后如下:
- import random as r
- import easygui as g
- legal_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,direction):
- if direction == '向前':
- new_y = self.y + 1
- if new_y > legal_y[1]:
- self.y = legal_y[1] - (new_y - legal_y[1])
- else:
- self.y = new_y
-
- if direction == '向后':
- new_y = self.y - 1
- if new_y < legal_y[0]:
- self.y = legal_y[0] - (new_y - legal_y[0])
- else:
- self.y = new_y
-
- if direction == '向左':
- new_x = self.x + 1
- if new_x > legal_x[1]:
- self.x = legal_x[1] - (new_x - legal_x[1])
- else:
- self.x = new_x
-
- if direction == '向右':
- new_x = self.x - 1
- if new_x < legal_x[0]:
- self.x = legal_x[0] - (new_x - legal_x[0])
- else:
- self.x = new_x
- 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(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,0,1])
- new_y = self.y + r.choice([-1,0,1])
- 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
- 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):
- newfish = Fish()
- fish.append(newfish)
- a = '嗯~哼~哼哼哼~~~'
- b = '小乌龟,爬呀爬~~~'
- c = '小鱼小鱼快出来~~~'
- d = '好无聊啊~~~'
- e = '妈妈在哪里呢~~~'
- f = '龟生的意义是什么。。。'
- g = '有点饿~~~'
- h = '谁来陪我玩。。。'
- say = r.choice([a,b,c,d,e,f,g,h])
- while True:
-
- if not len(fish):
- g.msgbox('鱼儿被吃完了,游戏结束。')
- break
- if not turtle.power:
- g.msgbox('乌龟体力耗尽,游戏结束。')
- break
-
- direction = g.buttonbox(say+'乌龟还有%d点体力\n\n要怎么游呢?'%turtle.power,choices = ('向前','向后','向左','向右','不玩了'))
- if direction == '不玩了':
- break
-
- pos = turtle.move(direction)
-
- for each_fish in fish[:]:
- if each_fish.move() == pos:
- fish.remove(each_fish)
- turtle.eat()
- g.msgbox('乌龟发现了一条鱼儿\n乌龟吃掉了鱼儿\n还有%d条鱼'%(len(fish)))
复制代码
运行以后出现这个问题:(不会插入图片)
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\测试.py", line 112, in <module>
direction = g.buttonbox(say+'乌龟还有%d点体力\n\n要怎么游呢?'%turtle.power,choices = ('向前','向后','向左','向右','不玩了'))
AttributeError: 'str' object has no attribute 'buttonbox'
请问改怎样解决?
python提示字符串没有buttonbox方法
是这样的,你向import easygui as g
但是又g = '有点饿~~~'覆盖了g
是变量名相同引起的~
|
-
最佳答案
查看完整内容
python提示字符串没有buttonbox方法
是这样的,你向import easygui as g
但是又g = '有点饿~~~'覆盖了g
是变量名相同引起的~
|