|

楼主 |
发表于 2020-4-9 15:04:47
|
显示全部楼层
本帖最后由 猪猪虾 于 2020-4-9 15:06 编辑
可是我把self全删掉之后,def turtle_move(self):turtle_now_x=random.choice([now_x+1,now_x+2,now_x-1,now_x-2,now_x]),显示now_x未定义(63行)
- import easygui as g
- import random
- location_x=[1,10]
- location_y=[1,10]
- class Fish:
- def __init__(self): #定义鱼的初始情况,__init__返回值为空
- fish_location_save=[] #存放乌龟和鱼重合位置的类似于(1,2)形式的坐标
- fish_x_location=[] #单独存放乌龟和鱼重合位置的X的坐标
- fish_y_location=[] #单独存放乌龟和鱼重合位置的y的坐标
- #一次性随即设定10个位置,即生成10条鱼
- while len(fish_location_save)!=10 :
- fish_now_x=random.randint(1,10)
- fish_now_y=random.randint(1,10)
- fish_location=(fish_now_x,fish_now_y) #生成元祖,类似于(x,y)形式,然后存于self.fish_location_save列表
- if fish_location not in fish_location_save:
- fish_location_save.append(fish_location)
- fish_x_location.append(fish_now_x)
- fish_y_location.append(fish_now_y)
-
- def fish_move(self):
- #10条鱼,他们都可动可不动
- new_fish_location_save=[] #存放移动后新的乌龟和鱼重合位置的类似于(1,2)形式的坐标
- new_fish_x_location=[] #单独移动后新的乌龟和鱼重合位置的X的坐标
- new_fish_y_location=[] #单独移动后新的乌龟和鱼重合位置的y的坐标
- #移动10条鱼的位置
- while len(new_fish_location_save) != 10:
- for i in len(self.fish_location_save):
- #鱼的最大移动为1,在初始坐标的基础上,x,y+1,或者不动,所以有三种情况
- new_fish_x=random.choice([fish_x_location[i],fish_x_location[i]+1,fish_x_location[i]-1])
- new_fish_y=random.choice([fish_y_location[i],fish_y_location[i]+1,fish_y_location[i]-1])
- #判断是否超出边界
- if new_fish_x not in location_x:
- new_fish_x-=1
- if new_fish_y not in location_y:
- new_fish_x-=1
- me=(new_fish_x,new_fish_y)
- if me not in new_fish_location_save:
- new_fish_location_save.append(me)
- new_fish_x_location.append(new_fish_x)
- new_fish_y_location.append(new_fish_y)
- turtle=Turtle()
- turtlr_location=turtle.turtle_move()
- if turtlr_location in new_fish_location_save:
- #乌龟和鱼的位置重合,删掉重合位置的鱼的坐标
- for i in len(self.fish_location_save):
- if fish_location == self.fish_location_save[i]:
- me=i
- break
- self.fish_location_save.pop(me)
-
- class Turtle:
- def __init__(self): #定义乌龟的初始情况
- #初始体力
- turtle_begin=100
- #随机设置初始位置
- now_x=random.randint(1,10)
- now_y=random.randint(1,10)
-
- def turtle_move(self):
- turtle_now_x=random.choice([now_x+1,now_x+2,now_x-1,now_x-2,now_x])
- turtle_now_y=random.choice([now_y+1,now_y+2,now_y-1,now_y-2,now_y])
- #判断是否超出边界
- if turtle_now_x not in location_x:
- #反向返回
- turtle_now_x-=1
- if turtle_now_y not in location_y:
- turtle_now_y-=1
- turtlr_location=(turtle_now_x,turtle_now_y)
- fish=Fish()
- fish_location_save=fish.fish_move()
- if turtlr_location in fish_location_save:
- turtle_begin += 20
- print("一条鱼已经被吃掉,还剩%d条鱼"%len(fish_location_save))
- return turtlr_location #返回重合位置的坐标
- else:
- return (0,0) #不重合的时候返回一个不在范围的坐标,避免鱼在盗用的时候报错
- turtle_begin -= 5
- if turtle_begin == 0:
- g.msgbox("game over")
- return turtle_begin
-
- fish_use=Fish()
- turtlr_use=Turtle()
- fish_use.__init__ ()
- turtlr_use.__init__()
- turtlr_use.turtle_move()
- fish_use.fish_move()
- while turtle_begin !=0 or len(fish_location_save) != 0 :
- turtlr_use.turtle_move()
- fish_use.fish_move()
-
复制代码 |
|