|  | 
 
 
 楼主|
发表于 2020-4-12 17:44:35
|
显示全部楼层 
| 楼上的代码:
 
 复制代码import random
class Playground:
    length = random.randint(1,10)
    width = random.randint(1,10)
class Animal_turtle(Playground):
    
    seat = [0,0]
    life = 100
    
    def funtion(self):
        num1 = random.randint(1,self.length)
        num2 = random.randint(1,self.width)
        self.seat[0] = num1
        self.seat[1] = num2
class Animal_fish(Playground):
    seat = [0,0]
    
    def funtion(self):
        num1 = random.randint(1,self.length)
        num2 = random.randint(1,self.width)
        self.seat[0] = num1
        self.seat[1] = num2
turtle = Animal_turtle()
turtle.funtion()
fish1 = Animal_fish()
fish1.funtion()
fish2 = Animal_fish()
fish2.funtion()
fish3 = Animal_fish()
fish3.funtion()
fish4 = Animal_fish()
fish4.funtion()
fish5 = Animal_fish()
fish5.funtion()
fish6 = Animal_fish()
fish6.funtion()
fish7 = Animal_fish()
fish7.funtion()
fish8 = Animal_fish()
fish8.funtion()
fish9 = Animal_fish()
fish9.funtion()
fish10 = Animal_fish()
fish10.funtion()
list_fish = [fish1,fish2,fish3,fish4,fish5,fish6,fish7,fish8,fish9,fish10]
fish_list1 = ['fish1','fish2','fish3','fish4','fish5'\
             ,'fish6','fish7','fish8','fish9','fish10']
fish_list2 = []
before = len(list_fish)
while before > 0 and turtle.life > 0:
    
    for each in list_fish:
        if each.seat == turtle.seat:
            index_value = list_fish.index(each)
            list_fish.remove(each)
            fish_list2.append(fish_list1[index_value])
            
    while True:
        #乌龟的移动判定
        count1 = random.randint(1,4)#方向,1:上,2:下,3:左,4:右
        count2 = random.randint(1,2)#移动的格数
        if count1 == 1 and count2 == 1:
            turtle.seat[0] += 1
            turtle.life -= 1
        elif count1 == 1 and count2 == 2:
            turtle.seat[0] += 2
            turtle.life -= 1
        elif count1 == 2 and count2 == 1:
            turtle.seat[0] -= 1
            turtle.life -= 1
        elif count1 == 2 and count2 == 2:
            turtle.seat[0] -= 2
            turtle.life -= 1
        elif count1 == 3 and count2 == 1:
            turtle.seat[1] -= 1
            turtle.life -= 1
        elif count1 == 3 and count2 == 2:
            turtle.seat[1] -= 2
            turtle.life -= 1
        elif count1 == 4 and count2 == 1:
            turtle.seat[1] += 1
            turtle.life -= 1
        elif count1 == 4 and count2 == 2:
            turtle.seat[1] += 2
            turtle.life -= 1
        elif turtle.seat[0] >= Playground().length:
            turtle.seat[0] -= 1
            turtle.life -= 1
        elif turtle.seat[0] <= 0:
            turtle.seat[0] += 1
            turtle.life -= 1
        elif turtle.seat[1] >= Playground().width:
            turtle.seat[1] -= 1
            turtle.life -= 1
        elif turtle.seat[1] <= 0:
            turtle.seat[1] += 1
            turtle.life -= 1
        break
    while True:
        #鱼的移动判定
        for each in list_fish:
            count3 = random.randint(1,4)#方向,1:上,2:下,3:左,4:右
            if count3 == 1:
                each.seat[0] += 1
            elif count3 == 2:
                each.seat[0] -= 1
            elif count3 == 3:
                each.seat[1] -= 1
            elif count3 == 4:
                each.seat[1] += 1
            elif each.seat[0] >= Playground().length:
                each.seat[0] -= 1
            elif each.seat[0] <= 0:
                each.seat[0] += 1
            elif each.seat[1] >= Playground().width:
                each.seat[1] -= 1
            elif each.seat[1] <= 0:
                each.seat[1] += 1
        break
    after = len(list_fish)
    turtle.life += (before-after)*20
    before = after
if turtle.life <= 0 and before > 0:
    print('乌龟失败啦,他最终还是没有胜利。\n不幸牺牲的小鱼有:',end='')
    for each in fish_list2:
        print(each,end=' ')
if before == 0 and turtle.life >0:
    print('乌龟胜利啦!!\n他还剩下%d点体力值。' % turtle.life)
 | 
 |